[jbpm-commits] JBoss JBPM SVN: r2630 - jbpm3/trunk/modules/core/src/test/java/org/jbpm/jpdl/par.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Oct 27 12:18:55 EDT 2008


Author: tom.baeyens at jboss.com
Date: 2008-10-27 12:18:55 -0400 (Mon, 27 Oct 2008)
New Revision: 2630

Modified:
   jbpm3/trunk/modules/core/src/test/java/org/jbpm/jpdl/par/ProcessArchiveClassLoadingDbTest.java
   jbpm3/trunk/modules/core/src/test/java/org/jbpm/jpdl/par/ResourceAction.java
Log:
[JBPM-1448] reworked classloading tests, re-added package information

Modified: jbpm3/trunk/modules/core/src/test/java/org/jbpm/jpdl/par/ProcessArchiveClassLoadingDbTest.java
===================================================================
--- jbpm3/trunk/modules/core/src/test/java/org/jbpm/jpdl/par/ProcessArchiveClassLoadingDbTest.java	2008-10-27 16:03:08 UTC (rev 2629)
+++ jbpm3/trunk/modules/core/src/test/java/org/jbpm/jpdl/par/ProcessArchiveClassLoadingDbTest.java	2008-10-27 16:18:55 UTC (rev 2630)
@@ -21,113 +21,194 @@
  */
 package org.jbpm.jpdl.par;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.List;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.jbpm.db.AbstractDbTestCase;
 import org.jbpm.graph.def.ProcessDefinition;
 import org.jbpm.graph.exe.ProcessInstance;
 import org.jbpm.util.ClassLoaderUtil;
 import org.jbpm.util.IoUtil;
 
