1 19 20 package org.netbeans.modules.exceptions; 21 22 import java.io.FileNotFoundException ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.Set ; 27 import junit.framework.TestCase; 28 import java.util.logging.Level ; 29 import java.util.logging.LogRecord ; 30 31 35 public class ExceptionLoggerTest extends TestCase { 36 37 public ExceptionLoggerTest(String testName) { 38 super(testName); 39 } 40 41 protected void setUp() throws Exception { 42 } 43 44 protected void tearDown() throws Exception { 45 } 46 47 Set <Throwable > causes = new HashSet <Throwable >(); 48 49 52 public void testLogError() { 53 System.out.println("logError"); 54 boolean inserted = false; 55 String str = "testing log record"; 56 ClassCircularityError error = new ClassCircularityError ("testing error"); 57 LogRecord rec = new LogRecord (Level.WARNING, str); 58 rec.setThrown(error); 59 ExceptionLogger.logError(rec); 60 LinkedList <LogRecord > logs = Collector.getDefault().getQueue(); 61 Iterator <LogRecord > it = logs.iterator(); 62 while (it.hasNext()){ 63 LogRecord log = it.next(); 64 if ((log.getLevel().equals(Level.WARNING))&&(log.getMessage().contains( 65 "WARNING *********** Exception occurred ************ at "))){ 66 Throwable t = log.getThrown(); 67 if ((t!= null)) inserted=true; 68 } 69 } 70 assertTrue(inserted); 71 72 } 73 74 77 public void testConvert() { 78 System.out.println("convert"); 79 String str = "conversion"; 80 String str2 = "reason"; 81 String s3 = "reason of reason"; 82 Throwable t = new NullPointerException (str); 83 causes.clear(); 84 Throwable result = ExceptionLogger.convert(t); 85 assertTrue(testEquals(t, result)); 86 Throwable t2 = new ClassCastException (str2); 87 t.initCause(t2); 88 causes.clear(); 89 result = ExceptionLogger.convert(t); 90 assertTrue(testEquals(t, result)); 91 Throwable t3 = new FileNotFoundException (s3); 92 t3.initCause(t2); 93 causes.clear(); 94 result = ExceptionLogger.convert(t3); 95 assertTrue(testEquals(t3, result)); 96 Throwable t4 = new NullPointerException (str); 97 Throwable t5 = new NullPointerException (str2); 98 t4.initCause(t5); 100 t5.initCause(t4); 101 causes.clear(); 102 result = ExceptionLogger.convert(t4); 103 assertTrue(testEquals(t4, result)); 104 105 } 106 107 private boolean testEquals(Throwable t, Throwable result){ StackTraceElement [] stack = t.getStackTrace(); 109 StackTraceElement [] result_stack = result.getStackTrace(); 110 if (t != null) causes.add(t); 111 for (int i = 0; i < stack.length; i++) { 112 assertEquals(stack[i], result_stack[i]); 113 } 114 if ((t.getCause() != null)&&(!causes.contains(t.getCause()))){ 115 assertTrue(result.getCause() != null); 116 return testEquals(t.getCause(), result.getCause()); 117 }else return true; 118 } 119 120 123 public void testLog() { 124 System.out.println("log"); 125 Level severity = Level.INFO; 126 String s = "info"; 127 boolean contains = false; 128 ExceptionLogger.log(severity, s); 129 LinkedList <LogRecord > list = Collector.getDefault().getQueue(); 130 Iterator <LogRecord > it = list.iterator(); 131 while (it.hasNext()){ 132 LogRecord rec =it.next(); 133 if ((rec.getLevel().equals(Level.INFO))&&(rec.getMessage().equals(s))) contains=true; 134 } 135 assertTrue(contains); 136 } 137 138 } 139 | Popular Tags |