User: d_jencks
  Date: 01/09/12 12:57:32

  Added:       src/main/org/jboss/test JBossTestCase.java
  Log:
  Added a JBossTestCase class with logging, access to mbean server, initial context, 
and deploy/undeploy methods for j2ee packages and service packages.  Converted 
ConnectionFactoryLoaderUnitTestCase and DeployServiceUnitTestCase to descend from 
JBossTestCase. Fixed cfl test case to work again
  
  Revision  Changes    Path
  1.1                  jbosstest/src/main/org/jboss/test/JBossTestCase.java
  
  Index: JBossTestCase.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.test;
  
  import java.io.File;
  import java.net.InetAddress;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.HashSet;
  import java.util.Set;
  import javax.management.MBeanException;
  import javax.management.MBeanRegistrationException;
  import javax.management.MalformedObjectNameException;
  import javax.management.ObjectInstance;
  import javax.management.ObjectName;
  import javax.management.ReflectionException;
  import javax.management.RuntimeErrorException;
  import javax.management.RuntimeMBeanException;
  import javax.management.RuntimeOperationsException;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingEnumeration;
  import javax.naming.NamingException;
  
  import junit.framework.*;
  
  import org.apache.log4j.Category;
  
  import org.jboss.jmx.connector.rmi.RMIConnector;
  
  /**
   * This is the standard base test case for jboss junit test cases. It supplies
   * access to log4j logging, the jboss jmx server, jndi, and a method for
   * deploying ejb packages. You may supply the name of the machine the jboss
   * server is on with the system property jbosstest.server.name (default
   * getInetAddress().getLocalHost().getHostName()) and the directory for
   * deployable packages with the system property jbosstest.deploy.dir (default
   * ../lib).
   *
   * @author    <a href="[EMAIL PROTECTED]">David Jencks</a>
   * @version   $Revision: 1.1 $
   */
  public class JBossTestCase
         extends TestCase
  {
  
     // Constants -----------------------------------------------------
     private final static String serviceDeployerName = 
"JBOSS-SYSTEM:service=ServiceDeployer";
     private final static String j2eeDeployerName = "J2EE:service=J2eeDeployer";
  
     // Attributes ----------------------------------------------------
     private RMIConnector server;
     private Category log;
     private InitialContext initialContext;
  
     private HashSet deployed = new HashSet();
  
  
     // Static --------------------------------------------------------
     // Constructors --------------------------------------------------
     /**
      * Constructor for the JBossTestCase object
      *
      * @param name  Test case name
      */
     public JBossTestCase(String name)
     {
        super(name);
     }
  
     // Public --------------------------------------------------------
  
  
     /**
      * The JUnit setup method
      *
      * @exception Exception  Description of Exception
      */
     public void setUp() throws Exception
     {
        log = Category.getInstance(getClass().getName());
        String serverName = System.getProperty("jbosstest.server.name");
        if (serverName == null)
        {
           serverName = InetAddress.getLocalHost().getHostName();
        }
        initialContext = new InitialContext();
        server = (RMIConnector)getInitialContext().lookup("jmx:" + serverName + 
":rmi");
     }
  
     /**
      * The teardown method for JUnit
      *
      * @exception Exception  Description of Exception
      */
     public void tearDown() throws Exception
     {
        server = null;
     }
  
     /**
      * This just checks the server is there... so you should get at least one
      * success!
      *
      * @exception Exception  Description of Exception
      */
     public void testServerFound() throws Exception
     {
        assertTrue("Server was not found", server != null);
     }
  
  
     //protected---------
  
     /**
      * Gets the InitialContext attribute of the JBossTestCase object
      *
      * @return   The InitialContext value
      */
     protected InitialContext getInitialContext()
     {
        return initialContext;
     }
  
     /**
      * Gets the Server attribute of the JBossTestCase object
      *
      * @return   The Server value
      */
     protected RMIConnector getServer()
     {
        return server;
     }
  
     /**
      * Gets the Log attribute of the JBossTestCase object
      *
      * @return   The Log value
      */
     protected Category getLog()
     {
        return log;
     }
  
     /**
      * Gets the ServiceDeployerName attribute of the JBossTestCase object
      *
      * @return                                  The ServiceDeployerName value
      * @exception MalformedObjectNameException  Description of Exception
      */
     protected ObjectName getServiceDeployerName() throws MalformedObjectNameException
     {
        return new ObjectName(serviceDeployerName);
     }
  
     /**
      * Gets the J2eeDeployerName attribute of the JBossTestCase object
      *
      * @return                                  The J2eeDeployerName value
      * @exception MalformedObjectNameException  Description of Exception
      */
     protected ObjectName getJ2eeDeployerName() throws MalformedObjectNameException
     {
        return new ObjectName(j2eeDeployerName);
     }
  
  
     /**
      * Returns the deployment directory to use. This does it's best to figure out
      * where you are looking. If you supply a complete url, it returns it.
      * Otherwise, it looks for jbosstest.deploy.dir or if missing ../lib. Then it
      * tries to construct a file url or a url.
      *
      * @param filename                   name of the file/url you want
      * @return                           A more or less canonical string for the
      *      url.
      * @exception MalformedURLException  Description of Exception
      */
     protected String getDeployURL(final String filename) throws MalformedURLException
     {
        //First see if it is already a complete url.
        try
        {
           return new URL(filename).toString();
        }
        catch (MalformedURLException mue)
        {
        }
        //OK, lets see if we can figure out what it might be.
        String deployDir = System.getProperty("jbosstest.deploy.dir");
        if (deployDir == null)
        {
           deployDir = "../lib";
        }
        String url = deployDir + "/" + filename;
        //try to canonicalize the strings a bit.
        if (new File(url).exists())
        {
           return new File(url).toURL().toString();
        }
        else
        {
           return new URL(url).toString();
        }
     }
  
  
     //is this good for something??????
     /**
      * Gets the Deployed attribute of the JBossTestCase object
      *
      * @param name  Description of Parameter
      * @return      The Deployed value
      */
     protected boolean isDeployed(String name)
     {
        return deployed.contains(name);
     }
  
     /**
      * invoke wraps an invoke call to the mbean server in a lot of exception
      * unwrapping.
      *
      * @param name           ObjectName of the mbean to be called
      * @param method         mbean method to be called
      * @param args           Object[] of arguments for the mbean method.
      * @param sig            String[] of types for the mbean methods parameters.
      * @return               Object returned by mbean method invocation.
      * @exception Exception  Description of Exception
      */
     protected Object invoke(ObjectName name, String method, Object[] args, String[] 
sig) throws Exception
     {
        try
        {
           return getServer().invoke(name, method, args, sig);
        }
        catch (MBeanException e)
        {
           log.error("MbeanException", e.getTargetException());
           throw e.getTargetException();
        }
        catch (ReflectionException e)
        {
           log.error("ReflectionException", e.getTargetException());
           throw e.getTargetException();
        }
        catch (RuntimeOperationsException e)
        {
           log.error("RuntimeOperationsException", e.getTargetException());
           throw e.getTargetException();
        }
        catch (RuntimeMBeanException e)
        {
           log.error("RuntimeMbeanException", e.getTargetException());
           throw e.getTargetException();
        }
        catch (RuntimeErrorException e)
        {
           log.error("RuntimeErrorException", e.getTargetError());
           throw e.getTargetError();
        }
     }
  
     /**
      * Deploy a j2ee package with the J2E deployer. The supplied name is
      * interpreted as a url, or as a filename in jbosstest.deploy.lib or ../lib.
      *
      * @param name           filename/url of package to deploy.
      * @exception Exception  Description of Exception
      */
     protected void deployJ2ee(String name) throws Exception
     {
        String deployName = getDeployURL(name);
        invoke(getJ2eeDeployerName(),
              "deploy",
              new Object[]{deployName},
              new String[]{"java.lang.String"});
        setDeployed(deployName);
     }
  
     /**
      * Undeploy a j2ee package with the J2E deployer. The supplied name is
      * interpreted as a url, or as a filename in jbosstest.deploy.lib or ../lib.
      *
      * @param name           filename/url of package to undeploy.
      * @exception Exception  Description of Exception
      */
     protected void undeployJ2ee(String name) throws Exception
     {
        String deployName = getDeployURL(name);
        invoke(getJ2eeDeployerName(),
              "undeploy",
              new Object[]{deployName},
              new String[]{"java.lang.String"});
        setUnDeployed(deployName);
     }
  
     /**
      * Deploy a jboss service package with the service deployer. The supplied
      * name is interpreted as a url, or as a filename in jbosstest.deploy.lib or
      * ../lib.
      *
      * @param name           filename/url of package to deploy.
      * @exception Exception  Description of Exception
      */
     protected void deployService(String name) throws Exception
     {
        String deployName = getDeployURL(name);
        invoke(getServiceDeployerName(),
              "deploy",
              new Object[]{deployName},
              new String[]{"java.lang.String"});
        setDeployed(deployName);
     }
  
     /**
      * Undeploy a jboss service package with the service deployer. The supplied
      * name is interpreted as a url, or as a filename in jbosstest.deploy.lib or
      * ../lib.
      *
      * @param name           filename/url of package to undeploy.
      * @exception Exception  Description of Exception
      */
     protected void undeployService(String name) throws Exception
     {
        String deployName = getDeployURL(name);
        invoke(getServiceDeployerName(),
              "undeploy",
              new Object[]{deployName},
              new String[]{"java.lang.String"});
        setUnDeployed(deployName);
     }
  
  
     //private methods--------------
  
     private void setDeployed(String name)
     {
        deployed.add(name);
     }
  
     private void setUnDeployed(String name)
     {
        deployed.remove(name);
     }
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to