001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.logging;
005:
006: import com.tc.exception.ImplementMe;
007: import com.tc.logging.LogLevel;
008: import com.tc.logging.TCLogger;
009:
010: import java.util.HashMap;
011: import java.util.Map;
012:
013: import junit.framework.TestCase;
014:
015: public class OptionsTest extends TestCase {
016: Map defaults = new HashMap();
017: Logger logger = new Logger();
018: String[] keys = new String[] { "key1", "key2", "key3" };
019: String input;
020: Options opts;
021:
022: protected void setUp() throws Exception {
023: super .setUp();
024:
025: defaults.put("key1", Boolean.TRUE);
026: defaults.put("key2", Boolean.TRUE);
027: defaults.put("key3", Boolean.FALSE);
028: }
029:
030: public void testExceptions() {
031: try {
032: new Options("", new String[] { "key1", "ALL" }, logger,
033: defaults);
034: fail();
035: } catch (IllegalArgumentException e) {
036: // expected
037: }
038:
039: try {
040: new Options("", new String[] { "NONE" }, logger, defaults);
041: fail();
042: } catch (IllegalArgumentException e) {
043: // expected
044: }
045:
046: defaults.clear();
047: defaults.put("key1", "not an instance of java.lang.Boolean");
048:
049: try {
050: new Options("", keys, logger, defaults);
051: fail();
052: } catch (IllegalArgumentException e) {
053: // expected
054: }
055: }
056:
057: public void test() {
058: input = "key1";
059: opts = new Options(input, keys, logger, null);
060: assertTrue(opts.getOption("key1"));
061: assertFalse(opts.getOption("key2"));
062: assertFalse(opts.getOption("key3"));
063: assertEquals(0, logger.warnCount);
064:
065: String[] empty = new String[] { null, "", " ", " \t", "\t ",
066: "\t", " ", " \t ", " ,\t" };
067: for (int i = 0; i < empty.length; i++) {
068: input = empty[i];
069: opts = new Options(input, keys, logger, null);
070: assertFalse(opts.getOption("key1"));
071: assertFalse(opts.getOption("key2"));
072: assertFalse(opts.getOption("key3"));
073: assertEquals(0, logger.warnCount);
074: }
075:
076: input = "this-aint-a-valid-option, key2";
077: opts = new Options(input, keys, logger, null);
078: assertEquals(1, logger.warnCount);
079: assertFalse(opts.getOption("key1"));
080: assertTrue(opts.getOption("key2"));
081: assertFalse(opts.getOption("key3"));
082:
083: String[] all = new String[] { "ALL", "toString, ALL",
084: "ALL, not-a-valid-option" };
085: for (int i = 0; i < all.length; i++) {
086: input = all[i];
087: opts = new Options(input, keys, logger, null);
088: assertTrue(opts.getOption("key1"));
089: assertTrue(opts.getOption("key2"));
090: assertTrue(opts.getOption("key3"));
091: }
092: }
093:
094: public void testNone() {
095: input = "NONE, key2";
096: opts = new Options(input, keys, logger, defaults);
097: assertEquals(0, logger.warnCount);
098: assertFalse(opts.getOption("key1"));
099: assertTrue(opts.getOption("key2"));
100: assertFalse(opts.getOption("key3"));
101:
102: input = "key1, key2,key3, NONE";
103: opts = new Options(input, keys, logger, defaults);
104: assertEquals(0, logger.warnCount);
105: assertFalse(opts.getOption("key1"));
106: assertFalse(opts.getOption("key2"));
107: assertFalse(opts.getOption("key3"));
108: }
109:
110: public void testNegation() {
111: input = "key1, -key2";
112: opts = new Options(input, keys, logger, defaults);
113: assertEquals(0, logger.warnCount);
114: assertTrue(opts.getOption("key1"));
115: assertFalse(opts.getOption("key2")); // default is true, but we negated it in the the input
116: assertFalse(opts.getOption("key3"));
117: }
118:
119: public void testAllPlusNegation() {
120: input = "ALL, -key2";
121: opts = new Options(input, keys, logger, defaults);
122: assertEquals(0, logger.warnCount);
123: assertTrue(opts.getOption("key1"));
124: assertFalse(opts.getOption("key2"));
125: assertTrue(opts.getOption("key3"));
126: }
127:
128: public void testDefaults() {
129: opts = new Options(null, keys, logger, defaults);
130: assertEquals(0, logger.warnCount);
131: assertTrue(opts.getOption("key1"));
132: assertTrue(opts.getOption("key2"));
133: assertFalse(opts.getOption("key3"));
134: }
135:
136: private static class Logger implements TCLogger {
137:
138: int warnCount;
139:
140: public void debug(Object message) {
141: throw new ImplementMe();
142: }
143:
144: public void debug(Object message, Throwable t) {
145: throw new ImplementMe();
146: }
147:
148: public void error(Object message) {
149: throw new ImplementMe();
150: }
151:
152: public void error(Object message, Throwable t) {
153: throw new ImplementMe();
154: }
155:
156: public void fatal(Object message) {
157: throw new ImplementMe();
158: }
159:
160: public void fatal(Object message, Throwable t) {
161: throw new ImplementMe();
162: }
163:
164: public void info(Object message) {
165: throw new ImplementMe();
166: }
167:
168: public void info(Object message, Throwable t) {
169: throw new ImplementMe();
170: }
171:
172: public void warn(Object message) {
173: this .warnCount++;
174: }
175:
176: public void warn(Object message, Throwable t) {
177: throw new ImplementMe();
178: }
179:
180: public void log(LogLevel level, Object message) {
181: throw new ImplementMe();
182: }
183:
184: public void log(LogLevel level, Object message, Throwable t) {
185: throw new ImplementMe();
186: }
187:
188: public boolean isDebugEnabled() {
189: return false;
190: }
191:
192: public boolean isInfoEnabled() {
193: return false;
194: }
195:
196: public void setLevel(LogLevel level) {
197: throw new ImplementMe();
198: }
199:
200: public LogLevel getLevel() {
201: throw new ImplementMe();
202: }
203:
204: public String getName() {
205: throw new ImplementMe();
206: }
207: }
208: }
|