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