KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > output > JUnitAntLogger


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit.output;
21
22 import java.io.File JavaDoc;
23 import org.apache.tools.ant.module.spi.AntEvent;
24 import org.apache.tools.ant.module.spi.AntLogger;
25 import org.apache.tools.ant.module.spi.AntSession;
26 import org.apache.tools.ant.module.spi.TaskStructure;
27 import org.netbeans.modules.junit.output.antutils.AntProject;
28 import org.netbeans.modules.junit.output.antutils.TestCounter;
29
30 /**
31  * Ant logger interested in task "junit",
32  * dispatching events to instances of the {@link JUnitOutputReader} class.
33  * There is one <code>JUnitOutputReader</code> instance created per each
34  * Ant session.
35  *
36  * @see JUnitOutputReader
37  * @see Report
38  * @author Marian Petras
39  */

40 public final class JUnitAntLogger extends AntLogger {
41     
42     /** levels of interest for logging (info, warning, error, ...) */
43     private static final int[] LEVELS_OF_INTEREST = {
44         AntEvent.LOG_INFO,
45         AntEvent.LOG_WARN, //test failures
46
AntEvent.LOG_VERBOSE
47     };
48     
49     public static final String JavaDoc TASK_JAVA = "java"; //NOI18N
50
public static final String JavaDoc TASK_JUNIT = "junit"; //NOI18N
51
private static final String JavaDoc[] INTERESTING_TASKS = {TASK_JAVA, TASK_JUNIT};
52     private static final String JavaDoc ANT_TEST_RUNNER_CLASS_NAME =
53             "org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner";//NOI18N
54
private static final String JavaDoc XML_FORMATTER_CLASS_NAME =
55             "org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter";//NOI18N
56

57     /** default constructor for lookup */
58     public JUnitAntLogger() { }
59     
60     public boolean interestedInSession(AntSession session) {
61         return true;
62     }
63     
64     public String JavaDoc[] interestedInTargets(AntSession session) {
65         return AntLogger.ALL_TARGETS;
66     }
67     
68     public String JavaDoc[] interestedInTasks(AntSession session) {
69         return INTERESTING_TASKS;
70     }
71     
72     /**
73      * Detects type of the Ant task currently running.
74      *
75      * @param event event produced by the currently running Ant session
76      * @return {@code TaskType.TEST_TASK} if the task is a JUnit test task,
77      * {@code TaskType.DEBUGGING_TEST_TASK} if the task is a JUnit
78      * test task running in debugging mode,
79      * {@code TaskType.OTHER_TASK} if the task is not a JUnit test
80      * task;
81      * or {@code null} if no Ant task is currently running
82      */

83     private static TaskType detectTaskType(AntEvent event) {
84         final String JavaDoc taskName = event.getTaskName();
85         
86         if (taskName == null) {
87             return null;
88         }
89         
90         if (taskName.equals(TASK_JUNIT)) {
91             return TaskType.TEST_TASK;
92         }
93         
94         if (taskName.equals(TASK_JAVA)) {
95             TaskStructure taskStructure = event.getTaskStructure();
96
97             String JavaDoc className = taskStructure.getAttribute("classname"); //NOI18N
98
if (className == null) {
99                 return TaskType.OTHER_TASK;
100             }
101             
102             className = event.evaluate(className);
103             if (className.equals("junit.textui.TestRunner") //NOI18N
104
|| className.equals(
105     "org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner")) { //NOI18N
106
TaskStructure[] nestedElems = taskStructure.getChildren();
107                 for (TaskStructure ts : nestedElems) {
108                     if (ts.getName().equals("jvmarg")) { //NOI18N
109
String JavaDoc value = ts.getAttribute("value"); //NOI18N
110
if ((value != null)
111                                 && event.evaluate(value)
112                                    .equals("-Xdebug")) { //NOI18N
113
return TaskType.DEBUGGING_TEST_TASK;
114                         }
115                     }
116                 }
117                 return TaskType.TEST_TASK;
118             }
119             
120             return TaskType.OTHER_TASK;
121         }
122         
123         assert false : "Unhandled task name"; //NOI18N
124
return TaskType.OTHER_TASK;
125     }
126     
127     /**
128      * Tells whether the given task type is a test task type or not.
129      *
130      * @param taskType taskType to be checked; may be {@code null}
131      * @return {@code true} if the given task type marks a test task;
132      * {@code false} otherwise
133      */

134     private static boolean isTestTaskType(TaskType taskType) {
135         return (taskType != null) && (taskType != TaskType.OTHER_TASK);
136     }
137     
138     public boolean interestedInScript(File JavaDoc script, AntSession session) {
139         return true;
140     }
141     
142     public int[] interestedInLogLevels(AntSession session) {
143         return LEVELS_OF_INTEREST;
144     }
145     
146     /**
147      */

148     public void messageLogged(final AntEvent event) {
149         if (isTestTaskRunning(event)) {
150             if (event.getLogLevel() != AntEvent.LOG_VERBOSE) {
151                 getOutputReader(event).messageLogged(event);
152             } else {
153                 /* verbose messages are logged no matter which task produced them */
154                 getOutputReader(event).verboseMessageLogged(event);
155             }
156         }
157     }
158     
159     /**
160      */

161     private boolean isTestTaskRunning(AntEvent event) {
162         return isTestTaskType(
163                 getSessionInfo(event.getSession()).currentTaskType);
164     }
165     
166     /**
167      */

168     public void taskStarted(final AntEvent event) {
169         TaskType taskType = detectTaskType(event);
170         if (isTestTaskType(taskType)) {
171             AntSessionInfo sessionInfo = getSessionInfo(event.getSession());
172             assert !isTestTaskType(sessionInfo.currentTaskType);
173             sessionInfo.timeOfTestTaskStart = System.currentTimeMillis();
174             sessionInfo.currentTaskType = taskType;
175             if (sessionInfo.sessionType == null) {
176                 sessionInfo.sessionType = taskType;
177             }
178             final int testClassCount = TestCounter.getTestClassCount(event);
179             final boolean hasXmlOutput = hasXmlOutput(event);
180             getOutputReader(event).testTaskStarted(testClassCount, hasXmlOutput);
181         }
182     }
183     
184     /**
185      */

186     public void taskFinished(final AntEvent event) {
187         AntSessionInfo sessionInfo = getSessionInfo(event.getSession());
188         if (isTestTaskType(sessionInfo.currentTaskType)) {
189             getOutputReader(event).testTaskFinished();
190             sessionInfo.currentTaskType = null;
191         }
192         
193     }
194     
195     /**
196      */

197     public void buildFinished(final AntEvent event) {
198         AntSession session = event.getSession();
199         AntSessionInfo sessionInfo = getSessionInfo(session);
200
201         if (isTestTaskType(sessionInfo.sessionType)) {
202             getOutputReader(event).buildFinished(event);
203         }
204         
205         session.putCustomData(this, null); //forget AntSessionInfo
206
}
207     
208     /**
209      * Retrieve existing or creates a new reader for the given session.
210      *
211      * @param session session to return a reader for
212      * @return output reader for the session
213      */

214     private JUnitOutputReader getOutputReader(final AntEvent event) {
215         assert isTestTaskType(getSessionInfo(event.getSession()).sessionType);
216         
217         final AntSession session = event.getSession();
218         final AntSessionInfo sessionInfo = getSessionInfo(session);
219         JUnitOutputReader outputReader = sessionInfo.outputReader;
220         if (outputReader == null) {
221             outputReader = new JUnitOutputReader(
222                                         session,
223                                         sessionInfo.sessionType,
224                                         sessionInfo.getTimeOfTestTaskStart());
225             sessionInfo.outputReader = outputReader;
226         }
227         return outputReader;
228     }
229     
230     /**
231      */

232     private AntSessionInfo getSessionInfo(final AntSession session) {
233         Object JavaDoc o = session.getCustomData(this);
234         assert (o == null) || (o instanceof AntSessionInfo);
235         
236         AntSessionInfo sessionInfo;
237         if (o != null) {
238             sessionInfo = (AntSessionInfo) o;
239         } else {
240             sessionInfo = new AntSessionInfo();
241             session.putCustomData(this, sessionInfo);
242         }
243         return sessionInfo;
244     }
245     
246     /**
247      * Finds whether the test report will be generated in XML format.
248      */

249     private static boolean hasXmlOutput(AntEvent event) {
250         final String JavaDoc taskName = event.getTaskName();
251         if (taskName.equals(TASK_JUNIT)) {
252             return hasXmlOutputJunit(event);
253         } else if (taskName.equals(TASK_JAVA)) {
254             return hasXmlOutputJava(event);
255         } else {
256             assert false;
257             return false;
258         }
259     }
260     
261     /**
262      * Finds whether the test report will be generated in XML format.
263      */

264     private static boolean hasXmlOutputJunit(AntEvent event) {
265         TaskStructure taskStruct = event.getTaskStructure();
266         for (TaskStructure child : taskStruct.getChildren()) {
267             String JavaDoc childName = child.getName();
268             if (childName.equals("formatter")) { //NOI18N
269
String JavaDoc type = child.getAttribute("type"); //NOI18N
270
String JavaDoc usefile = child.getAttribute("usefile"); //NOI18N
271
if ((type != null) && type.equals("xml") //NOI18N
272
&& (usefile != null) && !AntProject.toBoolean(usefile)) {
273                     String JavaDoc ifPropName = child.getAttribute("if"); //NOI18N
274
String JavaDoc unlessPropName =child.getAttribute("unless");//NOI18N
275

276                     if ((ifPropName == null
277                                 || event.getProperty(ifPropName) != null)
278                         && (unlessPropName == null
279                                 || event.getProperty(unlessPropName) == null)) {
280                         return true;
281                     }
282                 }
283             }
284         }
285         return false;
286     }
287     
288     /**
289      * Finds whether the test report will be generated in XML format.
290      */

291     private static boolean hasXmlOutputJava(AntEvent event) {
292         TaskStructure taskStruct = event.getTaskStructure();
293         
294         String JavaDoc classname = taskStruct.getAttribute("classname"); //NOI18N
295
if ((classname == null) ||
296                 !event.evaluate(classname).equals(ANT_TEST_RUNNER_CLASS_NAME)) {
297             return false;
298         }
299         
300         for (TaskStructure child : taskStruct.getChildren()) {
301             String JavaDoc childName = child.getName();
302             if (childName.equals("arg")) { //NOI18N
303
String JavaDoc argValue = child.getAttribute("value"); //NOI18N
304
if (argValue == null) {
305                     continue;
306                 }
307                 argValue = event.evaluate(argValue);
308                 if (argValue.startsWith("formatter=")) { //NOI18N
309
int clsNameStartIndex = "formatter=".length(); //NOI18N
310
if ((argValue.indexOf(',', clsNameStartIndex) == -1)
311                             && argValue.substring(clsNameStartIndex)
312                                .equals(XML_FORMATTER_CLASS_NAME)) {
313                         return true;
314                     }
315                 }
316             }
317         }
318         return false;
319     }
320     
321 }
322
Popular Tags