-public class ProcessArchiveClassLoadingDbTest extends AbstractDbTestCase
-{
+public class ProcessArchiveClassLoadingDbTest extends AbstractDbTestCase {
 
-  public static boolean isLoadedActionHandlerExecuted = false;
+  public static Log log = LogFactory.getLog(ProcessArchiveClassLoadingDbTest.class);
 
-  String getTestClassesDir()
-  {
+  String getTestClassesDir() {
     return ProcessArchiveDeploymentDbTest.class.getProtectionDomain().getCodeSource().getLocation().getFile();
   }
 
-  public void testProcessClassLoading() throws Exception
-  {
-    // first we read the processLoadedActionHandlerClassBytes from the file system
-    InputStream inputStream = ClassLoaderUtil.getStream("org/jbpm/jpdl/par/ProcessLoadedActionHandler.class");
-    byte[] processLoadedActionHandlerClassBytes = IoUtil.readBytes(inputStream);
-    inputStream.close();
+  public void testExecuteResourceUsingProcess() throws Exception {
+    // create a process archive
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    ZipOutputStream zipOutputStream = new ZipOutputStream(baos);
+    addEntry(zipOutputStream, "processdefinition.xml", "org/jbpm/jpdl/par/resourceprocess.xml");
+    addEntry(zipOutputStream, "classes/org/jbpm/jpdl/par/ResourceAction.class", "org/jbpm/jpdl/par/ResourceAction.class");
+    addEntry(zipOutputStream, "classes/org/jbpm/jpdl/par/classresource.txt", "org/jbpm/jpdl/par/classresource.txt");
+    addEntry(zipOutputStream, "archiveresource.txt", "org/jbpm/jpdl/par/archiveresource.txt");
+    zipOutputStream.close();
+    byte[] zipBytes = baos.toByteArray();
 
-    // then, we delete the ProcessLoadedActionHandler from the
-    // test classes dir, thereby removing it from this class's classloader
-    String classFileName = getTestClassesDir() + "/org/jbpm/jpdl/par/ProcessLoadedActionHandler.class";
-    if (!new File(classFileName).delete())
-    {
-      fail("couldn't delete " + classFileName);
+    // move the files
+    String classOriginalName = getTestClassesDir() + "org/jbpm/jpdl/par/ResourceAction.class";
+    String classTmpName = classOriginalName + ".hiddenFromTestClasspath";
+    assertTrue(new File(classOriginalName).renameTo(new File(classTmpName)));
+
+    String resourceOriginalName = getTestClassesDir() + "org/jbpm/jpdl/par/classresource.txt";
+    String resourceTmpName = resourceOriginalName + ".hiddenFromTestClasspath";
+    assertTrue(new File(resourceOriginalName).renameTo(new File(resourceTmpName)));
+
+    String archiveResourceOriginalName = getTestClassesDir() + "org/jbpm/jpdl/par/archiveresource.txt";
+    String archiveResourceTmpName = archiveResourceOriginalName + ".hiddenFromTestClasspath";
+    assertTrue(new File(archiveResourceOriginalName).renameTo(new File(archiveResourceTmpName)));
+
+    try {
+      ClassLoader testClassLoader = ProcessArchiveClassLoadingDbTest.class.getClassLoader();
+      assertNull(testClassLoader.getResource("org/jbpm/jpdl/par/classresource.txt"));
+      assertNull(testClassLoader.getResource("org/jbpm/jpdl/par/archiveresource.txt"));
+      try {
+        testClassLoader.loadClass("org.jbpm.jpdl.par.ResourceAction");
+        fail("expected exception");
+      } catch (ClassNotFoundException e) {
+        // OK
+      }
+
+      // deploy the process archive
+      ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBytes));
+      ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream);
+      jbpmContext.deployProcessDefinition(processDefinition);
+      try {
+        newTransaction();
+
+        ProcessInstance processInstance = jbpmContext.newProcessInstance("resourceprocess");
+        processInstance.signal();
+      } finally {
+        jbpmContext.getGraphSession().deleteProcessDefinition(processDefinition.getId());
+      }
+    } finally {
+      // put the files back into original position
+      new File(classTmpName).renameTo(new File(classOriginalName));
+      new File(resourceTmpName).renameTo(new File(resourceOriginalName));
+      new File(archiveResourceTmpName).renameTo(new File(archiveResourceOriginalName));
     }
+  }
 
-    try
-    {
-      try
-      {
-        // now, we gonna check if the ProcessArchiveDeployerDbTest is in the classpath of
-        // this test
-        ProcessArchiveClassLoadingDbTest.class.getClassLoader().loadClass("org.jbpm.jpdl.par.ProcessLoadedActionHandler");
+  public void testInstantiateClassInArchive() throws Exception {
+    // create a process archive
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    ZipOutputStream zipOutputStream = new ZipOutputStream(baos);
+    addEntry(zipOutputStream, "processdefinition.xml", "org/jbpm/jpdl/par/instantiateprocess.xml");
+    addEntry(zipOutputStream, "classes/org/jbpm/jpdl/par/InstantiateAction.class", "org/jbpm/jpdl/par/InstantiateAction.class");
+    addEntry(zipOutputStream, "classes/org/jbpm/jpdl/par/InstantiateClass.class", "org/jbpm/jpdl/par/InstantiateClass.class");
+    zipOutputStream.close();
+    byte[] zipBytes = baos.toByteArray();
+
+    // move the files
+    String instantiateActionOriginalName = getTestClassesDir() + "org/jbpm/jpdl/par/InstantiateAction.class";
+    String instantiateActionTmpName = instantiateActionOriginalName + ".hiddenFromTestClasspath";
+    assertTrue(new File(instantiateActionOriginalName).renameTo(new File(instantiateActionTmpName)));
+
+    String instantiateClassOriginalName = getTestClassesDir() + "org/jbpm/jpdl/par/InstantiateClass.class";
+    String instantiateClassTmpName = instantiateClassOriginalName + ".hiddenFromTestClasspath";
+    assertTrue(new File(instantiateClassOriginalName).renameTo(new File(instantiateClassTmpName)));
+
+    try {
+      ClassLoader testClassLoader = ProcessArchiveClassLoadingDbTest.class.getClassLoader();
+      try {
+        testClassLoader.loadClass("org.jbpm.jpdl.par.InstantiateAction");
         fail("expected exception");
+      } catch (ClassNotFoundException e) {
+        // OK
       }
-      catch (ClassNotFoundException e)
-      {
+      try {
+        testClassLoader.loadClass("org.jbpm.jpdl.par.InstantiateClass");
+        fail("expected exception");
+      } catch (ClassNotFoundException e) {
         // OK
       }
 
-      // next we create a process archive that includes the class file we just
-      // deleted from the file system.
-      String fileName = getTestClassesDir() + "/testarchive3.par";
-      FileOutputStream fileOutputStream = new FileOutputStream(fileName);
-      ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
-      addEntry(zipOutputStream, "processdefinition.xml", "org/jbpm/jpdl/par/classloadingprocess.xml");
-      addEntry(zipOutputStream, "classes/org/jbpm/jpdl/par/ProcessLoadedActionHandler.class", processLoadedActionHandlerClassBytes);
-      zipOutputStream.close();
+      // deploy the process archive
+      ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBytes));
+      ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream);
+      jbpmContext.deployProcessDefinition(processDefinition);
+      try {
+        newTransaction();
 
-      // and then, the process archive is deployed in the jbpm database
-      ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(fileName));
+        ProcessInstance processInstance = jbpmContext.newProcessInstance("instantiateprocess");
+        processInstance.signal();
+
+      } finally {
+        jbpmContext.getGraphSession().deleteProcessDefinition(processDefinition.getId());
+      }
+    } finally {
+      // put the files back into original position
+      new File(instantiateActionTmpName).renameTo(new File(instantiateActionOriginalName));
+      new File(instantiateClassTmpName).renameTo(new File(instantiateClassOriginalName));
+    }
+  }
+
+  public void testInstantiateClassOutsideArchive() throws Exception {
+    // create a process archive
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    ZipOutputStream zipOutputStream = new ZipOutputStream(baos);
+    addEntry(zipOutputStream, "processdefinition.xml", "org/jbpm/jpdl/par/instantiateprocess.xml");
+    addEntry(zipOutputStream, "classes/org/jbpm/jpdl/par/InstantiateAction.class", "org/jbpm/jpdl/par/InstantiateAction.class");
+    zipOutputStream.close();
+    byte[] zipBytes = baos.toByteArray();
+
+    // move the files
+    String instantiateActionOriginalName = getTestClassesDir() + "org/jbpm/jpdl/par/InstantiateAction.class";
+    String instantiateActionTmpName = instantiateActionOriginalName + ".hiddenFromTestClasspath";
+    assertTrue(new File(instantiateActionOriginalName).renameTo(new File(instantiateActionTmpName)));
+
+    try {
+      ClassLoader testClassLoader = ProcessArchiveClassLoadingDbTest.class.getClassLoader();
+      try {
+        testClassLoader.loadClass("org.jbpm.jpdl.par.InstantiateAction");
+        fail("expected exception");
+      } catch (ClassNotFoundException e) {
+        // OK
+      }
+      // InstantiateClass should be visible on the test classpath
+      testClassLoader.loadClass("org.jbpm.jpdl.par.InstantiateClass");
+
+      // deploy the process archive
+      ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBytes));
       ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream);
       jbpmContext.deployProcessDefinition(processDefinition);
