KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > deploymentdesc > TestLoading


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: TestLoading.java 1026 2006-08-04 14:54:10Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.tests.deploymentdesc;
27
28 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL2;
29 import static org.testng.Assert.assertEquals;
30 import static org.testng.Assert.assertNotNull;
31 import static org.testng.Assert.assertTrue;
32
33 import java.io.File JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.List JavaDoc;
36
37 import org.objectweb.easybeans.deployment.xml.parsing.EJB3DeploymentDescLoader;
38 import org.objectweb.easybeans.deployment.xml.parsing.ParsingException;
39 import org.objectweb.easybeans.deployment.xml.struct.EJB3;
40 import org.objectweb.easybeans.deployment.xml.struct.EnterpriseBeans;
41 import org.objectweb.easybeans.deployment.xml.struct.Session;
42 import org.objectweb.easybeans.deployment.xml.struct.common.EnvEntry;
43 import org.objectweb.easybeans.util.url.URLUtilsException;
44 import org.objectweb.easybeans.util.xml.DocumentParserException;
45 import org.testng.annotations.Test;
46 import org.xml.sax.SAXException JavaDoc;
47
48 /**
49  * Tests the parsing of an ejb-jar.xml file.
50  * @author Florent Benoit
51  */

52 public class TestLoading {
53
54     /**
55      * Prefix for XML files.
56      */

57     private static final String JavaDoc PREFIX = TestLoading.class.getPackage().getName().replace(".", "/") + "/xmls/";
58
59     /**
60      * Prefix of src for tests.
61      */

62     private static final String JavaDoc TEST_DIR_PREFIX = "tests/src/java/";
63
64     /**
65      * Gets an url for a given ejb-jar.xml filename.
66      * @param fileName the name of the fiel to search.
67      * @return an URL if found
68      * @throws ParsingException if no URL is found.
69      */

70     private static URL JavaDoc getURL(final String JavaDoc fileName) throws ParsingException {
71         URL JavaDoc url = TestLoading.class.getResource(PREFIX + fileName);
72         if (url == null) {
73             // try with filesystem
74
File JavaDoc file = new File JavaDoc(TEST_DIR_PREFIX + PREFIX + fileName);
75             if (file.exists()) {
76                 try {
77                     return fileToURL2(file);
78                 } catch (URLUtilsException e) {
79                     throw new ParsingException("Cannot transform file into url", e);
80                 }
81             }
82         }
83         if (url == null) {
84             throw new ParsingException("URl for '" + fileName + "' was not found.");
85         }
86         return url;
87     }
88
89     /**
90      * Parse a given filename and gets the resulting parsing.
91      * @param fileName the name of the file to parse
92      * @return a struct representing the EJB
93      * @throws ParsingException if parsing has failed.
94      */

95     private EJB3 getEjb3(final String JavaDoc fileName) throws ParsingException {
96         URL JavaDoc urlEjb01 = getURL(fileName);
97
98         return EJB3DeploymentDescLoader.loadDeploymentDescriptor(urlEjb01);
99
100     }
101
102     /**
103      * Checks that an xml file can be parsed by analyzing the result.
104      * @throws ParsingException if xml is not parsed.
105      */

106     @Test
107     public void analyzeParsedXml() throws ParsingException {
108         EJB3 ejb3 = getEjb3("ejb-jar01.xml");
109
110         // Ensure that there is a bean named TestBean.
111
EnterpriseBeans enterpriseBeans = ejb3.getEnterpriseBeans();
112         assertNotNull(enterpriseBeans);
113
114         // there are session beans
115
List JavaDoc<Session> sessionList = enterpriseBeans.getSessionList();
116         assertNotNull(sessionList);
117         assertTrue(sessionList.size() > 0);
118
119         // first bean = TestBean
120
Session testBean = sessionList.get(0);
121         assertNotNull(testBean);
122         assertEquals("TestBean", testBean.getEjbName());
123
124         // Env-Entry
125
List JavaDoc<EnvEntry> envEntries = testBean.getEnvEntryList();
126         // there are envEntries
127
assertNotNull(envEntries);
128         assertTrue(envEntries.size() > 0);
129
130         EnvEntry envEntry = envEntries.get(0);
131         assertEquals("testBoolean", envEntry.getEnvEntryName());
132         assertEquals("java.lang.Boolean", envEntry.getEnvEntryType());
133         assertEquals("false", envEntry.getEnvEntryValue());
134
135     }
136
137     /**
138      * Checks that an xml file is invalid.
139      */

140     @Test
141     public void error01() {
142         // don't use here @ExpectedExceptions
143
try {
144             getEjb3("ejb-jar-error01.xml");
145             assert false;
146         } catch (ParsingException e) {
147             Throwable JavaDoc t = e.getCause();
148             assertTrue(t instanceof DocumentParserException);
149             DocumentParserException dpe = (DocumentParserException) t;
150             Throwable JavaDoc tt = dpe.getCause();
151             assertTrue(tt instanceof SAXException JavaDoc);
152             SAXException JavaDoc se = (SAXException JavaDoc) tt;
153             // Check error message
154
assertTrue(se.getMessage().contains("The content of element 'session' is not complete"));
155         }
156
157     }
158
159 }
160
Popular Tags