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

import javax.ejb.EJBException;
import javax.ejb.NoSuchObjectLocalException;
import javax.ejb.RemoveException;
import javax.rmi.PortableRemoteObject;

import org.objectweb.jonas.jtests.beans.local.TargetLocal;
import org.objectweb.jonas.jtests.beans.local.TargetSLHome;
import org.objectweb.jonas.jtests.beans.local.TargetSLLocalHome;
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)
 * Here are testcases taht must be run remotly
 * @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 TargetSLLocalHome
     */
    public abstract TargetSLLocalHome getLocalHome() throws Exception;



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

    public void testLocalLookup() throws Exception {
        TargetSLLocalHome h = null;
        h = getLocalHome();
        assertTrue(h != null);
    }


    /**
     * EJB Reference
     *
     * This test verify that we can look up a Home Object of a bean in the same ejbjar file
     * via the java:comp/env subcontext using an ejb-reference contains an ejb-link.
     */
    public void testEjbRef() throws Exception {

        TargetSLHome home = null;
        String bName = "java:comp/env/ejb/targetremotelink";
        home = (TargetSLHome) PortableRemoteObject.narrow(ictx.lookup(bName), TargetSLHome.class);
        assertTrue(home != null);
    }

    /**
     * EJB Reference
     *
     * This test verify that we can look up a Home Object of a bean (in the same ejbjar file)
     * via the java:comp/env subcontext using an ejb-reference with a jonas-ejb-reference.
     */
    public void testEjbRefWithJonasEjbRef() throws Exception {

        TargetSLHome home = null;
        String bName = "java:comp/env/ejb/targetremotenolink";
        home = (TargetSLHome) PortableRemoteObject.narrow(ictx.lookup(bName), TargetSLHome.class);
        assertTrue(home != null);
    }


    /**
     * EJBLocal Reference
     *
     * This test verify that we can look up a LocalHome Object
     * via the java:comp/env subcontext
     */

    public void testEjbLocalRef() throws Exception {

        TargetSLLocalHome home = null;
        String bName = "java:comp/env/ejb/targetlocal";
        home = (TargetSLLocalHome) ictx.lookup(bName);
        assertTrue(home != null);
    }



    /**
     * This test verify we can create a Local session bean and remove it
     */
    public void testLocalCreateRemove() throws Exception {
        TargetLocal tl = getLocalHome().create();
        tl.remove();
    }


    /**
     * This test verify we can create a Local session , access to the bean
     * via its local interface and remove it
     * It verifies we cannot access to the bean after remove
     */
    public void testLocalBusinessMethod1() throws Exception {
        TargetLocal tl = getLocalHome().create();
        assertEquals(20, tl.getTwenty());
        tl.remove();
        try {
            tl.lmethod2("Bye");
            fail("NoSuchObjectLocalException must be raised");
        } catch (NoSuchObjectLocalException e) {
        }
    }


    /**
     * Same test than testLocalBusinessMethod1 but two calls to businessmethod
     */
    public void testLocalBusinessMethod2() throws Exception {
        TargetLocal tl = getLocalHome().create();
        assertEquals(20, tl.getTwenty());
        assertEquals(20, tl.getTwenty());
        tl.remove();
    }

    /**
     * test 3 Local Business Methods
     */
    public void testLocalBusinessMethod3() throws Exception {
        TargetLocal tl = getLocalHome().create();
        assertEquals(20, tl.getTwenty());
        tl.lmethod2("Hello");
        assertEquals(20, tl.getTwenty());
        tl.remove();
    }


    /**
     * test EJBLocalObject.isIdentical on the same bean
     */
    public void testLocalIsIdenticalOnSameBean() throws Exception {
        TargetLocal tl = getLocalHome().create();
        assertTrue(tl.isIdentical(tl));
        tl.remove();
    }


    /**
     * test EJBLocalObject.getPrimary Key
     * This test verify that an  EJB Exception must be catched
     */
    public void testLocalGetPrimaryKey() throws Exception {
        TargetLocal tl = getLocalHome().create();
        try {
            tl.getPrimaryKey();
            fail("getPrimaryKey should raise EJBException on a session bean");
        } catch (EJBException e) {
        } finally {
            tl.remove();
        }
    }



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


}
