001: /**
002: * EasyBeans
003: * Copyright (C) 2007 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: TestPersistenceUnitInfoLoader.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.persistence.xml;
025:
026: import java.net.URL;
027: import java.util.Properties;
028:
029: import javax.persistence.spi.PersistenceUnitTransactionType;
030:
031: import org.ow2.easybeans.persistence.xml.JPersistenceUnitInfo;
032: import org.ow2.easybeans.persistence.xml.JPersistenceUnitInfoLoader;
033: import org.testng.annotations.BeforeClass;
034: import org.testng.annotations.Test;
035: import static org.testng.Assert.assertEquals;
036: import static org.testng.Assert.assertNotNull;
037:
038: /**
039: * Check that the XML parsing/analyzing is working.
040: * @author Florent BENOIT
041: */
042: public class TestPersistenceUnitInfoLoader {
043:
044: /**
045: * Path to persistence XML file.
046: */
047: private static final String PERSISTENCE_XML_PATH = "org/ow2/easybeans/tests/persistence/xml/persistence1.xml";
048:
049: /**
050: * Instance of persistence unit info object.
051: */
052: private JPersistenceUnitInfo persistenceUnitInfo = null;
053:
054: /**
055: * Load persistence unit info object.
056: * @throws Exception if XML is not parsed.
057: */
058: @BeforeClass
059: public void loadPersistenceUnitInfo() throws Exception {
060: // get URL
061: URL persistenceXMl = this .getClass().getClassLoader()
062: .getResource(PERSISTENCE_XML_PATH);
063: if (persistenceXMl == null) {
064: throw new Exception("Unable to find '"
065: + PERSISTENCE_XML_PATH + "'.");
066: }
067:
068: // Load object
069: JPersistenceUnitInfo[] persistenceUnitInfos = JPersistenceUnitInfoLoader
070: .loadPersistenceUnitInfoImpl(persistenceXMl);
071: if (persistenceUnitInfos == null
072: || persistenceUnitInfos.length != 1) {
073: throw new Exception(
074: "Invalid size of persistence unit infos");
075: }
076:
077: // Keep object
078: persistenceUnitInfo = persistenceUnitInfos[0];
079:
080: }
081:
082: /**
083: * Test the persistence unit name.
084: */
085: @Test
086: public void testPersistenceUnitName() {
087: assertEquals(persistenceUnitInfo.getPersistenceUnitName(),
088: "my-persistence-unit");
089: }
090:
091: /**
092: * Test the persistence unit transaction type.
093: */
094: @Test
095: public void testPersistenceUnitTransactionType() {
096: assertEquals(persistenceUnitInfo.getTransactionType(),
097: PersistenceUnitTransactionType.JTA);
098: }
099:
100: /**
101: * Test the persistence provider.
102: */
103: @Test
104: public void testPersistenceProvider() {
105: assertEquals(persistenceUnitInfo
106: .getPersistenceProviderClassName(),
107: "my-persistence-provider");
108: }
109:
110: /**
111: * Test the persistence provider JTA datasource.
112: */
113: @Test
114: public void testPersistenceJTADataSource() {
115: assertEquals(persistenceUnitInfo.getJtaDataSourceName(),
116: "my-jta-datasource");
117: }
118:
119: /**
120: * Test the persistence provider no JTA datasource.
121: */
122: @Test
123: public void testPersistenceNoJTADataSource() {
124: assertEquals(persistenceUnitInfo.getNonJtaDataSourceName(),
125: "my-nojta-datasource");
126: }
127:
128: /**
129: * Test the persistence provider properties.
130: */
131: @Test
132: public void testPersistenceProperties() {
133: Properties p = persistenceUnitInfo.getProperties();
134: assertNotNull(p);
135: assertEquals(p.getProperty("myproperty"), "myvalue");
136: }
137:
138: }
|