import org.junit.Assert;\r
import org.junit.Before;\r
import org.junit.Test;\r
-\r
+import org.mockito.Mockito;\r
+import org.hamcrest.CoreMatchers;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+import java.io.PrintWriter;\r
+import java.io.StringWriter;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.io.PrintStream;\r
+import java.io.StringReader;\r
+import java.util.Enumeration;\r
import java.util.Properties;\r
\r
public class UnmodifiablePropertiesTest {\r
private static final String propValue2 = "testValue2";\r
private static final String noKey = "unusedKey";\r
private static final String noValue = "unusedValue";\r
+ private static final String propHeader = "test header";\r
private Properties properties = new Properties();\r
\r
private UnmodifiableProperties unmodifiableProperties = new UnmodifiableProperties(properties);\r
Assert.assertFalse(unmodifiableProperties.containsValue(noValue));\r
}\r
\r
+ @Test\r
+ public final void testElements() {\r
+ Enumeration<Object> propValues = unmodifiableProperties.elements();\r
+ Assert.assertEquals(propValue2, propValues.nextElement());\r
+ Assert.assertEquals(propValue1, propValues.nextElement());\r
+ }\r
+\r
@Test\r
public final void testEntrySet() {\r
- // Should match my properties K/V entries in setUp.\r
// Expect entrySet=[testKey2=testValue2, testKey1=testValue1].\r
- Assert.assertEquals(properties.entrySet(), unmodifiableProperties.entrySet());\r
+ Assert.assertEquals("Should match my properties K/V entries in setUp", properties.entrySet(),\r
+ unmodifiableProperties.entrySet());\r
}\r
\r
@Test\r
Assert.assertEquals(propValue2, unmodifiableProperties.getProperty(noKey, propValue2));\r
}\r
\r
+ @Test\r
+ public final void testHashCode() {\r
+ Assert.assertEquals("Should match my properties.hashcode() int.", properties.hashCode(),\r
+ unmodifiableProperties.hashCode());\r
+ }\r
+\r
@Test\r
public final void testIsEmpty() {\r
Assert.assertFalse(unmodifiableProperties.isEmpty());\r
}\r
\r
+ @Test\r
+ public final void testKeys() {\r
+ Enumeration<Object> propKeys = unmodifiableProperties.keys();\r
+ Assert.assertEquals(propKey2, propKeys.nextElement());\r
+ Assert.assertEquals(propKey1, propKeys.nextElement());\r
+ }\r
+\r
+ @Test\r
+ public final void testKeySet() {\r
+ // Expect keySet=[testKey2, testKey1].\r
+ Assert.assertEquals("Should match my properties key entries in SetUp", properties.keySet(),\r
+ unmodifiableProperties.keySet());\r
+ }\r
+\r
+ @Test\r
+ public final void testListPrintStream() {\r
+ ByteArrayOutputStream propByteArray = new ByteArrayOutputStream();\r
+ PrintStream listOut = new PrintStream(propByteArray);\r
+ unmodifiableProperties.list(listOut);\r
+ String propList = new String(propByteArray.toByteArray());\r
+ Assert.assertThat(propList, CoreMatchers.containsString("testKey2=testValue2"));\r
+ Assert.assertThat(propList, CoreMatchers.containsString("testKey1=testValue1"));\r
+ }\r
+\r
+ @Test\r
+ public final void testListPrintWriter() {\r
+ StringWriter listOut = new StringWriter();\r
+ PrintWriter writer = new PrintWriter(listOut);\r
+ unmodifiableProperties.list(writer);\r
+ String propList = listOut.toString();\r
+ Assert.assertThat(propList, CoreMatchers.containsString("testKey2=testValue2"));\r
+ Assert.assertThat(propList, CoreMatchers.containsString("testKey1=testValue1"));\r
+ }\r
+\r
+ @Test\r
+ public final void testLoadInputStream() throws IOException {\r
+ InputStream mockInStream = Mockito.mock(InputStream.class);\r
+ try {\r
+ unmodifiableProperties.load(mockInStream);\r
+ } catch (IOException ex) {\r
+ } catch (UnsupportedOperationException exceptionMessage) {\r
+ Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
+ }\r
+ }\r
+\r
+ @Test(expected = UnsupportedOperationException.class)\r
+ public final void testLoadReader() throws IOException {\r
+ String dummyPair = "key3=testKey3\nvalue3=testValue3";\r
+ StringReader reader = new StringReader(dummyPair);\r
+ try {\r
+ unmodifiableProperties.load(reader);\r
+ } catch (UnsupportedOperationException exceptionMessage) {\r
+ Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
+ throw exceptionMessage;\r
+ }\r
+ }\r
+\r
+ @Test\r
+ public final void testLoadFromXMLInputStream() throws IOException {\r
+ InputStream mockInStream = Mockito.mock(InputStream.class);\r
+ try {\r
+ unmodifiableProperties.loadFromXML(mockInStream);\r
+ } catch (IOException ex) {\r
+ } catch (UnsupportedOperationException exceptionMessage) {\r
+ Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
+ }\r
+ }\r
+\r
+ @Test\r
+ public final void testPropertyNames() {\r
+ Enumeration<?> propNames = unmodifiableProperties.propertyNames();\r
+ Assert.assertEquals(propKey2, propNames.nextElement());\r
+ Assert.assertEquals(propKey1, propNames.nextElement());\r
+ }\r
+\r
@Test(expected = UnsupportedOperationException.class)\r
public final void testPutObjectObject() {\r
try {\r
}\r
}\r
\r
+ @Test(expected = UnsupportedOperationException.class)\r
+ public final void testPutAllMapOfQextendsObjectQextendsObject() {\r
+ try {\r
+ unmodifiableProperties.putAll(properties);\r
+ } catch (UnsupportedOperationException exceptionMessage) {\r
+ Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());\r
+ throw exceptionMessage;\r
+ }\r
+ }\r
+\r
@Test(expected = UnsupportedOperationException.class)\r
public final void testRehash() {\r
try {\r
}\r
}\r
\r
+ @Test\r
+ public final void testSaveOutputStreamString() {\r
+ // Appl method is deprecated, but I still added this test since it is reachable.\r
+ OutputStream propByteArray = new ByteArrayOutputStream();\r
+ unmodifiableProperties.save(propByteArray, propHeader);\r
+ Assert.assertThat(propByteArray.toString(), CoreMatchers.startsWith("#test header"));\r
+ Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("testKey2=testValue2"));\r
+ Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("testKey1=testValue1"));\r
+ }\r
+\r
@Test(expected = UnsupportedOperationException.class)\r
public final void testSetPropertyStringString() {\r
try {\r
Assert.assertEquals(2, unmodifiableProperties.size());\r
}\r
\r
+ @Test\r
+ public final void testStoreOutputStreamString() throws IOException {\r
+ OutputStream propByteArray = new ByteArrayOutputStream();\r
+ unmodifiableProperties.store(propByteArray, propHeader);\r
+ // adds comment header and streams/appends properties file into propByteArray\r
+ // expected = "#test header\n#<Date>\ntestKey2=testValue2\ntestKey1=testValue1"\r
+ Assert.assertThat(propByteArray.toString(), CoreMatchers.startsWith("#test header"));\r
+ Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("testKey2=testValue2"));\r
+ Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("testKey1=testValue1"));\r
+ }\r
+\r
+ @Test\r
+ public final void testStoreWriterString() throws IOException {\r
+ StringWriter writer = new StringWriter();\r
+ unmodifiableProperties.store(writer, propHeader);\r
+ Assert.assertThat(writer.toString(), CoreMatchers.startsWith("#test header"));\r
+ Assert.assertThat(writer.toString(), CoreMatchers.containsString("testKey2=testValue2"));\r
+ Assert.assertThat(writer.toString(), CoreMatchers.containsString("testKey1=testValue1"));\r
+ }\r
+\r
+ @Test\r
+ public final void testStoreToXMLOutputStreamString() throws IOException {\r
+ OutputStream propByteArray = new ByteArrayOutputStream();\r
+ unmodifiableProperties.storeToXML(propByteArray, propHeader);\r
+ // adds XML comment header and streams/appends XML properties file into propByteArray\r
+ Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("<comment>test header</comment>"));\r
+ Assert.assertThat(propByteArray.toString(),\r
+ CoreMatchers.containsString("<entry key=\"testKey2\">testValue2</entry>"));\r
+ Assert.assertThat(propByteArray.toString(),\r
+ CoreMatchers.containsString("<entry key=\"testKey1\">testValue1</entry>"));\r
+ }\r
+\r
+ @Test\r
+ public final void testStoreToXMLOutputStreamStringString() throws IOException {\r
+ OutputStream propByteArray = new ByteArrayOutputStream();\r
+ unmodifiableProperties.storeToXML(propByteArray, propHeader, "UTF-8");\r
+ // adds XML comment header and streams/appends XML properties file into propByteArray\r
+ Assert.assertThat(propByteArray.toString(), CoreMatchers.containsString("<comment>test header</comment>"));\r
+ Assert.assertThat(propByteArray.toString(),\r
+ CoreMatchers.containsString("<entry key=\"testKey2\">testValue2</entry>"));\r
+ Assert.assertThat(propByteArray.toString(),\r
+ CoreMatchers.containsString("<entry key=\"testKey1\">testValue1</entry>"));\r
+ }\r
+\r
@Test\r
public final void testStringPropertyNames() {\r
- Assert.assertEquals(properties.stringPropertyNames(),unmodifiableProperties.stringPropertyNames());\r
+ Assert.assertEquals(properties.stringPropertyNames(), unmodifiableProperties.stringPropertyNames());\r
}\r
\r
@Test\r
public final void testToString() {\r
// toString=[{testKey2=testValue2, testKey1=testValue1}]\r
- Assert.assertEquals(properties.toString(),unmodifiableProperties.toString());\r
+ Assert.assertEquals(properties.toString(), unmodifiableProperties.toString());\r
+ }\r
+\r
+ @Test\r
+ public final void testValues() {\r
+ Assert.assertEquals(properties.values().toString(), unmodifiableProperties.values().toString());\r
}\r
}\r