Fix request handler class error
[appc.git] / appc-common / src / test / java / org / onap / appc / util / UnmodifiablePropertiesTest.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP : APPC\r
4  * ================================================================================\r
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.\r
6  * =============================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.appc.util;\r
22 \r
23 import org.junit.Assert;\r
24 import org.junit.Before;\r
25 import org.junit.Test;\r
26 import org.mockito.Mockito;\r
27 import org.hamcrest.CoreMatchers;\r
28 import java.io.ByteArrayOutputStream;\r
29 import java.io.IOException;\r
30 import java.io.PrintWriter;\r
31 import java.io.StringWriter;\r
32 import java.io.InputStream;\r
33 import java.io.OutputStream;\r
34 import java.io.PrintStream;\r
35 import java.io.StringReader;\r
36 import java.util.Enumeration;\r
37 import java.util.Properties;\r
38 \r
39 public class UnmodifiablePropertiesTest {\r
40 \r
41     private static final String propKey1 = "testKey1";\r
42     private static final String propKey2 = "testKey2";\r
43     private static final String propValue1 = "testValue1";\r
44     private static final String propValue2 = "testValue2";\r
45     private static final String noKey = "unusedKey";\r
46     private static final String noValue = "unusedValue";\r
47     private static final String propHeader = "test header";\r
48     private Properties properties = new Properties();\r
49 \r
50     private UnmodifiableProperties unmodifiableProperties = new UnmodifiableProperties(properties);\r
51     private String desiredMessage = "Property cannot be modified!";\r
52 \r
53     @Before\r
54     public void setUp() throws Exception {\r
55         properties.setProperty(propKey1, propValue1);\r
56         properties.setProperty(propKey2, propValue2);\r
57     }\r
58 \r
59     @Test(expected = UnsupportedOperationException.class)\r
60     public void testClear() {\r
61         try {\r
62             unmodifiableProperties.clear();\r
63         } catch (UnsupportedOperationException exceptionMessage) {\r
64             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
65             throw exceptionMessage;\r
66         }\r
67     }\r
68 \r
69     @Test(expected = UnsupportedOperationException.class)\r
70     public void testClone() {\r
71         try {\r
72             unmodifiableProperties.clone();\r
73         } catch (UnsupportedOperationException exceptionMessage) {\r
74             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
75             throw exceptionMessage;\r
76         }\r
77     }\r
78 \r
79     @Test\r
80     public final void testContainsObject() {\r
81         Assert.assertTrue(unmodifiableProperties.contains(propValue2));\r
82         Assert.assertFalse(unmodifiableProperties.contains(noValue));\r
83     }\r
84 \r
85     @Test\r
86     public final void testContainsKeyObject() {\r
87         Assert.assertTrue(unmodifiableProperties.containsKey(propKey1));\r
88         Assert.assertFalse(unmodifiableProperties.containsKey(noKey));\r
89     }\r
90 \r
91     @Test\r
92     public final void testContainsValueObject() {\r
93         Assert.assertTrue(unmodifiableProperties.containsValue(propValue1));\r
94         Assert.assertFalse(unmodifiableProperties.containsValue(noValue));\r
95     }\r
96 \r
97     @Test\r
98     public final void testElements() {\r
99         Enumeration<Object> propValues = unmodifiableProperties.elements();\r
100         Assert.assertEquals(propValue2, propValues.nextElement());\r
101         Assert.assertEquals(propValue1, propValues.nextElement());\r
102     }\r
103 \r
104     @Test\r
105     public final void testEntrySet() {\r
106         // Expect entrySet=[testKey2=testValue2, testKey1=testValue1].\r
107         Assert.assertEquals("Should match my properties K/V entries in setUp", properties.entrySet(),\r
108                 unmodifiableProperties.entrySet());\r
109     }\r
110 \r
111     @Test\r
112     public final void testEqualsObject() {\r
113         Assert.assertTrue(unmodifiableProperties.equals(properties));\r
114     }\r
115 \r
116     @Test\r
117     public final void testGetObject() {\r
118         Assert.assertEquals(propValue2, unmodifiableProperties.get(propKey2));\r
119     }\r
120 \r
121     @Test\r
122     public final void testGetPropertyString() {\r
123         Assert.assertEquals(propValue1, unmodifiableProperties.getProperty("testKey1"));\r
124     }\r
125 \r
126     @Test\r
127     public final void testGetPropertyStringString() {\r
128         Assert.assertEquals(propValue2, unmodifiableProperties.getProperty(propKey2, noValue));\r
129         Assert.assertEquals(propValue2, unmodifiableProperties.getProperty(noKey, propValue2));\r
130     }\r
131 \r
132     @Test\r
133     public final void testHashCode() {\r
134         Assert.assertEquals("Should match my properties.hashcode() int.", properties.hashCode(),\r
135                 unmodifiableProperties.hashCode());\r
136     }\r
137 \r
138     @Test\r
139     public final void testIsEmpty() {\r
140         Assert.assertFalse(unmodifiableProperties.isEmpty());\r
141     }\r
142 \r
143     @Test\r
144     public final void testKeys() {\r
145         Enumeration<Object> propKeys = unmodifiableProperties.keys();\r
146         Assert.assertEquals(propKey2, propKeys.nextElement());\r
147         Assert.assertEquals(propKey1, propKeys.nextElement());\r
148     }\r
149 \r
150     @Test\r
151     public final void testKeySet() {\r
152         // Expect keySet=[testKey2, testKey1].\r
153         Assert.assertEquals("Should match my properties key entries in SetUp", properties.keySet(),\r
154                 unmodifiableProperties.keySet());\r
155     }\r
156 \r
157     @Test\r
158     public final void testListPrintStream() {\r
159         ByteArrayOutputStream propByteArray = new ByteArrayOutputStream();\r
160         PrintStream listOut = new PrintStream(propByteArray);\r
161         unmodifiableProperties.list(listOut);\r
162         String propList = new String(propByteArray.toByteArray());\r
163         Assert.assertThat(propList, CoreMatchers.containsString("testKey2=testValue2"));\r
164         Assert.assertThat(propList, CoreMatchers.containsString("testKey1=testValue1"));\r
165     }\r
166 \r
167     @Test\r
168     public final void testListPrintWriter() {\r
169         StringWriter listOut = new StringWriter();\r
170         PrintWriter writer = new PrintWriter(listOut);\r
171         unmodifiableProperties.list(writer);\r
172         String propList = listOut.toString();\r
173         Assert.assertThat(propList, CoreMatchers.containsString("testKey2=testValue2"));\r
174         Assert.assertThat(propList, CoreMatchers.containsString("testKey1=testValue1"));\r
175     }\r
176 \r
177     @Test\r
178     public final void testLoadInputStream() throws IOException {\r
179         InputStream mockInStream = Mockito.mock(InputStream.class);\r
180         try {\r
181             unmodifiableProperties.load(mockInStream);\r
182         } catch (IOException ex) {\r
183         } catch (UnsupportedOperationException exceptionMessage) {\r
184             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
185         }\r
186     }\r
187 \r
188     @Test(expected = UnsupportedOperationException.class)\r
189     public final void testLoadReader() throws IOException {\r
190         String dummyPair = "key3=testKey3\nvalue3=testValue3";\r
191         StringReader reader = new StringReader(dummyPair);\r
192         try {\r
193             unmodifiableProperties.load(reader);\r
194         } catch (UnsupportedOperationException exceptionMessage) {\r
195             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
196             throw exceptionMessage;\r
197         }\r
198     }\r
199 \r
200     @Test\r
201     public final void testLoadFromXMLInputStream() throws IOException {\r
202         InputStream mockInStream = Mockito.mock(InputStream.class);\r
203         try {\r
204             unmodifiableProperties.loadFromXML(mockInStream);\r
205         } catch (IOException ex) {\r
206         } catch (UnsupportedOperationException exceptionMessage) {\r
207             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
208         }\r
209     }\r
210 \r
211     @Test\r
212     public final void testPropertyNames() {\r
213         Enumeration<?> propNames = unmodifiableProperties.propertyNames();\r
214         Assert.assertEquals(propKey2, propNames.nextElement());\r
215         Assert.assertEquals(propKey1, propNames.nextElement());\r
216     }\r
217 \r
218     @Test(expected = UnsupportedOperationException.class)\r
219     public final void testPutObjectObject() {\r
220         try {\r
221             unmodifiableProperties.put(propKey2, propValue1);\r
222         } catch (UnsupportedOperationException exceptionMessage) {\r
223             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
224             throw exceptionMessage;\r
225         }\r
226     }\r
227 \r
228     @Test(expected = UnsupportedOperationException.class)\r
229     public final void testPutAllMapOfQextendsObjectQextendsObject() {\r
230         try {\r
231             unmodifiableProperties.putAll(properties);\r
232         } catch (UnsupportedOperationException exceptionMessage) {\r
233             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
234             throw exceptionMessage;\r
235         }\r
236     }\r
237 \r
238     @Test(expected = UnsupportedOperationException.class)\r
239     public final void testRehash() {\r
240         try {\r
241             unmodifiableProperties.rehash();\r
242         } catch (UnsupportedOperationException exceptionMessage) {\r
243             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
244             throw exceptionMessage;\r
245         }\r
246     }\r
247 \r
248     @Test(expected = UnsupportedOperationException.class)\r
249     public final void testRemoveObject() {\r
250         try {\r
251             unmodifiableProperties.remove(propKey1);\r
252         } catch (UnsupportedOperationException exceptionMessage) {\r
253             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
254             throw exceptionMessage;\r
255         }\r
256     }\r
257 \r
258     @Test\r
259     public final void testSaveOutputStreamString() {\r
260         // Appl method is deprecated, but I still added this test since it is reachable.\r
261         OutputStream propByteArray = new ByteArrayOutputStream();\r
262         unmodifiableProperties.save(propByteArray, propHeader);\r
263         Assert.assertThat(propByteArray.toString(), CoreMatchers.startsWith("#test header"));\r
264         Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("testKey2=testValue2"));\r
265         Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("testKey1=testValue1"));\r
266     }\r
267 \r
268     @Test(expected = UnsupportedOperationException.class)\r
269     public final void testSetPropertyStringString() {\r
270         try {\r
271             unmodifiableProperties.setProperty(propKey1, propValue2);\r
272         } catch (UnsupportedOperationException exceptionMessage) {\r
273             Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
274             throw exceptionMessage;\r
275         }\r
276     }\r
277 \r
278     @Test\r
279     public final void testSize() {\r
280         Assert.assertEquals(2, unmodifiableProperties.size());\r
281     }\r
282 \r
283     @Test\r
284     public final void testStoreOutputStreamString() throws IOException {\r
285         OutputStream propByteArray = new ByteArrayOutputStream();\r
286         unmodifiableProperties.store(propByteArray, propHeader);\r
287         // adds comment header and streams/appends properties file into propByteArray\r
288         // expected = "#test header\n#<Date>\ntestKey2=testValue2\ntestKey1=testValue1"\r
289         Assert.assertThat(propByteArray.toString(), CoreMatchers.startsWith("#test header"));\r
290         Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("testKey2=testValue2"));\r
291         Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("testKey1=testValue1"));\r
292     }\r
293 \r
294     @Test\r
295     public final void testStoreWriterString() throws IOException {\r
296         StringWriter writer = new StringWriter();\r
297         unmodifiableProperties.store(writer, propHeader);\r
298         Assert.assertThat(writer.toString(), CoreMatchers.startsWith("#test header"));\r
299         Assert.assertThat(writer.toString(), CoreMatchers.containsString("testKey2=testValue2"));\r
300         Assert.assertThat(writer.toString(), CoreMatchers.containsString("testKey1=testValue1"));\r
301     }\r
302 \r
303     @Test\r
304     public final void testStoreToXMLOutputStreamString() throws IOException {\r
305         OutputStream propByteArray = new ByteArrayOutputStream();\r
306         unmodifiableProperties.storeToXML(propByteArray, propHeader);\r
307         // adds XML comment header and streams/appends XML properties file into propByteArray\r
308         Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("<comment>test header</comment>"));\r
309         Assert.assertThat(propByteArray.toString(),\r
310                 CoreMatchers.containsString("<entry key=\"testKey2\">testValue2</entry>"));\r
311         Assert.assertThat(propByteArray.toString(),\r
312                 CoreMatchers.containsString("<entry key=\"testKey1\">testValue1</entry>"));\r
313     }\r
314 \r
315     @Test\r
316     public final void testStoreToXMLOutputStreamStringString() throws IOException {\r
317         OutputStream propByteArray = new ByteArrayOutputStream();\r
318         unmodifiableProperties.storeToXML(propByteArray, propHeader, "UTF-8");\r
319         // adds XML comment header and streams/appends XML properties file into propByteArray\r
320         Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("<comment>test header</comment>"));\r
321         Assert.assertThat(propByteArray.toString(),\r
322                 CoreMatchers.containsString("<entry key=\"testKey2\">testValue2</entry>"));\r
323         Assert.assertThat(propByteArray.toString(),\r
324                 CoreMatchers.containsString("<entry key=\"testKey1\">testValue1</entry>"));\r
325     }\r
326 \r
327     @Test\r
328     public final void testStringPropertyNames() {\r
329         Assert.assertEquals(properties.stringPropertyNames(), unmodifiableProperties.stringPropertyNames());\r
330     }\r
331 \r
332     @Test\r
333     public final void testToString() {\r
334         // toString=[{testKey2=testValue2, testKey1=testValue1}]\r
335         Assert.assertEquals(properties.toString(), unmodifiableProperties.toString());\r
336     }\r
337 \r
338     @Test\r
339     public final void testValues() {\r
340         Assert.assertEquals(properties.values().toString(), unmodifiableProperties.values().toString());\r
341     }\r
342 }\r