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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.rmi.NoSuchObjectException;
import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.EJBObject;
import javax.ejb.RemoveException;
import javax.ejb.EJBMetaData;
import javax.ejb.Handle;
import javax.ejb.HomeHandle;
import javax.rmi.PortableRemoteObject;

import org.objectweb.jonas.jtests.beans.local.Target;
import org.objectweb.jonas.jtests.beans.local.TargetSLHome;
import org.objectweb.jonas.jtests.util.JTestCase;


/**
 * This Class defines Test cases that can be run either SF or SL bean.
 * These tests are related to the "Client View of a Session Bean"
 * A session bean that supports both Local & Remote interface is used
 * (TargetSL oe TargetSF in beans/local)
 * But here are testcases that can be run via a non remote suite.
 *
 * @see  EJB2.0 specification Chapter 6.
 * @author Ph Coq, Ph Durieux
 */

public abstract class A_ClientView extends JTestCase {

    public A_ClientView(String name) {
        super(name);
    }

    /**
     * @return TargetSLHome, that can be either SF or SL bean.
     */
    public abstract TargetSLHome getHome() throws Exception;



    /**
     * Lookup a Home Object
     *
     * This test verify that we can look up a Home Object
     * via the root context
     */

    public void testHomeLookup() throws Exception {
        TargetSLHome h = null;
        h = getHome();
        assertTrue(h != null);
    }




    /**
     * This test verify we can create and remove a session bean
     */
    public void testCreateRemove() throws Exception {
        Target tr = getHome().create();
        tr.remove();
    }



    /**
     * This test verify we can create and remove a session bean
     */
    public void testLongCreateRemove() throws Exception {
        for (int i = 0; i < 500; i++) {
            Target tr = getHome().create();
            tr.remove();
        }
    }


    /**
     * This test verify we can create, access to the bean via
     * its remote interface and remove it
     * It verifies we cannot access to the bean after remove
     */
    public void testBusinessMethod1() throws Exception {
        Target tr = getHome().create();
        assertEquals(10, tr.getTen());
        tr.remove();
        try {
            tr.method2("Bye");
            fail("NoSuchObjectException must be raised");
            //        } catch (NoSuchObjectException e) {
        } catch (NoSuchObjectException e) {
        }
    }


    /**
     * Same test than testBusinessMethod1 but two calls to businessmethod
     */
    public void testBusinessMethod2() throws Exception {
        Target tr = getHome().create();
        assertEquals(10, tr.getTen());
        assertEquals(10, tr.getTen());
        tr.remove();
    }



    /**
     * test 3 Remote Business Methods
     */
    public void testBusinessMethod3() throws Exception {
        Target tr = getHome().create();
        assertEquals(10, tr.getTen());
        tr.method2("Hello");
        assertEquals(10, tr.getTen());
        tr.remove();
    }


    /**
     * test EJBObject.isIdentical on the same bean
     */
    public void testIsIdenticalOnSameBean() throws Exception {
        Target tr = getHome().create();
        assertTrue(tr.isIdentical(tr));
        tr.remove();
    }


    /**
     * test EJBObject.getPrimary Key
     * This test verify that a Remote Exception must be catched
     */
    public void testGetPrimaryKey() throws Exception {
        Target tr = getHome().create();
        try {
            tr.getPrimaryKey();
            fail("getPrimaryKey should raise RemoteException on a session bean");
        } catch (RemoteException e) {
        } finally {
            tr.remove();
        }
    }


    /**
     * test EJBHome.getEJBMetaData().getPrimaryKeyClass()
     * This test verify that a EJBException must be catched
     */
    public void testGetPrimaryKeyClass() throws Exception {
        try {
            getHome().getEJBMetaData().getPrimaryKeyClass();
            fail("getEJBMetaData().getPrimaryKeyClass should raise EJBException on a session bean home");
        } catch (EJBException e) {
        }
    }


    /**
     * test that EJBHome.remove(pk) should raise RemoteException (API) or
     * RemoveException (spec EJB 2.0 6.3.2)
     */
    public void testRemoveByPK() throws Exception {
        Object pk = null;
        try {
            getHome().remove(pk);
            fail("getPrimaryKey should raise Exception on a session bean");
        } catch (RemoteException e) {
        } catch (RemoveException e) {
        }
    }


    /**
     * test that EJBHome.getEJBMetaData()
     * test we can obtain an handle for a session bean
     */
    public void testGetEJBMetaData() throws Exception {
        EJBMetaData hh = getHome().getEJBMetaData();
    }

    /**
     * test that EJBHome.getHomeHandle()
     * test we can obtain a handle for a session bean's home interface
     */
    public void testGetHomeHandle() throws Exception {
        HomeHandle hh = getHome().getHomeHandle();
    }

    /**
     * test serialize/deserialize handle.
     * @throws Exception
     */
    public void testSerializeHandle() throws Exception {
        TargetSLHome tgh = getHome();
        Target tr = tgh.create();
        Handle h = tr.getHandle();
        ObjectInputStream is = null;
        ObjectOutputStream os = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        os = new ObjectOutputStream(baos);
        os.writeObject(h);
        byte[] b = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(b);
        is = new ObjectInputStream(bais);
        Handle deserializedHandle = (Handle) is.readObject();
        EJBObject ejbObject = deserializedHandle.getEJBObject();
        Target beanRef2 = (Target) PortableRemoteObject.narrow(ejbObject, Target.class);
    }

    /**
     * test that EJBHome.removeHandle()
     * test we can remove a session via its  handle
     * it verifies the bean cannot be accessed after remove and
     * java.rmi.NoSuchObjectException is raised
     */
    public void testRemoveHandle() throws Exception {
        TargetSLHome tgh = getHome();
        Target tr = tgh.create();
        Handle h = tr.getHandle();
        tgh.remove(h);
        try {
            tr.method2("Bye");
            fail("Cannot remove a session bean via its handle");
        } catch ( NoSuchObjectException e) {

        } catch (Exception e) {
            fail("Should raise NoSuchObjectException, not this one:" + e);
        }
    }

}
