1 2 57 58 62 63 64 package dom.util; 65 66 import java.io.PrintWriter ; 67 import java.io.StringWriter ; 68 69 public class Assertion { 70 71 public static boolean assertTrue(boolean result) { 72 return assertTrue(result, null); 73 } 74 75 public static boolean assertTrue(boolean result, String error) { 76 if (!result) { 77 System.err.print("Assertion failed: "); 78 if (error != null) { 79 System.err.print(error); 80 } 81 System.err.println(); 82 System.err.println(getSourceLocation()); 83 } 84 return result; 85 } 86 87 public static boolean equals(String s1, String s2) { 88 boolean result = ((s1 != null && s1.equals(s2)) 89 || (s1 == null && s2 == null)); 90 if (!result) { 91 assertTrue(result); 92 System.err.println(" was: equals(" + s1 + ", \"" + s2 + "\")"); 93 } 94 return result; 95 } 96 97 public static String getSourceLocation() { 98 RuntimeException ex = new RuntimeException ("assertion failed"); 99 StringWriter writer = new StringWriter (); 100 PrintWriter printer = new PrintWriter (writer); 101 ex.printStackTrace(printer); 102 String buf = writer.toString(); 103 int index = buf.lastIndexOf("dom.util.Assertion."); 105 index = buf.indexOf('\n', index); 106 return buf.substring(index + 1); 107 } 108 } 109 | Popular Tags |