/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.clients.mcontext;

import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.objectweb.jonas.jtests.beans.mcontext.BeanAccessor;
import org.objectweb.jonas.jtests.beans.mcontext.BeanAccessorHome;
import org.objectweb.jonas.jtests.beans.mcontext.TestMC2;
import org.objectweb.jonas.jtests.beans.mcontext.TestMC2Home;
import org.objectweb.jonas.jtests.util.JWebServicesTestCase;

/**
 * test case for MessageContext use between web & ejb container
 */
public class F_MessageContext extends JWebServicesTestCase {

    public F_MessageContext(String name) {
        super(name);
    }
    
    public static Test suite() {
        return new TestSuite(F_MessageContext.class);
    }

    public void setUp() throws Exception {
        super.setUp();
        
        useEar("mcontext");
    }

    public void tearDown() throws Exception {
        super.tearDown();
    }


    public void testWSCallLocal() throws Exception {
        String port = System.getProperty("http.port");

        Service service = ServiceFactory.newInstance().createService(new QName("jonas:MessageContextTest",
                                                                               "MessageContextTestService"));
        Call call = service.createCall(new QName("LocalMContextPort"),
                                       new QName("serviceEndpointMethodHasMessageContext"));
        call.setTargetEndpointAddress("http://localhost:" + port + "/mcontext/MessageContextWS/LocalMContextPort");
        Boolean b = (Boolean) call.invoke(new Object[]{});
        
        assertTrue("ServiceEndpoint invokation cannot access the MessageContext",
                   b.booleanValue());
    }

    public void testWSCallRemote() throws Exception {
        String port = System.getProperty("http.port");

        Service service = ServiceFactory.newInstance().createService(new QName("jonas:MessageContextTest",
                                                                               "MessageContextTestService"));
        Call call = service.createCall(new QName("RemoteMContextPort"),
                                       new QName("serviceEndpointMethodHasMessageContext"));
        call.setTargetEndpointAddress("http://localhost:" + port + "/mcontext/MessageContextWS/RemoteMContextPort");
        Boolean b = (Boolean) call.invoke(new Object[]{});
        
        assertTrue("ServiceEndpoint invokation cannot access the MessageContext",
                   b.booleanValue());
    }

    public void testEJBCallLocal() throws Exception {
        BeanAccessorHome home = (BeanAccessorHome) ictx.lookup("BeanAccessorHome");
        BeanAccessor accessor = home.create();
        assertFalse("ejb call have access to the MessageContext.",
                    accessor.localBeanHasMessageContext());
    }

    public void testEJBCallRemote() throws Exception {
        Object obj = ictx.lookup("TestMC2Home");
        TestMC2Home home = (TestMC2Home) javax.rmi.PortableRemoteObject.narrow(obj, TestMC2Home.class);
        TestMC2 mc2 = home.create();
        assertFalse("ejb call have access to the MessageContext.",
                    mc2.remoteMethodHasMessageContext());
    }

    public static void main (String args[]) {
        String testtorun = null;
        // Get args
        for (int argn = 0; argn < args.length; argn++) {
            String s_arg = args[argn];
            Integer i_arg;
            if (s_arg.equals("-n")) {
                testtorun = args[++argn];
            }
        }
        if (testtorun == null) {
            junit.textui.TestRunner.run(suite());
        } else {
            junit.textui.TestRunner.run(new F_MessageContext(testtorun));
        }
    }

}
