01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with 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,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.lib.xml;
20:
21: import java.io.InputStreamReader;
22: import java.io.StringWriter;
23: import javax.xml.transform.Transformer;
24: import javax.xml.transform.TransformerFactory;
25: import javax.xml.transform.stream.StreamResult;
26: import javax.xml.transform.stream.StreamSource;
27:
28: import junit.framework.Test;
29: import junit.framework.TestCase;
30: import junit.framework.TestSuite;
31: import junit.textui.TestRunner;
32:
33: /**
34: * Tests the {@link XMLWriter} by comparing the results of passing the
35: * raw-source.xml file in this package through the writer to the correct
36: * output in the formatted-result.xml file, also in this package.
37: *
38: * @author Abe White
39: */
40: public class TestXMLWriter extends TestCase {
41:
42: public TestXMLWriter(String test) {
43: super (test);
44: }
45:
46: /**
47: * Tests the formatting works.
48: */
49: public void testPrettyPrint() throws Exception {
50: // get the raw xml from a file
51: StreamSource source = new StreamSource(getClass()
52: .getResourceAsStream("raw-source.xml"));
53:
54: // and write the formatted result to a string
55: StringWriter formatted = new StringWriter();
56: StreamResult result = new StreamResult(new XMLWriter(formatted));
57:
58: Transformer trans = TransformerFactory.newInstance()
59: .newTransformer();
60: trans.transform(source, result);
61:
62: // read the correct output into a buffer
63: StringBuffer correct = new StringBuffer();
64: InputStreamReader reader = new InputStreamReader(getClass()
65: .getResourceAsStream("formatted-result.xml"));
66: for (int c; (c = reader.read()) != -1; correct.append((char) c))
67: ;
68:
69: // assertTrue the formatted result and buffer are equal
70: assertEquals(fixNewline(correct.toString().trim()),
71: fixNewline(formatted.toString().trim()));
72: }
73:
74: private String fixNewline(String str) {
75: return serp.util.Strings.join(serp.util.Strings.split(str,
76: "\r\n", -1), "\n");
77: }
78:
79: public static Test suite() {
80: return new TestSuite(TestXMLWriter.class);
81: }
82:
83: public static void main(String[] args) {
84: TestRunner.run(suite());
85: }
86: }
|