KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > NotifyExceptionTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.Frame JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.LogRecord JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import javax.swing.ImageIcon JavaDoc;
29 import javax.swing.JDialog JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import org.netbeans.core.startup.TopLogging;
32 import org.netbeans.junit.*;
33 import org.openide.DialogDescriptor;
34 import org.openide.DialogDisplayer;
35 import org.openide.ErrorManager;
36 import org.openide.NotifyDescriptor;
37 import org.openide.util.Exceptions;
38 import org.openide.util.NbBundle;
39 import org.openide.util.lookup.AbstractLookup;
40 import org.openide.util.lookup.InstanceContent;
41
42 import org.openide.windows.WindowManager;
43
44 /**
45  * Test NotifyExcPanel class.
46  *
47  * @author Stanislav Aubrecht
48  */

49 public class NotifyExceptionTest extends NbTestCase {
50     static {
51         System.setProperty("org.openide.util.Lookup", Lkp.class.getName());
52     }
53     
54     public NotifyExceptionTest(String JavaDoc name) {
55         super(name);
56     }
57
58     private static void waitEQ() throws Exception JavaDoc {
59         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
60             public void run() {
61             }
62         });
63     }
64     
65     protected void setUp() throws Exception JavaDoc {
66         clearWorkDir();
67         System.setProperty("netbeans.user", getWorkDirPath());
68         // initialize the logging
69
new TopLogging();
70
71         DD.lastDescriptor = null;
72         DD.toReturn = null;
73
74         System.getProperties().remove("netbeans.exception.alert.min.level");
75         System.getProperties().remove("netbeans.exception.report.min.level");
76
77         NotifyExcPanel.cleanInstance();
78     }
79     
80     /**
81      * A simple test to ensure that error dialog window is not created modal
82      * until the MainWindow is visible.
83      */

