1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.kuali.rice.kew.plugin;
15
16 import java.io.File;
17
18 import javax.xml.namespace.QName;
19
20 import org.apache.commons.io.FileUtils;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.kuali.rice.core.config.Config;
24 import org.kuali.rice.core.config.ConfigContext;
25 import org.kuali.rice.core.config.SimpleConfig;
26 import org.kuali.rice.core.util.ClassLoaderUtils;
27 import org.kuali.rice.kew.plugin.Plugin;
28 import org.kuali.rice.kew.plugin.ZipFilePluginLoader;
29 import org.kuali.rice.kew.test.KEWTestCase;
30 import org.kuali.rice.kew.test.TestUtilities;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class ZipFilePluginLoaderTest extends KEWTestCase {
55
56 private Plugin plugin;
57 private File pluginDir;
58
59 @Before
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 @Test
88 public void testLoad() throws Exception {
89 Config config = ConfigContext.getCurrentContextConfig();
90 if (config == null) {
91
92 config = new SimpleConfig();
93 config.putProperty(Config.SERVICE_NAMESPACE, "KEW");
94 ConfigContext.init(config);
95 }
96
97 File pluginZipFile = new File(this.getBaseDir() + "/src/test/resources/org/kuali/rice/kew/plugin/ziptest.zip");
98 assertTrue(pluginZipFile.exists());
99 assertTrue(pluginZipFile.isFile());
100
101
102 pluginDir = TestUtilities.createTempDir();
103
104
105 FileUtils.copyFileToDirectory(pluginZipFile, pluginDir);
106 pluginZipFile = new File(pluginDir, pluginZipFile.getName());
107 assertTrue(pluginZipFile.exists());
108 pluginZipFile.deleteOnExit();
109
110
111 ZipFilePluginLoader loader = new ZipFilePluginLoader(pluginZipFile, null, ClassLoaderUtils.getDefaultClassLoader(),
112 ConfigContext.getRootConfig());
113 this.plugin = loader.load();
114 assertNotNull("Plugin should have been successfully loaded.", plugin);
115
116 assertEquals("Plugin QName should be '{KEW}ziptest'", new QName("KEW", "ziptest"), plugin.getName());
117
118
119 this.plugin.start();
120
121
122
123 File extractedDirectory = new File(pluginDir, plugin.getName().getLocalPart());
124 assertTrue("Plugin should have been extracted.", extractedDirectory.exists());
125 assertTrue(extractedDirectory.isDirectory());
126 File[] files = extractedDirectory.listFiles();
127 assertEquals("Should be 3 files", 3, files.length);
128
129
130 assertNotNull("Resource should exist.", plugin.getClassLoader().getResource("lib-test.txt"));
131 assertNotNull("Resource should exist.", plugin.getClassLoader().getResource("classes-test.txt"));
132
133
134 assertEquals(plugin.getConfig().getProperty("test.param.1"), "test.value.1");
135 assertEquals(plugin.getConfig().getProperty("test.param.2"), "test.value.2");
136 assertEquals(plugin.getConfig().getProperty("test.param.3"), "test.value.3");
137
138
139 assertFalse("Plugin should not be modifed at this point.", loader.isModified());
140
141 long lastModified = pluginDir.lastModified();
142
143
144
145 Thread.sleep(1000);
146
147
148 FileUtils.touch(pluginZipFile);
149 assertTrue("Plugin should be modifed after zip file is touched.", loader.isModified());
150 plugin.stop();
151
152
153 this.plugin = loader.load();
154
155 this.plugin.start();
156 assertFalse("After reload, plugin should no longer be modifed.", loader.isModified());
157
158
159 assertTrue("The extracted directory should have been modified.", pluginDir.lastModified() > lastModified);
160
161 try {
162 plugin.stop();
163 } catch (Exception e) {
164 e.printStackTrace();
165 }
166 try {
167 FileUtils.deleteDirectory(pluginDir);
168 } catch (Exception e) {
169
170 }
171 }
172 }