User: starksm 
  Date: 02/02/18 15:54:33

  Added:       src/main/org/jboss/test/jbossmx/implementation/server
                        ContextCLTestCase.java
  Log:
  Add test case for an mbean requiring its loader to be used as the operation
  thread context class loader.
  
  Revision  Changes    Path
  1.1                  
jbosstest/src/main/org/jboss/test/jbossmx/implementation/server/ContextCLTestCase.java
  
  Index: ContextCLTestCase.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.test.jbossmx.implementation.server;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.InputStream;
  import java.net.URL;
  import java.util.jar.JarOutputStream;
  import java.util.zip.ZipEntry;
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.ObjectName;
  import javax.management.RuntimeErrorException;
  
  import junit.extensions.TestSetup;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.log4j.Category;
  import org.apache.log4j.PatternLayout;
  import org.apache.log4j.WriterAppender;
  
  import org.jboss.test.JBossTestSetup;
  import org.jboss.test.jbossmx.implementation.TestCase;
  import org.jboss.test.jbossmx.implementation.server.support.ContextCL;
  import org.jboss.test.jbossmx.implementation.server.support.TestClassLoader;
  
  /** Test of the mbean operation invocation thread context class loader.
   *
   * @author  [EMAIL PROTECTED]
   * @version $Revision: 1.1 $
   */
  public class ContextCLTestCase extends TestCase 
  {
     private static URL dataClassURL;
     private static File dataJar;
  
     public ContextCLTestCase(String name)
     {
        super(name);
     }
  
     public void testInvokeNeedingTCL() throws Exception
     {
        MBeanServer server = MBeanServerFactory.createMBeanServer();
  
        // Register the mbean class loader
        URL[] urls = {dataJar.toURL()};
        TestClassLoader loader = new TestClassLoader(urls);
        ObjectName loaderName = new 
ObjectName("org.jboss.test.jbossmx.implementation.server.support:id=" 
+dataJar.getName());
        if( server.isRegistered(loaderName) == false )
           server.registerMBean(loader, loaderName);
        getLog().info("Registered TestClassLoader, urls="+urls[0]);
  
        // Create the ContextCL MBean using the TestClassLoader
        try
        {
           ObjectName beanName = new 
ObjectName("org.jboss.test.jbossmx.implementation.server.support:test=ContextCLTestCase");
           
server.createMBean("org.jboss.test.jbossmx.implementation.server.support.ContextCL", 
beanName, loaderName);
           getLog().info("Created ContextCL MBean");
  
           // Invoke the useTestData op to test the thread context class loader
           server.invoke(beanName, "useTestData", null, null);
           getLog().info("Invoked ContextCL.useTestData");
        }
        catch(RuntimeErrorException e)
        {
           getLog().error("NestedError", e.getTargetError());
           throw e;
        }
  
        MBeanServerFactory.releaseMBeanServer(server);
     }
  
     /** Remove the local TestData.class from the classpath and create a tmp
      *
      * @exception Exception  Description of Exception
      */
     public void createTestDataJar() throws Exception
     {
        dataJar = new File("testdata.jar");
        // Find the TestData class
        dataClassURL = 
getClass().getResource("/org/jboss/test/jbossmx/implementation/server/support/TestData.class");
        if( dataClassURL == null && dataJar.exists() == false )
           fail("Failed to find 
/org/jboss/test/jbossmx/implementation/server/support/TestData.class");
        if( dataClassURL != null )
        {
           getLog().info("Found TestData at: " + dataClassURL);
           // Build a jar file containing only the TestData.class
           FileOutputStream fos = new FileOutputStream(dataJar);
           JarOutputStream jos = new JarOutputStream(fos);
           ZipEntry entry = new 
ZipEntry("org/jboss/test/jbossmx/implementation/server/support/TestData.class");
           jos.putNextEntry(entry);
           InputStream dataIS = dataClassURL.openStream();
           byte[] bytes = new byte[512];
           int read = 0;
           while( (read = dataIS.read(bytes)) > 0 )
              jos.write(bytes, 0, read);
           jos.closeEntry();
           dataIS.close();
           getLog().info("Created TestData.class Jar at: "+dataJar.getAbsolutePath());
  
           // Now remote the TestData.class file from this classpath
           File dataClassFile = new File(dataClassURL.getFile());
           getLog().info("Removed TestData.class File: " + dataClassFile.delete());
        }
  
     }
  
     /**
      * Setup the test suite.
      */
     public static Test suite()
     {
        TestSuite suite = new TestSuite();
        suite.addTest(new ContextCLTestCase("createTestDataJar"));
        suite.addTest(new ContextCLTestCase("testInvokeNeedingTCL"));
        return suite;
     }
  
     public static void main(java.lang.String[] args)
     {
        // Set up a simple configuration that logs on the console.
        Category root = Category.getRoot();
        root.addAppender(new WriterAppender(new PatternLayout("%x%m%n"), System.out));
        Test suite = ContextCLTestCase.suite();
        junit.textui.TestRunner.run(suite);
     }
  }
  
  
  

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

Reply via email to