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

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.ejb.EJBHome;
import javax.ejb.Handle;
import javax.ejb.HomeHandle;
import javax.rmi.PortableRemoteObject;

import org.objectweb.jonas.jtests.beans.ebasic.Simple;
import org.objectweb.jonas.jtests.beans.ebasic.SimpleHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * test cases common to both suites CMP and BMP.
 */
public abstract class A_Handle extends JTestCase {

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

    protected void setUp() {
        super.setUp();
        useBeans("ebasic", true);
    }

    /**
     * return SimpleHome, that can be either BMP or CMP bean.
     */
    abstract public SimpleHome getHome();

    /**
     * Testing serialization of EJBObject handle
     * 
     *  
     */
    public void testHandle() throws Exception {
        int val1 = 11;
        int val2 = 12;
        Simple s1 = getHome().create("pkforhandle", val1, val2);
        Handle handle1 = s1.getHandle();
        FileOutputStream fos = new FileOutputStream("handleSimple.ser");
        ObjectOutputStream outStream = new ObjectOutputStream(fos);
        //System.out.println("Writing handle to file...");
        outStream.writeObject(handle1);
        outStream.flush();
        fos.close();

        // Deserialize the Handle handleSimple
        FileInputStream fis = new FileInputStream("handleSimple.ser");
        ObjectInputStream inStream = new ObjectInputStream(fis);
        //System.out.println("Reading handle from file...");
        Handle handle2 = (Handle)inStream.readObject();
        fis.close();

        // Reobtain a remote reference .
        //System.out.println("Acquiring reference using deserialized handle2...");
        Object ref = handle2.getEJBObject();
        Simple s2  = (Simple)PortableRemoteObject.narrow(ref, Simple.class);
        
        try {
            assertTrue(s1.isIdentical(s2));
        } finally {
            s1.remove();
        }
    }

    /**
     * Testing serialization of EJBHome handle
     * 
     *  
     */
    public void testHomeHandle() throws Exception {
        SimpleHome  sh1 = getHome();
        Simple s1 = sh1.create("pkforhomehandle",100, 102);
        HomeHandle hhandle1 = sh1.getHomeHandle();
        FileOutputStream fos = new FileOutputStream("homehandleSimple.ser");
        ObjectOutputStream outStream = new ObjectOutputStream(fos);
        //System.out.println("Writing homehandle to file...");
        outStream.writeObject(hhandle1);
        outStream.flush();
        fos.close();
        // Deserialize the Handle handleSimple
        FileInputStream fis = new FileInputStream("homehandleSimple.ser");
        ObjectInputStream inStream = new ObjectInputStream(fis);
        //System.out.println("Reading homehandle from file...");
        HomeHandle hhandle2 = (HomeHandle)inStream.readObject();
        fis.close();
        EJBHome eh = hhandle2.getEJBHome();
        SimpleHome sh2 = (SimpleHome)PortableRemoteObject.narrow(eh,SimpleHome.class) ;
        try {
            Simple s2 = sh2.findByPrimaryKey("pkforhomehandle");
        } finally {
           s1.remove(); 
        }

    }
}
