/*
 * 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.jca15;

import junit.framework.*;
import java.io.BufferedReader; 
import java.io.FileInputStream;
import java.io.InputStreamReader; 
import java.io.IOException;
import java.lang.String;
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.objectweb.jonas.jtests.beans.jca15.InboundCAHome;
import org.objectweb.jonas.jtests.beans.jca15.InboundCA;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * Tests JOnAS Connector Architecture Inbound Resource Adapter
 */
public class F_inboundTest extends JTestCase {

    static Context ctx = null; 

    // Lookup bean home

    protected static String BEAN_HOME = "InboundCAHome";
    static InboundCAHome home = null;
    static InboundCA bean = null;
    private static final String RAR_JNDI_NAME = "eis/ErsatzEIS";
    int CloseType = 0;
    
    public F_inboundTest(String name) {
        super(name);
    }

    protected void setUp() {
        super.setUp();

    // Get InitialContext

        try {

            // get JNDI initial context

            if (ctx == null) {
                ctx = new InitialContext();
            }

            if (home == null) {
                useBeans("jca15", false);  // does "jonas admin -j jca15.jar"
            }

            // Connecting to InboundCAHome thru JNDI

            if (home == null) {
                home = (InboundCAHome)PortableRemoteObject.narrow(
                                 ctx.lookup(BEAN_HOME),
                                 InboundCAHome.class);
                bean = home.create();
            }
        
            assertTrue (5 == 5);

        } catch (Exception e) {
            fail("Cannot do setUp: " +e);
        }
    }
    /**
     * 
     * Common tearDown method, used for every test.
     * Verify some Application Server tests by reviewing the log file.
     * 
     * The application server also calls ManagedConnection.destroy when it 
     * receives a connection error event notification that signals a fatal error 
     * on the physical connection. When method closeUp(int x) is called by
     * tearDown with x>0, a connection error event notification is sent to
     * the application server. Look for <code>ManagedConnection.destroy</code> entry.
     *
     */
    protected void tearDown() throws Exception {
        bean.closeUp(CloseType);
    }
    protected void startUp(String testName) {
	    try {
	        bean.method1(RAR_JNDI_NAME, testName);
	    } catch (Exception ee) {
	        ee.printStackTrace();
	        System.exit(2);
	    }
    }
// test list ****************************************************
    /**
     * The application server calls setter methods on the ResourceAdapterImpl
     * instance to set various configuration properties on this instance.     
     * ra.xml deployment supplies value
     *
     * This test compares the "set" value in ra.xml, EIS_URL=ErsatzEIS
     */
    public void testSetEIS_URLMethod() throws Exception {
        startUp("testSetEIS_URLMethod");
        String url = bean.getEIS_URL();
        assertEquals("ErsatzEIS", url);
    }
    /**
     * Send these messages to the Message Driven Bean MessageTakerMD.java
     * >InboundCASLR > ResourceAdapterImpl > WorkImpl > MessageTakerMD > WorkAdapterImpl
     * which is the "WorkListener"
     *
     * This test verifies the work submission call back mechanism in JCA 1.5, Figure 42
     * and endpoint deployment in Figure 12-6.
     * 
     * The Message Driven Bean is the endpoint which receives the messages.
     */
    public void testDeliverMsg() throws Exception {
        startUp("testDeliverMsg");
        boolean b = bean.deliverMsg("String message 11111111111111111111 to endpoint.");
        assertTrue(b);
        b = bean.deliverMsg("String message 22222222222222222222 to endpoint.");
        assertTrue(b);
    }
// end test list ****************************************************

    public static Test suite() {
        return new TestSuite(F_inboundTest.class);
    }

    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_inboundTest(testtorun));
        }
    }
}
