001: /**
002: * Copyright (C) 2002
003: */package org.objectweb.util.monolog;
004:
005: import junit.framework.TestCase;
006: import junit.textui.TestRunner;
007: import org.objectweb.util.monolog.api.Handler;
008: import org.objectweb.util.monolog.api.HandlerFactory;
009: import org.objectweb.util.monolog.api.LevelFactory;
010: import org.objectweb.util.monolog.api.LoggerFactory;
011: import org.objectweb.util.monolog.api.TopicalLogger;
012:
013: import java.io.*;
014: import java.util.Vector;
015:
016: /**
017: * This class is test helper which provides tool methods to check a log file.
018: * It extends TestCase to support the use with JUnit.
019: * @author Sebastien Chassande-Barrioz
020: */
021: public class TestHelper extends TestCase {
022: /**
023: * This constant field contains the list of the setter name needed to
024: * initialize this class.
025: */
026: public static final String[] SETTER_METHODS = { "setLoggerFactoryClassName", };
027:
028: /**
029: * The Logger factory used during the test
030: */
031: public static LoggerFactory lf = null;
032:
033: /**
034: * The handler factory used during the test
035: */
036: public static HandlerFactory hf = null;
037:
038: /**
039: * The level factory used during the test
040: */
041: public static LevelFactory lef = null;
042:
043: /**
044: * The default file name specify by a setter method.
045: */
046: protected String fileName = null;
047:
048: public TestHelper() {
049: super ("");
050: }
051:
052: public TestHelper(String s) {
053: super (s);
054: }
055:
056: protected void setUp() throws Exception {
057: if (lf == null) {
058: synchronized (TestHelper.class) {
059: lf = Monolog.getMonologFactory("monolog.properties");
060: hf = (HandlerFactory) lf;
061: lef = (LevelFactory) lf;
062: }
063: }
064: }
065:
066: /**
067: * It assigns the LoggerFactory class name. This method tries to get an
068: * instance with this class name.
069: */
070: public void setLoggerFactoryClassName(String lfcn) {
071: try {
072: lf = (LoggerFactory) Class.forName(lfcn).newInstance();
073: } catch (ClassCastException e) {
074: throw new UnsupportedOperationException(
075: "The specified class is not a Logger factory: "
076: + lfcn);
077: } catch (Exception e) {
078: throw new UnsupportedOperationException(
079: "Logger factory class is not availlable: " + lfcn);
080: }
081: if (lf instanceof HandlerFactory) {
082: hf = (HandlerFactory) lf;
083: }
084: if (lf instanceof LevelFactory) {
085: lef = (LevelFactory) lf;
086: }
087: }
088:
089: /**
090: * It assigns the default log file name.
091: */
092: public void setOutputFile(String filename) {
093: fileName = filename;
094: }
095:
096: /**
097: * It runs the test.
098: */
099: public static void run(Class c, String[] methods, String params[],
100: String lfcn, String fn) {
101: try {
102: TestRunner.run(getTestSuite(c, methods, params, lfcn, fn));
103: } catch (Exception e) {
104: e.printStackTrace();
105: }
106: }
107:
108: /**
109: * It runs the test.
110: */
111: public static void run(Class c, String[] methods, String params[],
112: String lfcn) {
113: try {
114: TestRunner.run(getTestSuite(c, methods, params, lfcn));
115: } catch (Exception e) {
116: e.printStackTrace();
117: }
118: }
119:
120: /**
121: * It retrieves a test suite.
122: */
123: public static TestSuite getTestSuite(Class c, String[] methods,
124: String params[], String lfcn, String fn) {
125: Object[] myparams = new Object[params.length + 2];
126: myparams[0] = lfcn;
127: myparams[1] = fn;
128: for (int i = 0; i < params.length; i++) {
129: myparams[i + 2] = params[i];
130: }
131: String[] mymethods = new String[methods.length + 2];
132: mymethods[0] = "setLoggerFactoryClassName";
133: mymethods[1] = "setOutputFile";
134: for (int i = 0; i < methods.length; i++) {
135: mymethods[i + 2] = methods[i];
136: }
137: try {
138: return new TestSuite(c, mymethods, myparams);
139: } catch (Exception e) {
140: e.printStackTrace();
141: return null;
142: }
143: }
144:
145: public static TestSuite getTestSuite(Class c, String[] methods,
146: String params[], String lfcn) {
147: Object[] myparams = new Object[params.length + 1];
148: myparams[0] = lfcn;
149: for (int i = 0; i < params.length; i++) {
150: myparams[i + 1] = params[i];
151: }
152: String[] mymethods = new String[methods.length + 1];
153: mymethods[0] = "setLoggerFactoryClassName";
154: for (int i = 0; i < methods.length; i++) {
155: mymethods[i + 1] = methods[i];
156: }
157: try {
158: return new TestSuite(c, mymethods, myparams);
159: } catch (Exception e) {
160: e.printStackTrace();
161: return null;
162: }
163: }
164:
165: public static void main(String[] args) {
166: if (args == null || args.length <= 1) {
167: System.out.println("Invalid argument");
168: System.exit(1);
169: }
170: try {
171: File f = new File(args[0]);
172: f.delete();
173: f.createNewFile();
174: PrintStream ps = new PrintStream(new FileOutputStream(f));
175: String[] required = new String[args.length - 1];
176: for (int i = 0; i < required.length; i++) {
177: required[i] = args[i + 1];
178: ps.println(required[i]);
179: }
180: ps.flush();
181: ps.close();
182: TestHelper to = new TestHelper();
183: to.setOutputFile(args[0]);
184: to.assertInFileEquals("my message", required);
185: f.delete();
186: } catch (IOException e) {
187: e.printStackTrace();
188: fail(e.getMessage());
189: }
190: }
191:
192: /**
193: * It checks that the last line of the default log file is equals to a
194: * specified string.
195: * @param message is the error message if the string is not found.
196: * @param required is the string which must be file in the default log file
197: */
198: public void assertInFileEquals(String message, String required) {
199: String[] r = new String[1];
200: r[0] = required;
201: assertInFileEquals(message, fileName, r);
202: }
203:
204: /**
205: * It checks that the last line of the specified log file is equals to a
206: * specified string.
207: * @param message is the error message if the string is not found.
208: * @param filename is the log file name
209: * @param required is the string which must be file in the default log file
210: */
211: public void assertInFileEquals(String message, String filename,
212: String required) {
213: String[] r = new String[1];
214: r[0] = required;
215: assertInFileEquals(message, filename, r);
216: }
217:
218: /**
219: * It checks that the last lines of the default log file is equals to a
220: * specified string string.
221: * @param message is the error message if the string array is not found.
222: * @param required is the string array which must be file in the default
223: * log file.
224: */
225: public void assertInFileEquals(String message, String[] required) {
226: String[] text = getFirstLines(fileName, required.length);
227: // Check the String
228: for (int i = 0; i < required.length; i++) {
229: debug("found : text[" + i + "]=" + text[i]
230: + " text[i].length = " + text[i].length());
231: debug("required : required[" + i + "]=" + required[i]
232: + " required[i].length = " + required[i].length());
233: assertEquals(message + " " + i, required[i], text[i]);
234: }
235: }
236:
237: /**
238: * It checks that the last lines of the specified log file is equals to a
239: * specified string string.
240: * @param message is the error message if the string array is not found.
241: * @param filename is the log file name
242: * @param required is the string array which must be file in the default
243: * log file.
244: */
245: public void assertInFileEquals(String message, String filename,
246: String[] required) {
247: String[] text = getFirstLines(filename, required.length);
248: // Check the String
249: for (int i = 0; i < required.length; i++) {
250: debug("found : text[" + i + "]=" + text[i]);
251: debug("required : required[" + i + "]=" + required[i]);
252: assertEquals(message + " " + i, required[i], text[i]);
253: }
254: }
255:
256: /**
257: * Return the last lines of the default log file.
258: * @param linenumber is the number of line which must be retrieved.
259: * @return a string array which matches to the last line.
260: */
261: public String[] getLastLines(int linenumber) {
262: return getLastLines(fileName, linenumber);
263: }
264:
265: /**
266: * Return the last lines of the default log file.
267: * @param linenumber is the number of line which must be retrieved.
268: * @return a string array which matches to the last line.
269: */
270: public String[] getFirstLines(int linenumber) {
271: return getFirstLines(fileName, linenumber);
272: }
273:
274: /**
275: * Return the first lines of the specified log file.
276: * @param linenumber is the number of line which must be retrieved.
277: * @param filename is the log file name
278: * @return a string array which matches to the last line.
279: */
280: public String[] getFirstLines(String filename, int linenumber) {
281: if (filename == null || linenumber == 0)
282: return (null);
283: Vector v = new Vector(linenumber);
284: try {
285: FileReader fr = new FileReader(filename);
286: LineNumberReader lnr = new LineNumberReader(fr);
287: String str = lnr.readLine();
288: int i = 0;
289: while (i < linenumber && str != null) {
290: v.addElement(str);
291: str = lnr.readLine();
292: i++;
293: }
294: int size = v.size();
295: String[] res = new String[size];
296: for (int j = 0; j < size; j++) {
297: res[j] = (String) v.elementAt(j);
298: }
299: return res;
300: } catch (Exception e) {
301: e.printStackTrace();
302: // fail(e.getMessage());
303: return null;
304: }
305: }
306:
307: /**
308: * Return the last lines of the specified log file.
309: * @param linenumber is the number of line which must be retrieved.
310: * @param filename is the log file name
311: * @return a string array which matches to the last line.
312: */
313: public String[] getLastLines(String filename, int linenumber) {
314: if (filename == null || linenumber == 0)
315: return (null);
316: Vector v = new Vector(linenumber);
317: try {
318: FileReader fr = new FileReader(filename);
319: LineNumberReader lnr = new LineNumberReader(fr);
320: String str = lnr.readLine();
321: int i = 0;
322: while (str != null) {
323: v.addElement(str);
324: str = lnr.readLine();
325: i++;
326: }
327: int size = v.size();
328: String[] res = new String[size];
329: for (int j = 0; j < size; j++) {
330: res[j] = (String) v.elementAt(size - 1 - j);
331: }
332: return res;
333: } catch (Exception e) {
334: e.printStackTrace();
335: // fail(e.getMessage());
336: return null;
337: }
338: }
339:
340: /**
341: * It removes all handlers of the root logger.
342: */
343: public void quietRootLogger() {
344: TopicalLogger l = (TopicalLogger) lf.getLogger("root");
345: Handler[] hcs = l.getHandler();
346: Handler hc = hf.createHandler("handlerOfroot", "file");
347: hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, "rootLogger.log");
348: hc.setAttribute(Handler.PATTERN_ATTRIBUTE, "%m%n");
349: hc.setAttribute("activation", lf);
350: try {
351: l.addHandler(hc);
352: for (int i = 0; i < hcs.length; i++) {
353: l.removeHandler(hcs[i]);
354: }
355: } catch (Exception e) {
356: fail("error" + e.getMessage());
357: }
358: l = null;
359: }
360:
361: static boolean debug = Boolean.valueOf(
362: System.getProperty("monolog.test.debug")).booleanValue();
363:
364: /**
365: * This methods is used to debug this helper.
366: */
367: protected void debug(String m) {
368: if (debug)
369: System.out.println(m);
370: }
371:
372: private int next(int i, int max) {
373: return (i + 1 == max ? 0 : i + 1);
374: }
375: }
|