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 java.io.BufferedInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.lang.String;
024:
025: import org.custommonkey.xmlunit.Diff;
026:
027: /**
028: * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
029: */
030: public class OpenejbJarTest extends TestCase {
031:
032: // public void test() throws Exception {
033: // WebServiceBindingType binding = new WebServiceBindingType();
034: // binding.setEjbName("FooBean");
035: // binding.setWebServiceAddress("/foo/bean");
036: // binding.getWebServiceVirtualHost().add("http://host");
037: // WebServiceBindingType.WebServiceSecurityType security = new WebServiceBindingType.WebServiceSecurityType();
038: // binding.setWebServiceSecurity(security);
039: // security.setAuthMethod(AuthMethodType.BASIC);
040: // security.setRealmName("foorealm");
041: // security.setSecurityRealmName("securityfoo");
042: // security.setTransportGuarantee(TransportGuaranteeType.NONE);
043: //
044: // String actual = JaxbOpenejbJar2.marshal(WebServiceBindingType.class, binding);
045: // assertEquals("", actual);
046: //
047: // }
048: public void testValidOpenejbJar() throws Exception {
049: unmarshalAndMarshal(OpenejbJarType.class,
050: "openejb-jar-2-full.xml");
051: }
052:
053: public void testInvalidOpenejbJar() throws Exception {
054: unmarshalAndMarshal(OpenejbJarType.class,
055: "openejb-jar-2-invalid.xml", "openejb-jar-2-full.xml");
056: }
057:
058: public void testGeronimoOpenejbXml() throws Exception {
059: unmarshalAndMarshal(GeronimoEjbJarType.class,
060: "geronimo-openejb-full.xml");
061: }
062:
063: public void testGeronimoOpenejbInvalidXml() throws Exception {
064: unmarshalAndMarshal(GeronimoEjbJarType.class,
065: "geronimo-openejb-invalid.xml",
066: "geronimo-openejb-corrected.xml");
067: }
068:
069: public void testOpenejbJarMoreInvalid() throws Exception {
070: unmarshalAndMarshal(OpenejbJarType.class,
071: "daytrader-original.xml", "daytrader-corrected.xml");
072: }
073:
074: private <T> void unmarshalAndMarshal(Class<T> type,
075: java.lang.String xmlFileName) throws Exception {
076: unmarshalAndMarshal(type, xmlFileName, xmlFileName);
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: String actual = JaxbOpenejbJar2.marshal(type, object);
087:
088: String expected;
089: if (xmlFileName.equals(expectedFile)) {
090: expected = readContent(getInputStream(xmlFileName));
091: } else {
092: expected = readContent(getInputStream(expectedFile));
093: }
094: Diff myDiff = new Diff(expected, actual);
095: assertTrue("Files are similar " + myDiff, myDiff.similar());
096: }
097:
098: private <T> InputStream getInputStream(String xmlFileName) {
099: return getClass().getClassLoader().getResourceAsStream(
100: xmlFileName);
101: }
102:
103: private String readContent(InputStream in) throws IOException {
104: StringBuffer sb = new StringBuffer();
105: in = new BufferedInputStream(in);
106: int i = in.read();
107: while (i != -1) {
108: sb.append((char) i);
109: i = in.read();
110: }
111: return sb.toString();
112: }
113: }
|