KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > logging > OptionsTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.logging;
5
6 import com.tc.exception.ImplementMe;
7 import com.tc.logging.LogLevel;
8 import com.tc.logging.TCLogger;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import junit.framework.TestCase;
14
15 public class OptionsTest extends TestCase {
16   Map JavaDoc defaults = new HashMap JavaDoc();
17   Logger logger = new Logger();
18   String JavaDoc[] keys = new String JavaDoc[] { "key1", "key2", "key3" };
19   String JavaDoc input;
20   Options opts;
21
22   protected void setUp() throws Exception JavaDoc {
23     super.setUp();
24
25     defaults.put("key1", Boolean.TRUE);
26     defaults.put("key2", Boolean.TRUE);
27     defaults.put("key3", Boolean.FALSE);
28   }
29
30   public void testExceptions() {
31     try {
32       new Options("", new String JavaDoc[] { "key1", "ALL" }, logger, defaults);
33       fail();
34     } catch (IllegalArgumentException JavaDoc e) {
35       // expected
36
}
37
38     try {
39       new Options("", new String JavaDoc[] { "NONE" }, logger, defaults);
40       fail();
41     } catch (IllegalArgumentException JavaDoc e) {
42       // expected
43
}
44
45     defaults.clear();
46     defaults.put("key1", "not an instance of java.lang.Boolean");
47
48     try {
49       new Options("", keys, logger, defaults);
50       fail();
51     } catch (IllegalArgumentException JavaDoc e) {
52       // expected
53
}
54   }
55
56   public void test() {
57     input = "key1";
58     opts = new Options(input, keys, logger, null);
59     assertTrue(opts.getOption("key1"));
60     assertFalse(opts.getOption("key2"));
61     assertFalse(opts.getOption("key3"));
62     assertEquals(0, logger.warnCount);
63
64     String JavaDoc[] empty = new String JavaDoc[] { null, "", " ", " \t", "\t ", "\t", " ", " \t ", " ,\t" };
65     for (int i = 0; i < empty.length; i++) {
66       input = empty[i];
67       opts = new Options(input, keys, logger, null);
68       assertFalse(opts.getOption("key1"));
69       assertFalse(opts.getOption("key2"));
70       assertFalse(opts.getOption("key3"));
71       assertEquals(0, logger.warnCount);
72     }
73
74     input = "this-aint-a-valid-option, key2";
75     opts = new Options(input, keys, logger, null);
76     assertEquals(1, logger.warnCount);
77     assertFalse(opts.getOption("key1"));
78     assertTrue(opts.getOption("key2"));
79     assertFalse(opts.getOption("key3"));
80
81     String JavaDoc[] all = new String JavaDoc[] { "ALL", "toString, ALL", "ALL, not-a-valid-option" };
82     for (int i = 0; i < all.length; i++) {
83       input = all[i];
84       opts = new Options(input, keys, logger, null);
85       assertTrue(opts.getOption("key1"));
86       assertTrue(opts.getOption("key2"));
87       assertTrue(opts.getOption("key3"));
88     }
89   }
90
91   public void testNone() {
92     input = "NONE, key2";
93     opts = new Options(input, keys, logger, defaults);
94     assertEquals(0, logger.warnCount);
95     assertFalse(opts.getOption("key1"));
96     assertTrue(opts.getOption("key2"));
97     assertFalse(opts.getOption("key3"));
98
99     input = "key1, key2,key3, NONE";
100     opts = new Options(input, keys, logger, defaults);
101     assertEquals(0, logger.warnCount);
102     assertFalse(opts.getOption("key1"));
103     assertFalse(opts.getOption("key2"));
104     assertFalse(opts.getOption("key3"));
105   }
106
107   public void testNegation() {
108     input = "key1, -key2";
109     opts = new Options(input, keys, logger, defaults);
110     assertEquals(0, logger.warnCount);
111     assertTrue(opts.getOption("key1"));
112     assertFalse(opts.getOption("key2")); // default is true, but we negated it in the the input
113
assertFalse(opts.getOption("key3"));
114   }
115
116   public void testAllPlusNegation() {
117     input = "ALL, -key2";
118     opts = new Options(input, keys, logger, defaults);
119     assertEquals(0, logger.warnCount);
120     assertTrue(opts.getOption("key1"));
121     assertFalse(opts.getOption("key2"));
122     assertTrue(opts.getOption("key3"));
123   }
124
125   public void testDefaults() {
126     opts = new Options(null, keys, logger, defaults);
127     assertEquals(0, logger.warnCount);
128     assertTrue(opts.getOption("key1"));
129     assertTrue(opts.getOption("key2"));
130     assertFalse(opts.getOption("key3"));
131   }
132
133   private static class Logger implements TCLogger {
134
135     int warnCount;
136
137     public void debug(Object JavaDoc message) {
138       throw new ImplementMe();
139     }
140
141     public void debug(Object JavaDoc message, Throwable JavaDoc t) {
142       throw new ImplementMe();
143     }
144
145     public void error(Object JavaDoc message) {
146       throw new ImplementMe();
147     }
148
149     public void error(Object JavaDoc message, Throwable JavaDoc t) {
150       throw new ImplementMe();
151     }
152
153     public void fatal(Object JavaDoc message) {
154       throw new ImplementMe();
155     }
156
157     public void fatal(Object JavaDoc message, Throwable JavaDoc t) {
158       throw new ImplementMe();
159     }
160
161     public void info(Object JavaDoc message) {
162       throw new ImplementMe();
163     }
164
165     public void info(Object JavaDoc message, Throwable JavaDoc t) {
166       throw new ImplementMe();
167     }
168
169     public void warn(Object JavaDoc message) {
170       this.warnCount++;
171     }
172
173     public void warn(Object JavaDoc message, Throwable JavaDoc t) {
174       throw new ImplementMe();
175     }
176
177     public void log(LogLevel level, Object JavaDoc message) {
178       throw new ImplementMe();
179     }
180
181     public void log(LogLevel level, Object JavaDoc message, Throwable JavaDoc t) {
182       throw new ImplementMe();
183     }
184
185     public boolean isDebugEnabled() {
186       return false;
187     }
188
189     public boolean isInfoEnabled() {
190       return false;
191     }
192
193     public void setLevel(LogLevel level) {
194       throw new ImplementMe();
195     }
196
197     public LogLevel getLevel() {
198       throw new ImplementMe();
199     }
200
201     public String JavaDoc getName() {
202       throw new ImplementMe();
203     }
204   }
205 }
206
Popular Tags