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.openide;
043:
044: import junit.framework.*;
045: import org.netbeans.junit.*;
046:
047: /** Test for general ErrorManager functionality.
048: *
049: * @author Jaroslav Tulach
050: */
051: public class ErrorManagerTest extends NbTestCase {
052:
053: public ErrorManagerTest(java.lang.String testName) {
054: super (testName);
055: }
056:
057: public static Test suite() {
058: TestSuite suite = new NbTestSuite(ErrorManagerTest.class);
059:
060: return suite;
061: }
062:
063: protected void setUp() {
064: System.setProperty("org.openide.util.Lookup",
065: "org.openide.ErrorManagerTest$Lkp");
066: assertNotNull("ErrManager has to be in lookup",
067: org.openide.util.Lookup.getDefault().lookup(
068: ErrManager.class));
069: }
070:
071: /** Test of getDefault method, of class org.openide.ErrorManager. */
072: public void testGetDefault() {
073: assertNotNull("There has to be a manager", ErrorManager
074: .getDefault());
075: }
076:
077: /** Test of notify method, of class org.openide.ErrorManager. */
078: public void testNotify() {
079: Throwable t = new Throwable();
080: ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, t);
081: ErrManager.get().assertNotify(ErrManager.INFORMATIONAL, t);
082: t = new Throwable();
083: ErrorManager.getDefault().notify(t);
084: ErrManager.get().assertNotify(ErrManager.UNKNOWN, t);
085: }
086:
087: /** Test of log method, of class org.openide.ErrorManager. */
088: public void testLog() {
089: ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
090: "A text");
091: ErrManager.get()
092: .assertLog(ErrorManager.INFORMATIONAL, "A text");
093: ErrorManager.getDefault().log("Another text");
094: ErrManager.get().assertLog(ErrorManager.INFORMATIONAL,
095: "Another text");
096: }
097:
098: /** Test of isLoggable method, of class org.openide.ErrorManager. */
099: public void testIsLoggable() {
100: ErrorManager.getDefault()
101: .isLoggable(ErrorManager.INFORMATIONAL);
102: }
103:
104: /** Test of annotate method, of class org.openide.ErrorManager. */
105: public void testReturnValues() {
106: Throwable t = new Throwable();
107: Throwable value = ErrorManager.getDefault().annotate(t,
108: ErrorManager.INFORMATIONAL, null, null, null, null);
109: assertEquals("Annotate must return the same exception", t,
110: value);
111:
112: value = ErrorManager.getDefault().copyAnnotation(t,
113: new Throwable());
114: assertEquals("copyAnnotation must return the same exception",
115: t, value);
116:
117: value = ErrorManager.getDefault().attachAnnotations(t,
118: new ErrorManager.Annotation[0]);
119: assertEquals(
120: "attachAnnotations must return the same exception", t,
121: value);
122:
123: }
124:
125: public void testGetInstanceIsInfluencedByChangesOfErrorManagersWeDelegateTo() {
126: Lkp.turn(false);
127: try {
128: ErrorManager man = ErrorManager.getDefault().getInstance(
129: "hi");
130: man.log("Anything");
131: ErrManager.get().assertLog(-1, null); // no logging because we are disabled
132: Lkp.turn(true);
133: man.log("Something");
134: ErrManager.assertLog(ErrorManager.INFORMATIONAL,
135: "Something");
136: } finally {
137: Lkp.turn(true);
138: }
139: }
140:
141: //
142: // Our fake lookup
143: //
144: public static final class Lkp extends
145: org.openide.util.lookup.AbstractLookup {
146: private ErrManager err = new ErrManager();
147: private org.openide.util.lookup.InstanceContent ic;
148:
149: public Lkp() {
150: this (new org.openide.util.lookup.InstanceContent());
151: }
152:
153: private Lkp(org.openide.util.lookup.InstanceContent ic) {
154: super (ic);
155: ic.add(err);
156: this .ic = ic;
157: }
158:
159: public static void turn(boolean on) {
160: Lkp lkp = (Lkp) org.openide.util.Lookup.getDefault();
161: if (on) {
162: lkp.ic.add(lkp.err);
163: } else {
164: lkp.ic.remove(lkp.err);
165: }
166: }
167: }
168:
169: //
170: // Manager to delegate to
171: //
172: public static final class ErrManager extends
173: org.openide.ErrorManager {
174: public static final StringBuffer messages = new StringBuffer();
175:
176: private String prefix;
177:
178: public ErrManager() {
179: this (null);
180: }
181:
182: public ErrManager(String prefix) {
183: this .prefix = prefix;
184: }
185:
186: public static ErrManager get() {
187: return (ErrManager) org.openide.util.Lookup.getDefault()
188: .lookup(ErrManager.class);
189: }
190:
191: public Throwable annotate(Throwable t, int severity,
192: String message, String localizedMessage,
193: Throwable stackTrace, java.util.Date date) {
194: return t;
195: }
196:
197: public Throwable attachAnnotations(Throwable t,
198: org.openide.ErrorManager.Annotation[] arr) {
199: return t;
200: }
201:
202: public org.openide.ErrorManager.Annotation[] findAnnotations(
203: Throwable t) {
204: return null;
205: }
206:
207: public org.openide.ErrorManager getInstance(String name) {
208: if (name.startsWith("org.netbeans.core.AutomountSupport")
209: || name
210: .startsWith("org.openide.loaders.FolderList")
211: || name
212: .startsWith("org.openide.loaders.FolderInstance")) {
213: return new ErrManager('[' + name + ']');
214: } else {
215: // either new non-logging or myself if I am non-logging
216: return new ErrManager();
217: }
218: }
219:
220: public void log(int severity, String s) {
221: lastSeverity = severity;
222: lastText = s;
223: }
224:
225: public void notify(int severity, Throwable t) {
226: lastThrowable = t;
227: lastSeverity = severity;
228: }
229:
230: private static int lastSeverity;
231: private static Throwable lastThrowable;
232: private static String lastText;
233:
234: public static void assertNotify(int sev, Throwable t) {
235: assertEquals("Severity is same", sev, lastSeverity);
236: assertSame("Throwable is the same", t, lastThrowable);
237: lastThrowable = null;
238: lastSeverity = -1;
239: }
240:
241: public static void assertLog(int sev, String t) {
242: assertEquals("Severity is same", sev, lastSeverity);
243: assertEquals("Text is the same", t, lastText);
244: lastText = null;
245: lastSeverity = -1;
246: }
247:
248: }
249:
250: }
|