annotate test/org/tmatesoft/hg/test/TestConfigFileParser.java @ 499:899a1b68ef03

Issue 38: add test for new keys and new section
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 26 Oct 2012 18:21:50 +0200
parents 02140be396d5
children
rev   line source
497
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2012 TMate Software Ltd
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.test;
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.ByteArrayInputStream;
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.io.ByteArrayOutputStream;
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.IOException;
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import org.junit.Assert;
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import org.junit.Test;
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import org.tmatesoft.hg.internal.ConfigFileParser;
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 /**
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 *
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 * @author Artem Tikhomirov
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 * @author TMate Software Ltd.
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 */
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 public class TestConfigFileParser {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 @Test
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 public void testParseOnly() throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 doTest("".getBytes(), new byte[0]);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 // line comments
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 byte[] inp = " # line comment \n; and another one".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 doTest(inp, inp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 // comments inside sections
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 inp = "[section1]\nkey1 = value\n # line comment\n[section2]\nkey2 = ;just presence\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 doTest(inp, inp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 // empty value
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 inp = "[section1]\nkey1 = \n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 doTest(inp, inp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 // multiline values
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 inp = "[section1]\nkey1 = a,\n b,\n c\nkey2=\n xyz\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 doTest(inp, inp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 // entry without EOL
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 inp = "[section1]\nkey1 = value".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 doTest(inp, inp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 // empty section
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 inp = "[section1]\nkey1 = value\n[section2]\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 doTest(inp, inp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 @Test
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 public void testLookup() throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 byte[] inp = "[section1]\nkey1 = a,\n b,\n c\nkey2=\n xyz\n\n[section2]\nkey3=\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 doTest(inp, inp, new Inspector() {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 public void visit(ConfigFileParser p) {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 Assert.assertTrue(p.exists("section1", "key1"));
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 Assert.assertTrue(p.exists("section1", "key2"));
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 Assert.assertFalse(p.exists("section1", "key3"));
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 Assert.assertTrue(p.exists("section2", "key3"));
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 Assert.assertFalse(p.exists("section2", "key1"));
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 Assert.assertFalse(p.exists("section2", "key2"));
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 });
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 @Test
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 public void testAddChangeEntries() throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 byte[] inp = "\n; line comment1\n[sect-a]\nkey1 = value1\n\n[sect-b]\nkey2=value2\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 byte[] exp = "\n; line comment1\n[sect-a]\nkey1 = value1\nkey3 = value3\n\n[sect-b]\nkey2=valueX\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 doTest(inp, exp, new Inspector() {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 public void visit(ConfigFileParser p) {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 Assert.assertTrue(p.exists("sect-b", "key2"));
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 p.add("sect-a", "key3", "value3");
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 p.change("sect-b", "key2", "valueX");
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 });
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 @Test
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 public void testAdditionTwoSectionsSameName() throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 byte[] inp = "[sect-a]\nkey1=value1\n\n[sect-b]\nkey2=\n\n[sect-a]\nkey3=value3\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 byte[] exp = "[sect-a]\nkey1=value1\nkey4 = value4\n\n[sect-b]\nkey2=\n\n[sect-a]\nkey3=value3\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 doTest(inp, exp, new Inspector() {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 public void visit(ConfigFileParser p) {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 p.add("sect-a", "key4", "value4");
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 });
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 @Test
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 public void testDeleteTwoSubsequentKeys() throws IOException{
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 byte[] inp = "# line comment1\n\n[sect-a]\nkey1=value1\nkey2=value2\n#line comment2\nkey3=value3\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 byte[] exp = "# line comment1\n\n[sect-a]\n\n\n#line comment2\nkey3=value3\n".getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 doTest(inp, exp, new Inspector() {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 public void visit(ConfigFileParser p) {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 p.delete("sect-a", "key1");
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 p.delete("sect-a", "key2");
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 });
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 @Test
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 public void testDeleteLastKeyInSection() throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 String text1 = "[sect-a]\nkey0 = value 0\n%skey1=value1\n%s[sect-b]\nkey3=value3\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 String text2 = "[sect-a]\nkey0 = value 0\n%s\n%s[sect-b]\nkey3=value3\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 withTwoCommentsDeleteKey1(text1, text2);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 @Test
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 public void testDeleteFirstKeyInSection() throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 String text1 = "[sect-a]\n%skey1=value1\n%skey2 = value 2\n[sect-b]\nkey3=value3\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 String text2 = "[sect-a]\n%s\n%skey2 = value 2\n[sect-b]\nkey3=value3\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 withTwoCommentsDeleteKey1(text1, text2);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
127 @Test
499
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
128 public void testDeleteOnlyKeyInSection() throws IOException {
497
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
129 String text1 = "[sect-a]\n%skey1=value1\n%s[sect-b]\nkey3=value3\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
130 String text2 = "[sect-a]\n%s\n%s[sect-b]\nkey3=value3\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131 withTwoCommentsDeleteKey1(text1, text2);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133
499
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
134 @Test
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
135 public void testAddNewSection() throws IOException {
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
136 byte[] inp = "[sect-a]\nkey1=value1\n".getBytes();
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
137 byte[] exp = "[sect-a]\nkey1=value1\n\n[sect-b]\nkey2 = value2\n".getBytes();
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
138 doTest(inp, exp, new Inspector() {
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
139
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
140 public void visit(ConfigFileParser p) {
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
141 p.add("sect-b", "key2", "value2");
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
142 }
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
143 });
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
144 }
899a1b68ef03 Issue 38: add test for new keys and new section
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 497
diff changeset
145
497
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 private void withTwoCommentsDeleteKey1(String text1, String text2) throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 String comment = "# line comment\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
148 Inspector insp = new Inspector() {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
149
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
150 public void visit(ConfigFileParser p) {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 p.delete("sect-a", "key1");
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
152 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153 };
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
154
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
155 byte[] inp = String.format(text1, "", "").getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
156 byte[] exp = String.format(text2, "", "").getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
157 doTest(inp, exp, insp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
158 inp = String.format(text1, comment, "").getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
159 exp = String.format(text2, comment, "").getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 doTest(inp, exp, insp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161 inp = String.format(text1, "", comment).getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 exp = String.format(text2, "", comment).getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 doTest(inp, exp, insp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
164 inp = String.format(text1, comment, comment).getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
165 exp = String.format(text2, comment, comment).getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
166 doTest(inp, exp, insp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
167 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
168
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
169 @Test
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
170 public void testAddEntryToEmptySection() throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
171 String text1 = "[sect-a]\n%s[sect-b]\nkey3=value3\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172 String text2 = "[sect-a]\n%skey1 = value1\n[sect-b]\nkey3=value3\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
173 String comment = "# line comment2\n";
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
174 Inspector insp = new Inspector() {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
175
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
176 public void visit(ConfigFileParser p) {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
177 p.add("sect-a", "key1", "value1");
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
178 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
179 };
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
180
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
181 byte[] inp = String.format(text1, "").getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
182 byte[] exp = String.format(text2, "").getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
183 doTest(inp, exp, insp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
184 inp = String.format(text1, comment).getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
185 exp = String.format(text2, comment).getBytes();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
186 doTest(inp, exp, insp);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
187 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
188
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
189
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
190 private void doTest(byte[] input, byte[] expected) throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
191 doTest(input, expected, null);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
192 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
193
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
194 private void doTest(byte[] input, byte[] expected, Inspector insp) throws IOException {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
195 ConfigFileParser p = new ConfigFileParser();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
196 p.parse(new ByteArrayInputStream(input));
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
197 if (insp != null) {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
198 insp.visit(p);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
199 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
200 ByteArrayOutputStream out = new ByteArrayOutputStream(input.length);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
201 p.update(out);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
202 byte[] result = out.toByteArray();
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
203 Assert.assertArrayEquals(expected, result);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
204 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
205
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
206 interface Inspector {
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
207 void visit(ConfigFileParser p);
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
208 }
02140be396d5 Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
209 }