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.io;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.util.jar.JarEntry;
027: import java.util.jar.JarInputStream;
028: import java.util.jar.JarOutputStream;
029:
030: import junit.framework.TestCase;
031:
032: import org.apache.commons.io.IOUtils;
033: import org.apache.pluto.descriptors.portlet.PortletAppDD;
034: import org.apache.pluto.descriptors.portlet.PortletDD;
035: import org.apache.pluto.descriptors.services.PortletAppDescriptorService;
036: import org.apache.pluto.descriptors.services.WebAppDescriptorService;
037: import org.apache.pluto.descriptors.services.castor.PortletAppDescriptorServiceImpl;
038: import org.apache.pluto.descriptors.services.castor.WebAppDescriptorServiceImpl;
039: import org.apache.pluto.descriptors.servlet.ServletDD;
040: import org.apache.pluto.descriptors.servlet.WebAppDD;
041: import org.apache.pluto.util.assemble.Assembler;
042:
043: /**
044: * This test class directly tests the Jar streaming assembly,
045: * bypassing the file assemblers (EarAssembler, WarAssembler).
046: */
047: public class AssemblyStreamTest extends TestCase {
048:
049: private InputStream webXmlIn = null;
050: private InputStream portletXmlIn = null;
051: private InputStream warIn = null;
052: private JarStreamingAssembly jarAssemblerUnderTest = null;
053: private WebXmlStreamingAssembly webXmlAssemblerUnderTest = null;
054:
055: private static final String webXmlToAssembleResource = "/org/apache/pluto/util/assemble/file/web.xml";
056: private static final String portletXmlResource = "/org/apache/pluto/util/assemble/file/portlet.xml";
057: private static final String warToAssembleResource = "/org/apache/pluto/util/assemble/war/WarDeployerTestPortlet.war";
058:
059: private static final String testPortletName = "WarTestPortletName";
060:
061: protected void setUp() throws Exception {
062: webXmlIn = getClass().getResourceAsStream(
063: webXmlToAssembleResource);
064: portletXmlIn = getClass().getResourceAsStream(
065: portletXmlResource);
066: warIn = getClass().getResourceAsStream(warToAssembleResource);
067: jarAssemblerUnderTest = new JarStreamingAssembly();
068: webXmlAssemblerUnderTest = new WebXmlStreamingAssembly();
069: }
070:
071: public void testJarStreamingAssembly() throws Exception {
072: File warFileOut = File.createTempFile(
073: "streamingAssemblyWarTest", ".war");
074: JarInputStream jarIn = new JarInputStream(warIn);
075: JarOutputStream warOut = new JarOutputStream(
076: new FileOutputStream(warFileOut));
077: jarAssemblerUnderTest.assembleStream(jarIn, warOut,
078: Assembler.DISPATCH_SERVLET_CLASS);
079: assertTrue("Assembled WAR file was not created.", warFileOut
080: .exists());
081: verifyAssembly(warFileOut);
082: warFileOut.delete();
083: }
084:
085: public void testWebXmlStreamingAssembly() throws Exception {
086: File assembledWebXml = File.createTempFile(
087: "streamingWebXmlTest", ".xml");
088: OutputStream assembledWebXmlOut = new FileOutputStream(
089: assembledWebXml);
090: webXmlAssemblerUnderTest.assembleStream(webXmlIn, portletXmlIn,
091: assembledWebXmlOut, Assembler.DISPATCH_SERVLET_CLASS);
092: verifyAssembly(new FileInputStream(assembledWebXml), getClass()
093: .getResourceAsStream(portletXmlResource));
094: assembledWebXml.delete();
095: }
096:
097: protected void verifyAssembly(InputStream webXml,
098: InputStream portletXml) throws Exception {
099: WebAppDescriptorService webSvc = new WebAppDescriptorServiceImpl();
100: PortletAppDescriptorService portletSvc = new PortletAppDescriptorServiceImpl();
101: WebAppDD webApp = webSvc.read(webXml);
102: PortletAppDD portletApp = portletSvc.read(portletXml);
103:
104: assertNotNull("Web Application Descripter was null.", webApp);
105: assertNotNull("Portlet Application Descriptor was null.",
106: portletApp);
107: assertTrue(
108: "Portlet Application Descriptor doesn't define any portlets.",
109: portletApp.getPortlets().size() > 0);
110: assertTrue(
111: "Web Application Descriptor doesn't define any servlets.",
112: webApp.getServlets().size() > 0);
113: assertTrue(
114: "Web Application Descriptor doesn't define any servlet mappings.",
115: webApp.getServletMappings().size() > 0);
116:
117: PortletDD portlet = (PortletDD) portletApp.getPortlets()
118: .iterator().next();
119: assertTrue("Unable to retrieve test portlet named ["
120: + testPortletName + "]", portlet.getPortletName()
121: .equals(testPortletName));
122:
123: ServletDD servlet = webApp.getServlet(testPortletName);
124: assertNotNull(
125: "Unable to retrieve portlet dispatch for portlet named ["
126: + testPortletName + "]", servlet);
127: assertEquals("Dispatcher servlet incorrect for test portlet ["
128: + testPortletName + "]",
129: Assembler.DISPATCH_SERVLET_CLASS, servlet
130: .getServletClass());
131: }
132:
133: protected void verifyAssembly(File warFile) throws Exception {
134: WebAppDescriptorService webSvc = new WebAppDescriptorServiceImpl();
135: PortletAppDescriptorService portletSvc = new PortletAppDescriptorServiceImpl();
136: int entryCount = 0;
137: ByteArrayOutputStream portletXmlBytes = new ByteArrayOutputStream();
138: ByteArrayOutputStream webXmlBytes = new ByteArrayOutputStream();
139: WebAppDD webApp = null;
140: PortletAppDD portletApp = null;
141:
142: JarInputStream assembledWarIn = new JarInputStream(
143: new FileInputStream(warFile));
144: JarEntry tempEntry;
145:
146: while ((tempEntry = assembledWarIn.getNextJarEntry()) != null) {
147: entryCount++;
148:
149: if (Assembler.PORTLET_XML.equals(tempEntry.getName())) {
150: IOUtils.copy(assembledWarIn, portletXmlBytes);
151: portletApp = portletSvc.read(new ByteArrayInputStream(
152: portletXmlBytes.toByteArray()));
153: }
154: if (Assembler.SERVLET_XML.equals(tempEntry.getName())) {
155: IOUtils.copy(assembledWarIn, webXmlBytes);
156: webApp = webSvc.read(new ByteArrayInputStream(
157: webXmlBytes.toByteArray()));
158: }
159: }
160:
161: assertTrue("Assembled WAR file was empty.", entryCount > 0);
162: assertNotNull("Web Application Descripter was null.", webApp);
163: assertNotNull("Portlet Application Descriptor was null.",
164: portletApp);
165: assertTrue(
166: "Portlet Application Descriptor doesn't define any portlets.",
167: portletApp.getPortlets().size() > 0);
168: assertTrue(
169: "Web Application Descriptor doesn't define any servlets.",
170: webApp.getServlets().size() > 0);
171: assertTrue(
172: "Web Application Descriptor doesn't define any servlet mappings.",
173: webApp.getServletMappings().size() > 0);
174:
175: PortletDD portlet = (PortletDD) portletApp.getPortlets()
176: .iterator().next();
177: assertTrue("Unable to retrieve test portlet named ["
178: + testPortletName + "]", portlet.getPortletName()
179: .equals(testPortletName));
180:
181: ServletDD servlet = webApp.getServlet(testPortletName);
182: assertNotNull(
183: "Unable to retrieve portlet dispatch for portlet named ["
184: + testPortletName + "]", servlet);
185: assertEquals("Dispatcher servlet incorrect for test portlet ["
186: + testPortletName + "]",
187: Assembler.DISPATCH_SERVLET_CLASS, servlet
188: .getServletClass());
189: }
190: }
|