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