1 19 20 package org.netbeans.modules.apisupport.jackpotrules; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.logging.Level ; 26 import java.util.regex.Matcher ; 27 import java.util.regex.Pattern ; 28 29 import org.netbeans.junit.NbTestCase; 30 31 35 public final class ErrorManagerTest extends NbTestCase { 36 37 private URL script; 38 39 40 public ErrorManagerTest(String s) { 41 super(s); 42 } 43 44 protected Level logLevel() { 45 return Level.INFO; 46 } 47 48 protected void setUp() throws Exception { 49 clearWorkDir(); 50 script = ErrorManagerTest.class.getResource("/org/netbeans/modules/apisupport/jackpotrules/annotate-to-initcause.rules"); 51 assertNotNull(script); 52 } 53 54 public void testRenameNotifyToPst() throws Exception { 55 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 56 "package test;\n" + 57 "class A {\n" + 58 " public static void main(String[] args) {\n" + 59 " Exception e = new Exception();\n" + 60 " org.openide.ErrorManager.getDefault().notify(e);\n" + 61 " }\n" + 62 "}\n" 63 ); 64 65 JackpotUtils.apply(getWorkDir(), script); 66 67 String txt = JackpotUtils.readFile(f, false); 68 69 if (txt.indexOf("ErrorManager") >= 0) { 70 fail("No ErrorManager shall be there:\n" + txt); 71 } 72 } 73 74 public void testErrMgrAnnoMsg() throws Exception { 75 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 76 "package test;\n" + 77 "import javax.swing.JButton;" + 78 "class A {\n" + 79 " public static void main(String[] args) {\n" + 80 " NullPointerException npe = new NullPointerException();\n" + 81 " org.openide.ErrorManager.getDefault().annotate(" + 82 " npe, \"localizedText\");" + 83 " }\n" + 84 "}\n" 85 ); 86 87 JackpotUtils.apply(getWorkDir(), script); 88 89 String txt = JackpotUtils.readFile(f, false); 90 91 if (txt.indexOf("ErrorManager") >= 0) { 92 fail("No ErrorManager shall be there:\n" + txt); 93 } 94 95 if (txt.indexOf("attachLocalizedMessage") < 0) { 96 fail("There should be a localized message usage:\n" + txt); 97 } 98 } 99 public void testErrMgrUnknown() throws Exception { 100 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 101 "package test;\n" + 102 "import javax.swing.JButton;" + 103 "class A {\n" + 104 " public static void main(String[] args) {\n" + 105 " NullPointerException npe = new NullPointerException();\n" + 106 " org.openide.ErrorManager.getDefault().annotate(" + 107 " npe, org.openide.ErrorManager.UNKNOWN," + 108 " \"msg\", null, null, null);" + 109 " }\n" + 110 "}\n" 111 ); 112 113 JackpotUtils.apply(getWorkDir(), script); 114 115 String txt = JackpotUtils.readFile(f, false); 116 117 if (txt.indexOf("ErrorManager") >= 0) { 118 fail("No ErrorManager shall be there:\n" + txt); 119 } 120 } 121 122 public void testErrMgrException() throws Exception { 123 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 124 "package test;\n" + 125 "import javax.swing.JButton;" + 126 "class A {\n" + 127 " public static void main(String[] args) {\n" + 128 " JButton r = new JButton();\n" + 129 " String icon = null;\n" + 130 " NullPointerException npe = new NullPointerException();\n" + 131 " org.openide.ErrorManager.getDefault().annotate(" + 132 " npe, org.openide.ErrorManager.EXCEPTION," + 133 " \"Probably an ImageIcon with a null source image: \" + icon + \" - \" + //NOI18N\n" + 134 " r.getText(), null, null, null\n" + 135 "); //NOI18N\n" + 136 " }\n" + 137 "}\n" 138 ); 139 140 JackpotUtils.apply(getWorkDir(), script); 141 142 String txt = JackpotUtils.readFile(f, false); 143 144 if (txt.indexOf("ErrorManager") >= 0) { 145 fail("No ErrorManager shall be there:\n" + txt); 146 } 147 } 148 public void testConvertToWarning() throws Exception { 149 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 150 "package test;\n" + 151 "class A {\n" + 152 " public static void main(String[] args) {\n" + 153 " NullPointerException e = new NullPointerException();\n" + 154 " org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.EXCEPTION, e);" + 155 " }\n" + 156 "}\n" 157 ); 158 159 JackpotUtils.apply(getWorkDir(), script); 160 161 String txt = JackpotUtils.readFile(f, false); 162 163 if (txt.indexOf("ErrorManager") >= 0) { 164 fail("No ErrorManager shall be there:\n" + txt); 165 } 166 if (!txt.replace('\n', ' ').matches(".*Logger.getLogger *\\( *\"global *\"\\).log *\\(Level.WARNING, *null, *e *\\).*")) { 167 fail("Logger.global shall be there:\n" + txt); 168 } 169 } 170 171 public void testConvertToLogging() throws Exception { 172 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 173 "package test;\n" + 174 "class A {\n" + 175 " public static void main(String[] args) {\n" + 176 " NullPointerException e = new NullPointerException();\n" + 177 " org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);" + 178 " }\n" + 179 "}\n" 180 ); 181 182 JackpotUtils.apply(getWorkDir(), script); 183 184 String txt = JackpotUtils.readFile(f, false); 185 186 if (txt.indexOf("ErrorManager") >= 0) { 187 fail("No ErrorManager shall be there:\n" + txt); 188 } 189 if (!txt.replace('\n', ' ').matches(".*Logger.getLogger *\\( *\"global\" *\\).log *\\(Level.INFO, *null, *e *\\).*")) { 190 fail("Logger.global shall be there:\n" + txt); 191 } 192 } 193 194 public void testGetRidOfCopyAnnotation() throws Exception { 195 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 196 "package test;\n" + 197 "class A {\n" + 198 " public static void main(String[] args) {\n" + 199 " NullPointerException npe = new NullPointerException();\n" + 200 " java.io.IOException io = null;\n" + 201 " org.openide.ErrorManager.getDefault().copyAnnotation(npe, io);" + 202 " }\n" + 203 "}\n" 204 ); 205 206 JackpotUtils.apply(getWorkDir(), script); 207 208 String txt = JackpotUtils.readFile(f, false); 209 210 if (txt.indexOf("ErrorManager") >= 0) { 211 fail("No ErrorManager shall be there:\n" + txt); 212 } 213 214 if (!txt.replace("\n", " ").matches(".*npe.initCause *\\( *io *\\).*")) { 215 fail("initCause shall be used:\n" + txt); 216 } 217 } 218 219 public void testNonLocalizedAnnotation() throws Exception { 220 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 221 "package test;\n" + 222 "class A {\n" + 223 " public static void main(String[] args) {\n" + 224 " NullPointerException npe = new NullPointerException();\n" + 225 " String msg = \"nonlocmsg\";\n" + 226 " org.openide.ErrorManager.getDefault().annotate(npe, org.openide.ErrorManager.UNKNOWN, null, msg, null, null);" + 227 " }\n" + 228 "}\n" 229 ); 230 231 JackpotUtils.apply(getWorkDir(), script); 232 233 String txt = JackpotUtils.readFile(f, false); 234 235 if (txt.indexOf("ErrorManager") >= 0) { 236 fail("No ErrorManager shall be there:\n" + txt); 237 } 238 239 if (!txt.replace("\n", " ").matches(".*Exceptions.attachLocalizedMessage.*")) { 240 fail("initCause shall be used:\n" + txt); 241 } 242 } 243 244 245 public void testFriendContractWithOwnLevel() throws Exception { 246 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 247 "package test;\n" + 248 "class A {\n" + 249 " public static void main(String[] args) {\n" + 250 " NullPointerException npe = new NullPointerException();\n" + 251 " String msg = \"nonlocmsg\";\n" + 252 " org.openide.ErrorManager.getDefault().annotate(npe, " + 253 " org.openide.ErrorManager.USER, msg == null ? \"\" : npe.getMessage()," + 254 " msg, npe, new java.util.Date()); //NOI18N\n" + 255 " }\n" + 256 "}\n" 257 ); 258 259 JackpotUtils.apply(getWorkDir(), script); 260 261 String txt = JackpotUtils.readFile(f, false); 262 263 if (txt.indexOf("ErrorManager") >= 0) { 264 fail("No ErrorManager shall be there:\n" + txt); 265 } 266 267 if (!txt.replace("\n", " ").matches(".*UIException.annotateUser *\\( *npe.*")) { 268 fail("UIException shall be used:\n" + txt); 269 } 270 } 271 272 public void testWarningGoesToLogging() throws Exception { 273 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 274 "package test;\n" + 275 "class A {\n" + 276 " public static void main(String[] args) {\n" + 277 " NullPointerException npe = new NullPointerException();\n" + 278 " org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, npe);" + 279 " }\n" + 280 "}\n" 281 ); 282 283 JackpotUtils.apply(getWorkDir(), script); 284 285 String txt = JackpotUtils.readFile(f, false); 286 287 if (txt.indexOf("ErrorManager") >= 0) { 288 fail("No ErrorManager shall be there:\n" + txt); 289 } 290 291 if (!txt.replace('\n', ' ').matches(".*Logger.getLogger *\\( *\"global\" *\\).log *\\(Level.WARNING, *null, *npe *\\).*")) { 292 fail("Logger.global shall be there:\n" + txt); 293 } 294 } 295 296 public void testAnnotateWithInfo() throws Exception { 297 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 298 "package test;\n" + 299 "class A {\n" + 300 " public static void main(String[] args) {\n" + 301 " NullPointerException cnfe = new NullPointerException();\n" + 302 " String msg = \"ahoj\";\n" + 303 " org.openide.ErrorManager.getDefault().annotate(cnfe, org.openide.ErrorManager.INFORMATIONAL, msg, null, null, null);" + 304 " }\n" + 305 "}\n" 306 ); 307 308 JackpotUtils.apply(getWorkDir(), script); 309 310 String txt = JackpotUtils.readFile(f, false); 311 312 if (txt.indexOf("ErrorManager") >= 0) { 313 fail("No ErrorManager shall be there:\n" + txt); 314 } 315 316 if (!txt.replace('\n', ' ').matches(".*Exceptions.attachMessage *\\(*cnfe, *msg\\).*")) { 317 fail("Exceptions.attachMessage shall be there:\n" + txt); 318 } 319 320 } 321 322 public void testRenamesEditor() throws Exception { 323 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 324 "package test;\n" + 325 "class A {\n" + 326 " public static void main(String[] args) {\n" + 327 " try {" + 328 " System.exit(0);\n" + 329 " } catch( IllegalArgumentException iaE ) {" + 330 " org.openide.ErrorManager.getDefault().annotate(iaE, org.openide.ErrorManager.USER, null, iaE.getLocalizedMessage(), null, null);\n" + 331 " throw iaE;\n" + 332 " }" + 333 " }\n" + 334 "}\n" 335 ); 336 337 JackpotUtils.apply(getWorkDir(), script); 338 339 String txt = JackpotUtils.readFile(f, false); 340 341 if (txt.indexOf("ErrorManager") >= 0) { 342 fail("No ErrorManager shall be there:\n" + txt); 343 } 344 345 if (!txt.replace('\n', ' ').matches(".*UIException.*")) { 346 fail("UIException shall be there:\n" + txt); 347 } 348 if (txt.replace('\n', ' ').indexOf("$") >= 0) { 349 fail("NO $ shall be there:\n" + txt); 350 } 351 } 352 353 354 public void testUsingAttachMessageAndThenInitCauseIsNotGoodIdea() throws Exception { 355 doUsingAttachMessageAndThenInitCauseIsNotGoodIdea("attachMessage"); 356 } 357 358 public void testUsingAttachLocMessageAndThenInitCauseIsNotGoodIdea() throws Exception { 359 doUsingAttachMessageAndThenInitCauseIsNotGoodIdea("attachLocalizedMessage"); 360 } 361 362 private void doUsingAttachMessageAndThenInitCauseIsNotGoodIdea(String method) throws Exception { 363 File f = JackpotUtils.extractString(new File (getWorkDir(), "A.java"), 364 "package test;\n" + 365 "import java.io.IOException;\n" + 366 "class A {\n" + 367 " public static void main(String[] args) throws IOException {\n" + 368 " try {" + 369 " System.exit(0);\n" + 370 " } catch( IllegalArgumentException iaE ) {" + 371 " IOException io = new IOException();" + 372 " org.openide.util.Exceptions." + method + "(io, \"msg\");\n" + 373 " iaE.printStackTrace();\n" + 374 " io.initCause(iaE);\n" + 375 " throw io;\n" + 376 " }" + 377 " }\n" + 378 "}\n" 379 ); 380 381 JackpotUtils.apply(getWorkDir(), script); 382 383 String txt = JackpotUtils.readFile(f, false); 384 385 Matcher m = Pattern.compile("initCause.*" + method, Pattern.DOTALL).matcher(txt); 386 if (!m.find()) { 387 fail("First of all we should use initCause and then attachMessage:\n" + txt); 388 } 389 } 390 391 } 392 | Popular Tags |