@Test public void testSetPrelinesOnExistingEntryWithAtrributes() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "#k1a", "k1 = v1 # comment" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setEntry("S1", "k1", null, null, null, Arrays.asList("#k1b")); assertTextEquals("[S1]|#k1b|k1 = v1 # comment|", cm); }
@Test public void testSetEntryToNullOnExistingEntry() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setEntry("S1", "k1", null, null, null, null); assertTextEquals("[S1]|k1 = v1|", cm); }
@Test public void testRemoveSectionOnExistingSection() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1", "[S2]", "k2 = v2" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.removeSection("S1"); assertTextEquals("[S2]|k2 = v2|", cm); }
@Test public void testSetValueOnExistingEntryWithAttributes() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "#k1", "k1 = v1 # comment" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setEntry("S1", "k1", "v2", null, null, null); assertTextEquals("[S1]|#k1|k1 = v2 # comment|", cm); }
@Test public void testSetValueOnNonExistingEntryOnNonExistentSection() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setEntry("S2", "k2", "v2", null, null, null); assertTextEquals("[S1]|k1 = v1|[S2]|k2 = v2|", cm); }
@Test public void testSetEntryWithCommentChars() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1" ); ConfigMap cm = s.getMap("Foo.cfg"); // If value has # in it, it should get escaped. cm.setEntry("S1", "k1", "v1 # foo", null, null, null); assertTextEquals("[S1]|k1 = v1 \\u0023 foo|", cm); }
@Test public void testSetValueOnExistingEntry() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setEntry("S1", "k1", "v2", null, null, null); assertTextEquals("[S1]|k1 = v2|", cm); }
@Test public void testSetValueToNullOnExistingEntry() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setEntry("S1", "k1", null, null, null, null); assertTextEquals("[S1]|k1 = v1|", cm); }
@Test public void testSetValueWithCommentChars() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1" ); ConfigMap cm = s.getMap("Foo.cfg"); // If value has # in it, it should get escaped. cm.setEntry("S1", "k1", "v1 # foo", null, null, null); assertTextEquals("[S1]|k1 = v1 \\u0023 foo|", cm); }
@Test public void testSetEntryOnNonExistingEntryOnNonExistentSection() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setEntry("S2", "k2", "v2", null, null, null); assertTextEquals("[S1]|k1 = v1|[S2]|k2 = v2|", cm); }
@Test public void testSetCommentOnExistingEntryWithAttributes() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "#k1a", "k1 = v1 # c1" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setEntry("S1", "k1", null, null, "c2", null); assertTextEquals("[S1]|#k1a|k1 = v1 # c2|", cm); }
@Test public void testRemoveDefaultSection() throws Exception { ConfigStore s = initStore("Foo.cfg", "k = v", "[S1]", "k1 = v1", "[S2]", "k2 = v2" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.removeSection(""); assertTextEquals("[S1]|k1 = v1|[S2]|k2 = v2|", cm); }
@Test public void testNonExistentConfig() throws Exception { ConfigStore s = ConfigMemoryStore.create().build(); ConfigMap cm = s.getMap("Foo.cfg"); assertEquals("", cm.toString()); }
@Test public void testSetSectionOnNewSection() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setSection("S2", Arrays.asList("#S2")); assertTextEquals("[S1]|k1 = v1|#S2|[S2]|", cm); cm.setSection("S3", Collections.<String>emptyList()); assertTextEquals("[S1]|k1 = v1|#S2|[S2]|[S3]|", cm); cm.setSection("S4", null); assertTextEquals("[S1]|k1 = v1|#S2|[S2]|[S3]|[S4]|", cm); }
@Test public void testDuplicateSectionNames() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "[S1]"); try { s.getMap("Foo.cfg"); fail("Exception expected."); } catch (ConfigException e) { assertEquals("Duplicate section found in configuration: [S1]", e.getLocalizedMessage()); } }
@Test public void testDuplicateEntryNames() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "foo=v1", "foo=v2"); try { s.getMap("Foo.cfg"); fail("Exception expected."); } catch (ConfigException e) { assertEquals("Duplicate entry found in section [S1] of configuration: foo", e.getLocalizedMessage()); } }
@Test public void testSetEntryOnNonExistingEntry() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = v1" ); ConfigMap cm = s.getMap("Foo.cfg"); cm.setEntry("S1", "k2", "v2", null, null, null); assertTextEquals("[S1]|k1 = v1|k2 = v2|", cm); cm.setEntry("S1", "k2", null, null, null, null); assertTextEquals("[S1]|k1 = v1|k2 = v2|", cm); cm.setEntry("S1", "k2", "", null, null, null); assertTextEquals("[S1]|k1 = v1|k2 = |", cm); }
@Test public void testEntriesWithOddComments() throws Exception { ConfigStore s = initStore("Foo.cfg", "[S1]", "k1 = foo#", "k2 = foo # " ); ConfigMap cm = s.getMap("Foo.cfg"); assertTextEquals("[S1]|k1 = foo#|k2 = foo # |", cm); assertEquals("", cm.getEntry("S1", "k1").getComment()); assertEquals("", cm.getEntry("S1", "k2").getComment()); }
@Test public void testNonExistentValues() throws Exception { ConfigStore s = initStore("Foo.cfg", "[MySection]", "foo=bar" ); ConfigMap cm = s.getMap("Foo.cfg"); assertTextEquals("[MySection]|foo=bar|", cm); assertEquals("", join(cm.getPreLines(""), '|')); assertNull(cm.getPreLines("XXX")); assertNull(cm.getEntry("XXX", "yyy")); assertNull(cm.getEntry("MySection", "yyy")); }