1 19 20 package org.openide.util; 21 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 import java.util.logging.Level ; 25 import junit.framework.TestCase; 26 import org.netbeans.junit.Log; 27 28 31 public class ExceptionsTest extends TestCase { 32 33 public ExceptionsTest(String testName) { 34 super(testName); 35 } 36 37 private void assertCleanStackTrace(Throwable t) { 38 StringWriter w = new StringWriter (); 39 PrintWriter pw = new PrintWriter (w); 40 t.printStackTrace(pw); 41 pw.flush(); 42 String m = w.toString(); 43 assertFalse(m.replace("\n", "\\n").replace("\t", "\\t"), m.contains("AnnException")); 44 assertFalse(m.replace("\n", "\\n").replace("\t", "\\t"), m.contains("msg")); 45 } 46 47 public void testAttachMessage() { 48 Exception e = new Exception ("Help"); 49 String msg = "me please"; 50 51 Exception result = Exceptions.attachMessage(e, msg); 52 53 assertSame(result, e); 54 55 StringWriter w = new StringWriter (); 56 result.printStackTrace(new PrintWriter (w)); 57 58 String m = w.toString(); 59 60 if (m.indexOf(msg) == -1) { 61 fail(msg + " shall be part of output:\n" + m); 62 } 63 64 assertCleanStackTrace(e); 65 } 66 67 public void testAttachMessageForClassNotFound() { 68 Exception e = new ClassNotFoundException ("Help"); 69 String msg = "me please"; 70 71 Exception result = Exceptions.attachMessage(e, msg); 72 73 assertSame(result, e); 74 75 CharSequence log = Log.enable("", Level.WARNING); 76 Exceptions.printStackTrace(e); 77 78 String m = log.toString(); 79 80 if (m.indexOf(msg) == -1) { 81 fail(msg + " shall be part of output:\n" + m); 82 } 83 84 assertCleanStackTrace(e); 85 } 86 87 public void testAttachLocalizedMessage() { 88 Exception e = new Exception ("Help"); 89 String msg = "me please"; 90 91 Exception expResult = e; 92 Exception result = Exceptions.attachLocalizedMessage(e, msg); 93 assertEquals(expResult, result); 94 95 String fnd = Exceptions.findLocalizedMessage(e); 96 97 assertEquals("The same msg", msg, fnd); 98 99 assertCleanStackTrace(e); 100 } 101 102 public void testAttachLocalizedMessageForClassNFE() { 103 Exception e = new ClassNotFoundException ("Help"); 104 String msg = "me please"; 105 106 Exception expResult = e; 107 Exception result = Exceptions.attachLocalizedMessage(e, msg); 108 assertEquals(expResult, result); 109 110 String fnd = Exceptions.findLocalizedMessage(e); 111 112 assertEquals("The same msg", msg, fnd); 113 114 assertCleanStackTrace(e); 115 } 116 117 public void testAttachLocalizedMessageForClassNFEIfNoMsg() { 118 Exception e = new ClassNotFoundException ("Help"); 119 String msg = "me please"; 120 121 Exception expResult = e; 122 Exception result = Exceptions.attachMessage(e, msg); 123 assertEquals(expResult, result); 124 125 String fnd = Exceptions.findLocalizedMessage(e); 126 127 assertEquals("No localized msg found", null, fnd); 128 129 assertCleanStackTrace(e); 130 } 131 132 } 133 | Popular Tags |