-      try
-      {
+      try {
         newTransaction();
 
-        List allProcessDefinitions = graphSession.findAllProcessDefinitions();
-        assertEquals(1, allProcessDefinitions.size());
-        processDefinition = (ProcessDefinition)allProcessDefinitions.get(0);
-        ProcessInstance processInstance = new ProcessInstance(processDefinition);
+        ProcessInstance processInstance = jbpmContext.newProcessInstance("instantiateprocess");
         processInstance.signal();
 
-        assertTrue(isLoadedActionHandlerExecuted);
-      }
-      finally
-      {
+      } finally {
         jbpmContext.getGraphSession().deleteProcessDefinition(processDefinition.getId());
       }
-
+    } finally {
+      // put the files back into original position
+      new File(instantiateActionTmpName).renameTo(new File(instantiateActionOriginalName));
     }
-    finally
-    {
-      FileOutputStream fileOutputStream = new FileOutputStream(classFileName);
-      fileOutputStream.write(processLoadedActionHandlerClassBytes);
-      fileOutputStream.flush();
-      fileOutputStream.close();
-    }
   }
 
-  private static void addEntry(ZipOutputStream zipOutputStream, String entryName, String resource) throws IOException
-  {
+  private static void addEntry(ZipOutputStream zipOutputStream, String entryName, String resource) throws IOException {
     InputStream inputStream = ClassLoaderUtil.getStream(resource);
     byte[] bytes = IoUtil.readBytes(inputStream);
     addEntry(zipOutputStream, entryName, bytes);
+    inputStream.close();
   }
 
-  private static void addEntry(ZipOutputStream zipOutputStream, String entryName, byte[] content) throws IOException
-  {
+  private static void addEntry(ZipOutputStream zipOutputStream, String entryName, byte[] content) throws IOException {
     ZipEntry zipEntry = new ZipEntry(entryName);
     zipOutputStream.putNextEntry(zipEntry);
     zipOutputStream.write(content);
   }
-
 }

Modified: jbpm3/trunk/modules/core/src/test/java/org/jbpm/jpdl/par/ResourceAction.java
===================================================================
--- jbpm3/trunk/modules/core/src/test/java/org/jbpm/jpdl/par/ResourceAction.java	2008-10-27 16:03:08 UTC (rev 2629)
+++ jbpm3/trunk/modules/core/src/test/java/org/jbpm/jpdl/par/ResourceAction.java	2008-10-27 16:18:55 UTC (rev 2630)
@@ -1,3 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This 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 (at your option) any later version.
+ *
+ * This software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 package org.jbpm.jpdl.par;
 
 import java.io.BufferedReader;
@@ -5,97 +26,105 @@
 import java.io.InputStreamReader;
 import java.net.URL;
 
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.jbpm.graph.def.ActionHandler;
 import org.jbpm.graph.exe.ExecutionContext;
 
-public class ResourceAction implements ActionHandler
-{
+public class ResourceAction implements ActionHandler {
 
   private static final long serialVersionUID = 1L;
+  
+  public static Log log = LogFactory.getLog(ProcessArchiveClassLoadingDbTest.class);
 
-  public void execute(ExecutionContext executionContext) throws Exception
-  {
-    // remember action class instance
-    ProcessArchiveDeploymentDbTest.resourceActionInstance = this;
-    
-    // class resources
+  public void execute(ExecutionContext executionContext) throws Exception {
 
+    TestCase.assertEquals("org.jbpm.jpdl.par", getClass().getPackage().getName());
+
     URL resource = getClass().getResource("classresource.txt");
     InputStream stream = resource.openStream();
-    ProcessArchiveDeploymentDbTest.classResourceUrl = read(stream);
+    String classResourceUrl = read(stream);
+    TestCase.assertEquals("the class resource content", classResourceUrl);
 
     stream = getClass().getResourceAsStream("classresource.txt");
-    ProcessArchiveDeploymentDbTest.classResourceStream = read(stream);
+    String classResourceStream = read(stream);
+    TestCase.assertEquals("the class resource content", classResourceStream);
 
-    resource = ResourceAction.class.getClassLoader().getResource("org/jbpm/jpdl/par/classresource.txt");
+    ClassLoader resourceActionClassLoader = ResourceAction.class.getClassLoader();
+    log.info("resource action classloader: " + getClass().getClassLoader());
+    log.info("parent of resource action classloader: " + getClass().getClassLoader().getParent());
+    resource = resourceActionClassLoader.getResource("org/jbpm/jpdl/par/classresource.txt");
     stream = resource.openStream();
-    ProcessArchiveDeploymentDbTest.classLoaderResourceUrl = read(stream);
+    String classLoaderResourceUrl = read(stream);
+    TestCase.assertEquals("the class resource content", classLoaderResourceUrl);
 
-    stream = ResourceAction.class.getClassLoader().getResourceAsStream("org/jbpm/jpdl/par/classresource.txt");
-    ProcessArchiveDeploymentDbTest.classLoaderResourceStream = read(stream);
+    stream = resourceActionClassLoader.getResourceAsStream("org/jbpm/jpdl/par/classresource.txt");
+    String classLoaderResourceStream = read(stream);
+    TestCase.assertEquals("the class resource content", classLoaderResourceStream);
 
-    // archive resources
-
     resource = getClass().getResource("//archiveresource.txt");
     stream = resource.openStream();
-    ProcessArchiveDeploymentDbTest.archiveResourceUrl = read(stream);
+    String archiveResourceUrl = read(stream);
+    TestCase.assertEquals("the archive resource content", archiveResourceUrl);
 
     stream = getClass().getResourceAsStream("//archiveresource.txt");
-    ProcessArchiveDeploymentDbTest.archiveResourceStream = read(stream);
+    String archiveResourceStream = read(stream);
+    TestCase.assertEquals("the archive resource content", archiveResourceStream);
 
-    resource = ResourceAction.class.getClassLoader().getResource("//archiveresource.txt");
+    resource = resourceActionClassLoader.getResource("//archiveresource.txt");
     stream = resource.openStream();
-    ProcessArchiveDeploymentDbTest.archiveClassLoaderResourceUrl = read(stream);
+    String archiveClassLoaderResourceUrl = read(stream);
+    TestCase.assertEquals("the archive resource content", archiveClassLoaderResourceUrl);
 
-    stream = ResourceAction.class.getClassLoader().getResourceAsStream("//archiveresource.txt");
-    ProcessArchiveDeploymentDbTest.archiveClassLoaderResourceStream = read(stream);
+    stream = resourceActionClassLoader.getResourceAsStream("//archiveresource.txt");
+    String archiveClassLoaderResourceStream = read(stream);
+    TestCase.assertEquals("the archive resource content", archiveClassLoaderResourceStream);
 
-    // unexisting resources
-    try
-    {
-      ProcessArchiveDeploymentDbTest.unexistingClassResourceStream = getClass().getResourceAsStream("unexistingresource.txt");
+    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+    log.info("resource action context classloader: " + contextClassLoader);
+    log.info("parent of resource action context classloader: " + contextClassLoader.getParent());
+    resource = contextClassLoader.getResource("//archiveresource.txt");
+    stream = resource.openStream();
+    String contextClassLoaderResourceUrl = read(stream);
+    TestCase.assertEquals("the archive resource content", contextClassLoaderResourceUrl);
+
+    stream = contextClassLoader.getResourceAsStream("//archiveresource.txt");
+    String contextClassLoaderResourceStream = read(stream);
+    TestCase.assertEquals("the archive resource content", contextClassLoaderResourceStream);
+
+    try {
+      getClass().getResourceAsStream("unexistingresource.txt");
+    } catch (RuntimeException e) {
+      // ok
     }
-    catch (RuntimeException e)
-    {
-      // ignore
+
+    try {
+      resourceActionClassLoader.getResourceAsStream("org/jbpm/jpdl/par/unexistingresource.txt");
+    } catch (RuntimeException e) {
+      // ok
     }
-    
-    try
-    {
-      ProcessArchiveDeploymentDbTest.unexistingClassLoaderResourceStream = ResourceAction.class.getClassLoader().getResourceAsStream("org/jbpm/jpdl/par/unexistingresource.txt");
+
+    try {
+      getClass().getResourceAsStream("//unexistingarchiveresource.txt");
+    } catch (RuntimeException e) {
+      // ok
     }
-    catch (RuntimeException e)
-    {
-      // ignore
+    try {
+      resourceActionClassLoader.getResourceAsStream("//unexistingarchiveresource.txt");
+    } catch (RuntimeException e) {
+      // ok
     }
-    
-    try
-    {
-      ProcessArchiveDeploymentDbTest.unexistingArchiveResourceStream = getClass().getResourceAsStream("//unexistingarchiveresource.txt");
-    }
-    catch (RuntimeException e)
-    {
-      // ignore
-    }
-    try
-    {
-      ProcessArchiveDeploymentDbTest.unexistingArchiveLoaderResourceStream = ResourceAction.class.getClassLoader().getResourceAsStream("//unexistingarchiveresource.txt");
-    }
-    catch (RuntimeException e)
-    {
-      // ignore
-    }
   }
-
-  private String read(InputStream resourceAsStream) throws Exception
-  {
+  
+  static String read(InputStream resourceAsStream) throws Exception {
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resourceAsStream));
     StringBuffer buffer = new StringBuffer();
     String l;
-    while ((l = bufferedReader.readLine()) != null)
-    {
+    while ((l = bufferedReader.readLine()) != null) {
       buffer.append(l);
     }
     return buffer.toString();
   }
-}
+}
\ No newline at end of file




More information about the jbpm-commits mailing list