1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.log4j.helpers;
22
23 import org.apache.log4j.Level;
24 import org.apache.log4j.xml.XLevel;
25
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28 import junit.framework.Test;
29 import java.util.Properties;
30
31
32
33
34
35
36
37 public class OptionConverterTestCase extends TestCase {
38
39 Properties props;
40
41 public OptionConverterTestCase(String name) {
42 super(name);
43 }
44
45 public
46 void setUp() {
47 props = new Properties();
48 props.put("TOTO", "wonderful");
49 props.put("key1", "value1");
50 props.put("key2", "value2");
51 System.setProperties(props);
52
53
54 }
55
56 public
57 void tearDown() {
58 props = null;
59 }
60
61 public
62 void varSubstTest1() {
63 String r;
64
65 r = OptionConverter.substVars("hello world.", null);
66 assertEquals("hello world.", r);
67
68 r = OptionConverter.substVars("hello ${TOTO} world.", null);
69
70 assertEquals("hello wonderful world.", r);
71 }
72
73
74 public
75 void varSubstTest2() {
76 String r;
77
78 r = OptionConverter.substVars("Test2 ${key1} mid ${key2} end.", null);
79 assertEquals("Test2 value1 mid value2 end.", r);
80 }
81
82 public
83 void varSubstTest3() {
84 String r;
85
86 r = OptionConverter.substVars(
87 "Test3 ${unset} mid ${key1} end.", null);
88 assertEquals("Test3 mid value1 end.", r);
89 }
90
91 public
92 void varSubstTest4() {
93 String val = "Test4 ${incomplete ";
94 try {
95 OptionConverter.substVars(val, null);
96 }
97 catch(IllegalArgumentException e) {
98 String errorMsg = e.getMessage();
99
100 assertEquals('"'+val
101 + "\" has no closing brace. Opening brace at position 6.",
102 errorMsg);
103 }
104 }
105
106 public
107 void varSubstTest5() {
108 Properties props = new Properties();
109 props.put("p1", "x1");
110 props.put("p2", "${p1}");
111 String res = OptionConverter.substVars("${p2}", props);
112 System.out.println("Result is ["+res+"].");
113 assertEquals(res, "x1");
114 }
115
116 public
117 void toLevelTest1() {
118 String val = "INFO";
119 Level p = OptionConverter.toLevel(val, null);
120 assertEquals(p, Level.INFO);
121 }
122
123 public
124 void toLevelTest2() {
125 String val = "INFO#org.apache.log4j.xml.XLevel";
126 Level p = OptionConverter.toLevel(val, null);
127 assertEquals(p, Level.INFO);
128 }
129
130 public
131 void toLevelTest3() {
132 String val = "TRACE#org.apache.log4j.xml.XLevel";
133 Level p = OptionConverter.toLevel(val, null);
134 assertEquals(p, XLevel.TRACE);
135 }
136
137 public
138 void toLevelTest4() {
139 String val = "TR#org.apache.log4j.xml.XLevel";
140 Level p = OptionConverter.toLevel(val, null);
141 assertEquals(p, null);
142 }
143
144 public
145 void toLevelTest5() {
146 String val = "INFO#org.apache.log4j.xml.TOTO";
147 Level p = OptionConverter.toLevel(val, null);
148 assertEquals(p, null);
149 }
150
151
152 public
153 static
154 Test suite() {
155 TestSuite suite = new TestSuite();
156 suite.addTest(new OptionConverterTestCase("varSubstTest5"));
157 suite.addTest(new OptionConverterTestCase("varSubstTest1"));
158 suite.addTest(new OptionConverterTestCase("varSubstTest2"));
159 suite.addTest(new OptionConverterTestCase("varSubstTest3"));
160 suite.addTest(new OptionConverterTestCase("varSubstTest4"));
161
162
163 suite.addTest(new OptionConverterTestCase("toLevelTest1"));
164 suite.addTest(new OptionConverterTestCase("toLevelTest2"));
165 suite.addTest(new OptionConverterTestCase("toLevelTest3"));
166 suite.addTest(new OptionConverterTestCase("toLevelTest4"));
167 suite.addTest(new OptionConverterTestCase("toLevelTest5"));
168 return suite;
169 }
170
171 }