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

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 TestStatelessHome home = null;
    private static TestStateless statelessBean = null;

    static {
        try {
            Context ctx = new InitialContext();
            Object obj = ctx.lookup("/ejb/TestStatelessEJB");
            home = (TestStatelessHome) javax.rmi.PortableRemoteObject.narrow(obj, TestStatelessHome.class);
            statelessBean = home.create();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    // setup 方法每调用一个 test 方法都会事先调用
    protected void setUp() throws Exception {
    }

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


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

    }

    public void testGetVoice() {
        try {
            System.out.print("test method getVoice()...");
            assertTrue(statelessBean.getVoice().equals("Hello,Stateless SessionBean!"));
            System.out.println("OK!");
        }
        catch(Exception e) {
            e.printStackTrace();
            fail(e.toString());
        }
    }

    public void testHomeHandle() {
        try {
            System.out.print("test HomeHandle serialize & deserialize...");
            HomeHandle homeHandle = home.getHomeHandle();
            writeHandle(homeHandle);
            homeHandle = (HomeHandle) readHandle();
            home = (TestStatelessHome) homeHandle.getEJBHome();
            statelessBean = home.create();
            assertTrue(statelessBean.getVoice().equals("Hello,Stateless SessionBean!"));
            System.out.println("OK!");
        }
        catch(Exception e) {
            e.printStackTrace();
            fail(e.toString());
        }
    }

    public void testHandle() {
        try {
            System.out.print("test Handle serialize & deserialize...");
            Handle handle = statelessBean.getHandle();
            writeHandle(handle);
            handle = (Handle) readHandle();
            statelessBean = (TestStateless) handle.getEJBObject();
            assertTrue(statelessBean.getVoice().equals("Hello,Stateless SessionBean!"));
            System.out.println("OK!");
        }
        catch(Exception e) {
            e.printStackTrace();
            fail(e.toString());
        }
    }

    public void testGetWorld() {
        try {
            System.out.print("test getCount()...");
            int count = statelessBean.getCount();
            System.out.println(count + " OK!");
        }
        catch(Exception e) {
            e.printStackTrace();
            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);
    }

}
