01: /*
02: * Copyright (C) 2007 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 13. September 2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.core.util;
12:
13: import java.io.ByteArrayInputStream;
14: import java.io.IOException;
15: import java.io.InputStreamReader;
16: import java.io.LineNumberReader;
17:
18: import junit.framework.TestCase;
19:
20: /**
21: * @author Jörg Schaible
22: */
23: public class XmlHeaderAwareReaderTest extends TestCase {
24:
25: public void testKeepsAllBytesInStream() throws IOException {
26: ByteArrayInputStream in = new ByteArrayInputStream(
27: "\n<!-- no header --><html/>".getBytes("us-ascii"));
28: LineNumberReader reader = new LineNumberReader(
29: new XmlHeaderAwareReader(in));
30: assertEquals("", reader.readLine());
31: assertEquals("<!-- no header --><html/>", reader.readLine());
32: }
33:
34: public void testDefaultValues() throws IOException {
35: ByteArrayInputStream in = new ByteArrayInputStream("\n<?xml ?>"
36: .getBytes("us-ascii"));
37: XmlHeaderAwareReader reader = new XmlHeaderAwareReader(in);
38: assertEquals(1.0, reader.getVersion(), 0.001);
39: assertEquals(new InputStreamReader(in, "utf-8").getEncoding(),
40: reader.getEncoding());
41: }
42:
43: public void testEvaluatesVersion() throws IOException {
44: ByteArrayInputStream in = new ByteArrayInputStream(
45: "\n<?xml foo=\"bar\" version='1.1' ?>"
46: .getBytes("us-ascii"));
47: XmlHeaderAwareReader reader = new XmlHeaderAwareReader(in);
48: assertEquals(1.1, reader.getVersion(), 0.001);
49: }
50:
51: public void testEvaluatesEncoding() throws IOException {
52: ByteArrayInputStream in = new ByteArrayInputStream(
53: "<?xml encoding=\"iso-8859-15\" ?>"
54: .getBytes("us-ascii"));
55: XmlHeaderAwareReader reader = new XmlHeaderAwareReader(in);
56: assertEquals(new InputStreamReader(in, "iso-8859-15")
57: .getEncoding(), reader.getEncoding());
58: }
59:
60: public void testValueEscaping() throws IOException {
61: ByteArrayInputStream in = new ByteArrayInputStream(
62: "<?xml version='1.\\1' ?>".getBytes("us-ascii"));
63: XmlHeaderAwareReader reader = new XmlHeaderAwareReader(in);
64: assertEquals(1.1, reader.getVersion(), 0.001);
65: }
66: }
|