KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > deployers > WebAppParsingDeployerTestCase


1 package org.jboss.test.deployers;
2
3 import java.net.URL JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.jboss.deployers.plugins.deployer.AbstractDeploymentUnit;
10 import org.jboss.deployers.plugins.deployment.MainDeployerImpl;
11 import org.jboss.deployers.plugins.structure.AbstractDeploymentContext;
12 import org.jboss.deployers.spi.deployer.DeploymentUnit;
13 import org.jboss.deployers.spi.structure.DeploymentContext;
14 import org.jboss.deployers.spi.structure.StructureDetermined;
15 import org.jboss.metadata.EjbLocalRefMetaData;
16 import org.jboss.metadata.EnvEntryMetaData;
17 import org.jboss.metadata.WebMetaData;
18 import org.jboss.metadata.web.ParamValue;
19 import org.jboss.test.BaseTestCase;
20 import org.jboss.virtual.VFS;
21 import org.jboss.virtual.VirtualFile;
22 import org.jboss.deployment.JBossWebAppParsingDeployer;
23 import org.jboss.deployment.WebAppParsingDeployer;
24
25 /**
26  * This is a test of the org.jboss.deployment.WebAppParsingDeployer and
27  * JBossWebAppParsingDeployer metadata deployers. It should be in a server
28  * related testsuite, but including such a test in the server project slows the
29  * workspace build so its here until there is better separation of
30  * tests/projects.
31  *
32  * @author Scott.Stark@jboss.org
33  * @version $Revision:$
34  */

