1 16 package org.mortbay.util; 17 18 import java.io.InputStream ; 19 import java.util.Enumeration ; 20 import java.util.Vector ; 21 22 import org.apache.commons.logging.Log; 23 import org.mortbay.log.LogFactory; 24 25 26 51 public class TestCase 52 { 53 private static Log log = LogFactory.getLog(TestCase.class); 54 55 56 private static Vector tests = new Vector (); 57 private static final String fail = "FAIL"; 58 private static final char[] spaces = " ".toCharArray(); 59 60 static final String SelfFailTest = 61 "org.mortbay.util.TestCase all fail"; 62 63 64 private String testCase; 65 private StringBuffer reportBuf=new StringBuffer (512); 66 private boolean passed = true; 67 68 69 72 public TestCase(String testCase) 73 { 74 if(log.isDebugEnabled())log.debug("Constructed test case: "+testCase); 75 this.testCase=testCase; 76 tests.addElement(this); 77 } 78 79 80 84 public void check(boolean b,String check) 85 { 86 if (!b) 87 { 88 reportBuf.append(testCase+" : "+check+" - "); 89 passed=false; 90 reportBuf.append(fail); 91 reportBuf.append('\n'); 92 reportBuf.append(spaces,0,testCase.length()+3); 93 reportBuf.append("check!=true"); 94 if(log.isDebugEnabled())log.debug(check+" FAILED"); 95 } 96 reportBuf.append('\n'); 97 } 98 99 100 101 104 public int checkContains(String string, String subString, String check) 105 { 106 return realCheckContains(string,0,subString,check); 107 } 108 109 110 113 public int checkContains(String string, 114 int offset, 115 String subString, String check) 116 { 117 return realCheckContains(string,offset,subString,check); 118 } 119 120 121 124 public int realCheckContains(String string, 125 int offset, 126 String subString, String check) 127 { 128 int index=-1; 129 if ((string==null && subString==null) 130 || (string!=null && (subString==null || 131 (index=string.indexOf(subString,offset))>=0))) 132 { 133 } 135 else 136 { 137 reportBuf.append(testCase+" : "+check+" - "); 138 passed=false; 139 reportBuf.append(fail); 140 reportBuf.append('\n'); 141 reportBuf.append(spaces,0,testCase.length()+3); 142 reportBuf.append('"' + subString + "\" not contained in \"" ); 143 144 if (offset<string.length()) 145 reportBuf.append(string.substring(offset)); 146 else 147 reportBuf.append("string<offset:"+offset+":'"+string+"'"); 148 reportBuf.append("\""); 149 if(log.isDebugEnabled())log.debug(check+" FAILED: "+reportBuf.toString()); 150 reportBuf.append('\n'); 151 } 152 return index; 153 } 154 155 156 157 160 public int checkNotContained(String string, String subString, String check) 161 { 162 return checkNotContained(string,0,subString,check); 163 } 164 165 166 169 public int checkNotContained(String string, 170 int offset, 171 String subString, String check) 172 { 173 int index=-1; 174 if ((string==null && subString==null) 175 || (string!=null && (subString==null || 176 (index=string.indexOf(subString,offset))>=0))) 177 { 178 reportBuf.append(testCase+" : "+check+" - "); 179 passed=false; 180 reportBuf.append(fail); 181 reportBuf.append('\n'); 182 reportBuf.append(spaces,0,testCase.length()+3); 183 reportBuf.append('"' + subString + "\" IS contained in \"" + 184 string.substring(offset) + '"'); 185 if(log.isDebugEnabled())log.debug(check+" FAILED"); 186 reportBuf.append('\n'); 187 } 188 return index; 189 } 190 191 192 193 194 199 public void checkEquals(Object o1,Object o2,String check) 200 { 201 commonCheckEquals(o1,o2,check); 202 } 203 204 205 210 public void checkEquals(long l1,long l2,String check) 211 { 212 commonCheckEquals(new Long (l1),new Long (l2),check); 213 } 214 215 216 221 public void checkEquals(double d1,double d2,String check) 222 { 223 commonCheckEquals(new Double (d1),new Double (d2),check); 224 } 225 226 227 232 public void checkEquals(char c1,char c2,String check) 233 { 234 commonCheckEquals(new Character (c1),new Character (c2),check); 235 } 236 237 238 243 public void checkEquals(InputStream in1,InputStream in2,String check) 244 { 245 int c1; 246 int c2; 247 try{ 248 while ((c1=in1.read())==(c2=in2.read())) 249 { 250 if (c1==-1) 251 { 252 commonCheckEquals(null,null,check); 253 return; 254 } 255 } 256 commonCheckEquals(""+c1,""+c2,check); 257 } 258 catch(Exception e) 259 { 260 commonCheckEquals(e.toString(),null,check); 261 } 262 } 263 264 265 270 private void commonCheckEquals(Object o1,Object o2,String check) 271 { 272 if (o1==o2 || ( o1!=null && o1.equals(o2))) 273 { 274 } 276 else 277 { 278 reportBuf.append(testCase+" : "+check+" - "); 279 passed=false; 280 reportBuf.append(fail); 281 reportBuf.append('\n'); 282 reportBuf.append(spaces,0,testCase.length()+3); 283 reportBuf.append(((o1!=null)?(o1.toString()):"null") + " != " + 284 ((o2!=null)?(o2.toString()):"null")); 285 if(log.isDebugEnabled())log.debug(3+check+" FAILED"); 286 reportBuf.append('\n'); 287 } 288 } 289 290 291 294 public static void report() 295 { 296 Enumeration e = tests.elements(); 297 while (e.hasMoreElements()) 298 { 299 TestCase t = (TestCase) e.nextElement(); 300 if (! t.passed) { 301 System.err.print("\nTest Case: "+t.testCase); 302 System.err.println(" - FAILED"); 303 System.err.println(t.reportBuf.toString()); 304 } 305 } 306 307 System.err.println("\nTEST SUMMARY:"); 308 e = tests.elements(); 309 boolean failed=false; 310 while (e.hasMoreElements()) 311 { 312 TestCase t = (TestCase) e.nextElement(); 313 if (!t.passed) 314 { 315 if (!t.testCase.equals(SelfFailTest)) 316 { 317 System.err.print("Test Case: "+t.testCase); 318 System.err.println(" - FAILED"); 319 failed=true; 320 } 321 } 322 } 323 if (failed) 324 System.exit(1); 325 System.exit(0); 326 } 327 } 328 | Popular Tags |