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.jee.oejb2;
017:
018: import junit.framework.TestCase;
019:
020: import javax.xml.bind.JAXBElement;
021: import javax.xml.namespace.QName;
022:
023: import org.custommonkey.xmlunit.Diff;
024:
025: import java.io.BufferedInputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028:
029: /**
030: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
031: */
032: public class ConversionTest extends TestCase {
033:
034: public void testConversion() throws Exception {
035: JAXBElement element = (JAXBElement) JaxbOpenejbJar2.unmarshal(
036: OpenejbJarType.class,
037: getInputStream("openejb-jar-2-full.xml"));
038: OpenejbJarType o2 = (OpenejbJarType) element.getValue();
039:
040: GeronimoEjbJarType g2 = new GeronimoEjbJarType();
041:
042: g2.setEnvironment(o2.getEnvironment());
043: g2.setSecurity(o2.getSecurity());
044: g2.getService().addAll(o2.getService());
045: g2.getMessageDestination().addAll(o2.getMessageDestination());
046:
047: for (EnterpriseBean bean : o2.getEnterpriseBeans()) {
048: g2.getAbstractNamingEntry().addAll(
049: bean.getAbstractNamingEntry());
050: g2.getEjbLocalRef().addAll(bean.getEjbLocalRef());
051: g2.getEjbRef().addAll(bean.getEjbRef());
052: g2.getResourceEnvRef().addAll(bean.getResourceEnvRef());
053: g2.getResourceRef().addAll(bean.getResourceRef());
054: g2.getServiceRef().addAll(bean.getServiceRef());
055:
056: if (bean instanceof RpcBean) {
057: RpcBean rpcBean = (RpcBean) bean;
058: if (rpcBean.getTssLink() != null) {
059: g2.getTssLink().add(
060: new TssLinkType(rpcBean.getEjbName(),
061: rpcBean.getTssLink(), rpcBean
062: .getJndiName()));
063: }
064: }
065: }
066:
067: JAXBElement root = new JAXBElement(
068: new QName(
069: "http://geronimo.apache.org/xml/ns/j2ee/ejb/openejb-2.0",
070: "ejb-jar"), GeronimoEjbJarType.class, g2);
071: String result = JaxbOpenejbJar2.marshal(
072: GeronimoEjbJarType.class, root);
073: String expected = readContent(getInputStream("geronimo-openejb-converted.xml"));
074: Diff myDiff = new Diff(expected, result);
075: assertTrue("Files are similar " + myDiff, myDiff.similar());
076:
077: }
078:
079: private <T> void unmarshalAndMarshal(Class<T> type,
080: java.lang.String xmlFileName, java.lang.String expectedFile)
081: throws Exception {
082:
083: Object object = JaxbOpenejbJar2.unmarshal(type,
084: getInputStream(xmlFileName));
085:
086: java.lang.String actual = JaxbOpenejbJar2.marshal(type, object);
087:
088: if (xmlFileName.equals(expectedFile)) {
089: String sourceXml = readContent(getInputStream(xmlFileName));
090: assertEquals(sourceXml, actual);
091: } else {
092: String expected = readContent(getInputStream(expectedFile));
093: assertEquals(expected, actual);
094: }
095: }
096:
097: private <T> InputStream getInputStream(String xmlFileName) {
098: return getClass().getClassLoader().getResourceAsStream(
099: xmlFileName);
100: }
101:
102: private String readContent(InputStream in) throws IOException {
103: StringBuffer sb = new StringBuffer();
104: in = new BufferedInputStream(in);
105: int i = in.read();
106: while (i != -1) {
107: sb.append((char) i);
108: i = in.read();
109: }
110: return sb.toString();
111: }
112:
113: }
|