1 package org.jbpm.jpdl.par; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileOutputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.util.List ; 9 import java.util.zip.ZipEntry ; 10 import java.util.zip.ZipInputStream ; 11 import java.util.zip.ZipOutputStream ; 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 getTestClassesDir() { 23 return ProcessArchiveDeployerDbTest.class.getProtectionDomain().getCodeSource().getLocation().getFile(); 24 } 25 26 public void testProcessClassLoading() throws Exception { 27 InputStream inputStream = ClassLoaderUtil.getStream("org/jbpm/jpdl/par/ProcessLoadedActionHandler.class"); 29 byte[] processLoadedActionHandlerClassBytes = ProcessArchive.readBytes(inputStream); 30 inputStream.close(); 31 32 String classFileName = getTestClassesDir()+"/org/jbpm/jpdl/par/ProcessLoadedActionHandler.class"; 35 if (! new File (classFileName).delete()) { 36 fail("couldn't delete "+classFileName); 37 } 38 39 try { 40 try { 41 ProcessArchiveClassLoadingDbTest.class.getClassLoader().loadClass("org.jbpm.jpdl.par.ProcessLoadedActionHandler"); 44 fail("expected exception"); 45 } catch (ClassNotFoundException e) { 46 } 48 49 String fileName = getTestClassesDir()+"/testarchive3.par"; 52 FileOutputStream fileOutputStream = new FileOutputStream (fileName); 53 ZipOutputStream zipOutputStream = new ZipOutputStream (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 ZipInputStream zipInputStream = new ZipInputStream (new FileInputStream (fileName)); 60 ProcessArchiveDeployer.deployZipInputStream(zipInputStream, AbstractDbTestCase.getJbpmSessionFactory()); 61 62 newTransaction(); 63 64 List 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 fileOutputStream = new FileOutputStream (classFileName); 74 fileOutputStream.write(processLoadedActionHandlerClassBytes); 75 fileOutputStream.flush(); 76 fileOutputStream.close(); 77 } 78 } 79 80 private static void addEntry(ZipOutputStream zipOutputStream, String entryName, String resource) throws IOException { 81 InputStream inputStream = ClassLoaderUtil.getStream(resource); 82 byte[] bytes = ProcessArchive.readBytes(inputStream); 83 addEntry(zipOutputStream, entryName, bytes); 84 } 85 86 private static void addEntry(ZipOutputStream zipOutputStream, String entryName, byte[] content) throws IOException { 87 ZipEntry zipEntry = new ZipEntry (entryName); 88 zipOutputStream.putNextEntry(zipEntry); 89 zipOutputStream.write(content); 90 } 91 92 } 93 | Popular Tags |