TypedProperties { public Object get(String key) { Map.Entry<String, List<String>> entry = _storage.get(key); if (entry == null) { return null; } String string = entry.getKey(); if (string != null) { return _convertFromString(string); } return string; }  Object get(String key); Set<String> keySet(); void load(Reader reader); void put(String key, Object value); void remove(String key); void save(Writer writer);  }
@Test public void testLoadandSaveArray() throws IOException { String line = "testKey = [\"testValue1\", \"testValue2\"]"; TypedProperties typedProperties = _createTypedProperties(line); Assert.assertArrayEquals( new String[] {"testValue1", "testValue2"}, (String[])typedProperties.get("testKey")); _assertSave(typedProperties, line); }
@Test public void testLoadandSaveCollection() throws IOException { String line = "testKey = (\"testValue1\", \"testValue2\")"; TypedProperties typedProperties = _createTypedProperties(line); Assert.assertEquals( Arrays.asList("testValue1", "testValue2"), typedProperties.get("testKey")); _assertSave(typedProperties, line); }
@Test public void testLoadandSaveComment() throws IOException { String line = "#comment\ntestKey = \"testValue\""; TypedProperties typedProperties = _createTypedProperties(line); Assert.assertEquals("testValue", typedProperties.get("testKey")); _assertSave(typedProperties, line); }
@Test public void testLoadandSaveEscapedEquals() throws IOException { String line = "testKey = \"testValue\\=test\""; TypedProperties typedProperties = _createTypedProperties(line); Assert.assertEquals("testValue=test", typedProperties.get("testKey")); _assertSave(typedProperties, line); }
@Test public void testLoadandSaveMultiline() throws IOException { String line = "testKey = [\\\n\t\"testValue1\",\\\n\t\"testValue2\"\\\n]"; TypedProperties typedProperties = _createTypedProperties(line); Assert.assertArrayEquals( new String[] {"testValue1", "testValue2"}, (String[])typedProperties.get("testKey")); _assertSave(typedProperties, line); }
@Test public void testLoadAndSaveSubstitution() throws IOException { String systemKey = "testSystemKey"; String line = StringBundler.concat("testKey = \"${", systemKey, "}\""); String systemValue = "testSystemValue"; String oldSystemValue = System.getProperty(systemKey); System.setProperty(systemKey, systemValue); TypedProperties typedProperties = _createTypedProperties(line); try { Assert.assertEquals( "testSystemValue", typedProperties.get("testKey")); } finally { if (oldSystemValue == null) { System.clearProperty(systemKey); } else { System.setProperty(systemKey, oldSystemValue); } } _assertSave(typedProperties, line); }
@Test public void testLoadandStoreMultilineString() throws IOException { String line = "testKey = \"testValue1,\\\n\ttestValue2\""; TypedProperties typedProperties = _createTypedProperties(line); Assert.assertEquals( "testValue1,testValue2", typedProperties.get("testKey")); _assertSave(typedProperties, line); }
@Test public void testLoadNontyped() throws IOException { TypedProperties typedProperties = _createTypedProperties( "testKey = \"testValue\""); Assert.assertEquals("testValue", typedProperties.get("testKey")); }
@Test public void testLoadTyped() throws IOException { TypedProperties typedProperties = _createTypedProperties( "testKey = I\"1\""); Assert.assertEquals(1, typedProperties.get("testKey")); }