001: package org.testng;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007: import java.util.Vector;
008:
009: /**
010: * This class is used for test methods to log messages that will be
011: * included in the HTML reports generated by TestNG.
012: * <br>
013: * <br>
014: * <b>Implementation details.</b>
015: * <br>
016: * <br>
017: * The reporter keeps a combined output of strings (in m_output) and also
018: * a record of which method output which line. In order to do this, callers
019: * specify what the current method is with setCurrentMethod() and the
020: * Reporter maintaing a mapping of each method with a list of integers.
021: * These integers are indices in the combined output (avoids duplicating
022: * the output).
023: *
024: * Created on Nov 2, 2005
025: * @author cbeust
026: */
027: public class Reporter {
028: // when tests are run in parallel, each thread may be working with different
029: // 'current test result'. Also, this value should be inherited if the test code
030: // spawns its own thread.
031: private static ThreadLocal<ITestResult> m_currentTestResult = new InheritableThreadLocal<ITestResult>();
032:
033: /**
034: * All output logged in a sequential order.
035: */
036: private static List<String> m_output = new Vector<String>();
037:
038: private static Map<ITestResult, List<Integer>> m_methodOutputMap = new HashMap<ITestResult, List<Integer>>();
039:
040: public static void setCurrentTestResult(ITestResult m) {
041: m_currentTestResult.set(m);
042: }
043:
044: public static List<String> getOutput() {
045: return m_output;
046: }
047:
048: private static synchronized void log(String s, ITestResult m) {
049: // synchronization needed to ensure the line number and m_output are updated atomically
050: int n = getOutput().size();
051: List<Integer> lines = m_methodOutputMap.get(m);
052: if (lines == null) {
053: lines = new ArrayList<Integer>();
054: m_methodOutputMap.put(m, lines);
055: }
056: lines.add(n);
057: getOutput().add(s);
058: }
059:
060: /**
061: * Log the passed string to the HTML reports
062: * @param s The message to log
063: */
064: public static void log(String s) {
065: log(s, getCurrentTestResult());
066: }
067:
068: /**
069: * Log the passed string to the HTML reports if the current verbosity
070: * is equal or greater than the one passed in parameter. If logToStandardOut
071: * is true, the string will also be printed on standard out.
072: *
073: * @param s The message to log
074: * @param level The verbosity of this message
075: * @param logToStandardOut Whether to print this string on standard
076: * out too
077: */
078: public static void log(String s, int level, boolean logToStandardOut) {
079: if (TestRunner.getVerbose() >= level) {
080: log(s, getCurrentTestResult());
081: if (logToStandardOut) {
082: System.out.println(s);
083: }
084: }
085: }
086:
087: /**
088: * Log the passed string to the HTML reports. If logToStandardOut
089: * is true, the string will also be printed on standard out.
090: *
091: * @param s The message to log
092: * @param logToStandardOut Whether to print this string on standard
093: * out too
094: */
095: public static void log(String s, boolean logToStandardOut) {
096: log(s, getCurrentTestResult());
097: if (logToStandardOut) {
098: System.out.println(s);
099: }
100: }
101:
102: /**
103: * Log the passed string to the HTML reports if the current verbosity
104: * is equal or greater than the one passed in parameter
105: *
106: * @param s The message to log
107: * @param level The verbosity of this message
108: */
109: public static void log(String s, int level) {
110: if (TestRunner.getVerbose() >= level) {
111: log(s, getCurrentTestResult());
112: }
113: }
114:
115: private static ITestResult getCurrentTestResult() {
116: return m_currentTestResult.get();
117: }
118:
119: private static void ppp(String s) {
120: System.out.println("[Reporter] " + s);
121: }
122:
123: public static List<String> getOutput(ITestResult tr) {
124: List<String> result = new ArrayList<String>();
125: List<Integer> lines = m_methodOutputMap.get(tr);
126: if (lines != null) {
127: for (Integer n : lines) {
128: result.add(getOutput().get(n));
129: }
130: }
131:
132: return result;
133: }
134:
135: // public static void setCurrentOutput(List<String> tmr) {
136: // m_output = tmr;
137: //// m_localResult.set(tmr);
138: // }
139:
140: // public static List<String> getCurrentOutput() {
141: // return m_output;
142: //// List<String> result = (List<String>) m_localResult.get();
143: //// return result;
144: // }
145:
146: }
|