/*
 * 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.relation.lcp.Session2;
import org.objectweb.jonas.jtests.beans.relation.lcp.Session2Home;
import org.objectweb.jonas.jtests.util.JTestCase;


/**
 * This is a test suite on CMP 2 and relations ships.
 * It reproduces a problem found by a JOnAS user.
 * @author Helene Joanin
 */
public class F_Relation_lcpEC2 extends JTestCase {

    private static Session2Home home = null;

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

    protected boolean isInit = false;

    protected void setUp() {
        super.setUp();
        if (!isInit) {
            useBeans("lcp", true);
            try {
                home = (Session2Home) PortableRemoteObject.narrow(ictx.lookup("relation_lcp_Session2"),
                                                                  Session2Home.class);
            } catch (NamingException e) {
                fail("Cannot get bean home: " + e.getMessage());
            }
            isInit = true;
        }
    }

    /**
     * Test the getting of existing children.
     * @throws Exception
     */
    public void testGetExistChildren() throws Exception {
        Session2 s = home.create();
        assertEquals("c1_1,c1_2", s.getSimpleChildren("p1"));
    }

    /**
     * Test the getting of no existing children.
     * @throws Exception
     */
    public void testGetNoExistChildren() throws Exception {
        Session2 s = home.create();
        assertEquals("", s.getSimpleChildren("p0"));
    }

    /**
     * Test the getting of existing parent.
     * @throws Exception
     */
    public void testGetExistParent() throws Exception {
        Session2 s = home.create();
        assertEquals("p1", s.getChildParent("c1_1"));
    }

    /**
     * Test the getting of no existing parent.
     * @throws Exception
     */
    public void testGetNoExistParent() throws Exception {
        Session2 s = home.create();
        assertEquals("", s.getChildParent("c0"));
    }

    protected boolean initStateOK() throws Exception {
        return true;
    }

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

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