001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.plugin;
018:
019: import java.io.File;
020:
021: import javax.xml.namespace.QName;
022:
023: import org.apache.commons.io.FileUtils;
024: import org.junit.After;
025: import org.junit.Before;
026: import org.junit.Test;
027: import org.kuali.rice.config.Config;
028: import org.kuali.rice.config.SimpleConfig;
029: import org.kuali.rice.core.Core;
030: import org.kuali.rice.test.LoggableTestCase;
031:
032: import edu.iu.uis.eden.test.TestUtilities;
033: import edu.iu.uis.eden.util.ClassLoaderUtils;
034:
035: /**
036: * Tests the ZipFilePluginLoader. The zip file which is checked in has the following format:
037: *
038: * <pre>
039: * classes/
040: * |---> test-classes.txt
041: * |---> workflow2.xml
042: * lib/
043: * |---> test.jar
044: * META-INF/
045: * |---> workflow.xml
046: * </pre>
047: *
048: * <p>The test.jar which is in the zip file has one resource in it which is named test-lib.txt.
049: *
050: * <p>The workflow.xml file has 2 params in it and includes the workflow2.xml file.
051: *
052: * @author Eric Westfall
053: */
054: public class ZipFilePluginLoaderTest extends LoggableTestCase {
055:
056: private Plugin plugin;
057: private File pluginDir;
058:
059: @Before
060: public void setUp() throws Exception {
061: SimpleConfig config = new SimpleConfig();
062: config.getProperties().put(Config.MESSAGE_ENTITY, "KEW");
063: Core.init(config);
064: }
065:
066: @After
067: public void tearDown() throws Exception {
068: try {
069: plugin.stop();
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073: try {
074: FileUtils.deleteDirectory(pluginDir);
075: } catch (Exception e) {
076: }
077:
078: }
079:
080: @Test
081: public void testLoad() throws Exception {
082: File pluginZipFile = new File(
083: "test/src/edu/iu/uis/eden/plugin/ziptest.zip");
084: assertTrue(pluginZipFile.exists());
085: assertTrue(pluginZipFile.isFile());
086:
087: // create a temp directory to copy the zip file into
088: pluginDir = TestUtilities.createTempDir();
089:
090: // copy the zip file
091: FileUtils.copyFileToDirectory(pluginZipFile, pluginDir);
092: pluginZipFile = new File(pluginDir, pluginZipFile.getName());
093: assertTrue(pluginZipFile.exists());
094: pluginZipFile.deleteOnExit();
095:
096: // create the ZipFilePluginLoader and load the plugin
097: ZipFilePluginLoader loader = new ZipFilePluginLoader(
098: pluginZipFile, null, ClassLoaderUtils
099: .getDefaultClassLoader(), Core.getRootConfig(),
100: false);
101: this .plugin = loader.load();
102: assertNotNull("Plugin should have been successfully loaded.",
103: plugin);
104: // check the plugin name, it's QName should be '{KEW}ziptest', it's plugin name should be 'ziptest'
105: assertEquals("Plugin QName should be '{KEW}ziptest'",
106: new QName("KEW", "ziptest"), plugin.getName());
107:
108: // start the plugin
109: this .plugin.start();
110:
111: // verify that the plugin was extracted, should be in a directory named the same as the local part of the QName
112: File extractedDirectory = new File(pluginDir, plugin.getName()
113: .getLocalPart());
114: assertTrue("Plugin should have been extracted.",
115: extractedDirectory.exists());
116: assertTrue(extractedDirectory.isDirectory());
117: File[] files = extractedDirectory.listFiles();
118: assertEquals("Should be 3 files", 3, files.length);
119:
120: // try loading some classes and checking that things got loaded properly
121: assertNotNull("Resource should exist.", plugin.getClassLoader()
122: .getResource("lib-test.txt"));
123: assertNotNull("Resource should exist.", plugin.getClassLoader()
124: .getResource("classes-test.txt"));
125:
126: // check the config values
127: assertEquals(plugin.getConfig().getProperty("test.param.1"),
128: "test.value.1");
129: assertEquals(plugin.getConfig().getProperty("test.param.2"),
130: "test.value.2");
131: assertEquals(plugin.getConfig().getProperty("test.param.3"),
132: "test.value.3");
133:
134: // verify the modification checks on the plugin which drive hot deployment
135: assertFalse("Plugin should not be modifed at this point.",
136: loader.isModified());
137: // record the last modified date of the extracted directory
138: long lastModified = pluginDir.lastModified();
139:
140: // sleep for a milliseconds before touching the file, this will help our last modified check so we don't
141: // get the same value
142: Thread.sleep(1000);
143:
144: // touch the zip file
145: FileUtils.touch(pluginZipFile);
146: assertTrue(
147: "Plugin should be modifed after zip file is touched.",
148: loader.isModified());
149: plugin.stop();
150:
151: // reload the plugin
152: this .plugin = loader.load();
153: this .plugin.start();
154: assertFalse(
155: "After reload, plugin should no longer be modifed.",
156: loader.isModified());
157:
158: // check the last modified date of the extracted directory
159: assertTrue(
160: "The extracted directory should have been modified.",
161: pluginDir.lastModified() > lastModified);
162:
163: }
164:
165: }
|