001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.config;
017:
018: import java.io.BufferedInputStream;
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.util.HashMap;
024: import javax.xml.bind.JAXBContext;
025: import javax.xml.bind.JAXBException;
026: import javax.xml.bind.Marshaller;
027:
028: import junit.framework.TestCase;
029: import org.apache.openejb.jee.EjbJar;
030: import org.apache.openejb.jee.JaxbJavaee;
031: import org.apache.openejb.jee.oejb3.OpenejbJar;
032: import org.apache.openejb.jee.jpa.EntityMappings;
033: import org.custommonkey.xmlunit.Diff;
034:
035: /**
036: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
037: */
038: public class OpenEjb2ConversionTest extends TestCase {
039: public void testItests22() throws Exception {
040: convert("convert/oej2/cmp/itest-2.2/itest-2.2-");
041: }
042:
043: public void testDaytrader() throws Exception {
044: convert("convert/oej2/cmp/daytrader/daytrader-");
045: }
046:
047: public void testOneToOne() throws Exception {
048: convert("convert/oej2/cmp/onetoone/simplepk/");
049: }
050:
051: public void testOneToOneUni() throws Exception {
052: convert("convert/oej2/cmp/onetoone/simplepk/unidirectional-");
053: }
054:
055: public void testOneToMany() throws Exception {
056: convert("convert/oej2/cmp/onetomany/simplepk/");
057: }
058:
059: public void testOneToManyUni() throws Exception {
060: convert("convert/oej2/cmp/onetomany/simplepk/one-unidirectional-");
061: }
062:
063: public void testManyToOneUni() throws Exception {
064: convert("convert/oej2/cmp/onetomany/simplepk/many-unidirectional-");
065: }
066:
067: public void testManyToMany() throws Exception {
068: convert("convert/oej2/cmp/manytomany/simplepk/");
069: }
070:
071: public void testManyToManyUni() throws Exception {
072: convert("convert/oej2/cmp/manytomany/simplepk/unidirectional-");
073: }
074:
075: private EntityMappings convert(String prefix) throws Exception {
076: return convert(prefix + "ejb-jar.xml", prefix
077: + "openejb-jar.xml", prefix + "orm.xml");
078: }
079:
080: private EntityMappings convert(String ejbJarFileName,
081: String openejbJarFileName, String expectedFileName)
082: throws Exception {
083: InputStream in = getClass().getClassLoader()
084: .getResourceAsStream(ejbJarFileName);
085: EjbJar ejbJar = (EjbJar) JaxbJavaee.unmarshal(EjbJar.class,
086: new ByteArrayInputStream(readContent(in).getBytes()));
087:
088: // create and configure the module
089: EjbModule ejbModule = new EjbModule(
090: getClass().getClassLoader(), "TestModule",
091: ejbJarFileName, ejbJar, new OpenejbJar());
092: InitEjbDeployments initEjbDeployments = new InitEjbDeployments();
093: initEjbDeployments.deploy(ejbModule,
094: new HashMap<String, String>());
095: AppModule appModule = new AppModule(
096: getClass().getClassLoader(), "TestModule");
097: appModule.getEjbModules().add(ejbModule);
098:
099: // add the altDD
100: ejbModule.getAltDDs().put(
101: "openejb-jar.xml",
102: getClass().getClassLoader().getResource(
103: openejbJarFileName));
104:
105: // convert the cmp declarations into jpa entity declarations
106: CmpJpaConversion cmpJpaConversion = new CmpJpaConversion();
107: cmpJpaConversion.deploy(appModule);
108: // EntityMappings entityMappings = cmpJpaConversion.generateEntityMappings(ejbModule);
109:
110: // load the openejb-jar.xml file
111: // String openejbJarXml = readContent(getClass().getClassLoader().getResourceAsStream(openejbJarFileName));
112: // JAXBElement element = (JAXBElement) JaxbOpenejbJar2.unmarshal(OpenejbJarType.class, new ByteArrayInputStream(openejbJarXml.getBytes()));
113: // OpenejbJarType openejbJarType = (OpenejbJarType) element.getValue();
114:
115: // fill in the jpa entity declarations with database mappings from the openejb-jar.xml file
116: OpenEjb2Conversion openEjb2Conversion = new OpenEjb2Conversion();
117: openEjb2Conversion.deploy(appModule);
118: // openEjb2CmpConversion.mergeEntityMappings(entityMappings, openejbJarType);
119:
120: // compare the results to the expected results
121: if (expectedFileName != null) {
122: in = getClass().getClassLoader().getResourceAsStream(
123: expectedFileName);
124: String expected = readContent(in);
125: String actual = toString(appModule.getCmpMappings());
126: Diff myDiff = new Diff(expected, actual);
127: assertTrue("Files are similar " + myDiff, myDiff.similar());
128: }
129: return appModule.getCmpMappings();
130: }
131:
132: private String toString(EntityMappings entityMappings)
133: throws JAXBException {
134: JAXBContext entityMappingsContext = JAXBContext
135: .newInstance(EntityMappings.class);
136:
137: Marshaller marshaller = entityMappingsContext
138: .createMarshaller();
139: marshaller.setProperty("jaxb.formatted.output", true);
140:
141: ByteArrayOutputStream baos = new ByteArrayOutputStream();
142: marshaller.marshal(entityMappings, baos);
143:
144: String actual = new String(baos.toByteArray());
145: return actual.trim();
146: }
147:
148: private String readContent(InputStream in) throws IOException {
149: StringBuffer sb = new StringBuffer();
150: in = new BufferedInputStream(in);
151: int i = in.read();
152: while (i != -1) {
153: sb.append((char) i);
154: i = in.read();
155: }
156: return sb.toString().trim();
157: }
158:
159: }
|