|
1 |
| package org.jconfig.parser; |
|
2 |
| |
|
3 |
| import java.io.File; |
|
4 |
| |
|
5 |
| import junit.framework.TestCase; |
|
6 |
| |
|
7 |
| import org.jconfig.Configuration; |
|
8 |
| import org.jconfig.ConfigurationManager; |
|
9 |
| import org.jconfig.handler.XMLFileHandler; |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| public class NestedConfigParserTest extends TestCase { |
|
18 |
| |
|
19 |
4
| public NestedConfigParserTest(String arg0) {
|
|
20 |
4
| super(arg0);
|
|
21 |
| } |
|
22 |
| |
|
23 |
0
| public static void main(String[] args) {
|
|
24 |
0
| junit.textui.TestRunner.run(NestedConfigParserTest.class);
|
|
25 |
| } |
|
26 |
| |
|
27 |
4
| public void tearDown() throws Exception {
|
|
28 |
4
| System.setProperty("jconfig.parser","org.jconfig.parser.DefaultConfigParser");
|
|
29 |
| } |
|
30 |
| |
|
31 |
1
| public void testParseNestedConfig() {
|
|
32 |
1
| System.setProperty("jconfig.parser","org.jconfig.parser.NestedConfigParser");
|
|
33 |
1
| Configuration config = ConfigurationManager.getConfiguration("nested");
|
|
34 |
1
| String next = config.getProperty("inner",null,"inner/myinner");
|
|
35 |
1
| assertEquals("value",next);
|
|
36 |
1
| next = config.getProperty("hello",null,"inner/myinner/moreinner");
|
|
37 |
1
| assertEquals("universe",next);
|
|
38 |
1
| next = config.getProperty("text",null,"inner/2");
|
|
39 |
1
| assertNull(next);
|
|
40 |
1
| next = config.getProperty("special",null,"inner/2");
|
|
41 |
1
| assertEquals("one",next);
|
|
42 |
1
| config.setProperty("max","10000","inner/myinner/moreinner");
|
|
43 |
1
| next = config.getProperty("max",null,"inner/myinner/moreinner");
|
|
44 |
1
| assertEquals("10000",next);
|
|
45 |
1
| next = config.getProperty("vartest","not set","inner/myinner/moreinner");
|
|
46 |
1
| assertEquals("VarValue-is here",next);
|
|
47 |
1
| config.removeProperty("max","inner/myinner/moreinner");
|
|
48 |
1
| next = config.getProperty("max",null,"inner/myinner/moreinner");
|
|
49 |
1
| assertEquals(null,next);
|
|
50 |
| } |
|
51 |
| |
|
52 |
1
| public void testCreateNewTopLevelCategory() {
|
|
53 |
1
| System.setProperty("jconfig.parser","org.jconfig.parser.NestedConfigParser");
|
|
54 |
1
| Configuration config = ConfigurationManager.getConfiguration("nested");
|
|
55 |
1
| config.setProperty("another", "value", "brand/new");
|
|
56 |
1
| assertEquals("value",config.getProperty("another",null,"brand/new"));
|
|
57 |
| } |
|
58 |
| |
|
59 |
1
| public void testCreateNewNestedCategory() {
|
|
60 |
1
| System.setProperty("jconfig.parser","org.jconfig.parser.NestedConfigParser");
|
|
61 |
1
| Configuration config = ConfigurationManager.getConfiguration("nested");
|
|
62 |
1
| config.setProperty("hello", "new", "inner/myinner/new");
|
|
63 |
1
| assertEquals("new",config.getProperty("hello",null,"inner/myinner/new"));
|
|
64 |
| } |
|
65 |
| |
|
66 |
1
| public void testParseSaveLoadNestedConfig() {
|
|
67 |
1
| System.setProperty("jconfig.parser","org.jconfig.parser.NestedConfigParser");
|
|
68 |
1
| try {
|
|
69 |
1
| XMLFileHandler fileHandler = new XMLFileHandler();
|
|
70 |
1
| String filename = System.getProperty("java.io.tmpdir")+"test_config.xml";
|
|
71 |
1
| File file = new File(filename);
|
|
72 |
1
| fileHandler.setFile(file);
|
|
73 |
1
| Configuration config = ConfigurationManager.getConfiguration("nested");
|
|
74 |
1
| fileHandler.store(config);
|
|
75 |
1
| config = fileHandler.load(file, "MyTest");
|
|
76 |
1
| String next = config.getProperty("inner",null,"inner/myinner");
|
|
77 |
1
| assertEquals("value",next);
|
|
78 |
1
| next = config.getProperty("hello",null,"inner/myinner/moreinner");
|
|
79 |
1
| assertEquals("universe",next);
|
|
80 |
1
| next = config.getProperty("text",null,"inner/2");
|
|
81 |
1
| assertNull(next);
|
|
82 |
1
| next = config.getProperty("special",null,"inner/2");
|
|
83 |
1
| assertEquals("one",next);
|
|
84 |
| } |
|
85 |
| catch (Exception e) { |
|
86 |
0
| e.printStackTrace();
|
|
87 |
0
| fail("unexpected exception");
|
|
88 |
| } |
|
89 |
| } |
|
90 |
| |
|
91 |
| } |