/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.examples.ejb.stateful;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.ejb.EJBMetaData;
import javax.ejb.Handle;
import javax.ejb.HomeHandle;
import javax.naming.Context;
import javax.naming.InitialContext;

import junit.framework.TestCase;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class TestMain extends TestCase {

    private static TestStatefulHome home = null;
    private static TestStateful world = null;

    static {
        try {
            Context ctx = new InitialContext();
            Object obj = ctx.lookup("/ejb/TestStatefulEJB");
            home = (TestStatefulHome) javax.rmi.PortableRemoteObject.narrow(obj, TestStatefulHome.class);
//      ((ExtendedEJBHome)home).useProtocol("soap");
            world = home.create();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    protected void setUp() throws Exception {
    }

    public void testToString() {
        System.out.println(home.toString());
        System.out.println(world);
    }

    public void testGetEJBMetaData() {
        try {
            System.out.print("test getEJBMetaData()...");
            EJBMetaData meta = home.getEJBMetaData();
            System.out.println("EJBMetaData: " + meta);
        }
        catch(Exception e) {
            fail(e.toString());
        }
    }

    public void testGetWord() {
        try {
            assertTrue(world.getName().equals("Hello,Stateful SessionBean!"));
        }
        catch(Exception e) {
            e.printStackTrace();
            fail(e.toString());
        }
    }

    public void testSetWord() {
        try {
            System.out.print("test setName()...");
            world.setName("Hello,All");
            // for test passivate & active
//      Thread.sleep(70000L);
            assertTrue(world.getName().equals("Hello,All"));
            System.out.println("OK!");
        }
        catch(Exception e) {
            e.printStackTrace();
            fail(e.toString());
        }

    }

    public void testHomeHandle() {
        try {
            System.out.print("test getHomeHandle()...");
            HomeHandle homeHandle = home.getHomeHandle();
            writeHandle(homeHandle);
            homeHandle = (HomeHandle) readHandle();
            home = (TestStatefulHome) homeHandle.getEJBHome();
            world = home.create();
            assertTrue(world.getName().equals("Hello,Stateful SessionBean!"));
            System.out.println("OK!");
        }
        catch(Exception e) {
            fail(e.toString());
        }
    }

    public void testHandle() {
        try {
            System.out.print("test getHandle()...");
            Handle handle = world.getHandle();
            writeHandle(handle);
            handle = (Handle) readHandle();
            world = (TestStateful) handle.getEJBObject();
            assertTrue(world.getName().equals("Hello,Stateful SessionBean!"));
            System.out.println("OK!");
        }
        catch(Exception e) {
            fail(e.toString());
        }
    }

    private void writeHandle(Object obj) throws Exception {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("../temp/" + this.getClass() + ".ser"));
        out.writeObject(obj);
        out.close();
    }

    private Object readHandle() throws Exception {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("../temp/" + this.getClass() + ".ser"));
        Object handle = in.readObject();
        in.close();
        return handle;
    }


    public static void main(String[] args) {
        junit.textui.TestRunner.run(TestMain.class);
    }

}
