KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.jpdl.par;
2
3 import java.io.*;
4 import java.util.*;
5 import java.util.zip.ZipEntry JavaDoc;
6 import java.util.zip.ZipInputStream JavaDoc;
7 import java.util.zip.ZipOutputStream JavaDoc;
8
9 import org.jbpm.db.*;
10 import org.jbpm.graph.def.*;
11 import org.jbpm.instantiation.*;
12
13 public class ProcessArchiveDeployerDbTest extends AbstractDbTestCase {
14
15   String JavaDoc getTestClassesDir() {
16     return ProcessArchiveDeployerDbTest.class.getProtectionDomain().getCodeSource().getLocation().getFile();
17   }
18
19   public void testDeployProcess() throws Exception JavaDoc {
20
21     // create a process archive file and save it to disk
22
String JavaDoc fileName = getTestClassesDir()+"/testarchive.par";
23     FileOutputStream fileOutputStream = new FileOutputStream(fileName);
24     ZipOutputStream JavaDoc zipOutputStream = new ZipOutputStream JavaDoc(fileOutputStream);
25     addEntry(zipOutputStream, "processdefinition.xml", "org/jbpm/jpdl/par/deployableprocess.xml");
26     zipOutputStream.close();
27
28     // deploy the saved par file
29
ZipInputStream JavaDoc zipInputStream = new ZipInputStream JavaDoc(new FileInputStream(fileName));
30     ProcessArchiveDeployer.deployZipInputStream(zipInputStream, AbstractDbTestCase.getJbpmSessionFactory());
31
32     newTransaction();
33
34     List allProcessDefinitions = graphSession.findAllProcessDefinitions();
35     assertEquals(1, allProcessDefinitions.size());
36     ProcessDefinition processDefinition = (ProcessDefinition) allProcessDefinitions.get(0);
37     assertEquals("the deployable process", processDefinition.getName());
38   }
39
40   public void testDeployProcessWithFile() throws Exception JavaDoc {
41     // create a process archive file and save it to disk
42
String JavaDoc fileName = getTestClassesDir()+"/testarchive.par";
43     FileOutputStream fileOutputStream = new FileOutputStream(fileName);
44     ZipOutputStream JavaDoc zipOutputStream = new ZipOutputStream JavaDoc(fileOutputStream);
45     addEntry(zipOutputStream, "processdefinition.xml", "org/jbpm/jpdl/par/deployableprocess.xml");
46     addEntry(zipOutputStream, "classes/org/jbpm/jpdl/par/ProcessArchiveDeployerDbTest.class", "org/jbpm/jpdl/par/ProcessArchiveDeployerDbTest.class");
47     zipOutputStream.close();
48
49     // deploy the saved par file
50
ZipInputStream JavaDoc zipInputStream = new ZipInputStream JavaDoc(new FileInputStream(fileName));
51     ProcessArchiveDeployer.deployZipInputStream(zipInputStream, AbstractDbTestCase.getJbpmSessionFactory());
52
53     newTransaction();
54
55     List allProcessDefinitions = graphSession.findAllProcessDefinitions();
56     assertEquals(1, allProcessDefinitions.size());
57     ProcessDefinition processDefinition = (ProcessDefinition) allProcessDefinitions.get(0);
58     byte[] processBytes = processDefinition.getFileDefinition().getBytes("classes/org/jbpm/jpdl/par/ProcessArchiveDeployerDbTest.class");
59     byte[] expectedBytes = ProcessArchive.readBytes(ClassLoaderUtil.getStream("org/jbpm/jpdl/par/ProcessArchiveDeployerDbTest.class"));
60
61     assertTrue(Arrays.equals(expectedBytes, processBytes));
62   }
63
64   public void testDeployTwoVersionOfProcess() throws Exception JavaDoc {
65
66     // create a process archive file and save it to disk
67
String JavaDoc fileName = getTestClassesDir()+"/testarchive.par";
68     FileOutputStream fileOutputStream = new FileOutputStream(fileName);
69     ZipOutputStream JavaDoc zipOutputStream = new ZipOutputStream JavaDoc(fileOutputStream);
70     addEntry(zipOutputStream, "processdefinition.xml", "org/jbpm/jpdl/par/deployableprocess.xml");
71     zipOutputStream.close();
72
73     // deploy the saved par file
74
ZipInputStream JavaDoc zipInputStream = new ZipInputStream JavaDoc(new FileInputStream(fileName));
75     ProcessArchiveDeployer.deployZipInputStream(zipInputStream, AbstractDbTestCase.getJbpmSessionFactory());
76
77     newTransaction();
78
79     // deploy the saved par file again
80
zipInputStream = new ZipInputStream JavaDoc(new FileInputStream(fileName));
81     ProcessArchiveDeployer.deployZipInputStream(zipInputStream, AbstractDbTestCase.getJbpmSessionFactory());
82
83     newTransaction();
84
85     assertEquals(2, graphSession.findAllProcessDefinitions().size());
86     assertEquals(2, graphSession.findLatestProcessDefinition("the deployable process").getVersion());
87   }
88
89   public void testDeployProcessWithValidation() throws Exception JavaDoc {
90     // create a process archive file and save it to disk
91
String JavaDoc fileName = getTestClassesDir()+"/testarchive.par";
92     FileOutputStream fileOutputStream = new FileOutputStream(fileName);
93     ZipOutputStream JavaDoc zipOutputStream = new ZipOutputStream JavaDoc(fileOutputStream);
94     addEntry(zipOutputStream, "processdefinition.xml", "org/jbpm/jpdl/par/deployableprocesswithns.xml");
95     zipOutputStream.close();
96
97     // deploy the saved par file
98
ZipInputStream JavaDoc zipInputStream = new ZipInputStream JavaDoc(new FileInputStream(fileName));
99     ProcessArchiveDeployer.deployZipInputStream(zipInputStream, AbstractDbTestCase.getJbpmSessionFactory());
100
101     newTransaction();
102
103     List allProcessDefinitions = graphSession.findAllProcessDefinitions();
104     assertEquals(1, allProcessDefinitions.size());
105     ProcessDefinition processDefinition = (ProcessDefinition) allProcessDefinitions.get(0);
106     assertEquals("the deployable process", processDefinition.getName());
107   }
108
109   private static void addEntry(ZipOutputStream JavaDoc zipOutputStream, String JavaDoc entryName, String JavaDoc resource) throws IOException {
110     InputStream JavaDoc inputStream = ClassLoaderUtil.getStream(resource);
111     byte[] bytes = ProcessArchive.readBytes(inputStream);
112     addEntry(zipOutputStream, entryName, bytes);
113   }
114
115   private static void addEntry(ZipOutputStream JavaDoc zipOutputStream, String JavaDoc entryName, byte[] content) throws IOException {
116     ZipEntry JavaDoc zipEntry = new ZipEntry JavaDoc(entryName);
117     zipOutputStream.putNextEntry(zipEntry);
118     zipOutputStream.write(content);
119   }
120 }
121
Popular Tags