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.pluto.util.assemble.ear;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.util.jar.JarEntry;
023: import java.util.jar.JarInputStream;
024:
025: import org.apache.commons.io.FileUtils;
026: import org.apache.commons.io.IOUtils;
027: import org.apache.pluto.descriptors.portlet.PortletAppDD;
028: import org.apache.pluto.descriptors.portlet.PortletDD;
029: import org.apache.pluto.descriptors.services.PortletAppDescriptorService;
030: import org.apache.pluto.descriptors.services.WebAppDescriptorService;
031: import org.apache.pluto.descriptors.services.castor.PortletAppDescriptorServiceImpl;
032: import org.apache.pluto.descriptors.services.castor.WebAppDescriptorServiceImpl;
033: import org.apache.pluto.descriptors.servlet.ServletDD;
034: import org.apache.pluto.descriptors.servlet.WebAppDD;
035: import org.apache.pluto.util.assemble.Assembler;
036: import org.apache.pluto.util.assemble.AssemblerConfig;
037: import org.apache.pluto.util.assemble.ArchiveBasedAssemblyTest;
038:
039: /**
040: * This test assembles an EAR file which contains a single portlet
041: * web application for assembly.
042: */
043: public class EarAssemblerTest extends ArchiveBasedAssemblyTest {
044:
045: private static final String earResource = "/org/apache/pluto/util/assemble/ear/EarDeployerTest.ear";
046: private static final String testPortletName = "WarTestPortletName";
047: private File earFile = null;
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: this .earFile = new File(this .getClass()
052: .getResource(earResource).getFile());
053: }
054:
055: protected void tearDown() throws Exception {
056: super .tearDown();
057: this .earFile = null;
058: }
059:
060: public void testEarAssemblyToTempDir() throws Exception {
061: AssemblerConfig config = new AssemblerConfig();
062: config.setSource(earFile);
063: File assembledEar = File.createTempFile(earFile.getName(),
064: ".ear");
065: config.setDestination(assembledEar);
066: EarAssembler assembler = new EarAssembler();
067: assembler.assemble(config);
068: validateEarAssembly(assembledEar);
069: assembledEar.delete();
070: }
071:
072: public void testEarAssemblyInPlace() throws Exception {
073: // copy the test ear file to a temp directory, so we don't overwrite the
074: // test ear file distributed with Pluto.
075: File inplaceEarFile = File.createTempFile(earFile.getName(),
076: ".ear");
077: FileUtils.copyFile(earFile, inplaceEarFile);
078:
079: AssemblerConfig config = new AssemblerConfig();
080: config.setSource(inplaceEarFile);
081: config.setDestination(inplaceEarFile);
082: EarAssembler assembler = new EarAssembler();
083: assembler.assemble(config);
084: validateEarAssembly(inplaceEarFile);
085:
086: inplaceEarFile.delete();
087: }
088:
089: protected void validateEarAssembly(File earFile) throws Exception {
090: assertTrue("EAR archive [" + earFile.getAbsolutePath()
091: + "] cannot be found or cannot be read", earFile
092: .exists()
093: && earFile.canRead());
094:
095: PortletAppDescriptorService portletSvc = new PortletAppDescriptorServiceImpl();
096: WebAppDescriptorService webSvc = new WebAppDescriptorServiceImpl();
097: PortletAppDD portletAppDD = null;
098: WebAppDD webAppDD = null;
099:
100: int earEntryCount = 0;
101: int warEntryCount = 0;
102:
103: JarInputStream earIn = new JarInputStream(new FileInputStream(
104: earFile));
105:
106: JarEntry earEntry;
107: JarEntry warEntry;
108:
109: while ((earEntry = earIn.getNextJarEntry()) != null) {
110: earEntryCount++;
111: if (earEntry.getName().endsWith(".war")) {
112: warEntryCount++;
113: JarInputStream warIn = new JarInputStream(earIn);
114: while ((warEntry = warIn.getNextJarEntry()) != null) {
115: if (Assembler.PORTLET_XML
116: .equals(warEntry.getName())) {
117: portletAppDD = portletSvc
118: .read(new ByteArrayInputStream(IOUtils
119: .toByteArray(warIn)));
120: }
121: if (Assembler.SERVLET_XML
122: .equals(warEntry.getName())) {
123: webAppDD = webSvc
124: .read(new ByteArrayInputStream(IOUtils
125: .toByteArray(warIn)));
126: }
127: }
128: }
129: }
130:
131: assertTrue("EAR archive did not contain any entries",
132: earEntryCount > 0);
133: assertTrue("WAR archive did not contain any entries",
134: warEntryCount > 0);
135: assertNotNull("WAR archive did not contain a portlet.xml",
136: portletAppDD);
137: assertNotNull("WAR archive did not contain a servlet.xml",
138: webAppDD);
139: assertTrue("WAR archive did not contain any servlets", webAppDD
140: .getServlets().size() > 0);
141: assertTrue("WAR archive did not contain any servlet mappings",
142: webAppDD.getServletMappings().size() > 0);
143: assertTrue("WAR archive did not contain any portlets",
144: portletAppDD.getPortlets().size() > 0);
145:
146: PortletDD portlet = (PortletDD) portletAppDD.getPortlets()
147: .iterator().next();
148: assertEquals("Unexpected test portlet name.", testPortletName,
149: portlet.getPortletName());
150:
151: ServletDD servlet = webAppDD.getServlet(testPortletName);
152: assertNotNull(
153: "web.xml does not contain assembly for test portlet",
154: servlet);
155: assertEquals(
156: "web.xml does not contain correct dispatch servet",
157: Assembler.DISPATCH_SERVLET_CLASS, servlet
158: .getServletClass());
159:
160: }
161:
162: protected Assembler getAssemblerUnderTest() {
163: return new EarAssembler();
164: }
165:
166: protected File getFileToAssemble() {
167: return earFile;
168: }
169:
170: }
|