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.test.umllib.testcases;
043:
044: import java.lang.reflect.InvocationTargetException;
045: import java.lang.reflect.Method;
046: import java.lang.reflect.Modifier;
047:
048: /**
049: * ported from qa.jse.MutiTestCase
050: * @author sp153251
051: */
052: public abstract class UMLMultiTestCase extends UMLTestCase {
053:
054: private int _instanceCount = 1;
055: private Throwable err = null;
056:
057: /**
058: * Creates a new instance of UMLMultiTestCase
059: * @param name
060: */
061: public UMLMultiTestCase(String name) {
062: super (name);
063: }
064:
065: public UMLMultiTestCase() {
066: super (null);
067: setName(shortName(this .getClass().getName()));
068: }
069:
070: /**
071: *
072: * @throws java.lang.Throwable
073: */
074: protected void runTest() throws Throwable {
075: System.out.println("MultiTestCase:runTest " + getName());
076: execute();
077: }
078:
079: /**
080: *
081: * @throws java.lang.Throwable
082: */
083: public void runBare() throws Throwable {
084: if (err != null) {
085: throw (err);
086: } else {
087: super .runBare();
088: }
089: }
090:
091: //Safe preparation for testing
092: /**
093: *
094: * @return
095: */
096: final boolean isPrepared() {
097: boolean result = false;
098: try {
099: prepare();
100: result = true;
101: } catch (Throwable e) {
102: err = e;
103: System.out
104: .println("Exception occured while preparing for test "
105: + getName() + ": " + e.toString());
106: e.printStackTrace();
107: }
108: return result;
109: }
110:
111: final void cleanit() {
112: try {
113: cleanup();
114: } catch (Throwable e) {
115: err = e;
116: System.out
117: .println("Exception occured while cleaning after test "
118: + getName() + ": " + e.toString());
119: e.printStackTrace();
120: }
121: }
122:
123: /**
124: *
125: * @return
126: */
127: final boolean gotFailed() {
128: return err != null;
129: }
130:
131: /**
132: *
133: * @return
134: */
135: final UMLMultiTestCase internalCreate() {
136: UMLMultiTestCase testcase = null;
137: try {
138: testcase = create();
139: } catch (Throwable e) {
140: err = e;
141: System.out
142: .println("Exception occured while creating MultiTestCase: "
143: + e.toString());
144: e.printStackTrace();
145: }
146: return testcase;
147: }
148:
149: //Methods
150: public void prepare() {
151: }
152:
153: public void cleanup() {
154:
155: }
156:
157: public abstract void execute();
158:
159: //Default implementation of MultiTestCreator interface
160: /**
161: *
162: * @return
163: */
164: public UMLMultiTestCase create() {
165: if (_instanceCount > 0) {
166: _instanceCount--;
167: return this ;
168: }
169: return null;
170: }
171:
172: //Utils
173: /**
174: *
175: * @param longName
176: * @return
177: */
178: static String shortName(String longName) {
179: String shortName = longName;
180: char[] delims = { '.', '$' };
181: int pos;
182: for (char ch : delims) {
183: pos = shortName.lastIndexOf(ch);
184: if (pos >= 0)
185: shortName = shortName.substring(pos + 1);
186: }
187: return shortName;
188: }
189: }
|