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.core;
043:
044: import java.awt.Dialog;
045: import java.awt.Frame;
046: import java.util.ResourceBundle;
047: import java.util.logging.Level;
048: import java.util.logging.LogRecord;
049: import java.util.logging.Logger;
050: import javax.swing.ImageIcon;
051: import javax.swing.JDialog;
052: import javax.swing.SwingUtilities;
053: import org.netbeans.core.startup.TopLogging;
054: import org.netbeans.junit.*;
055: import org.openide.DialogDescriptor;
056: import org.openide.DialogDisplayer;
057: import org.openide.ErrorManager;
058: import org.openide.NotifyDescriptor;
059: import org.openide.util.Exceptions;
060: import org.openide.util.NbBundle;
061: import org.openide.util.lookup.AbstractLookup;
062: import org.openide.util.lookup.InstanceContent;
063:
064: import org.openide.windows.WindowManager;
065:
066: /**
067: * Test NotifyExcPanel class.
068: *
069: * @author Stanislav Aubrecht
070: */
071: public class NotifyExceptionTest extends NbTestCase {
072: static {
073: System.setProperty("org.openide.util.Lookup", Lkp.class
074: .getName());
075: }
076:
077: public NotifyExceptionTest(String name) {
078: super (name);
079: }
080:
081: private static void waitEQ() throws Exception {
082: SwingUtilities.invokeAndWait(new Runnable() {
083: public void run() {
084: }
085: });
086: }
087:
088: protected void setUp() throws Exception {
089: clearWorkDir();
090: System.setProperty("netbeans.user", getWorkDirPath());
091: // initialize the logging
092: new TopLogging();
093:
094: DD.lastDescriptor = null;
095: DD.toReturn = null;
096:
097: System.getProperties().remove(
098: "netbeans.exception.alert.min.level");
099: System.getProperties().remove(
100: "netbeans.exception.report.min.level");
101:
102: NotifyExcPanel.cleanInstance();
103: }
104:
105: /**
106: * A simple test to ensure that error dialog window is not created modal
107: * until the MainWindow is visible.
108: */
109: public void testNoModalErrorDialog() throws Exception {
110: Frame mainWindow = WindowManager.getDefault().getMainWindow();
111: final JDialog modalDialog = new HiddenDialog(mainWindow, true);
112: DD.toReturn = modalDialog;
113:
114: Logger.global.log(Level.WARNING, "Something is wrong",
115: new NullPointerException("npe"));
116: waitEQ();
117: assertNotNull("Really returned", DD.lastDescriptor);
118: assertEquals("It is DialogDescriptor", DialogDescriptor.class,
119: DD.lastDescriptor.getClass());
120: DialogDescriptor dd = (DialogDescriptor) DD.lastDescriptor;
121: assertFalse("The request is for non-modal dialog", dd.isModal());
122: assertFalse("Main window is not visible", mainWindow
123: .isVisible());
124: }
125:
126: public void testExceptionWillGetTheLevelFromAnnoatation()
127: throws Exception {
128: NullPointerException npe = new NullPointerException("npe");
129: ErrorManager.getDefault().annotate(npe, ErrorManager.WARNING,
130: null, null, null, null);
131:
132: DD.toReturn = new HiddenDialog();
133: Exceptions.printStackTrace(npe);
134:
135: waitEQ();
136: assertNotNull("We are going to display a warning",
137: DD.lastDescriptor);
138:
139: }
140:
141: public void testDirectlyLoggingAnExceptionWithALocalizedMessageAndTheRightLevelShowsItInADialog()
142: throws Exception {
143: NullPointerException npe = new NullPointerException("npe");
144:
145: LogRecord rec = new LogRecord(OwnLevel.UI, "MSG_KEY");
146: rec.setThrown(npe);
147: ResourceBundle b = ResourceBundle
148: .getBundle("org/netbeans/core/NotifyExceptionBundle");
149: rec.setResourceBundle(b);
150: DD.toReturn = new HiddenDialog();
151: Logger.global.log(rec);
152: waitEQ();
153: assertNotNull("We are going to display a warning",
154: DD.lastDescriptor);
155: assertTrue("We want message: " + DD.lastDescriptor,
156: DD.lastDescriptor instanceof NotifyDescriptor.Message);
157: NotifyDescriptor.Message msg = (NotifyDescriptor.Message) DD.lastDescriptor;
158: assertEquals("Info msg", NotifyDescriptor.INFORMATION_MESSAGE,
159: msg.getMessageType());
160: assertEquals("Msg is localized", b.getString("MSG_KEY"), msg
161: .getMessage());
162: }
163:
164: public void testYesDialogShown() throws Exception {
165: Frame mainWindow = WindowManager.getDefault().getMainWindow();
166: final JDialog modalDialog = new HiddenDialog(mainWindow, true);
167: DD.toReturn = modalDialog;
168:
169: Logger l = Logger.getLogger(getName());
170: l.setLevel(Level.ALL);
171: System
172: .setProperty("netbeans.exception.report.min.level",
173: "200");
174: l.log(Level.CONFIG, "Something is wrong",
175: new NullPointerException("npe"));
176: waitEQ();
177: assertNotNull("Really returned", DD.lastDescriptor);
178: assertEquals("It is DialogDescriptor", DialogDescriptor.class,
179: DD.lastDescriptor.getClass());
180: DialogDescriptor dd = (DialogDescriptor) DD.lastDescriptor;
181: assertFalse("The request is for non-modal dialog", dd.isModal());
182: assertFalse("Main window is not visible", mainWindow
183: .isVisible());
184: }
185:
186: public void testNoDialogShownJustFlashing() throws Exception {
187: class MockFlashingIcon extends FlashingIcon {
188: public int cnt;
189:
190: public MockFlashingIcon() {
191: super (new ImageIcon());
192: }
193:
194: protected void onMouseClick() {
195: }
196:
197: protected void timeout() {
198: }
199:
200: public void startFlashing() {
201: cnt++;
202: }
203: }
204: MockFlashingIcon mock = new MockFlashingIcon();
205:
206: NotifyExcPanel.flasher = mock;
207:
208: Logger l = Logger.getLogger(getName());
209: l.setLevel(Level.ALL);
210: System.setProperty("netbeans.exception.alert.min.level", "200");
211: l.log(Level.CONFIG, "Something is wrong",
212: new NullPointerException("npe"));
213: waitEQ();
214: assertNull("Really returned", DD.lastDescriptor);
215:
216: assertEquals("Flasher flashing", 1, mock.cnt);
217: }
218:
219: private static final class OwnLevel extends Level {
220: public static final Level UI = new OwnLevel("UI", 1973);
221:
222: private OwnLevel(String n, int i) {
223: super (n, i);
224: }
225: }
226:
227: private static final class DD extends DialogDisplayer {
228: public static NotifyDescriptor lastDescriptor;
229: public static Object toReturn;
230:
231: public Object notify(NotifyDescriptor descriptor) {
232: Object t = toReturn;
233: toReturn = null;
234: assertNotNull("There is something to return", t);
235: lastDescriptor = descriptor;
236: return t;
237: }
238:
239: public Dialog createDialog(DialogDescriptor descriptor) {
240: return (Dialog) notify(descriptor);
241: }
242: } // end of DD
243:
244: public static final class Lkp extends AbstractLookup {
245: public static InstanceContent IC;
246:
247: public Lkp() {
248: this (new InstanceContent());
249: }
250:
251: private Lkp(InstanceContent ic) {
252: super (ic);
253: ic.add(new DD());
254: ic.add(new NbErrorManager());
255: }
256: }
257:
258: private static final class HiddenDialog extends JDialog {
259: private boolean v;
260:
261: public HiddenDialog() {
262: }
263:
264: public HiddenDialog(Frame p, boolean b) {
265: super (p, b);
266: }
267:
268: public void setVisible(boolean b) {
269: v = b;
270: }
271:
272: public boolean isVisible() {
273: return v;
274: }
275: }
276: }
|