2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.onap.appc.util;
\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
39 public class UnmodifiablePropertiesTest {
\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
50 private UnmodifiableProperties unmodifiableProperties = new UnmodifiableProperties(properties);
\r
51 private String desiredMessage = "Property cannot be modified!";
\r
54 public void setUp() throws Exception {
\r
55 properties.setProperty(propKey1, propValue1);
\r
56 properties.setProperty(propKey2, propValue2);
\r
59 @Test(expected = UnsupportedOperationException.class)
\r
60 public void testClear() {
\r
62 unmodifiableProperties.clear();
\r
63 } catch (UnsupportedOperationException exceptionMessage) {
\r
64 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\r
65 throw exceptionMessage;
\r
69 @Test(expected = UnsupportedOperationException.class)
\r
70 public void testClone() {
\r
72 unmodifiableProperties.clone();
\r
73 } catch (UnsupportedOperationException exceptionMessage) {
\r
74 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\r
75 throw exceptionMessage;
\r
80 public final void testContainsObject() {
\r
81 Assert.assertTrue(unmodifiableProperties.contains(propValue2));
\r
82 Assert.assertFalse(unmodifiableProperties.contains(noValue));
\r
86 public final void testContainsKeyObject() {
\r
87 Assert.assertTrue(unmodifiableProperties.containsKey(propKey1));
\r
88 Assert.assertFalse(unmodifiableProperties.containsKey(noKey));
\r
92 public final void testContainsValueObject() {
\r
93 Assert.assertTrue(unmodifiableProperties.containsValue(propValue1));
\r
94 Assert.assertFalse(unmodifiableProperties.containsValue(noValue));
\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
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
112 public final void testEqualsObject() {
\r
113 Assert.assertTrue(unmodifiableProperties.equals(properties));
\r
117 public final void testGetObject() {
\r
118 Assert.assertEquals(propValue2, unmodifiableProperties.get(propKey2));
\r
122 public final void testGetPropertyString() {
\r
123 Assert.assertEquals(propValue1, unmodifiableProperties.getProperty("testKey1"));
\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
133 public final void testHashCode() {
\r
134 Assert.assertEquals("Should match my properties.hashcode() int.", properties.hashCode(),
\r
135 unmodifiableProperties.hashCode());
\r
139 public final void testIsEmpty() {
\r
140 Assert.assertFalse(unmodifiableProperties.isEmpty());
\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
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
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
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
178 public final void testLoadInputStream() throws IOException {
\r
179 InputStream mockInStream = Mockito.mock(InputStream.class);
\r
181 unmodifiableProperties.load(mockInStream);
\r
182 } catch (IOException ex) {
\r
183 } catch (UnsupportedOperationException exceptionMessage) {
\r
184 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\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
193 unmodifiableProperties.load(reader);
\r
194 } catch (UnsupportedOperationException exceptionMessage) {
\r
195 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\r
196 throw exceptionMessage;
\r
201 public final void testLoadFromXMLInputStream() throws IOException {
\r
202 InputStream mockInStream = Mockito.mock(InputStream.class);
\r
204 unmodifiableProperties.loadFromXML(mockInStream);
\r
205 } catch (IOException ex) {
\r
206 } catch (UnsupportedOperationException exceptionMessage) {
\r
207 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\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
218 @Test(expected = UnsupportedOperationException.class)
\r
219 public final void testPutObjectObject() {
\r
221 unmodifiableProperties.put(propKey2, propValue1);
\r
222 } catch (UnsupportedOperationException exceptionMessage) {
\r
223 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\r
224 throw exceptionMessage;
\r
228 @Test(expected = UnsupportedOperationException.class)
\r
229 public final void testPutAllMapOfQextendsObjectQextendsObject() {
\r
231 unmodifiableProperties.putAll(properties);
\r
232 } catch (UnsupportedOperationException exceptionMessage) {
\r
233 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\r
234 throw exceptionMessage;
\r
238 @Test(expected = UnsupportedOperationException.class)
\r
239 public final void testRehash() {
\r
241 unmodifiableProperties.rehash();
\r
242 } catch (UnsupportedOperationException exceptionMessage) {
\r
243 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\r
244 throw exceptionMessage;
\r
248 @Test(expected = UnsupportedOperationException.class)
\r
249 public final void testRemoveObject() {
\r
251 unmodifiableProperties.remove(propKey1);
\r
252 } catch (UnsupportedOperationException exceptionMessage) {
\r
253 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\r
254 throw exceptionMessage;
\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
268 @Test(expected = UnsupportedOperationException.class)
\r
269 public final void testSetPropertyStringString() {
\r
271 unmodifiableProperties.setProperty(propKey1, propValue2);
\r
272 } catch (UnsupportedOperationException exceptionMessage) {
\r
273 Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
\r
274 throw exceptionMessage;
\r
279 public final void testSize() {
\r
280 Assert.assertEquals(2, unmodifiableProperties.size());
\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
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
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
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
328 public final void testStringPropertyNames() {
\r
329 Assert.assertEquals(properties.stringPropertyNames(), unmodifiableProperties.stringPropertyNames());
\r
333 public final void testToString() {
\r
334 // toString=[{testKey2=testValue2, testKey1=testValue1}]
\r
335 Assert.assertEquals(properties.toString(), unmodifiableProperties.toString());
\r
339 public final void testValues() {
\r
340 Assert.assertEquals(properties.values().toString(), unmodifiableProperties.values().toString());
\r