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.Ignore;
025: import org.junit.Test;
026: import org.kuali.rice.core.Core;
027: import org.kuali.workflow.test.WorkflowTestCase;
028:
029: import edu.iu.uis.eden.test.TestUtilities;
030:
031: /**
032: * Tests the HotDeployer and Reloader which handle hot deployment and hot reloading
033: * of Plugins.
034: *
035: * @author Eric Westfall
036: */
037: public class HotDeployTest extends WorkflowTestCase {
038:
039: private File pluginDir;
040:
041: @Override
042: public void setUp() throws Exception {
043: TestUtilities.initializePluginDirectories();
044: this .pluginDir = TestUtilities.getPluginsDirectory();
045: super .setUp();
046: }
047:
048: @Override
049: public void tearDown() throws Exception {
050: super .tearDown();
051: TestUtilities.cleanupPluginDirectories();
052: }
053:
054: @Ignore
055: @Test
056: public void testHotDeploy() throws Exception {
057: // Grab the ServerPluginRegistry
058: PluginRegistry theRegistry = PluginUtils.getPluginRegistry();
059: assertNotNull("PluginRegistry should exist.", theRegistry);
060: assertTrue(theRegistry instanceof ServerPluginRegistry);
061: ServerPluginRegistry registry = (ServerPluginRegistry) theRegistry;
062:
063: // Let's shut down the asynchronous reloader and hot deployer because we want to do this synchronously.
064: HotDeployer hotDeployer = registry.getHotDeployer();
065: Reloader reloader = registry.getReloader();
066: registry.stopHotDeployer();
067: registry.stopReloader();
068:
069: // Assert that there are currently no plugins
070: assertEquals("There should be no plugins.", 0, registry
071: .getPluginEnvironments().size());
072: assertEquals("Resource loader should have no children.", 0,
073: registry.getResourceLoaders().size());
074:
075: // query the hot deployer directly about it's added and removed plugins
076: assertEquals("There should be no plugins added.", 0,
077: hotDeployer.getAddedPlugins().size());
078: assertEquals("There should be no plugins removed.", 0,
079: hotDeployer.getRemovedPlugins().size());
080: hotDeployer.run();
081: assertEquals("There should still be no plugins.", 0, registry
082: .getPluginEnvironments().size());
083:
084: // now let's copy a plugin over and run the hot deployer
085: File pluginZipFile = new File(
086: "test/src/edu/iu/uis/eden/plugin/ziptest.zip");
087: assertTrue(pluginZipFile.exists());
088: assertTrue(pluginZipFile.isFile());
089: FileUtils.copyFileToDirectory(pluginZipFile, pluginDir);
090:
091: assertEquals("There should be one plugin added.", 1,
092: hotDeployer.getAddedPlugins().size());
093: assertEquals("There should be no plugins removed.", 0,
094: hotDeployer.getRemovedPlugins().size());
095:
096: hotDeployer.run();
097:
098: // the plugin should have been hot deployed
099: assertEquals("Plugin should have been hot deployed.", 1,
100: registry.getPluginEnvironments().size());
101:
102: // check added plugins again, it should now indicate no new added plugins
103: assertEquals("There should be no plugins added.", 0,
104: hotDeployer.getAddedPlugins().size());
105: assertEquals("There should be no plugins removed.", 0,
106: hotDeployer.getRemovedPlugins().size());
107:
108: // verify that the resource loading and the registry are sane and properly set up with the new plugin
109: assertEquals("Resource loader should have 1 plugin child.", 1,
110: registry.getResourceLoaders().size());
111: Plugin plugin = (Plugin) registry.getResourceLoaders().get(0);
112: assertEquals("Plugin has wrong name.", new QName(Core
113: .getCurrentContextConfig().getMessageEntity(),
114: "ziptest"), plugin.getName());
115: assertTrue("Plugin should be started.", plugin.isStarted());
116: assertEquals(
117: "Plugin in resource loader and environment should be the same.",
118: plugin, registry.getPluginEnvironment(plugin.getName())
119: .getPlugin());
120:
121: // The reloader should have a reference to the environment
122: assertEquals(
123: "Reloader should have a reference to environment.", 1,
124: reloader.getReloadables().size());
125:
126: // now remove the plugin and ensure that it goes away
127: FileUtils.forceDelete(new File(pluginDir, "ziptest.zip"));
128: assertEquals("There should be no plugins added.", 0,
129: hotDeployer.getAddedPlugins().size());
130: assertEquals("There should be one plugin removed.", 1,
131: hotDeployer.getRemovedPlugins().size());
132: hotDeployer.run();
133:
134: // verify that the resource loading and the registry no longer contain the plugin
135: assertEquals("No plugins should be deployed.", 0, registry
136: .getPluginEnvironments().size());
137: assertEquals("Resource loader should have 0 plugin children.",
138: 0, registry.getResourceLoaders().size());
139:
140: // also assert that the reloader no longer has a reference to the environment
141: assertEquals(
142: "Reloader should no longer have reference to environment.",
143: 0, reloader.getReloadables().size());
144:
145: }
146:
147: @Ignore
148: @Test
149: public void testReloader() throws Exception {
150: // Grab the ServerPluginRegistry
151: PluginRegistry theRegistry = PluginUtils.getPluginRegistry();
152: assertNotNull("PluginRegistry should exist.", theRegistry);
153: assertTrue(theRegistry instanceof ServerPluginRegistry);
154: ServerPluginRegistry registry = (ServerPluginRegistry) theRegistry;
155:
156: // Let's shut down the asynchronous reloader and hot deployer because we want to do this synchronously.
157: HotDeployer hotDeployer = registry.getHotDeployer();
158: Reloader reloader = registry.getReloader();
159: registry.stopHotDeployer();
160: registry.stopReloader();
161:
162: // Assert that there are currently no plugins
163: assertEquals("There should be no plugins.", 0, registry
164: .getPluginEnvironments().size());
165: assertEquals("Resource loader should have no children.", 0,
166: registry.getResourceLoaders().size());
167:
168: // now let's copy a plugin over and run the hot deployer
169: File pluginZipFile = new File(
170: "test/src/edu/iu/uis/eden/plugin/ziptest.zip");
171: assertTrue(pluginZipFile.exists());
172: assertTrue(pluginZipFile.isFile());
173: FileUtils.copyFileToDirectory(pluginZipFile, pluginDir);
174:
175: // update pluginZipFile to point to the copy
176: pluginZipFile = new File(pluginDir, pluginZipFile.getName());
177: assertTrue(pluginZipFile.exists());
178:
179: // execute a hot deploy
180: hotDeployer.run();
181:
182: // the plugin should have been hot deployed
183: assertEquals("Plugin should have been hot deployed.", 1,
184: registry.getPluginEnvironments().size());
185: assertEquals("Resource loader should have 1 plugin child.", 1,
186: registry.getResourceLoaders().size());
187: PluginEnvironment environment = registry
188: .getPluginEnvironments().get(0);
189: Plugin plugin = environment.getPlugin();
190: assertTrue(environment.isReloadable());
191: assertFalse(environment.isReloadNeeded());
192:
193: // let's attempt to execute a Reload
194: reloader.run();
195:
196: // a reload should not have occurred here since nothing was updated
197: assertTrue("Original plugin should still be running.", plugin
198: .isStarted());
199: assertEquals("Plugin should not have changed.", plugin,
200: registry.getPluginEnvironments().get(0).getPlugin());
201:
202: // touch the plugin file and then reload
203: FileUtils.touch(pluginZipFile);
204: assertTrue("A reload should be needed now.", environment
205: .isReloadNeeded());
206: reloader.run();
207:
208: // the original plugin should now be stopped
209: assertTrue("original plugin should be stopped.", !plugin
210: .isStarted());
211: assertEquals("There should only be one Plugin.", 1, registry
212: .getResourceLoaders().size());
213:
214: PluginEnvironment newPluginEnvironment = registry
215: .getPluginEnvironments().get(0);
216: Plugin newPlugin = newPluginEnvironment.getPlugin();
217: assertEquals("There should still only be one environment.", 1,
218: registry.getPluginEnvironments().size());
219: assertEquals(
220: "The plugin environments should still be the same.",
221: environment, registry.getPluginEnvironments().get(0));
222:
223: assertFalse("The old and new plugins should be different.",
224: newPlugin.equals(plugin));
225:
226: // verify that the resource loader was updated
227: assertEquals(
228: "The resource loaders should have been updated with the new plugin.",
229: newPlugin, registry.getResourceLoaders().get(0));
230:
231: }
232:
233: }
|