001: /*
002: * ========================================================================
003: *
004: * Copyright 2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant;
021:
022: import java.io.File;
023: import java.util.Iterator;
024:
025: import org.codehaus.cargo.module.application.ApplicationXml;
026: import org.codehaus.cargo.module.application.DefaultEarArchive;
027: import org.codehaus.cargo.module.application.EarArchive;
028: import org.codehaus.cargo.module.webapp.WarArchive;
029: import org.codehaus.cargo.module.webapp.WebXml;
030: import org.codehaus.cargo.module.webapp.WebXmlTag;
031: import org.codehaus.cargo.module.webapp.weblogic.WeblogicXml;
032: import org.codehaus.cargo.module.webapp.weblogic.WeblogicXmlTag;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.NodeList;
035:
036: /**
037: * Test class for the CactifyEar task.
038: *
039: * @version $Id: TestCactifyEarTask.java 239162 2005-04-26 09:57:59Z grimsell $
040: */
041: public class TestCactifyEarTask extends AntTestCase {
042: /**
043: * @see AntTestCase#AntTestCase
044: */
045: public TestCactifyEarTask() {
046: super ("org/apache/cactus/integration/ant/test-cactifyear.xml");
047: }
048:
049: /**
050: * @see junit.framework.TestCase#setUp()
051: */
052: protected void setUp() throws Exception {
053: super .setUp();
054:
055: getProject().addTaskDefinition("cactifyear",
056: CactifyEarTask.class);
057: }
058:
059: /**
060: * Tests that the basic function of the task works,
061: * that is to add a cactified war to the ear.
062: *
063: * @throws Exception If an unexpected error occurs
064: */
065: public void testCanAddCactifiedWar() throws Exception {
066: executeTestTarget();
067:
068: File destFile = getProject().resolveFile("work/cactified.ear");
069: EarArchive destEar = new DefaultEarArchive(destFile);
070: ApplicationXml appXml = destEar.getApplicationXml();
071: assertEquals("/cactus", appXml
072: .getWebModuleContextRoot("cactus.war"));
073: WarArchive cactusWar = destEar.getWebModule("cactus.war");
074: WebXml webXml = cactusWar.getWebXml();
075: assertNotNull(webXml.getServlet("ServletRedirector"));
076: }
077:
078: /**
079: * Tests that the context of the cactus.war can be specified.
080: *
081: * @throws Exception If an unexpected error occurs
082: */
083: public void testCustomCactusWarContext() throws Exception {
084: executeTestTarget();
085:
086: File destFile = getProject().resolveFile("work/cactified.ear");
087: EarArchive destEar = new DefaultEarArchive(destFile);
088: ApplicationXml appXml = destEar.getApplicationXml();
089: assertEquals("/myTestFramework", appXml
090: .getWebModuleContextRoot("cactus.war"));
091: }
092:
093: /**
094: * @throws Exception If an unexpected error occurs
095: */
096: public void testAddEjbReferences() throws Exception {
097: executeTestTarget();
098:
099: File destFile = getProject().resolveFile("work/cactified.ear");
100: EarArchive destEar = new DefaultEarArchive(destFile);
101: WarArchive cactusWar = destEar.getWebModule("cactus.war");
102:
103: // test web.xml
104: WebXml webXml = cactusWar.getWebXml();
105: Iterator i = webXml.getElements(WebXmlTag.EJB_LOCAL_REF);
106: assertEjbRef((Element) i.next(), "ejb/Session2", "Session",
107: "com.wombat.Session2", "com.wombat.Session2Home");
108: assertEjbRef((Element) i.next(), "ejb/Entity1", "Entity",
109: "com.wombat.Entity1", "com.wombat.Entity1Home");
110: assertFalse(i.hasNext());
111:
112: // test weblogic.xml
113: WeblogicXml weblogicXml = (WeblogicXml) webXml
114: .getVendorDescriptor();
115: i = weblogicXml
116: .getElements(WeblogicXmlTag.EJB_REFERENCE_DESCRIPTION);
117: assertWeblogicEjbRef((Element) i.next(), "ejb/Session2",
118: "/wombat/Session2");
119: assertWeblogicEjbRef((Element) i.next(), "ejb/Entity1",
120: "/wombat/Entity1");
121: assertFalse(i.hasNext());
122: }
123:
124: /**
125: * Help method to check that a given element is a correct ejb-ref
126: *
127: * @param theElement the Element to check
128: * @param theName correct name of the ejb-ref
129: * @param theType correct ejb-ref type
130: * @param theLocal correct local interface of the ejb-ref
131: * @param theLocalHome correct local home interface of the ejb-ref
132: */
133: private void assertEjbRef(Element theElement, String theName,
134: String theType, String theLocal, String theLocalHome) {
135: NodeList nl = theElement.getElementsByTagName("ejb-ref-name");
136: Element f = (Element) nl.item(0);
137: assertEquals(theName, f.getFirstChild().getNodeValue());
138: nl = theElement.getElementsByTagName("ejb-ref-type");
139: f = (Element) nl.item(0);
140: assertEquals(theType, f.getFirstChild().getNodeValue());
141: nl = theElement.getElementsByTagName("local-home");
142: f = (Element) nl.item(0);
143: assertEquals(theLocalHome, f.getFirstChild().getNodeValue());
144: nl = theElement.getElementsByTagName("local");
145: f = (Element) nl.item(0);
146: assertEquals(theLocal, f.getFirstChild().getNodeValue());
147: }
148:
149: /**
150: * Help method to check that a given element is a correct weblogic ejb-ref
151: *
152: * @param theElement the Element to check
153: * @param theName correct name of the ejb-ref
154: * @param theJndiName correct jndi name of the ejb-ref
155: */
156: private void assertWeblogicEjbRef(Element theElement,
157: String theName, String theJndiName) {
158: NodeList nl = theElement.getElementsByTagName("ejb-ref-name");
159: Element f = (Element) nl.item(0);
160: assertEquals(theName, f.getFirstChild().getNodeValue());
161: nl = theElement.getElementsByTagName("jndi-name");
162: f = (Element) nl.item(0);
163: assertEquals(theJndiName, f.getFirstChild().getNodeValue());
164: }
165: }
|