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.junit;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.io.PrintStream;
048: import java.util.ResourceBundle;
049: import java.util.logging.Level;
050: import java.util.logging.LogRecord;
051: import java.util.logging.Logger;
052: import junit.framework.AssertionFailedError;
053: import junit.framework.TestFailure;
054: import junit.framework.TestResult;
055:
056: /** Checks that we can do proper logging.
057: *
058: * @author Jaroslav Tulach
059: */
060: public class AssertInstancesTest extends NbTestCase {
061: private static Object hold;
062:
063: private Logger LOG;
064:
065: public AssertInstancesTest(String testName) {
066: super (testName);
067: }
068:
069: public static NbTestSuite suite() {
070: NbTestSuite suite = new NbTestSuite(AssertInstancesTest.class);
071: return suite;
072: }
073:
074: @Override
075: protected void setUp() throws Exception {
076: LOG = Logger.getLogger("test." + getName());
077: }
078:
079: public void testCannotGC() throws Throwable {
080: String s = new String("Ahoj");
081: hold = s;
082:
083: Log.enableInstances(LOG, null, Level.FINEST);
084: LOG.log(Level.FINE, "Text", s);
085:
086: try {
087: Log.assertInstances("Cannot GC");
088: } catch (AssertionFailedError ex) {
089: // ok
090: return;
091: }
092:
093: fail("The unreleased reference shall be spotted");
094: }
095:
096: public void testCannotGCWithNameOfMessages() throws Throwable {
097: String s = new String("Ahoj");
098: hold = s;
099:
100: Log.enableInstances(LOG, "Text", Level.FINEST);
101: LOG.log(Level.FINE, "Text", s);
102:
103: try {
104: Log.assertInstances("Cannot GC");
105: } catch (AssertionFailedError ex) {
106: // ok
107: return;
108: }
109:
110: fail("The unreleased reference shall be spotted");
111: }
112:
113: public void testCannotGCButOKAsNotTrackedMessage() throws Throwable {
114: String s = new String("Ahoj");
115: hold = s;
116:
117: Log.enableInstances(LOG, "StrangeMessages", Level.FINEST);
118: LOG.log(Level.FINE, "Text", s);
119: LOG.log(Level.FINE, "StrangeMessages", new String(
120: "some not hold instance"));
121:
122: Log.assertInstances("Can GC because the object is not tracked");
123: }
124:
125: public void testCanGC() throws Throwable {
126: String s = new String("Ahoj");
127:
128: Log.enableInstances(LOG, null, Level.FINEST);
129: LOG.log(Level.FINE, "Text", s);
130:
131: s = null;
132:
133: Log.assertInstances("Can GC without problems");
134: }
135:
136: public void testFailIfNoMessage() throws Throwable {
137: String s = new String("Ahoj");
138: hold = s;
139:
140: Log.enableInstances(LOG, "StrangeMessages", Level.FINEST);
141:
142: try {
143: Log.assertInstances("Fails as there is no message");
144: } catch (AssertionFailedError ex) {
145: return;
146: }
147:
148: fail("Shall fails as there is no logged object");
149: }
150:
151: }
|