View Javadoc

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