/*
 * 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 javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.objectweb.jonas.jtests.beans.inherit.Person;
import org.objectweb.jonas.jtests.beans.inherit.PersonHome;
import org.objectweb.jonas.jtests.beans.inherit.User;
import org.objectweb.jonas.jtests.beans.inherit.UserHome;
import org.objectweb.jonas.jtests.util.JTestCase;

/**
 * This test check it's possible to use inheritance to develop beans.
 * (The test is essentially do at the beans compilation and beans deployment !!!)
 * Beans used: inherit
 * @author Helene Joanin (jonas team)
 */
public class F_Inherit extends JTestCase {

    private static String BEAN_HOME_PERSON = "inheritPersonECHome";
    protected static PersonHome phome = null;
    private static String BEAN_HOME_USER = "inheritUserECHome";
    protected static UserHome uhome = null;
    private static String BEAN_HOME_DERIVED_USER = "inheritDerivedUserECHome";
    protected static UserHome duhome = null;

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

    protected void setUp() {
	super.setUp();
	if ((phome == null) || (uhome == null) || (duhome == null)) {
	    useBeans("inherit", true);
	    try {
		phome = (PersonHome) PortableRemoteObject.narrow(
                                        ictx.lookup(BEAN_HOME_PERSON),
					PersonHome.class);
		uhome = (UserHome) PortableRemoteObject.narrow(
                                        ictx.lookup(BEAN_HOME_USER),
					UserHome.class);
		duhome = (UserHome) PortableRemoteObject.narrow(
                                        ictx.lookup(BEAN_HOME_DERIVED_USER),
					UserHome.class);
	    } catch (NamingException e) {
		fail("Cannot get bean home: "+e.getMessage());
	    }
	}
    }

    /**
     * test that the bean Person works well
     */
    public void testPerson() throws Exception {
	int id = 0;
	String name = "Rose";
	int age = 1; // initial value for Person

	Person prs = phome.create(id, name);
        try {
            assertEquals("Age", age, prs.getAge());
            assertEquals("obj.getValue(10)", "10", prs.getValue(10));
        } finally {
            prs.remove();
        }

	assertEquals("home.getValue()", "ejbHomeGetValue", phome.getValue(10));
    }

    /**
     * test that the bean User works well
     */
    public void testUser() throws Exception {
	int id = 10;
	String name = "Helene";
	String passwd = "oode";
	int age = 40;


	User usr = uhome.create(id, name);
        try {
            usr.changePassword(passwd);
            usr.setAge(age);
            assertEquals("Password", passwd, usr.getPassword());
            assertEquals("Age", age, usr.getAge());
            assertEquals("getData()", "UserEC.getData", usr.getData());
            assertEquals("obj.getValue(10)", "10", usr.getValue(10));
        } finally {
            usr.remove();
        }

	assertEquals("home.getValue()", "ejbHomeGetValue", uhome.getValue(10));
    }

    /**
     * test that the bean DerivedUser works well
     */
    public void testDerivedUser() throws Exception {
	int id = 20;
	String name = "Eric";

	User dusr = duhome.create(id, name);
        try {
            assertEquals("getData()", "DerivedUserEC.getData", dusr.getData());
        } finally {
            dusr.remove();
        }
    }

    public static Test suite() {
	return new TestSuite(F_Inherit.class);
    }

    public static void main (String args[]) {
	String testtorun = null;
	// Get args
	for (int argn = 0; argn < args.length; argn++) {
	    String s_arg = args[argn];
	    Integer i_arg;
	    if (s_arg.equals("-n")) {
		testtorun = args[++argn];
	    }
	}
	if (testtorun == null) {
	    junit.textui.TestRunner.run(suite());
	} else {
	    junit.textui.TestRunner.run(new F_Inherit(testtorun));
	}
    }
}
