View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
11   * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
12   * language governing permissions and limitations under the License.
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   * Tests the ZipFilePluginLoader. The zip file which is checked in has the following format:
35   * 
36   * <pre>
37   *   classes/
38   *   |---&gt; test-classes.txt
39   *   |---&gt; workflow2.xml
40   *   lib/
41   *   |---&gt; test.jar
42   *   META-INF/
43   *   |---&gt; workflow.xml
44   * </pre>
45   * 
46   * <p>
47   * The test.jar which is in the zip file has one resource in it which is named test-lib.txt.
48   * 
49   * <p>
50   * The workflow.xml file has 2 params in it and includes the workflow2.xml file.
51   * 
52   * @author Kuali Rice Team (rice.collab@kuali.org)
53   */
54  public class ZipFilePluginLoaderTest extends KEWTestCase {
55  
56      private Plugin plugin;
57      private File pluginDir;
58  
59      @Before
60      // public void setUp() throws Exception {
61      // super.setUp();
62      // Config config = ConfigContext.getCurrentContextConfig();
63      // if (config == null) {
64      // // because of previously running tests, the config might already be initialized
65      // config = new SimpleConfig();
66      // config.getProperties().put(Config.SERVICE_NAMESPACE, "KEW");
67      // ConfigContext.init(config);
68      // }
69      // // from RiceTestCase if this ever get put into that hierarchy
70      //
71      // }
72      //
73      // @After
74      // public void tearDown() throws Exception {
75      // super.setUp();
76      // try {
77      // plugin.stop();
78      // } catch (Exception e) {
79      // e.printStackTrace();
80      // }
81      // try {
82      // FileUtils.deleteDirectory(pluginDir);
83      // } catch (Exception e) {
84      //
85      // }
86      // }
87      @Test
88      public void testLoad() throws Exception {
89  	Config config = ConfigContext.getCurrentContextConfig();
90  	if (config == null) {
91  	    // because of previously running tests, the config might already be initialized
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 	// create a temp directory to copy the zip file into
102 	pluginDir = TestUtilities.createTempDir();
103 
104 	// copy the zip file
105 	FileUtils.copyFileToDirectory(pluginZipFile, pluginDir);
106 	pluginZipFile = new File(pluginDir, pluginZipFile.getName());
107 	assertTrue(pluginZipFile.exists());
108 	pluginZipFile.deleteOnExit();
109 
110 	// create the ZipFilePluginLoader and load the plugin
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 	// check the plugin name, it's QName should be '{KEW}ziptest', it's plugin name should be 'ziptest'
116 	assertEquals("Plugin QName should be '{KEW}ziptest'", new QName("KEW", "ziptest"), plugin.getName());
117 
118 	// start the plugin
119 	this.plugin.start();
120 
121 	// verify that the plugin was extracted, should be in a directory named the same as the local part of the
122 	// QName
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 	// try loading some classes and checking that things got loaded properly
130 	assertNotNull("Resource should exist.", plugin.getClassLoader().getResource("lib-test.txt"));
131 	assertNotNull("Resource should exist.", plugin.getClassLoader().getResource("classes-test.txt"));
132 
133 	// check the config values
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 	// verify the modification checks on the plugin which drive hot deployment
139 	assertFalse("Plugin should not be modifed at this point.", loader.isModified());
140 	// record the last modified date of the extracted directory
141 	long lastModified = pluginDir.lastModified();
142 
143 	// sleep for a milliseconds before touching the file, this will help our last modified check so we don't
144 	// get the same value
145 	Thread.sleep(1000);
146 
147 	// touch the zip file
148 	FileUtils.touch(pluginZipFile);
149 	assertTrue("Plugin should be modifed after zip file is touched.", loader.isModified());
150 	plugin.stop();
151 
152 	// reload the plugin
153 	this.plugin = loader.load();
154 	
155 	this.plugin.start();
156 	assertFalse("After reload, plugin should no longer be modifed.", loader.isModified());
157 
158 	// check the last modified date of the extracted directory
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 }