View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Tests {@link XmlDescriptorDetector}.
35   */
36  public class XmlDescriptorDetectorTest
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
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    // --- members --------------------------------------------------------------
53  
54    @Uut
55    private XmlDescriptorDetector uut;
56  
57    // ****************************** Inner Classes *****************************
58  
59    // ********************************* Methods ********************************
60  
61    // --- prepare --------------------------------------------------------------
62  
63    @Before
64    public void setUp()
65    {
66      uut = new XmlDescriptorDetector();
67    }
68  
69    // --- helper ---------------------------------------------------------------
70  
71    private String createTestXmlDocument(final boolean encoding,
72        final boolean nameSpace)
73    {
74      // NOTE that the target namespace is *not* evaluated!
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    // --- tests ----------------------------------------------------------------
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 }