1 19 20 package org.netbeans.core; 21 22 import java.awt.Dialog ; 23 import java.awt.Frame ; 24 import java.util.ResourceBundle ; 25 import java.util.logging.Level ; 26 import java.util.logging.LogRecord ; 27 import java.util.logging.Logger ; 28 import javax.swing.ImageIcon ; 29 import javax.swing.JDialog ; 30 import javax.swing.SwingUtilities ; 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 49 public class NotifyExceptionTest extends NbTestCase { 50 static { 51 System.setProperty("org.openide.util.Lookup", Lkp.class.getName()); 52 } 53 54 public NotifyExceptionTest(String name) { 55 super(name); 56 } 57 58 private static void waitEQ() throws Exception { 59 SwingUtilities.invokeAndWait(new Runnable () { 60 public void run() { 61 } 62 }); 63 } 64 65 protected void setUp() throws Exception { 66 clearWorkDir(); 67 System.setProperty("netbeans.user", getWorkDirPath()); 68 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 84 public void testNoModalErrorDialog() throws Exception { 85 Frame mainWindow = WindowManager.getDefault().getMainWindow(); 86 final JDialog modalDialog = new HiddenDialog( mainWindow, true ); 87 DD.toReturn = modalDialog; 88 89 Logger.global.log(Level.WARNING, "Something is wrong", new NullPointerException ("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 { 99 NullPointerException npe = new NullPointerException ("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 { 111 NullPointerException npe = new NullPointerException ("npe"); 112 113 LogRecord rec = new LogRecord (OwnLevel.UI, "MSG_KEY"); 114 rec.setThrown(npe); 115 ResourceBundle 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 { 128 Frame mainWindow = WindowManager.getDefault().getMainWindow(); 129 final JDialog modalDialog = new HiddenDialog( mainWindow, true ); 130 DD.toReturn = modalDialog; 131 132 Logger 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 ("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 { 144 class MockFlashingIcon extends FlashingIcon { 145 public int cnt; 146 147 public MockFlashingIcon() { 148 super(new ImageIcon ()); 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 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 ("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 { 175 public static final Level UI = new OwnLevel("UI", 1973); 176 177 private OwnLevel(String 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 toReturn; 185 186 public Object notify(NotifyDescriptor descriptor) { 187 Object t = toReturn; 188 toReturn = null; 189 assertNotNull("There is something to return", t); 190 lastDescriptor = descriptor; 191 return t; 192 } 193 194 public Dialog createDialog(DialogDescriptor descriptor) { 195 return (Dialog )notify(descriptor); 196 } 197 } 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 { 213 private boolean v; 214 215 public HiddenDialog() { 216 } 217 218 public HiddenDialog(Frame 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 |