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.cnd.modelimpl.test;
043:
044: import java.io.File;
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.Collections;
048: import org.netbeans.junit.Manager;
049: import org.netbeans.modules.cnd.modelimpl.debug.DiagnosticExceptoins;
050: import org.netbeans.modules.cnd.test.BaseTestCase;
051:
052: /**
053: * IMPORTANT NOTE:
054: * If This class is not compiled with the notification about not resolved
055: * BaseTestCase class => cnd/core tests are not compiled
056: *
057: * To solve this problem compile or run tests for cnd/core
058: *
059: * If problems with NB JUnit see comment to @see BaseTestCase
060: */
061:
062: /**
063: * base class for modelimpl module tests
064: * @author Vladimir Voskresensky
065: */
066: public abstract class ModelImplBaseTestCase extends BaseTestCase {
067:
068: public static final String PROPERTY_DATA_PATH = "cnd.modelimpl.unit.data";
069: public static final String PROPERTY_GOLDEN_PATH = "cnd.modelimpl.unit.golden";
070: public static final String PROPERTY_WORK_PATH = "cnd.modelimpl.unit.workdir";
071:
072: private final Collection<Throwable> exceptions = Collections
073: .synchronizedList(new ArrayList<Throwable>());
074:
075: /**
076: * Creates a new instance of ModelImplBaseTestCase
077: */
078: public ModelImplBaseTestCase(String testName) {
079: super (testName);
080: }
081:
082: @Override
083: public String getWorkDirPath() {
084: String workDirPath = System.getProperty(PROPERTY_WORK_PATH); // NOI18N
085: if (workDirPath == null || workDirPath.length() == 0) {
086: return super .getWorkDirPath();
087: } else {
088: return workDirPath;
089: }
090: }
091:
092: @Override
093: public File getGoldenFile(String filename) {
094: String goldenDirPath = System.getProperty(PROPERTY_GOLDEN_PATH); // NOI18N
095: if (goldenDirPath == null || goldenDirPath.length() == 0) {
096: return super .getGoldenFile(filename);
097: } else {
098: return Manager.normalizeFile(new File(goldenDirPath,
099: filename));
100: }
101: }
102:
103: @Override
104: protected File getDataFile(String filename) {
105: String dataDirPath = System.getProperty(PROPERTY_DATA_PATH); // NOI18N
106: if (dataDirPath == null || dataDirPath.length() == 0) {
107: return super .getDataFile(filename);
108: } else {
109: return Manager
110: .normalizeFile(new File(dataDirPath, filename));
111: }
112: }
113:
114: @Override
115: protected void setUp() throws Exception {
116: super .setUp();
117: DiagnosticExceptoins.Hook hook = new DiagnosticExceptoins.Hook() {
118: public void exception(Throwable thr) {
119: thr.printStackTrace();
120: exceptions.add(thr);
121: }
122: };
123: DiagnosticExceptoins.setHook(hook);
124: }
125:
126: /**
127: * Registers an exception.
128: * The idea is to process an exception that occurs in main thread
129: * in the same way as exceptions that occur in code model threads
130: */
131: protected void registerException(Throwable thr) {
132: exceptions.add(thr);
133: }
134:
135: /** Asserts that no exceptions occur in code model threads */
136: protected void assertNoExceptions() throws Exception {
137: assertEmpty(exceptions);
138: }
139:
140: private void assertEmpty(Collection<Throwable> errors)
141: throws Exception {
142: // the idea here was to somehow make JUnit infrastructure
143: // display all caught exceptions;
144: // but I don't yet know how to;
145: // so for the time being we just throw 1-st one
146: if (!errors.isEmpty()) {
147: for (Throwable thr : errors) {
148: if (thr instanceof Exception) {
149: throw (Exception) thr;
150: } else if (thr instanceof Error) {
151: throw (Error) thr;
152: } else {
153: throw new Exception(thr);
154: }
155: }
156: }
157: }
158:
159: }
|