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-2006 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.junit;
043:
044: import java.io.IOException;
045: import java.lang.ref.WeakReference;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import junit.framework.TestResult;
049: import junit.framework.AssertionFailedError;
050:
051: /** Regular test of the behaviour.
052: *
053: * @author jarda
054: */
055: public class NbTestCaseTest extends NbTestCase {
056:
057: public NbTestCaseTest(String testName) {
058: super (testName);
059: }
060:
061: protected void setUp() throws Exception {
062: }
063:
064: protected void tearDown() throws Exception {
065: }
066:
067: public void testNetBeansFullHackIsSet() throws Exception {
068: assertEquals("true", System.getProperty("netbeans.full.hack"));
069: }
070:
071: public void testJustRunTestCase() {
072: class Fail extends NbTestCase {
073: public Fail() {
074: super ("testFail");
075: }
076:
077: public void testFail() {
078: throw new IllegalStateException();
079: }
080: }
081:
082: Fail f = new Fail();
083:
084: TestResult res = new TestResult();
085: f.run(res);
086:
087: assertEquals("One error", 1, res.errorCount());
088:
089: }
090:
091: public void testLoggingUtil() throws Exception {
092: CharSequence seq = Log.enable("", Level.WARNING);
093:
094: Logger log = Logger.getLogger(getName());
095: log.log(Level.SEVERE, "Ahoj");
096: log.log(Level.FINE, "Jardo");
097:
098: String s = seq.toString();
099: if (s.indexOf("Ahoj") == -1) {
100: fail("There should be Ahoj\n" + s);
101: }
102: assertEquals("Not logged for FINE: " + s, -1, s
103: .indexOf("Jardo"));
104:
105: WeakReference<CharSequence> r = new WeakReference<CharSequence>(
106: seq);
107: seq = null;
108: assertGC("Sequence can go away", r);
109:
110: int len = Logger.getLogger("").getHandlers().length;
111:
112: log.log(Level.WARNING, "Go away");
113:
114: assertEquals("One logger is gone", len - 1, Logger
115: .getLogger("").getHandlers().length);
116: }
117:
118: public void testAssertGcPasses() {
119: Object o = new Object();
120: WeakReference<Object> wr = new WeakReference<Object>(o);
121:
122: o = null;
123: assertGC("The object is really not referenced", wr);
124: }
125:
126: static Object REF_O;
127:
128: public void testAssertGcFails() {
129: REF_O = new Object();
130: WeakReference<Object> wr = new WeakReference<Object>(REF_O);
131: try {
132: assertGC("The object is really not referenced", wr);
133: } catch (AssertionFailedError afe) {
134: assertTrue("Found the reference", afe.getMessage().indexOf(
135: "REF_O") >= 0);
136: return;
137: } finally {
138: REF_O = null;
139: }
140: fail("The assertion should fail");
141: }
142:
143: static Object REF_O2;
144:
145: static class Node {
146: Object next;
147:
148: Node(Object o) {
149: next = o;
150: }
151: }
152:
153: public void testAssertGcFailsWithTwoPaths() {
154: Object target = new Object();
155: REF_O = new Node(new Node(new Node(target)));
156: REF_O2 = new Node(new Node(new Node(target)));
157: WeakReference<Object> wr = new WeakReference<Object>(target);
158: target = null;
159:
160: String old = System.getProperty("assertgc.paths");
161: System.setProperty("assertgc.paths", "3");
162: try {
163: assertGC("The object is really not referenced", wr);
164: } catch (AssertionFailedError afe) {
165: assertTrue("Found the reference", afe.getMessage().indexOf(
166: "REF_O") >= 0);
167: assertTrue("Found the other reference", afe.getMessage()
168: .indexOf("REF_O2") >= 0);
169: return;
170: } finally {
171: REF_O = null;
172: REF_O2 = null;
173: if (old == null) {
174: System.clearProperty("assertgc.paths");
175: } else {
176: System.setProperty("assertgc.paths", old);
177: }
178: }
179: fail("The assertion should fail");
180: }
181:
182: public void testAssertGcFailsForUntracableObject() {
183: Object o = new Object();
184: WeakReference<Object> wr = new WeakReference<Object>(o);
185:
186: try {
187: assertGC("The object is really not referenced", wr);
188: } catch (AssertionFailedError afe) {
189: assertTrue("Found the reference", afe.getMessage().indexOf(
190: "Not found") >= 0);
191: return;
192: }
193: fail("The assertion should fail");
194: }
195:
196: private static String previous;
197:
198: public void testWorkDirLength() throws IOException {
199: System.setProperty("nbjunit.too.long", "10");
200:
201: getLog().println("Ahoj");
202: clearWorkDir();
203:
204: String s = getWorkDirPath();
205: if (s.equals(previous)) {
206: fail("Inside one testcase, one directory shall not be used multiple times: "
207: + previous + " now: " + s);
208: }
209: previous = s;
210:
211: if (s.length() > 200) {
212: fail("Too long (" + s.length() + "): " + s);
213: }
214: if (s.indexOf(".netbeans.") > 0) {
215: fail("package names shall be abbreviated: " + s);
216: }
217: if (s.indexOf("testWorkDir") > 0) {
218: fail("no testWorkDir: " + s);
219: }
220: }
221:
222: public void testWorkDirLengthsimilarname() throws IOException {
223: testWorkDirLength();
224: }
225: }
|