01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.jee.jpa;
17:
18: import junit.framework.TestCase;
19:
20: import java.io.InputStream;
21: import java.io.IOException;
22: import java.io.BufferedInputStream;
23:
24: import org.custommonkey.xmlunit.Diff;
25:
26: /**
27: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
28: */
29: public class JaxbTest extends TestCase {
30:
31: public void testEntityMappings() throws Exception {
32: unmarshalAndMarshal(EntityMappings.class,
33: "jpa-mapping-full.xml");
34: }
35:
36: private <T> void unmarshalAndMarshal(Class<T> type,
37: java.lang.String xmlFileName) throws Exception {
38: unmarshalAndMarshal(type, xmlFileName, xmlFileName);
39: }
40:
41: private <T> void unmarshalAndMarshal(Class<T> type,
42: java.lang.String xmlFileName, java.lang.String expectedFile)
43: throws Exception {
44:
45: Object object = JpaJaxbUtil.unmarshal(type,
46: getInputStream(xmlFileName));
47:
48: String actual = JpaJaxbUtil.marshal(type, object);
49:
50: String expected;
51: if (xmlFileName.equals(expectedFile)) {
52: expected = readContent(getInputStream(xmlFileName));
53: } else {
54: expected = readContent(getInputStream(expectedFile));
55: }
56: Diff myDiff = new Diff(expected, actual);
57: assertTrue("Files are similar " + myDiff, myDiff.similar());
58: }
59:
60: private <T> InputStream getInputStream(String xmlFileName) {
61: return getClass().getClassLoader().getResourceAsStream(
62: xmlFileName);
63: }
64:
65: private String readContent(InputStream in) throws IOException {
66: StringBuffer sb = new StringBuffer();
67: in = new BufferedInputStream(in);
68: int i = in.read();
69: while (i != -1) {
70: sb.append((char) i);
71: i = in.read();
72: }
73: return sb.toString();
74: }
75:
76: }
|