001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
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 org.apache.jetspeed.deployment;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.nio.channels.FileChannel;
025:
026: import junit.framework.AssertionFailedError;
027: import junit.framework.Test;
028: import junit.framework.TestSuite;
029:
030: import org.apache.jetspeed.AbstractRequestContextTestCase;
031: import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
032: import org.apache.jetspeed.container.window.PortletWindowAccessor;
033: import org.apache.jetspeed.deployment.impl.StandardDeploymentManager;
034: import org.apache.jetspeed.factory.PortletFactory;
035: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
036: import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
037: import org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManager;
038: import org.apache.jetspeed.util.DirectoryHelper;
039: import org.apache.jetspeed.util.JarHelper;
040: import org.apache.pluto.om.portlet.PortletDefinition;
041:
042: /**
043: * <p>
044: * TestSimpleDeployment
045: * </p>
046: *
047: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
048: * @version $Id: TestSimpleDeployment.java 517719 2007-03-13 15:05:48Z ate $
049: *
050: */
051: public class TestSimpleDeployment extends
052: AbstractRequestContextTestCase {
053: protected static final String TEST_PORTLET_APP_NAME = "HW_App";
054: protected String webAppsDir;
055:
056: protected File deploySrc;
057:
058: protected File deployRootFile;
059:
060: protected String testDb;
061: protected File webAppsDirFile;
062: protected File copyFrom;
063: protected PortletWindowAccessor windowAccess;
064: protected PortletFactory portletFactory;
065: protected ApplicationServerManager manager;
066:
067: /**
068: * Start the tests.
069: *
070: * @param args
071: * the arguments. Not used
072: */
073: public static void main(String args[]) {
074: junit.awtui.TestRunner
075: .main(new String[] { TestSimpleDeployment.class
076: .getName() });
077: }
078:
079: /**
080: * Creates the test suite.
081: *
082: * @return a test suite (<code>TestSuite</code>) that includes all
083: * methods starting with "test"
084: */
085: public static Test suite() {
086: // All methods starting with "test" will be executed in the test suite.
087: // return new JetspeedTestSuite(TestSimpleDeployment.class);
088: return new TestSuite(TestSimpleDeployment.class);
089: }
090:
091: public void testFileSystemHelperOnWar() throws Exception {
092: File demoApp = new File(deploySrc, "demo.war");
093:
094: JarHelper jarHelper = new JarHelper(demoApp, true);
095: File rootDirectory = jarHelper.getRootDirectory();
096: File webXml = new File(rootDirectory, "WEB-INF/web.xml");
097: assertTrue(webXml.exists());
098: jarHelper.close();
099: assertFalse(webXml.exists());
100:
101: // Test for keeping jar temp files around
102: jarHelper = new JarHelper(demoApp, false);
103: assertTrue(webXml.exists());
104: jarHelper.close();
105: assertTrue(webXml.exists());
106: }
107:
108: public void testFileSystemManagerOnDir() throws Exception {
109: File demoApp = new File("./test/testdata/deploy/webapp");
110: assertTrue(demoApp.exists());
111:
112: DirectoryHelper dirHelper = new DirectoryHelper(demoApp);
113: File webXml = new File(dirHelper.getRootDirectory(),
114: "WEB-INF/web.xml");
115: assertTrue(webXml.exists());
116:
117: }
118:
119: /*
120:
121: public void testDeploy() throws Exception
122: {
123:
124: System.out.println("Deployment src: " + deploySrc);
125: manager = new TomcatManager("", "", 0, "", 0, "", "");
126: SimpleRegistry simpleRegistry = new InMemoryRegistryImpl();
127: DeployDecoratorEventListener ddel = new DeployDecoratorEventListener(simpleRegistry, deployRootFile
128: .getAbsolutePath());
129:
130: DeployPortletAppEventListener dpal = new DeployPortletAppEventListener(webAppsDir, new FileSystemPAM(
131: webAppsDir, portletRegistry, entityAccess, windowAccess, portletCache, portletFactory, manager), portletRegistry );
132: ArrayList eventListeners = new ArrayList(2);
133: eventListeners.add(ddel);
134: eventListeners.add(dpal);
135: // Use a -1 delay to disable auto scan
136: StandardDeploymentManager autoDeployment = new StandardDeploymentManager(deploySrc.getAbsolutePath(), -1, eventListeners );
137:
138: autoDeployment.start();
139: autoDeployment.fireDeploymentEvent();
140:
141: File decoratorVm = new File(deployRootFile.getAbsolutePath() + File.separator + "generic" + File.separator + "html" + File.separator
142: + "portletstd" + File.separator + "decorator.vm");
143:
144: File demoAppDeployed = new File(webAppsDirFile, TEST_PORTLET_APP_NAME);
145: File demoApp = demoAppDeployed;
146: File securityApp = new File(webAppsDirFile, "TestSecurityRoles");
147:
148: assertTrue(decoratorVm.getCanonicalPath() + " was not created!", decoratorVm.exists());
149:
150: verifyDemoAppCreated(TEST_PORTLET_APP_NAME, demoApp);
151: verifyDemoAppCreated("TestSecurityRoles", securityApp);
152:
153: MutablePortletApplication jetspeedApp = portletRegistry.getPortletApplicationByIdentifier("jetspeed");
154: assertNotNull("jetspeed was not registered into the portlet registery.", jetspeedApp);
155: assertFalse("local app, jetspeed, got deployed when it should have only been registered.", new File(webAppsDir
156: + "/jetspeed").exists());
157:
158: //make sure we can load registered app's classes
159: Iterator portletDefItr = jetspeedApp.getPortletDefinitions().iterator();
160: while (portletDefItr.hasNext())
161: {
162: PortletDefinition def = (PortletDefinition) portletDefItr.next();
163: try
164: {
165: Portlet portlet = JetspeedPortletFactoryProxy.loadPortletClass(def.getClassName());
166: assertNotNull("Could not load portlet class: "+def.getClassName(), portlet);
167: }
168: catch (Exception e)
169: {
170: assertNull("Unable to load registered portlet class, " + def.getClassName(), e);
171: }
172:
173: }
174:
175: // test undeploy
176: File demoWar = new File(deploySrc, "demo.war");
177: demoWar.delete();
178: autoDeployment.fireUndeploymentEvent();
179: verifyDemoAppDeleted(TEST_PORTLET_APP_NAME, demoApp);
180:
181: // test deploy again
182: copyDeployables();
183: autoDeployment.fireDeploymentEvent();
184: verifyDemoAppCreated(TEST_PORTLET_APP_NAME, demoApp);
185: demoWar.delete();
186: autoDeployment.fireUndeploymentEvent();
187: verifyDemoAppDeleted(TEST_PORTLET_APP_NAME, demoApp);
188:
189: // test redeploy
190:
191: // So, first deploy the typical demo.war we have been using before.
192: copyDeployables();
193: autoDeployment.fireDeploymentEvent();
194: verifyDemoAppCreated(TEST_PORTLET_APP_NAME, demoApp);
195: DirectoryHelper demoAppDeployedDir = new DirectoryHelper(demoAppDeployed);
196: long beforeSize = new File(demoAppDeployedDir.getRootDirectory(), "WEB-INF/portlet.xml").length();
197:
198: // Trigger re-deployment using a demo.war that has a slightly larger portlet.xml
199: // then the one we just deployed. We will use size comparisons as or litmus test.
200: File redeployDemoWar = new File("./test/deployment/redeploy/demo.war");
201: FileChannel srcDemoWarChannel = new FileInputStream(redeployDemoWar).getChannel();
202: FileChannel dstDemoWarChannel = new FileOutputStream(demoWar).getChannel();
203: dstDemoWarChannel.transferFrom(srcDemoWarChannel, 0, srcDemoWarChannel.size());
204: srcDemoWarChannel.close();
205: dstDemoWarChannel.close();
206:
207: // Make sure the demo.war that will trigger redeploy has a larger portlet.xml then the current one
208: JarHelper rdDemoWar = new JarHelper(demoWar, true);
209: assertTrue(new File(rdDemoWar.getRootDirectory(), "WEB-INF/portlet.xml").length() > beforeSize);
210:
211: // Need to slow it down so the timestamp check works
212: Thread.sleep(500);
213: demoWar.setLastModified(System.currentTimeMillis());
214: autoDeployment.fireRedeploymentEvent();
215:
216: long afterSize = new File(demoAppDeployedDir.getRootDirectory(), "WEB-INF/portlet.xml").length();
217: // The portlet.xml in re-deploy has an additional portlet entry in portlet.xml, so it should be bigger
218: assertTrue(afterSize > beforeSize);
219: autoDeployment.stop();
220:
221: }
222:
223:
224: public void testUndeployVersusRedeploy() throws Exception
225: {
226: manager = new TomcatManager("", "", 0, "", 0, "", "");
227:
228: DeployPortletAppEventListener dpal = new DeployPortletAppEventListener(webAppsDir, new FileSystemPAM(
229: webAppsDir, portletRegistry, entityAccess, windowAccess, portletCache, portletFactory, manager), portletRegistry );
230: ArrayList eventListeners = new ArrayList(1);
231:
232: eventListeners.add(dpal);
233:
234: // Use a -1 delay to disable auto scan
235: StandardDeploymentManager autoDeployment = new StandardDeploymentManager(deploySrc.getAbsolutePath(), -1, eventListeners );
236: autoDeployment.start();
237:
238: buildEntityTestData(autoDeployment);
239:
240:
241: MutablePortletEntity entity = entityAccess.getPortletEntity("testEnity");
242:
243: PreferenceSetCtrl prefs = (PreferenceSetCtrl) entity.getPreferenceSet();
244: List values = new ArrayList(1);
245: values.add("some value");
246: prefs.add("pref1", values);
247:
248: entity.store();
249:
250: assertNotNull(entity);
251:
252: Preference pref = entity.getPreferenceSet().get("pref1");
253:
254: assertNotNull(pref);
255:
256: //test entity removal via undeploy
257: File demoWar = new File(deploySrc, "demo.war");
258: demoWar.delete();
259:
260: autoDeployment.fireUndeploymentEvent();
261:
262:
263: entity = entityAccess.getPortletEntity("testEnity");
264:
265: assertNull(entity);
266:
267: // Now test that redploy DOES NOT kill the entity
268: buildEntityTestData(autoDeployment);
269:
270: entity = entityAccess.getPortletEntity("testEnity");
271:
272: assertNotNull(entity);
273:
274: pref = entity.getPreferenceSet().get("pref1");
275:
276: assertNull("Preference was not deleted with last undeploy",pref);
277:
278: demoWar.setLastModified(System.currentTimeMillis());
279:
280: autoDeployment.fireRedeploymentEvent();
281:
282: entity = entityAccess.getPortletEntity("testEnity");
283:
284: assertNotNull(entity);
285:
286: }
287: */
288:
289: /**
290: * <p>
291: * buildEntityTestData
292: * </p>
293: *
294: * @param autoDeployment
295: * @throws IOException
296: * @throws PortletEntityNotStoredException
297: */
298: protected void buildEntityTestData(
299: StandardDeploymentManager autoDeployment) throws Exception {
300: copyDeployables();
301:
302: File demoApp = new File(webAppsDirFile, TEST_PORTLET_APP_NAME);
303:
304: autoDeployment.fireDeploymentEvent();
305:
306: verifyDemoAppCreated(TEST_PORTLET_APP_NAME, demoApp);
307:
308: MutablePortletApplication app = portletRegistry
309: .getPortletApplication(TEST_PORTLET_APP_NAME);
310:
311: PortletDefinition portlet = (PortletDefinition) app
312: .getPortletDefinitionList().iterator().next();
313:
314: MutablePortletEntity entity = entityAccess
315: .newPortletEntityInstance(portlet);
316: entity.setId("testEnity");
317:
318: entityAccess.storePortletEntity(entity);
319:
320: }
321:
322: /**
323: * <p>
324: * verifyDemoAppCreated
325: * </p>
326: *
327: * @param demoApp
328: */
329: private void verifyDemoAppCreated(String appName, File appFile) {
330: assertNotNull(
331: appName
332: + " was not registered into the portlet registery.",
333: portletRegistry
334: .getPortletApplicationByIdentifier(TEST_PORTLET_APP_NAME));
335: assertTrue(appName
336: + " directory was not created, app not deployed.",
337: appFile.exists());
338: }
339:
340: /**
341: * <p>
342: * verifyDemoAppDeleted
343: * </p>
344: *
345: * @param demoApp
346: *
347: private void verifyDemoAppDeleted( String appName, File appFile )
348: {
349: assertNull(appName + " was not removed from the registry.", portletRegistry
350: .getPortletApplicationByIdentifier(TEST_PORTLET_APP_NAME));
351: assertFalse(appName+" directory was not deleted.", appFile.exists());
352: }
353: */
354:
355: /**
356: * @see junit.framework.TestCase#setUp()
357: */
358: public void setUp() {
359:
360: try {
361: super .setUp();
362: copyFrom = new File("./test/deployment/deploy");
363: deploySrc = new File("./target/deployment/deploy");
364: deploySrc.mkdirs();
365: deployRootFile = new File(
366: "./target/deployment/templates/decorators");
367: deployRootFile.mkdirs();
368: webAppsDirFile = new File("./target/deployment/webapps");
369: webAppsDirFile.mkdirs();
370:
371: webAppsDir = webAppsDirFile.getCanonicalPath();
372: testDb = new File("./test/db/hsql/Registry")
373: .getCanonicalPath();
374:
375: copyDeployables();
376: // windowAccess = new PortletWindowAccessorImpl(entityAccess, true);
377: } catch (Exception e) {
378: e.printStackTrace();
379: throw new AssertionFailedError(
380: "Unable to set up test environment " + e.toString());
381: }
382:
383: }
384:
385: /**
386: * <p>
387: * copyDeployables
388: * </p>
389: * @throws IOException
390: */
391: protected void copyDeployables() throws IOException {
392:
393: copyFiles(copyFrom, deploySrc);
394:
395: }
396:
397: /**
398: * <p>
399: * copyFiles
400: * </p>
401: *
402: * @throws IOException
403: * @throws FileNotFoundException
404: */
405: protected void copyFiles(File srcDir, File dstDir)
406: throws IOException, FileNotFoundException {
407: File[] children = srcDir.listFiles();
408: for (int i = 0; i < children.length; i++) {
409: File child = children[i];
410: if (child.isFile()) {
411: File toFile = new File(dstDir, child.getName());
412: toFile.createNewFile();
413: FileChannel srcChannel = new FileInputStream(child)
414: .getChannel();
415: FileChannel dstChannel = new FileOutputStream(toFile)
416: .getChannel();
417: dstChannel.transferFrom(srcChannel, 0, srcChannel
418: .size());
419: srcChannel.close();
420: dstChannel.close();
421: } else {
422: File newSubDir = new File(dstDir, child.getName());
423: newSubDir.mkdir();
424: copyFiles(child, newSubDir);
425: }
426: }
427: }
428:
429: /**
430: * @see junit.framework.TestCase#tearDown()
431: */
432: public void tearDown() throws Exception {
433: /*
434: manager = new TomcatManager("", "", 0, "", 0, "", "");
435: FileSystemPAM pam = new FileSystemPAM(webAppsDir, portletRegistry, entityAccess, windowAccess, portletCache, portletFactory, manager);
436:
437: try
438: {
439: DirectoryHelper dirHelper = new DirectoryHelper(new File(webAppsDir + "/" + TEST_PORTLET_APP_NAME));
440: paWar1 = new PortletApplicationWar(dirHelper, TEST_PORTLET_APP_NAME, "/"
441: + TEST_PORTLET_APP_NAME);
442: pam.undeploy(paWar1);
443: }
444: catch (Exception e1)
445: {
446:
447: }
448:
449: pam.unregister("jetspeed");
450:
451: try
452: {
453: DirectoryHelper dirHelper = new DirectoryHelper(new File(webAppsDir + "/TestSecurityRoles"));
454: paWar3 = new PortletApplicationWar(dirHelper, "TestSecurityRoles", "/TestSecurityRoles" );
455:
456: pam.undeploy(paWar3);
457: }
458: catch (Exception e3)
459: {
460:
461: }
462:
463: // DirectoryUtils.rmdir(new File("./target/deployment"));
464: new DirectoryHelper(new File("./target/deployment")).remove();
465: */
466: super.tearDown();
467:
468: }
469:
470: }
|