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.Arrays;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.jar.JarEntry;
026: import java.util.jar.JarInputStream;
027:
028: import org.apache.commons.io.IOUtils;
029: import org.apache.pluto.descriptors.portlet.PortletAppDD;
030: import org.apache.pluto.descriptors.portlet.PortletDD;
031: import org.apache.pluto.descriptors.services.PortletAppDescriptorService;
032: import org.apache.pluto.descriptors.services.WebAppDescriptorService;
033: import org.apache.pluto.descriptors.services.castor.PortletAppDescriptorServiceImpl;
034: import org.apache.pluto.descriptors.services.castor.WebAppDescriptorServiceImpl;
035: import org.apache.pluto.descriptors.servlet.ServletDD;
036: import org.apache.pluto.descriptors.servlet.WebAppDD;
037: import org.apache.pluto.util.assemble.Assembler;
038: import org.apache.pluto.util.assemble.AssemblerConfig;
039: import org.apache.pluto.util.assemble.ArchiveBasedAssemblyTest;
040:
041: /**
042: * This test assembles an EAR file which contains two portlet
043: * web applications for assembly. There is a third war file
044: * which is not a portlet, and therefore shouldn't be assembled,
045: * just copied.
046: */
047: public class ComplexEarAssemblerTest extends ArchiveBasedAssemblyTest {
048:
049: private static final String earResource = "/org/apache/pluto/util/assemble/ear/ComplexEarDeployerTest.ear";
050:
051: private static final String[] testPortletNames;
052: private static final String[] testWarEntryNames;
053: private static final String[] unassembledWarEntryName;
054: static {
055: testPortletNames = new String[] { "WarTestPortletName",
056: "SecondWarTestPortletName" };
057: testWarEntryNames = new String[] {
058: "WarDeployerTestPortlet.war",
059: "SecondWarDeployerTestPortlet.war" };
060: unassembledWarEntryName = new String[] { "WarWithNoPortlet.war" };
061: }
062:
063: private File earFile = null;
064:
065: protected void setUp() throws Exception {
066: super .setUp();
067: earFile = new File(getClass().getResource(earResource)
068: .getFile());
069: }
070:
071: public void testMultipleWarFileAssembly() throws Exception {
072: File tmpFile = File.createTempFile("earTest", "tmp");
073: File destDir = new File(tmpFile.getName() + ".dir");
074: destDir.mkdirs();
075:
076: assertTrue("Source ear file doesn't exist! [" + earFile + "]",
077: earFile.exists());
078: assertTrue("Destination directory doesn't exist! [" + destDir
079: + "]", destDir.exists() && destDir.isDirectory());
080:
081: AssemblerConfig config = new AssemblerConfig();
082: config.setSource(earFile);
083: config.setDestination(destDir);
084: EarAssembler assembler = new EarAssembler();
085:
086: assembler.assemble(config);
087:
088: File destFile = new File(destDir, earFile.getName());
089:
090: assertTrue("Assembled ear file doesn't exist, assembly failed",
091: destFile.exists());
092:
093: validateEarAssembly(destFile);
094:
095: tmpFile.delete();
096: destFile.delete();
097: destDir.delete();
098: }
099:
100: protected void validateEarAssembly(File earFile) throws Exception {
101: assertTrue("EAR archive [" + earFile.getAbsolutePath()
102: + "] cannot be found or cannot be read", earFile
103: .exists()
104: && earFile.canRead());
105:
106: PortletAppDescriptorService portletSvc = new PortletAppDescriptorServiceImpl();
107: WebAppDescriptorService webSvc = new WebAppDescriptorServiceImpl();
108: PortletAppDD portletAppDD = null;
109: WebAppDD webAppDD = null;
110:
111: List portletWarEntries = Arrays.asList(testWarEntryNames);
112: List unassembledWarEntries = Arrays
113: .asList(unassembledWarEntryName);
114: List testPortlets = Arrays.asList(testPortletNames);
115:
116: int earEntryCount = 0;
117: int totalWarEntryCount = 0;
118: int portletWarEntryCount = 0;
119:
120: JarInputStream earIn = new JarInputStream(new FileInputStream(
121: earFile));
122:
123: JarEntry earEntry;
124: JarEntry warEntry;
125:
126: while ((earEntry = earIn.getNextJarEntry()) != null) {
127: earEntryCount++;
128:
129: if (earEntry.getName().endsWith(".war")) {
130: totalWarEntryCount++;
131: JarInputStream warIn = new JarInputStream(earIn);
132:
133: while ((warEntry = warIn.getNextJarEntry()) != null) {
134: if (Assembler.PORTLET_XML
135: .equals(warEntry.getName())) {
136: portletAppDD = portletSvc
137: .read(new ByteArrayInputStream(IOUtils
138: .toByteArray(warIn)));
139: }
140: if (Assembler.SERVLET_XML
141: .equals(warEntry.getName())) {
142: webAppDD = webSvc
143: .read(new ByteArrayInputStream(IOUtils
144: .toByteArray(warIn)));
145: }
146: }
147:
148: if (portletWarEntries.contains(earEntry.getName())) {
149: portletWarEntryCount++;
150: assertNotNull(
151: "WAR archive did not contain a portlet.xml",
152: portletAppDD);
153: assertNotNull(
154: "WAR archive did not contain a servlet.xml",
155: webAppDD);
156: assertTrue(
157: "WAR archive did not contain any servlets",
158: webAppDD.getServlets().size() > 0);
159: assertTrue(
160: "WAR archive did not contain any servlet mappings",
161: webAppDD.getServletMappings().size() > 0);
162: assertTrue(
163: "WAR archive did not contain any portlets",
164: portletAppDD.getPortlets().size() > 0);
165:
166: for (Iterator iter = portletAppDD.getPortlets()
167: .iterator(); iter.hasNext();) {
168: PortletDD portlet = (PortletDD) iter.next();
169: if (!testPortlets.contains(portlet
170: .getPortletName())) {
171: fail("Unexpected test portlet name encountered: ["
172: + portlet.getPortletName() + "]");
173: }
174: ServletDD servlet = webAppDD.getServlet(portlet
175: .getPortletName());
176: assertNotNull(
177: "web.xml does not contain assembly for test portlet",
178: servlet);
179: assertEquals(
180: "web.xml does not contain correct dispatch servet",
181: Assembler.DISPATCH_SERVLET_CLASS,
182: servlet.getServletClass());
183: }
184:
185: }
186:
187: webAppDD = null;
188: portletAppDD = null;
189:
190: }
191:
192: }
193:
194: assertTrue("EAR archive did not contain any entries",
195: earEntryCount > 0);
196: assertEquals(
197: "EAR archive did not contain the expected test war entries.",
198: portletWarEntries.size(), portletWarEntryCount);
199: assertEquals(
200: "WAR archive did not contain the correct number of entries",
201: portletWarEntries.size() + unassembledWarEntries.size(),
202: totalWarEntryCount);
203:
204: }
205:
206: protected Assembler getAssemblerUnderTest() {
207: return new EarAssembler();
208: }
209:
210: protected File getFileToAssemble() {
211: return earFile;
212: }
213:
214: }
|