001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.junit;
043:
044: import java.util.MissingResourceException;
045: import java.util.ResourceBundle;
046: import org.openide.ErrorManager;
047: import org.openide.util.NbBundle;
048:
049: /**
050: * Helper class representing reasons for skipping a class in the test
051: * generation process. The class enumerates known reasons, why a class may
052: * not be considered testable, allows to combine the reasons and provide
053: * human-readable representation of them.
054: */
055: final class TestabilityResult {
056: // bitfield of reasons for skipping a class
057: private long reason;
058:
059: // reason constants
060: public static final TestabilityResult OK = new TestabilityResult(0);
061: public static final TestabilityResult PACKAGE_PRIVATE_CLASS = new TestabilityResult(
062: 1);
063: public static final TestabilityResult NO_TESTEABLE_METHODS = new TestabilityResult(
064: 2);
065: public static final TestabilityResult TEST_CLASS = new TestabilityResult(
066: 4);
067: public static final TestabilityResult ABSTRACT_CLASS = new TestabilityResult(
068: 8);
069: public static final TestabilityResult NONSTATIC_INNER_CLASS = new TestabilityResult(
070: 16);
071: public static final TestabilityResult EXCEPTION_CLASS = new TestabilityResult(
072: 32);
073: public static final TestabilityResult PRIVATE_CLASS = new TestabilityResult(
074: 64);
075:
076: // bundle keys for reason descriptions
077: private static final String[] reasonBundleKeys = {
078: "TestabilityResult_PkgPrivate",
079: "TestabilityResult_NoTestableMethods",
080: "TestabilityResult_TestClass",
081: "TestabilityResult_AbstractClass",
082: "TestabilityResult_NonstaticInnerClass",
083: "TestabilityResult_ExceptionClass",
084: "TestabilityResult_Private" };
085:
086: private TestabilityResult(long reason) {
087: this .reason = reason;
088: }
089:
090: /**
091: * Combine two result reasons into a new one.
092: *
093: * The combination is the union
094: * of the failure reasons represented by the two results. Thus,
095: * if both are success (no failure), the combination is a success. If
096: * some of them is failed, the result is failed.
097: *
098: * @param lhs the first TestabilityResult
099: * @param rhs the second TestabilityResult
100: * @return a new TestabilityResult representing the combination of the two
101: * results
102: **/
103: public static TestabilityResult combine(TestabilityResult lhs,
104: TestabilityResult rhs) {
105: return new TestabilityResult(lhs.reason | rhs.reason);
106: }
107:
108: /**
109: * Returns true if the result is for a testable class.
110: * @return true or false
111: */
112: public boolean isTestable() {
113: return reason == 0;
114: }
115:
116: /**
117: * Returns true if the result is for a non-testable class.
118: * @return true if the result is for a non-testable class.
119: */
120: public boolean isFailed() {
121: return reason != 0;
122: }
123:
124: /**
125: * Returns a human-readable representation of the reason. If the reason
126: * is a combination of multiple reasons, they are separated with ",".
127: * @return String
128: */
129: public String getReason() {
130: return getReason(", ", ", "); //NOI18N
131: }
132:
133: /**
134: * Returns {@link #getReason()}.
135: * @return String
136: */
137: public String toString() {
138: return getReason(", ", ", "); //NOI18N
139: }
140:
141: /**
142: * Returns a human-readable representation of the reason. If the reason
143: * is a combination of multiple reasons, they are separated with
144: * {@code separ} except for the last reason, which is separated
145: * with {@code terminalSepar}
146: * <p>
147: * For example: getReason(", ", " or ") might return
148: * "abstract, package private or without testable methods".
149: *
150: * @return String
151: */
152: public String getReason(String separ, String terminalSepar) {
153: try {
154: ResourceBundle bundle = NbBundle
155: .getBundle(TestCreator.class);
156: if (reason == 0) {
157: return bundle.getString("TestabilityResult_OK"); //NOI18N
158: } else {
159: String str = ""; //NOI18N
160: boolean lastPrep = true;
161: for (long i = 0, r = reason; r > 0; r >>= 1, i++) {
162: if ((r & 1) != 0) {
163: if (str.length() > 0) {
164: if (lastPrep) {
165: str = terminalSepar + str;
166: lastPrep = false;
167: } else {
168: str = separ + str;
169: }
170: }
171: str = bundle
172: .getString(reasonBundleKeys[(int) i])
173: + str;
174: }
175: }
176: return str;
177: }
178: } catch (MissingResourceException ex) {
179: ErrorManager.getDefault().notify(ex);
180: return "";
181: }
182: }
183:
184: /**
185: * Class for holding name of a skipped java class
186: * together with the reason why it was skipped.
187: */
188: static final class SkippedClass {
189: final String clsName;
190: final TestabilityResult reason;
191:
192: SkippedClass(String clsName, TestabilityResult reason) {
193: this.clsName = clsName;
194: this.reason = reason;
195: }
196: }
197:
198: }
|