/*
 * 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
 */

package org.objectweb.jonas.jtests.clients.entity;


import java.util.Collection;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.jonas.jtests.beans.ebasic.*;


/**
 * This is an advanced test suite for home interface on pk auto incremented CMP2
 */
public class F_PkAutoEC2 extends A_Cmp2Util {

    private static String BEAN_HOME_PKAUTO = "pkautoobjectcmp2Home";

    protected static pkautoObjectHome pkautoobjectcmp2home = null;

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

    protected boolean isInit = false;

    protected void setUp() {
        super.setUp();
        boolean ok = false;
        int nbtry = 0;
        while (!ok && nbtry < 3) {
            if (!isInit) {
                // load bean if not loaded yet
                useBeans("ebasic", false);
                // lookup home used in the tests
                try {
                    pkautoobjectcmp2home = (pkautoObjectHome) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_PKAUTO),
                                                                              pkautoObjectHome.class);
                } catch (NamingException e) {
                    fail("Cannot get bean home: " + e.getMessage());
                }
                // check if tables have been initialized
                try {
                    Collection bpk = pkautoobjectcmp2home.findAll();
                    if (bpk.isEmpty()) {
                        throw new Exception("Table vide");
                    }
                } catch (Exception e) {
                    // Make the initialization needed for the tests
                    try {
                        utx.begin(); 
                        for (int i = 1; i <= 10; i++) {
                            pkautoobjectcmp2home.create(10, 20);
                        }
                    } catch (Exception i) {
                        i.printStackTrace(System.out);
                        fail("InitialState creation problem: " + i);
                    } finally {
                        try {
                            utx.commit();
                        } catch (Exception ii) {
                        }
                    }
                }
                isInit = true;
            }
            // Check that all is OK. Sometimes, a test has failed and has corrupted
            // the bean state in the database. We must unload and reload the bean then.
            nbtry++;
            try {
                if (initStateOK()) {
                    ok = true;
                }
            } catch (Exception e) {
                e.printStackTrace(System.out);
            }
            if (!ok) {
                isInit = false;
                unloadBeans("ebasic");
            }
        }
    }


    /*
     * Check that we are in the same state as after the tables creation for thoses beans A and B
     * (ie if it is the initial state)
     */
    boolean initStateOK() throws Exception {
        msgerror = new StringBuffer();
        Collection bpk = pkautoobjectcmp2home.findAll();
        return (bpk.size() == 10);
    }

    /**
     *
     */
    public void testEmpty() throws Exception {
        checkIsInitialState();
    }

    /**
     */
    public static Test suite() {
        return new TestSuite(F_PkAutoEC2.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_PkAutoEC2(testtorun));
        }
    }
}

