KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > TestabilityResult


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit;
21
22 import java.util.MissingResourceException JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import org.openide.ErrorManager;
25 import org.openide.util.NbBundle;
26
27 /**
28  * Helper class representing reasons for skipping a class in the test
29  * generation process. The class enumerates known reasons, why a class may
30  * not be considered testable, allows to combine the reasons and provide
31  * human-readable representation of them.
32  */

33 final class TestabilityResult {
34     // bitfield of reasons for skipping a class
35
private long reason;
36
37     // reason constants
38
public static final TestabilityResult OK = new TestabilityResult(0);
39     public static final TestabilityResult PACKAGE_PRIVATE_CLASS = new TestabilityResult(1);
40     public static final TestabilityResult NO_TESTEABLE_METHODS = new TestabilityResult(2);
41     public static final TestabilityResult TEST_CLASS = new TestabilityResult(4);
42     public static final TestabilityResult ABSTRACT_CLASS = new TestabilityResult(8);
43     public static final TestabilityResult NONSTATIC_INNER_CLASS = new TestabilityResult(16);
44     public static final TestabilityResult EXCEPTION_CLASS = new TestabilityResult(32);
45     public static final TestabilityResult PRIVATE_CLASS = new TestabilityResult(64);
46
47
48     // bundle keys for reason descriptions
49
private static final String JavaDoc [] reasonBundleKeys = {
50         "TestabilityResult_PkgPrivate",
51         "TestabilityResult_NoTestableMethods",
52         "TestabilityResult_TestClass",
53         "TestabilityResult_AbstractClass",
54         "TestabilityResult_NonstaticInnerClass",
55         "TestabilityResult_ExceptionClass",
56         "TestabilityResult_Private"};
57
58     private TestabilityResult(long reason) {
59         this.reason = reason;
60     }
61
62     /**
63      * Combine two result reasons into a new one.
64      *
65      * The combination is the union
66      * of the failure reasons represented by the two results. Thus,
67      * if both are success (no failure), the combination is a success. If
68      * some of them is failed, the result is failed.
69      *
70      * @param lhs the first TestabilityResult
71      * @param rhs the second TestabilityResult
72      * @return a new TestabilityResult representing the combination of the two
73      * results
74      **/

75     public static TestabilityResult combine(TestabilityResult lhs, TestabilityResult rhs) {
76         return new TestabilityResult(lhs.reason | rhs.reason);
77     }
78
79     /**
80      * Returns true if the result is for a testable class.
81      * @return true or false
82      */

83     public boolean isTestable() {
84         return reason == 0;
85     }
86
87     /**
88      * Returns true if the result is for a non-testable class.
89      * @return true if the result is for a non-testable class.
90      */

91     public boolean isFailed() {
92         return reason != 0;
93     }
94
95     /**
96      * Returns a human-readable representation of the reason. If the reason
97      * is a combination of multiple reasons, they are separated with ",".
98      * @return String
99      */

100     public String JavaDoc getReason() {
101         return getReason(", ", ", "); //NOI18N
102
}
103
104     /**
105      * Returns {@link #getReason()}.
106      * @return String
107      */

108     public String JavaDoc toString() {
109         return getReason(", ", ", "); //NOI18N
110
}
111
112     /**
113      * Returns a human-readable representation of the reason. If the reason
114      * is a combination of multiple reasons, they are separated with
115      * {@code separ} except for the last reason, which is separated
116      * with {@code terminalSepar}
117      * <p>
118      * For example: getReason(", ", " or ") might return
119      * "abstract, package private or without testable methods".
120      *
121      * @return String
122      */

123     public String JavaDoc getReason(String JavaDoc separ, String JavaDoc terminalSepar) {
124         try {
125             ResourceBundle JavaDoc bundle = NbBundle.getBundle(TestCreator.class);
126             if (reason == 0) {
127                 return bundle.getString("TestabilityResult_OK"); //NOI18N
128
} else {
129                 String JavaDoc str = ""; //NOI18N
130
boolean lastPrep = true;
131                 for (long i = 0, r = reason; r > 0; r >>= 1, i++) {
132                     if ((r & 1) != 0) {
133                         if (str.length() > 0) {
134                             if (lastPrep) {
135                                 str = terminalSepar + str;
136                                 lastPrep = false;
137                             } else {
138                                 str = separ + str;
139                             }
140                         }
141                         str = bundle.getString(reasonBundleKeys[(int)i]) + str;
142                     }
143                 }
144                 return str;
145             }
146         } catch (MissingResourceException JavaDoc ex) {
147             ErrorManager.getDefault().notify(ex);
148             return "";
149         }
150     }
151     
152     /**
153      * Class for holding name of a skipped java class
154      * together with the reason why it was skipped.
155      */

156     static final class SkippedClass {
157         final String JavaDoc clsName;
158         final TestabilityResult reason;
159         SkippedClass(String JavaDoc clsName,
160                      TestabilityResult reason) {
161             this.clsName = clsName;
162             this.reason = reason;
163         }
164     }
165
166 }
167
Popular Tags