001: /**
002: * Copyright (C) 2002
003: */package org.objectweb.util.monolog;
004:
005: import org.objectweb.util.monolog.api.BasicLevel;
006: import org.objectweb.util.monolog.api.LevelFactory;
007: import org.objectweb.util.monolog.api.Level;
008: import junit.framework.TestCase;
009: import junit.textui.TestRunner;
010:
011: /**
012: * It verifies a Level implementation and a LevelFactory implementation.
013: *
014: * @author Sebastien Chassande-Barrioz
015: */
016: public class TestLevel extends TestCase {
017:
018: /**
019: * This constant field contains the list of the setter name needed to
020: * initialize this class.
021: */
022: public static final String[] SETTER_METHODS = { "setLevelFactoryClassName" };
023:
024: protected LevelFactory lf = null;
025:
026: private Level l = null;
027:
028: public TestLevel() {
029: super ("");
030: }
031:
032: /**
033: * It assigns the LevelFactory class name. This method tries to get an
034: * instance with this class name.
035: */
036: public void setLevelFactoryClassName(String lfcn) {
037: try {
038: lf = (LevelFactory) Class.forName(lfcn).newInstance();
039: } catch (ClassCastException e) {
040: throw new UnsupportedOperationException(
041: "The specified class is not a Level factory: "
042: + lfcn);
043: } catch (Exception e) {
044: throw new UnsupportedOperationException(
045: "Level factory class is not availlable: " + lfcn);
046: }
047: }
048:
049: /**
050: * For running the TestLevel suite standalone. A particular TestSuite is
051: * used to initialized the class instance by setter methods.
052: * One parameter is required:
053: * <ul>
054: * <li>a LevelFactory class name</li>
055: * </ul>
056: */
057: public static void main(String args[]) {
058: if (args.length < 1) {
059: System.out.println("Syntax error !");
060: System.out
061: .println("java TestLevel <level factory class name>");
062: System.exit(12);
063: }
064: Object[] params = { args[0] };
065: try {
066: TestSuite suite = new TestSuite(TestLevel.class,
067: SETTER_METHODS, params);
068: TestRunner.run(suite);
069: } catch (Exception e) {
070: e.printStackTrace();
071: }
072: }
073:
074: public static TestSuite getTestSuite(String lfcn) {
075: Object[] params = { lfcn };
076: try {
077: return new TestSuite(TestLevel.class, SETTER_METHODS,
078: params);
079: } catch (Exception e) {
080: e.printStackTrace();
081: return null;
082: }
083: }
084:
085: // ------ TEST METHODS ------ //
086: //----------------------------//
087:
088: /**
089: * This method tests if it is possible to define a LevelConf with a
090: * predefined level and the + operator: DEBUG + 1
091: */
092: public void testInterLevelDef() {
093: assertEquals("Predefined level unreconized", BasicLevel.DEBUG,
094: lf.getLevel("DEBUG").getIntValue());
095: l = lf.defineLevel("MY_LEVEL", "DEBUG + 1");
096: assertNotNull("Null level", l);
097: assertEquals("intermediate level definition",
098: BasicLevel.DEBUG + 1, l.getIntValue());
099: }
100:
101: /**
102: * This method tests if it is possible to define a LevelConf with an
103: * other level and the + operator:
104: * <ul>
105: * <li>MY_LEVEL = DEBUG + 1</li>
106: * <li>MY_LEVEL2 = MY_LEVEL + 1</li>
107: * </ul>
108: */
109: public void testInterLevelDefWithOther() {
110: lf.defineLevel("MY_LEVEL", "DEBUG + 1");
111: l = lf.defineLevel("MY_LEVEL2", "MY_LEVEL + 1");
112: assertEquals(
113: "intermediate level definition based on another intermediate level",
114: BasicLevel.DEBUG + 2, l.getIntValue());
115: }
116:
117: /**
118: * This method tests if it is possible to define a LevelConf with a
119: * predefined level and the minus operator: DEBUG + 1
120: */
121: public void testInterLevelDefByMinus() {
122: l = lf.defineLevel("TOTO", "DEBUG - 1");
123: assertEquals("intermediate level definition minus operator",
124: BasicLevel.DEBUG - 1, l.getIntValue());
125: }
126:
127: /**
128: * This method tests if it is possible to define a LevelConf with a
129: * predefined level, the + operator and a number with several figures:
130: * DEBUG + 156
131: */
132: public void testInterLevelDefWithFigures() {
133: l = lf.defineLevel("TOTO2", "INFO +156 ");
134: assertEquals(
135: "intermediate level definition, number with several figures",
136: BasicLevel.INFO + 156, l.getIntValue());
137: }
138:
139: /**
140: * This method tests if it is possible to define a LevelConf with a
141: * predefined level witten with a combinaison of lower case and upper case
142: * and the + operator: InFO + 1
143: */
144: public void testInterLevelWithLowerCase() {
145: l = lf.defineLevel("TOTO3", " InFO + 1 ");
146: assertEquals(
147: "intermediate level definition, level name with lower case",
148: BasicLevel.INFO + 1, l.getIntValue());
149: }
150:
151: /**
152: * This method tests if it is possible to define a LevelConf with just a
153: * number.
154: */
155: public void testInterLevelJustNb() {
156: l = lf.defineLevel("TOTO4", "23455");
157: assertEquals(
158: "intermediate level definition, level value is just a number",
159: 23455, l.getIntValue());
160: l = lf.defineLevel("TOTO7", 23455);
161: assertEquals(
162: "intermediate level definition, level value is just a number",
163: 23455, l.getIntValue());
164:
165: }
166:
167: /**
168: * This method tests if it is possible to define a LevelConf with just an
169: * other level.
170: */
171: public void testInterLevelJustOther() {
172: lf.defineLevel("TOTO3", " InFO + 1 ");
173: l = lf.defineLevel("TOTO5", "TOTO3");
174: assertEquals(
175: "intermediate level definition, level value is just another intermediate level",
176: BasicLevel.INFO + 1, l.getIntValue());
177: }
178:
179: }
|