01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.jee.oejb3;
18:
19: import junit.framework.TestCase;
20:
21: import javax.xml.bind.JAXBContext;
22: import javax.xml.bind.Unmarshaller;
23: import javax.xml.bind.Marshaller;
24: import javax.xml.bind.ValidationEventHandler;
25: import javax.xml.bind.ValidationEvent;
26:
27: import org.custommonkey.xmlunit.Diff;
28:
29: import java.io.InputStream;
30: import java.io.ByteArrayInputStream;
31: import java.io.ByteArrayOutputStream;
32: import java.io.IOException;
33: import java.io.BufferedInputStream;
34:
35: /**
36: * @version $Revision: 605052 $ $Date: 2007-12-17 16:24:24 -0800 $
37: */
38: public class OpenejbJarTest extends TestCase {
39:
40: public void testAll() throws Exception {
41: JAXBContext ctx = JAXBContext.newInstance(OpenejbJar.class);
42: Unmarshaller unmarshaller = ctx.createUnmarshaller();
43:
44: InputStream in = this .getClass().getClassLoader()
45: .getResourceAsStream("openejb-jar.xml");
46: String expected = readContent(in);
47:
48: unmarshaller.setEventHandler(new TestValidationEventHandler());
49: Object object = unmarshaller
50: .unmarshal(new ByteArrayInputStream(expected.getBytes()));
51:
52: assertTrue(object instanceof OpenejbJar);
53:
54: Marshaller marshaller = ctx.createMarshaller();
55: marshaller.setProperty("jaxb.formatted.output", true);
56:
57: ByteArrayOutputStream baos = new ByteArrayOutputStream();
58: marshaller.marshal(object, baos);
59:
60: String actual = new String(baos.toByteArray());
61:
62: Diff myDiff = new Diff(expected, actual);
63: assertTrue("Files are similar " + myDiff, myDiff.similar());
64: }
65:
66: private java.lang.String readContent(InputStream in)
67: throws IOException {
68: StringBuffer sb = new StringBuffer();
69: in = new BufferedInputStream(in);
70: int i = in.read();
71: while (i != -1) {
72: sb.append((char) i);
73: i = in.read();
74: }
75: java.lang.String content = sb.toString();
76: return content;
77: }
78:
79: private static class TestValidationEventHandler implements
80: ValidationEventHandler {
81: public boolean handleEvent(ValidationEvent validationEvent) {
82: System.out.println(validationEvent.getMessage());
83: return true;
84: }
85: }
86: }
|