LoggingUtil.java revision 93102446a7b7c3d17888064b4e2e4e5cb534e6d0
1/*
2 * Copyright 2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.test;
17
18import junit.framework.TestCase;
19import junit.framework.TestSuite;
20
21/**
22 * Provides facilities to log the start and end of a test run.
23 *
24 * May want to refactor this and create two subclasses: TestCaseLogger
25 * and TestSuiteLogger.
26 *
27 * @version $Revision: 88 $ - $Date: 2007-10-27 20:18:11 -0400 (Sat, 27 Oct 2007) $
28 *
29 * @author Chris Mair
30 */
31public final class LoggingUtil {
32
33    private static final String TEST_CASE_SEPARATOR = "---------------";
34    private static final String TEST_SUITE_SEPARATOR = "####################";
35
36    private String testTitle;
37    private String separator;
38    private long startTime;
39
40
41    //-------------------------------------------------------------------------
42    // General-purpose API to log messages
43    //-------------------------------------------------------------------------
44
45    /**
46     * Log the specified message from the caller object.
47     * @param caller the calling object
48     * @param message the message to log
49     */
50    public static void log(Object caller, Object message) {
51
52        String classNameNoPackage = getClassName(caller);
53        String messageStr = (message==null) ? "null" : message.toString();
54        String formattedMessage = "[" + classNameNoPackage + "] " + messageStr;
55        writeLogMessage(formattedMessage);
56    }
57
58
59    //-------------------------------------------------------------------------
60    // Factory Methods to get instance for TestCase or TestSuite
61    //-------------------------------------------------------------------------
62
63    /**
64     * Return a LoggingUtil instance suitable for logging TestCase start and end
65     * @param testCase the TestCase
66     * @return a LoggingUtil
67     */
68    public static LoggingUtil getTestCaseLogger(TestCase testCase) {
69
70        String title = getClassName(testCase) + "." + testCase.getName();
71        return new LoggingUtil(title, TEST_CASE_SEPARATOR);
72    }
73
74
75    /**
76     * Return a LoggingUtil instance suitable for logging TestSuite start and end
77     * @param testSuite the TestSuite
78     * @return a LoggingUtil
79     */
80    public static LoggingUtil getTestSuiteLogger(TestSuite testCase) {
81
82        String title = "SUITE " + getClassName(testCase);
83        return new LoggingUtil(title, TEST_SUITE_SEPARATOR);
84    }
85
86
87    /**
88     * Constructor. Private to force access through the factory method(s)
89     */
90    private LoggingUtil(String title, String separator) {
91        this.startTime = System.currentTimeMillis();
92        this.testTitle = title;
93        this.separator = separator;
94    }
95
96
97    /**
98     * Write out the the name of the test class and test name to the log
99     */
100    public void logStartOfTest() {
101
102        writeLogMessage(separator + " [ START: " + testTitle + " ] " + separator);
103    }
104
105
106    /**
107     * Write out the the name of the test class and test name to the log
108     */
109    public void logEndOfTest() {
110
111        long elapsedTime = System.currentTimeMillis() - startTime;
112        writeLogMessage(separator + " [ END: "
113            + testTitle
114            + "   Time=" + elapsedTime
115            + "ms ] "+ separator + "\n");
116    }
117
118
119    /**
120     * Return the name of the class for the specified object, stripping off the package name
121     * @return the name of the class, stripping off the package name
122     */
123    private static String getClassName(Object object) {
124
125        // If it's already a class, then use as is
126        Class theClass = (object instanceof Class) ? ((Class)object) : object.getClass();
127        String className =  theClass.getName();
128
129        int index = className.lastIndexOf(".");
130        if (index != -1) {
131            className = className.substring(index+1);
132        }
133        return className;
134    }
135
136
137    /**
138     * Write the specified message out to the log
139     * @param message the message to write
140     */
141    private static void writeLogMessage(String message) {
142        // Don't want to use Trace -- it requires initialization of the system configuration
143        //Trace.trace(message);
144        System.out.println(message);
145    }
146
147}
148