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 java.util.Set;
025: import java.util.HashSet;
026: import java.util.Arrays;
027: import javax.xml.bind.JAXBContext;
028: import javax.xml.bind.JAXBException;
029: import javax.xml.bind.Marshaller;
030:
031: import junit.framework.TestCase;
032: import org.apache.openejb.jee.EjbJar;
033: import org.apache.openejb.jee.JaxbJavaee;
034: import org.apache.openejb.jee.oejb3.OpenejbJar;
035: import org.apache.openejb.jee.jpa.EntityMappings;
036: import org.apache.openejb.jee.jpa.Entity;
037: import org.apache.openejb.jee.jpa.GeneratedValue;
038: import org.apache.openejb.jee.jpa.GenerationType;
039: import org.custommonkey.xmlunit.Diff;
040:
041: /**
042: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
043: */
044: public class SunCmpConversionTest extends TestCase {
045: public void testItests22() throws Exception {
046: convert("convert/oej2/cmp/itest-2.2/itest-2.2-");
047: }
048:
049: // public void testDaytrader() throws Exception {
050: // convert("convert/oej2/cmp/daytrader/daytrader-");
051: // }
052: //
053: public void testOneToOne() throws Exception {
054: convert("convert/oej2/cmp/onetoone/simplepk/");
055: }
056:
057: public void testOneToOneUni() throws Exception {
058: convert("convert/oej2/cmp/onetoone/simplepk/unidirectional-");
059: }
060:
061: public void testOneToMany() throws Exception {
062: convert("convert/oej2/cmp/onetomany/simplepk/");
063: }
064:
065: public void testOneToManyUni() throws Exception {
066: convert("convert/oej2/cmp/onetomany/simplepk/one-unidirectional-");
067: }
068:
069: public void testManyToOneUni() throws Exception {
070: convert("convert/oej2/cmp/onetomany/simplepk/many-unidirectional-");
071: }
072:
073: public void testManyToMany() throws Exception {
074: convert("convert/oej2/cmp/manytomany/simplepk/");
075: }
076:
077: public void testManyToManyUni() throws Exception {
078: convert("convert/oej2/cmp/manytomany/simplepk/unidirectional-");
079: }
080:
081: private EntityMappings convert(String prefix) throws Exception {
082: return convert(prefix + "ejb-jar.xml", prefix
083: + "sun-ejb-jar.xml", prefix + "sun-cmp-mappings.xml",
084: prefix + "orm.xml");
085: }
086:
087: private EntityMappings convert(String ejbJarFileName,
088: String sunEjbJarFileName, String sunCmpMappingsFileName,
089: String expectedFileName) throws Exception {
090: InputStream in = getClass().getClassLoader()
091: .getResourceAsStream(ejbJarFileName);
092: EjbJar ejbJar = (EjbJar) JaxbJavaee.unmarshal(EjbJar.class,
093: new ByteArrayInputStream(readContent(in).getBytes()));
094:
095: // create and configure the module
096: EjbModule ejbModule = new EjbModule(
097: getClass().getClassLoader(), "TestModule",
098: ejbJarFileName, ejbJar, new OpenejbJar());
099: InitEjbDeployments initEjbDeployments = new InitEjbDeployments();
100: initEjbDeployments.deploy(ejbModule,
101: new HashMap<String, String>());
102: AppModule appModule = new AppModule(
103: getClass().getClassLoader(), "TestModule");
104: appModule.getEjbModules().add(ejbModule);
105:
106: // add the altDD
107: ejbModule.getAltDDs().put(
108: "sun-cmp-mappings.xml",
109: getClass().getClassLoader().getResource(
110: sunCmpMappingsFileName));
111: ejbModule.getAltDDs().put(
112: "sun-ejb-jar.xml",
113: getClass().getClassLoader().getResource(
114: sunEjbJarFileName));
115:
116: // convert the cmp declarations into jpa entity declarations
117: CmpJpaConversion cmpJpaConversion = new CmpJpaConversion();
118: cmpJpaConversion.deploy(appModule);
119: // EntityMappings entityMappings = cmpJpaConversion.generateEntityMappings(ejbModule);
120:
121: // // load the sun-cmp-mappings.xml file
122: // String sunCmpMappingsXml = readContent(getClass().getClassLoader().getResourceAsStream(sunCmpMappingsFileName));
123: // SunCmpMappings sunCmpMappings = (SunCmpMappings) JaxbSun.unmarshal(SunCmpMappings.class, new ByteArrayInputStream(sunCmpMappingsXml.getBytes()));
124:
125: // fill in the jpa entity declarations with database mappings from the sun-cmp-mappings.xml file
126: SunConversion sunConversion = new SunConversion();
127: // sunCmpConversion.mergeEntityMappings(ejbModule, entityMappings);
128: sunConversion.deploy(appModule);
129:
130: // compare the results to the expected results
131: if (expectedFileName != null) {
132: in = getClass().getClassLoader().getResourceAsStream(
133: expectedFileName);
134: String expected = readContent(in);
135:
136: // Sun doen't really support generated primary keys, so we need to add them by hand here
137: Set<String> generatedPks = new HashSet<String>(Arrays
138: .asList("BasicCmp2", "AOBasicCmp2", "EncCmp2",
139: "Cmp2RmiIiop"));
140: EntityMappings cmpMappings = appModule.getCmpMappings();
141: for (Entity entity : cmpMappings.getEntity()) {
142: if (generatedPks.contains(entity.getName())) {
143: entity.getAttributes().getId().get(0)
144: .setGeneratedValue(
145: new GeneratedValue(
146: GenerationType.IDENTITY));
147: }
148: }
149: String actual = toString(cmpMappings);
150: Diff myDiff = new Diff(expected, actual);
151: assertTrue("Files are similar " + myDiff, myDiff.similar());
152: }
153:
154: return appModule.getCmpMappings();
155: }
156:
157: private String toString(EntityMappings entityMappings)
158: throws JAXBException {
159: JAXBContext entityMappingsContext = JAXBContext
160: .newInstance(EntityMappings.class);
161:
162: Marshaller marshaller = entityMappingsContext
163: .createMarshaller();
164: marshaller.setProperty("jaxb.formatted.output", true);
165:
166: ByteArrayOutputStream baos = new ByteArrayOutputStream();
167: marshaller.marshal(entityMappings, baos);
168:
169: String actual = new String(baos.toByteArray());
170: return actual.trim();
171: }
172:
173: private String readContent(InputStream in) throws IOException {
174: StringBuffer sb = new StringBuffer();
175: in = new BufferedInputStream(in);
176: int i = in.read();
177: while (i != -1) {
178: sb.append((char) i);
179: i = in.read();
180: }
181: return sb.toString().trim();
182: }
183: }
|