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 org.junit.Test;
22  
23  import de.smartics.testdoc.annotations.TestDocHint;
24  import de.smartics.testdoc.annotations.Uut;
25  
26  /**
27   * Tests {@link XmlDescriptor}.
28   */
29  @Uut(type = XmlDescriptor.class)
30  public class XmlDescriptorTest
31  { // NOPMD
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    private static final String XML_ENCODING_TEXT = " encoded with ";
37  
38    private static final String XML_VERSION_TEXT = " XML version ";
39  
40    private static final String PUBLIC_ID =
41        "http://www.smartics.de/schema/example";
42  
43    private static final String SYSTEM_ID = "/temp/schema.xml";
44  
45    private static final String VERSION = "1.0";
46  
47    private static final String ENCODING = "UTF-8";
48  
49    // --- members --------------------------------------------------------------
50  
51    // ****************************** Inner Classes *****************************
52  
53    // ********************************* Methods ********************************
54  
55    // --- prepare --------------------------------------------------------------
56  
57    // --- helper ---------------------------------------------------------------
58  
59    // --- tests ----------------------------------------------------------------
60  
61    @Test
62    public void allowsNoInformation()
63    {
64      final XmlDescriptor uut = new XmlDescriptor.Builder().build();
65  
66      final String string = uut.toString();
67  
68      assertEquals("No information provided.", string);
69    }
70  
71    @Test
72    public void allowsOnlyPublicIdToBeSet()
73    {
74      final XmlDescriptor uut =
75          new XmlDescriptor.Builder().withPublicId(PUBLIC_ID).build();
76  
77      assertEquals(PUBLIC_ID, uut.getPublicId());
78      assertNull(uut.getSystemId());
79      assertNull(uut.getVersion());
80      assertNull(uut.getEncoding());
81  
82      final String string = uut.toString();
83  
84      final String expected = PUBLIC_ID + '.';
85      assertEquals(expected, string);
86    }
87  
88    @Test
89    public void allowsOnlySystemIdToBeSet()
90    {
91      final XmlDescriptor uut =
92          new XmlDescriptor.Builder().withSystemId(SYSTEM_ID).build();
93  
94      assertNull(uut.getPublicId());
95      assertEquals(SYSTEM_ID, uut.getSystemId());
96      assertNull(uut.getVersion());
97      assertNull(uut.getEncoding());
98  
99      final String string = uut.toString();
100 
101     final String expected = SYSTEM_ID + '.';
102     assertEquals(expected, string);
103   }
104 
105   @Test
106   public void allowsOnlyVersionToBeSet()
107   {
108     final XmlDescriptor uut =
109         new XmlDescriptor.Builder().withVersion(VERSION).build();
110 
111     assertNull(uut.getPublicId());
112     assertNull(uut.getSystemId());
113     assertEquals(VERSION, uut.getVersion());
114     assertNull(uut.getEncoding());
115 
116     final String string = uut.toString();
117 
118     final String expected = "XML version " + VERSION + '.';
119     assertEquals(expected, string);
120   }
121 
122   @Test
123   public void allowsOnlyEncodingToBeSet()
124   {
125     final XmlDescriptor uut =
126         new XmlDescriptor.Builder().withEncoding(ENCODING).build();
127 
128     assertNull(uut.getPublicId());
129     assertNull(uut.getSystemId());
130     assertNull(uut.getVersion());
131     assertEquals(ENCODING, uut.getEncoding());
132 
133     final String string = uut.toString();
134 
135     final String expected = "XML" + XML_ENCODING_TEXT + ENCODING + '.';
136     assertEquals(expected, string);
137   }
138 
139   @Test
140   public void allowsOnlyPublicAndSystemIdToBeSet()
141   {
142     final XmlDescriptor uut =
143         new XmlDescriptor.Builder().withPublicId(PUBLIC_ID)
144             .withSystemId(SYSTEM_ID).build();
145 
146     assertEquals(PUBLIC_ID, uut.getPublicId());
147     assertEquals(SYSTEM_ID, uut.getSystemId());
148     assertNull(uut.getVersion());
149     assertNull(uut.getEncoding());
150 
151     final String string = uut.toString();
152 
153     final String expected = PUBLIC_ID + '/' + SYSTEM_ID + '.';
154     assertEquals(expected, string);
155   }
156 
157   @Test
158   public void allowsOnlyPublicAndSystemIdAndEncodingToBeSet()
159   {
160     final XmlDescriptor uut =
161         new XmlDescriptor.Builder().withPublicId(PUBLIC_ID)
162             .withSystemId(SYSTEM_ID).withEncoding(ENCODING).build();
163 
164     assertEquals(PUBLIC_ID, uut.getPublicId());
165     assertEquals(SYSTEM_ID, uut.getSystemId());
166     assertNull(uut.getVersion());
167     assertEquals(ENCODING, uut.getEncoding());
168 
169     final String string = uut.toString();
170 
171     final String expected =
172         PUBLIC_ID + '/' + SYSTEM_ID + " XML" + XML_ENCODING_TEXT + ENCODING
173             + '.';
174     assertEquals(expected, string);
175   }
176 
177   @Test
178   public void allowsOnlyPublicAndSystemIdAndVersionToBeSet()
179   {
180     final XmlDescriptor uut =
181         new XmlDescriptor.Builder().withPublicId(PUBLIC_ID)
182             .withSystemId(SYSTEM_ID).withVersion(VERSION).build();
183 
184     assertEquals(PUBLIC_ID, uut.getPublicId());
185     assertEquals(SYSTEM_ID, uut.getSystemId());
186     assertEquals(VERSION, uut.getVersion());
187     assertNull(uut.getEncoding());
188 
189     final String string = uut.toString();
190 
191     final String expected =
192         PUBLIC_ID + '/' + SYSTEM_ID + XML_VERSION_TEXT + VERSION + '.';
193     assertEquals(expected, string);
194   }
195 
196   @Test
197   public void allowsOnlySystemIdAndVersionToBeSet()
198   {
199     final XmlDescriptor uut =
200         new XmlDescriptor.Builder().withSystemId(SYSTEM_ID)
201             .withVersion(VERSION).build();
202 
203     assertNull(uut.getPublicId());
204     assertEquals(SYSTEM_ID, uut.getSystemId());
205     assertEquals(VERSION, uut.getVersion());
206     assertNull(uut.getEncoding());
207 
208     final String string = uut.toString();
209 
210     final String expected = SYSTEM_ID + XML_VERSION_TEXT + VERSION + '.';
211     assertEquals(expected, string);
212   }
213 
214   @Test
215   public void allowsOnlyPublicIdAndVersionToBeSet()
216   {
217     final XmlDescriptor uut =
218         new XmlDescriptor.Builder().withPublicId(PUBLIC_ID)
219             .withVersion(VERSION).build();
220 
221     assertEquals(PUBLIC_ID, uut.getPublicId());
222     assertNull(uut.getSystemId());
223     assertEquals(VERSION, uut.getVersion());
224     assertNull(uut.getEncoding());
225 
226     final String string = uut.toString();
227 
228     final String expected = PUBLIC_ID + XML_VERSION_TEXT + VERSION + '.';
229     assertEquals(expected, string);
230   }
231 
232   @Test
233   public void allowsOnlyPublicIdAndEncodingToBeSet()
234   {
235     final XmlDescriptor uut =
236         new XmlDescriptor.Builder().withPublicId(PUBLIC_ID)
237             .withEncoding(ENCODING).build();
238 
239     assertEquals(PUBLIC_ID, uut.getPublicId());
240     assertNull(uut.getSystemId());
241     assertNull(uut.getVersion());
242     assertEquals(ENCODING, uut.getEncoding());
243 
244     final String string = uut.toString();
245 
246     final String expected =
247         PUBLIC_ID + " XML" + XML_ENCODING_TEXT + ENCODING + '.';
248     assertEquals(expected, string);
249   }
250 
251   @Test
252   @TestDocHint(sentence = "Allows public Id, system Id, version, and encoding to be set.")
253   public void allowsAllToBeSet()
254   {
255     final XmlDescriptor uut =
256         new XmlDescriptor.Builder().withPublicId(PUBLIC_ID)
257             .withSystemId(SYSTEM_ID).withVersion(VERSION)
258             .withEncoding(ENCODING).build();
259 
260     assertEquals(PUBLIC_ID, uut.getPublicId());
261     assertEquals(SYSTEM_ID, uut.getSystemId());
262     assertEquals(VERSION, uut.getVersion());
263     assertEquals(ENCODING, uut.getEncoding());
264 
265     final String string = uut.toString();
266 
267     final String expected =
268         PUBLIC_ID + '/' + SYSTEM_ID + XML_VERSION_TEXT + VERSION
269             + XML_ENCODING_TEXT + ENCODING + '.';
270     assertEquals(expected, string);
271   }
272 }