001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: TestLoading.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.deploymentdesc;
025:
026: import static org.ow2.easybeans.util.url.URLUtils.fileToURL2;
027: import static org.testng.Assert.assertEquals;
028: import static org.testng.Assert.assertNotNull;
029: import static org.testng.Assert.assertTrue;
030:
031: import java.io.File;
032: import java.net.URL;
033: import java.util.List;
034:
035: import org.ow2.easybeans.deployment.xml.parsing.EJB3DeploymentDescLoader;
036: import org.ow2.easybeans.deployment.xml.parsing.ParsingException;
037: import org.ow2.easybeans.deployment.xml.struct.EJB3;
038: import org.ow2.easybeans.deployment.xml.struct.EnterpriseBeans;
039: import org.ow2.easybeans.deployment.xml.struct.Session;
040: import org.ow2.easybeans.deployment.xml.struct.common.EnvEntry;
041: import org.ow2.easybeans.util.url.URLUtilsException;
042: import org.ow2.easybeans.util.xml.DocumentParserException;
043: import org.testng.annotations.Test;
044: import org.xml.sax.SAXException;
045:
046: /**
047: * Tests the parsing of an ejb-jar.xml file.
048: * @author Florent Benoit
049: */
050: public class TestLoading {
051:
052: /**
053: * Prefix for XML files.
054: */
055: private static final String PREFIX = TestLoading.class.getPackage()
056: .getName().replace(".", "/")
057: + "/xmls/";
058:
059: /**
060: * Prefix of src for tests.
061: */
062: private static final String TEST_DIR_PREFIX = "tests/src/main/java/";
063:
064: /**
065: * Gets an url for a given ejb-jar.xml filename.
066: * @param fileName the name of the fiel to search.
067: * @return an URL if found
068: * @throws ParsingException if no URL is found.
069: */
070: private static URL getURL(final String fileName)
071: throws ParsingException {
072: URL url = TestLoading.class.getResource(PREFIX + fileName);
073: if (url == null) {
074: // try with filesystem
075: File file = new File(TEST_DIR_PREFIX + PREFIX + fileName);
076: if (file.exists()) {
077: try {
078: return fileToURL2(file);
079: } catch (URLUtilsException e) {
080: throw new ParsingException(
081: "Cannot transform file into url", e);
082: }
083: }
084: }
085: if (url == null) {
086: throw new ParsingException("URl for '" + fileName
087: + "' was not found.");
088: }
089: return url;
090: }
091:
092: /**
093: * Parse a given filename and gets the resulting parsing.
094: * @param fileName the name of the file to parse
095: * @return a struct representing the EJB
096: * @throws ParsingException if parsing has failed.
097: */
098: private EJB3 getEjb3(final String fileName) throws ParsingException {
099: URL urlEjb01 = getURL(fileName);
100:
101: return EJB3DeploymentDescLoader
102: .loadDeploymentDescriptor(urlEjb01);
103:
104: }
105:
106: /**
107: * Checks that an xml file can be parsed by analyzing the result.
108: * @throws ParsingException if xml is not parsed.
109: */
110: @Test
111: public void analyzeParsedXml() throws ParsingException {
112: EJB3 ejb3 = getEjb3("ejb-jar01.xml");
113:
114: // Ensure that there is a bean named TestBean.
115: EnterpriseBeans enterpriseBeans = ejb3.getEnterpriseBeans();
116: assertNotNull(enterpriseBeans);
117:
118: // there are session beans
119: List<Session> sessionList = enterpriseBeans.getSessionList();
120: assertNotNull(sessionList);
121: assertTrue(sessionList.size() > 0);
122:
123: // first bean = TestBean
124: Session testBean = sessionList.get(0);
125: assertNotNull(testBean);
126: assertEquals("TestBean", testBean.getEjbName());
127:
128: // Env-Entry
129: List<EnvEntry> envEntries = testBean.getEnvEntryList();
130: // there are envEntries
131: assertNotNull(envEntries);
132: assertTrue(envEntries.size() > 0);
133:
134: EnvEntry envEntry = envEntries.get(0);
135: assertEquals("testBoolean", envEntry.getEnvEntryName());
136: assertEquals("java.lang.Boolean", envEntry.getEnvEntryType());
137: assertEquals("false", envEntry.getEnvEntryValue());
138:
139: }
140:
141: /**
142: * Checks that an xml file is invalid.
143: */
144: @Test
145: public void error01() {
146: // don't use here @ExpectedExceptions
147: try {
148: getEjb3("ejb-jar-error01.xml");
149: assert false;
150: } catch (ParsingException e) {
151: Throwable t = e.getCause();
152: assertTrue(t instanceof DocumentParserException);
153: DocumentParserException dpe = (DocumentParserException) t;
154: Throwable tt = dpe.getCause();
155: assertTrue(tt instanceof SAXException);
156: SAXException se = (SAXException) tt;
157: // Check error message
158: assertTrue(se.getMessage().contains(
159: "The content of element 'session' is not complete"));
160: }
161:
162: }
163:
164: }
|