KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > output > antutils > TestCounter


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.antutils;
21
22 import org.apache.tools.ant.module.spi.AntEvent;
23 import org.apache.tools.ant.module.spi.TaskStructure;
24 import org.netbeans.modules.junit.output.JUnitAntLogger;
25
26 /**
27  * Counts test classes to be executed by the current Ant test task.
28  *
29  * @author Marian Petras
30  */

31 public final class TestCounter {
32     
33     
34     // --------------- static members -----------------
35

36     
37     /**
38      * Counts test classes.
39      *
40      * @param event Ant event holding information about the current context
41      * of the Ant task
42      * @return (approximate) number of test classes that are going to be
43      * executed by the task
44      */

45     public static int getTestClassCount(AntEvent event) {
46         TestCounter counter = new TestCounter(event);
47         return counter.countTestClasses();
48     }
49     
50     
51     // ------------ non-static members ----------------
52

53     
54     /**
55      * Ant event holding information about the current context of the running
56      * Ant task.
57      */

58     private final AntEvent event;
59     
60     /**
61      * Creates a new instance of the test counter.
62      *
63      * @param event Ant event holding information about the current context
64      * of the Ant task
65      */

66     private TestCounter(AntEvent event) {
67         this.event = event;
68     }
69     
70     /**
71      * Counts test classes going to be executed by the current test task.
72      *
73      * @return (approximate) number of test classes that are going to be
74      * executed by the task
75      */

76     private int countTestClasses() {
77         final String JavaDoc taskName = event.getTaskName();
78         
79         if (taskName.equals(JUnitAntLogger.TASK_JUNIT)) {
80             return countTestClassesInJUnitTask();
81         } else if (taskName.equals(JUnitAntLogger.TASK_JAVA)) {
82             return countTestClassesInJavaTask();
83         }
84         
85         assert false : "Unhandled task name"; //NOI18N
86
return -1;
87     }
88     
89     /**
90      * Counts number of test classes that are going to be executed
91      * by the current {@code <junit>} task.
92      *
93      * @param event event produced by the currently running Ant session
94      * @return approximate number of test classes;
95      * or {@code -1} if the number is unknown
96      */

97     private int countTestClassesInJUnitTask() {
98         int count = 0;
99         
100         TaskStructure taskStruct = event.getTaskStructure();
101         for (TaskStructure child : taskStruct.getChildren()) {
102             String JavaDoc childName = child.getName();
103             if (childName.equals("test")) { //NOI18N
104
if (conditionsMet(child)) {
105                     count++;
106                 }
107                 continue;
108             }
109             if (childName.equals("batchtest")) { //NOI18N
110
if (conditionsMet(child)) {
111                     AntProject project = new AntProject(event);
112                     BatchTest batchTest = new BatchTest(project);
113                     batchTest.handleChildrenAndAttrs(child);
114                     int n = batchTest.countTestClasses();
115                     if (n > 0) {
116                         count += n;
117                     }
118                 }
119                 continue;
120             }
121         }
122         return count;
123     }
124     
125     /**
126      * Checks whether {@code if} and {@code unless} conditions of the given
127      * Ant XML element are met.
128      *
129      * @param struct Ant XML element to be probed
130      * @param event Ant event which allows evaluation of Ant variables
131      * @return {@code false} if there are conditions that are not met,
132      * {@code true} otherwise
133      */

134     private boolean conditionsMet(TaskStructure struct) {
135         String JavaDoc ifPropName = struct.getAttribute("if"); //NOI18N
136
String JavaDoc unlessPropName = struct.getAttribute("unless"); //NOI18N
137

138         if ((ifPropName != null)
139                 && (event.getProperty(ifPropName) == null)) {
140             return false;
141         }
142         if ((unlessPropName != null)
143                 && (event.getProperty(unlessPropName) != null)) {
144             return false;
145         }
146         return true;
147     }
148     
149     /**
150      * Counts number of test classes that are going to be executed
151      * by the current {@code <java>} task.
152      *
153      * @param event event produced by the currently running Ant session
154      * @return approximate number of test classes;
155      * or {@code -1} if the number is unknown
156      */

157     private int countTestClassesInJavaTask() {
158         return 1;
159     }
160     
161 }
162
Popular Tags