35 public class WebAppParsingDeployerTestCase extends BaseTestCase
36 {
37
38    public WebAppParsingDeployerTestCase(String JavaDoc name)
39    {
40       super(name);
41    }
42
43    /**
44     * Overriden to direct the LogginPlugin to the
45     * org.jboss.test.logging.Log4jConsoleLoggingPlugin version to avoid
46     * finding spurious log4j configuration resources in the classpath.
47     */

48    @Override JavaDoc
49    protected void setUp() throws Exception JavaDoc
50    {
51       System.setProperty("org.jboss.test.logging.LogginPlugin",
52             "org.jboss.test.logging.Log4jConsoleLoggingPlugin");
53       super.setUp();
54    }
55
56    /**
57     * Parse a servlet 2.4 web.xml descriptor
58     * @throws Exception
59     */

60    public void testWebApp24Ex1()
61       throws Exception JavaDoc
62    {
63       log.info("testWebApp24Ex1");
64       DeploymentUnit unit = createDeploymentUnit("org/jboss/test/deployers");
65       WebAppParsingDeployer deployer = new WebAppParsingDeployer();
66
67       deployer.setWebXmlPath("web24-ex1.xml");
68       deployer.deploy(unit);
69       WebMetaData dd = unit.getAttachment(WebMetaData.class);
70       assertNotNull("WebMetaData dd", dd);
71       validateWebMetaData24Ex1(dd, false);
72    }
73
74    /**
75     * Parse a servlet 2.4 web.xml descriptor and a jboss-web.xml descriptor to
76     * validate that the WebAppParsingDeployer and JBossWebAppParsingDeployer
77     * work together to build an aggregate WebMetaData instance.
78     *
79     * @throws Exception
80     */

81    public void testWebAppJBossWebApp()
82       throws Exception JavaDoc
83    {
84       DeploymentUnit unit = createDeploymentUnit("org/jboss/test/deployers");
85       MainDeployerImpl md = new MainDeployerImpl();
86       WebAppParsingDeployer d1 = new WebAppParsingDeployer();
87       d1.setWebXmlPath("web24-ex1.xml");
88       md.addDeployer(d1);
89       JBossWebAppParsingDeployer d2 = new JBossWebAppParsingDeployer();
90       md.addDeployer(d2);
91       d2.setWebXmlPath("jboss-web24-ex1.xml");
92       DeploymentContext ctx = unit.getDeploymentContext();
93       ctx.setStructureDetermined(StructureDetermined.PREDETERMINED);
94       md.addDeploymentContext(ctx);
95       md.process();
96       
97       WebMetaData dd = unit.getAttachment(WebMetaData.class);
98       assertNotNull("WebMetaData dd", dd);
99       validateWebMetaData24Ex1(dd, true);
100    }
101
102    /**
103     * Utility method that validates the web.xml/jboss-web.xml to WebMetaData
104     * mappings.
105     *
106     * @param dd - the WebMetaData instance to validate
107     * @param hasJBossWeb - a flag indicating if the jboss-web.xml specific
108     * fields should be validated.
109     */

110    protected void validateWebMetaData24Ex1(WebMetaData dd, boolean hasJBossWeb)
111    {
112       assertEquals("web-app/description", "Web App 2.4 Descriptor", dd.getDescription());
113       assertEquals("web-app/display-name", "Web App 2.4 Descriptor on Display", dd.getDisplayName());
114       assertEquals("web-app/distributable", true, dd.getDistributable());
115       // web-app/context-param
116
List JavaDoc<ParamValue> contextParams = dd.getContextParams();
117       assertEquals("context-params.size = 1", 1, contextParams.size());
118       ParamValue pv = contextParams.get(0);
119       assertEquals("pv.description", "A test context-param", pv.getDescription());
120       assertEquals("pv.name", "contextParam1", pv.getName());
121       assertEquals("pv.value", "contextParam1Value", pv.getValue());
122       // web-app/ejb-local-ref
123
Map JavaDoc<String JavaDoc, EjbLocalRefMetaData> ejbLocalRefs = dd.getEjbLocalReferenceMap();
124       assertEquals("ejb-local-ref.size = 1", 1, ejbLocalRefs.size());
125       EjbLocalRefMetaData elrmd = ejbLocalRefs.get("SomeLocalEJB");
126       assertNotNull("SomeLocalEJB", elrmd);
127       assertEquals("SomeLocalEJB", elrmd.getName());
128       assertEquals("SomeLocalEJB.Local", "org.jboss.ejb.ISomeLocal", elrmd.getLocal());
129       assertEquals("SomeLocalEJB.LocalHome", "org.jboss.ejb.ISomeLocalHome", elrmd.getLocalHome());
130       assertEquals("SomeLocalEJB.Type", "Session", elrmd.getType());
131       // web-app/env-entry
132
HashSet JavaDoc<String JavaDoc> expectedNames = new HashSet JavaDoc<String JavaDoc>();
133       expectedNames.add("aString");
134       expectedNames.add("anInt");
135       Iterator JavaDoc<EnvEntryMetaData> envEntryIter = dd.getEnvironmentEntries();
136       while( envEntryIter.hasNext() )
137       {
138          EnvEntryMetaData entry = envEntryIter.next();
139          if( entry.getName().endsWith("aString") )
140          {
141             assertEquals("type is String", "java.lang.String", entry.getType());
142             assertEquals("aStringValue", "aStringValue", entry.getValue());
143          }
144          else if( entry.getName().endsWith("anInt") )
145          {
146             assertEquals("type is Integer", "java.lang.Integer", entry.getType());
147             assertEquals("aStringValue", "123456", entry.getValue());
148          }
149          expectedNames.remove(entry.getName());
150       }
151       assertEquals("All expected names seen", 0, expectedNames.size());
152       if( hasJBossWeb == false )
153          return;
154       
155       String JavaDoc contextRoot = dd.getContextRoot();
156       assertEquals("context-root", "testWebAppJBossWebApp", contextRoot);
157    }
158
159    /**
160     *
161     * @param root
162     * @param path
163     * @return
164     * @throws Exception
165     */

166    protected VirtualFile getVirtualFile(String JavaDoc path) throws Exception JavaDoc
167    {
168       URL JavaDoc url = getClass().getProtectionDomain().getCodeSource().getLocation();
169       log.debug("CodeSource: "+url);
170       return VFS.getVirtualFile(url, path);
171    }
172    protected DeploymentUnit createDeploymentUnit(String JavaDoc path)
173       throws Exception JavaDoc
174    {
175       VirtualFile file = getVirtualFile(path);
176       log.debug("Resolved path to: "+file);
177       AbstractDeploymentContext context = new AbstractDeploymentContext(file);
178       // Use the path as the metadata location
179
context.setMetaDataPath("/");
180       AbstractDeploymentUnit unit = new AbstractDeploymentUnit(context);
181       return unit;
182    }
183
184 }
185
Popular Tags