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

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.transaction.UserTransaction;
import junit.framework.TestCase;
import org.objectweb.jonas.adm.AdmInterface;

/**
 * JTestCase extends TestCase to provide a set of standard routines
 * used in jonas tests.
 */
public abstract class JTestCase extends TestCase {
    protected static String jonasName = "jonas"; // change this XXX
    protected static String testtorun = null;
    protected static Context ictx = null;
    public static UserTransaction utx = null; // public besause threads accesses this
    private static AdmInterface admI = null;

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

    private Context getInitialContext() throws NamingException {

        String registryPort = System.getProperty("jonas.registryport");
        Context ctx = null;

        if (registryPort != null) {
            // it means we are using David
            Properties prop = new Properties();
            prop.put("java.naming.factory.initial", "com.sun.jndi.rmi.registry.RegistryContextFactory");
            prop.put("java.naming.provider.url", "rmi://localhost:" + registryPort);
            ctx = new InitialContext(prop);
        } else {
            ctx = new InitialContext(); 
        }
        return ctx;
    }

    /**
     * common setUp routine, used for every test.
     */
    protected void setUp() throws Exception {
        // get InitialContext
        if (ictx == null) {
            ictx = getInitialContext();
        }

        // get UserTransaction
        if (utx == null) {
            utx = (UserTransaction) PortableRemoteObject.narrow(ictx.lookup("javax.transaction.UserTransaction"), UserTransaction.class);
        }
    }

    /**
     * load a bean jar file in the jonas server
     */
    public void useBeans(String filename) {

        try {
            // Load bean in EJBServer if not already loaded.
            if (ictx == null) {
                ictx = getInitialContext();
            }
            if (admI == null) {
                admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
            }
            if (!  admI.isLoaded(filename + ".jar")) {
                admI.addBeans(filename + ".jar");
            }
        } catch (Exception e) {
            System.err.println("Cannot load bean: " + e);
        }
    }

    /**
     * Set the Default Transaction Timeout value on the server.
     * @param tt timeout in seconds
     */
    public static void stopTxAt(int tt) {
        try {
            if (admI == null) {
                admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
            }
            admI.setTransactionTimeout(tt);
        } catch (Exception e) {
            System.err.println("Cannot set transaction timeout: " + e);
        }
    }

    /**
     * for debugging : Uncomment the line.
     */
    public static void debug(String msg) {
        //System.out.println(msg);
    }

    /**
     * sleep n millisec.
     */
    public void sleep(int msec) {
        try {
            Thread.sleep(msec);
        } catch (InterruptedException e) {
            fail(e.toString());
        }
    }

    public void testEmpty() throws Exception {
    }
}
