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