1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.xml.encoding;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNull;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.InputStream;
23 import java.io.UnsupportedEncodingException;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.experimental.categories.Category;
28 import org.xml.sax.InputSource;
29
30 import de.smartics.testdoc.annotations.Uut;
31 import de.smartics.testdoc.categories.Technical;
32
33
34
35
36 public class XmlDescriptorDetectorTest
37 {
38
39
40
41
42 private static final String ENCODING = "UTF-16";
43
44 private static final String VERSION = "1.0";
45
46 private static final String TNS = "http://example.com/test/1";
47
48 private static final String PUBLIC_ID = "http://example.com/test";
49
50 private static final String SYSTEM_ID = "file:///tmp/test.xml";
51
52
53
54 @Uut
55 private XmlDescriptorDetector uut;
56
57
58
59
60
61
62
63 @Before
64 public void setUp()
65 {
66 uut = new XmlDescriptorDetector();
67 }
68
69
70
71 private String createTestXmlDocument(final boolean encoding,
72 final boolean nameSpace)
73 {
74
75 return "<?xml version='" + VERSION + "'"
76 + (encoding ? " encoding='" + ENCODING + "'" : "") + "?>\n<root"
77 + (nameSpace ? " targetNamespace='" + TNS + "'" : "") + "/>\n";
78 }
79
80 private InputStream createTestXmlDocumentInputStream(final boolean encoding,
81 final boolean nameSpace) throws UnsupportedEncodingException
82 {
83 final String xmlDocument = createTestXmlDocument(encoding, nameSpace);
84 final InputStream xmlInput =
85 new ByteArrayInputStream(xmlDocument.getBytes(ENCODING));
86 return xmlInput;
87 }
88
89
90
91 @Test
92 public void supportsReadingFromInputStreamButWithoutIdDetection()
93 throws Exception
94 {
95 final InputStream input = createTestXmlDocumentInputStream(true, true);
96 final XmlDescriptor descriptor = uut.readDescriptor(input);
97
98 assertEquals(VERSION, descriptor.getVersion());
99 assertEquals(ENCODING, descriptor.getEncoding());
100 assertNull(descriptor.getPublicId());
101 assertNull(descriptor.getSystemId());
102 }
103
104 @Test
105 @Category(Technical.class)
106 public void theEncodingInformationIsNotRequired() throws Exception
107 {
108 final InputStream stream = createTestXmlDocumentInputStream(false, true);
109 final XmlDescriptor descriptor = uut.readDescriptor(stream);
110
111 assertEquals(VERSION, descriptor.getVersion());
112 assertEquals(ENCODING, descriptor.getEncoding());
113 }
114
115 @Test
116 public void supportsReadingFromInputSource() throws Exception
117 {
118 final InputStream stream = createTestXmlDocumentInputStream(true, true);
119
120 final InputSource source = new InputSource(stream);
121 source.setSystemId(SYSTEM_ID);
122 source.setPublicId(PUBLIC_ID);
123
124 final XmlDescriptor descriptor = uut.readDescriptor(source);
125
126 assertEquals(VERSION, descriptor.getVersion());
127 assertEquals(ENCODING, descriptor.getEncoding());
128 assertEquals(PUBLIC_ID, descriptor.getPublicId());
129 assertEquals(SYSTEM_ID, descriptor.getSystemId());
130 }
131 }