84     public void testNoModalErrorDialog() throws Exception JavaDoc {
85         Frame JavaDoc mainWindow = WindowManager.getDefault().getMainWindow();
86         final JDialog JavaDoc modalDialog = new HiddenDialog( mainWindow, true );
87         DD.toReturn = modalDialog;
88
89         Logger.global.log(Level.WARNING, "Something is wrong", new NullPointerException JavaDoc("npe"));
90         waitEQ();
91         assertNotNull("Really returned", DD.lastDescriptor);
92         assertEquals("It is DialogDescriptor", DialogDescriptor.class, DD.lastDescriptor.getClass());
93         DialogDescriptor dd = (DialogDescriptor)DD.lastDescriptor;
94         assertFalse( "The request is for non-modal dialog", dd.isModal());
95         assertFalse("Main window is not visible", mainWindow.isVisible());
96     }
97
98     public void testExceptionWillGetTheLevelFromAnnoatation() throws Exception JavaDoc {
99         NullPointerException JavaDoc npe = new NullPointerException JavaDoc("npe");
100         ErrorManager.getDefault().annotate(npe, ErrorManager.WARNING, null, null, null, null);
101
102         DD.toReturn = new HiddenDialog();
103         Exceptions.printStackTrace(npe);
104
105         waitEQ();
106         assertNotNull("We are going to display a warning", DD.lastDescriptor);
107
108     }
109
110     public void testDirectlyLoggingAnExceptionWithALocalizedMessageAndTheRightLevelShowsItInADialog() throws Exception JavaDoc {
111         NullPointerException JavaDoc npe = new NullPointerException JavaDoc("npe");
112
113         LogRecord JavaDoc rec = new LogRecord JavaDoc(OwnLevel.UI, "MSG_KEY");
114         rec.setThrown(npe);
115         ResourceBundle JavaDoc b = ResourceBundle.getBundle("org/netbeans/core/NotifyExceptionBundle");
116         rec.setResourceBundle(b);
117         DD.toReturn = new HiddenDialog();
118         Logger.global.log(rec);
119         waitEQ();
120         assertNotNull("We are going to display a warning", DD.lastDescriptor);
121         assertTrue("We want message: " + DD.lastDescriptor, DD.lastDescriptor instanceof NotifyDescriptor.Message);
122         NotifyDescriptor.Message msg = (NotifyDescriptor.Message)DD.lastDescriptor;
123         assertEquals("Info msg", NotifyDescriptor.INFORMATION_MESSAGE, msg.getMessageType());
124         assertEquals("Msg is localized", b.getString("MSG_KEY"), msg.getMessage());
125     }
126
127     public void testYesDialogShown() throws Exception JavaDoc {
128         Frame JavaDoc mainWindow = WindowManager.getDefault().getMainWindow();
129         final JDialog JavaDoc modalDialog = new HiddenDialog( mainWindow, true );
130         DD.toReturn = modalDialog;
131
132         Logger JavaDoc l = Logger.getLogger(getName());
133         l.setLevel(Level.ALL);
134         System.setProperty("netbeans.exception.report.min.level", "200");
135         l.log(Level.CONFIG, "Something is wrong", new NullPointerException JavaDoc("npe"));
136         waitEQ();
137         assertNotNull("Really returned", DD.lastDescriptor);
138         assertEquals("It is DialogDescriptor", DialogDescriptor.class, DD.lastDescriptor.getClass());
139         DialogDescriptor dd = (DialogDescriptor)DD.lastDescriptor;
140         assertFalse( "The request is for non-modal dialog", dd.isModal());
141         assertFalse("Main window is not visible", mainWindow.isVisible());
142     }
143     public void testNoDialogShownJustFlashing() throws Exception JavaDoc {
144         class MockFlashingIcon extends FlashingIcon {
145             public int cnt;
146
147             public MockFlashingIcon() {
148                 super(new ImageIcon JavaDoc());
149             }
150             protected void onMouseClick() {
151             }
152
153             protected void timeout() {
154             }
155
156             public void startFlashing() {
157                 cnt++;
158             }
159         }
160         MockFlashingIcon mock = new MockFlashingIcon();
161
162         NotifyExcPanel.flasher = mock;
163
164         Logger JavaDoc l = Logger.getLogger(getName());
165         l.setLevel(Level.ALL);
166         System.setProperty("netbeans.exception.alert.min.level", "200");
167         l.log(Level.CONFIG, "Something is wrong", new NullPointerException JavaDoc("npe"));
168         waitEQ();
169         assertNull("Really returned", DD.lastDescriptor);
170
171         assertEquals("Flasher flashing", 1, mock.cnt);
172     }
173
174     private static final class OwnLevel extends Level JavaDoc {
175         public static final Level JavaDoc UI = new OwnLevel("UI", 1973);
176
177         private OwnLevel(String JavaDoc n, int i) {
178             super(n, i);
179         }
180     }
181
182     private static final class DD extends DialogDisplayer {
183         public static NotifyDescriptor lastDescriptor;
184         public static Object JavaDoc toReturn;
185
186         public Object JavaDoc notify(NotifyDescriptor descriptor) {
187             Object JavaDoc t = toReturn;
188             toReturn = null;
189             assertNotNull("There is something to return", t);
190             lastDescriptor = descriptor;
191             return t;
192         }
193
194         public Dialog JavaDoc createDialog(DialogDescriptor descriptor) {
195             return (Dialog JavaDoc)notify(descriptor);
196         }
197     } // end of DD
198

199     public static final class Lkp extends AbstractLookup {
200         public static InstanceContent IC;
201
202         public Lkp() {
203             this(new InstanceContent());
204         }
205         private Lkp(InstanceContent ic) {
206             super(ic);
207             ic.add(new DD());
208             ic.add(new NbErrorManager());
209         }
210     }
211
212     private static final class HiddenDialog extends JDialog JavaDoc {
213         private boolean v;
214
215         public HiddenDialog() {
216         }
217
218         public HiddenDialog(Frame JavaDoc p, boolean b) {
219             super(p, b);
220         }
221
222         public void setVisible(boolean b) {
223             v = b;
224         }
225         public boolean isVisible() {
226             return v;
227         }
228     }
229 }
230
Popular Tags