KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > jpdl > par > ProcessArchiveClassLoadingDbTest


1 package org.jbpm.jpdl.par;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.zip.ZipEntry JavaDoc;
10 import java.util.zip.ZipInputStream JavaDoc;
11 import java.util.zip.ZipOutputStream JavaDoc;
12
13 import org.jbpm.db.AbstractDbTestCase;
14 import org.jbpm.graph.def.ProcessDefinition;
15 import org.jbpm.graph.exe.ProcessInstance;
16 import org.jbpm.instantiation.ClassLoaderUtil;
17
18 public class ProcessArchiveClassLoadingDbTest extends AbstractDbTestCase {
19   
20   public static boolean isLoadedActionHandlerExecuted = false;
21
22   String JavaDoc getTestClassesDir() {
23     return ProcessArchiveDeployerDbTest.class.getProtectionDomain().getCodeSource().getLocation().getFile();
24   }
25
26   public void testProcessClassLoading() throws Exception JavaDoc {
27     // first we read the processLoadedActionHandlerClassBytes from the file system
28
InputStream JavaDoc inputStream = ClassLoaderUtil.getStream("org/jbpm/jpdl/par/ProcessLoadedActionHandler.class");
29     byte[] processLoadedActionHandlerClassBytes = ProcessArchive.readBytes(inputStream);
30     inputStream.close();
31     
32     // then, we delete the ProcessLoadedActionHandler from the
33
// test classes dir, thereby removing it from this class's classloader
34
String JavaDoc classFileName = getTestClassesDir()+"/org/jbpm/jpdl/par/ProcessLoadedActionHandler.class";
35     if (! new File JavaDoc(classFileName).delete()) {
36       fail("couldn't delete "+classFileName);
37     }
38     
39     try {
40       try {
41         // now, we gonna check if the ProcessArchiveDeployerDbTest is in the classpath of
42
// this test
43
ProcessArchiveClassLoadingDbTest.class.getClassLoader().loadClass("org.jbpm.jpdl.par.ProcessLoadedActionHandler");
44         fail("expected exception");
45       } catch (ClassNotFoundException JavaDoc e) {
46         // OK
47
}
48       
49       // next we create a process archive that includes the class file we just
50
// deleted from the file system.
51
String JavaDoc fileName = getTestClassesDir()+"/testarchive3.par";
52       FileOutputStream JavaDoc fileOutputStream = new FileOutputStream JavaDoc(fileName);
53       ZipOutputStream JavaDoc zipOutputStream = new ZipOutputStream JavaDoc(fileOutputStream);
54       addEntry(zipOutputStream, "processdefinition.xml", "org/jbpm/jpdl/par/classloadingprocess.xml");
55       addEntry(zipOutputStream, "classes/org/jbpm/jpdl/par/ProcessLoadedActionHandler.class", processLoadedActionHandlerClassBytes);
56       zipOutputStream.close();
57       
58       // and then, the process archive is deployed in the jbpm database
59
ZipInputStream JavaDoc zipInputStream = new ZipInputStream JavaDoc(new FileInputStream JavaDoc(fileName));
60       ProcessArchiveDeployer.deployZipInputStream(zipInputStream, AbstractDbTestCase.getJbpmSessionFactory());
61       
62       newTransaction();
63       
64       List JavaDoc allProcessDefinitions = graphSession.findAllProcessDefinitions();
65       assertEquals(1, allProcessDefinitions.size());
66       ProcessDefinition processDefinition = (ProcessDefinition) allProcessDefinitions.get(0);
67       ProcessInstance processInstance = new ProcessInstance(processDefinition);
68       processInstance.signal();
69       
70       assertTrue(isLoadedActionHandlerExecuted);
71
72     } finally {
73       FileOutputStream JavaDoc fileOutputStream = new FileOutputStream JavaDoc(classFileName);
74       fileOutputStream.write(processLoadedActionHandlerClassBytes);
75       fileOutputStream.flush();
76       fileOutputStream.close();
77     }
78   }
79   
80   private static void addEntry(ZipOutputStream JavaDoc zipOutputStream, String JavaDoc entryName, String JavaDoc resource) throws IOException JavaDoc {
81     InputStream JavaDoc inputStream = ClassLoaderUtil.getStream(resource);
82     byte[] bytes = ProcessArchive.readBytes(inputStream);
83     addEntry(zipOutputStream, entryName, bytes);
84   }
85   
86   private static void addEntry(ZipOutputStream JavaDoc zipOutputStream, String JavaDoc entryName, byte[] content) throws IOException JavaDoc {
87     ZipEntry JavaDoc zipEntry = new ZipEntry JavaDoc(entryName);
88     zipOutputStream.putNextEntry(zipEntry);
89     zipOutputStream.write(content);
90   }
91
92 }
93
Popular Tags