KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > TestCase


1 // ========================================================================
2
// $Id: TestCase.java,v 1.8 2005/08/13 00:01:28 gregwilkins Exp $
3
// Copyright 1997-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17
18 import java.io.InputStream JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.mortbay.log.LogFactory;
24
25 /*-------------------------------------------------------------------*/
26 /** Test Harness and report.
27  * Test Harness for production of standard test reports:
28  *
29  * <pre>
30  * Test t1 = new Test("All_Pass");
31  * Test t2 = new Test("All_Fail");
32  *
33  * t1.check(true,"Boolean check that passes");
34  * t2.check(false,"Boolean check that fails");
35  * t1.checkEquals("Foo","Foo","Object comparison that passes");
36  * t2.checkEquals("Foo","Bar","Object comparison that fails");
37  * t1.checkEquals(1,1,"Long comparison that passes");
38  * t2.checkEquals(1,2,"Long comparison that fails");
39  * t1.checkEquals(1.1,1.1,"Double comparison that passes");
40  * t2.checkEquals(1.1,2.2,"Double comparison that fails");
41  * t1.checkEquals('a','a',"Char comparison that passes");
42  * t2.checkEquals('a','b',"Char comparison that fails");
43  *
44  * Test.report();
45  * </pre>
46  *
47  * @see org.mortbay.util.Code
48  * @version $Id: TestCase.java,v 1.8 2005/08/13 00:01:28 gregwilkins Exp $
49  * @author Greg Wilkins
50  */

51 public class TestCase
52 {
53     private static Log log = LogFactory.getLog(TestCase.class);
54
55     /*-------------------------------------------------------------------*/
56     private static Vector JavaDoc tests = new Vector JavaDoc();
57     private static final String JavaDoc fail = "FAIL";
58     private static final char[] spaces = " ".toCharArray();
59     
60     static final String JavaDoc SelfFailTest =
61         "org.mortbay.util.TestCase all fail";
62     
63     /*-------------------------------------------------------------------*/
64     private String JavaDoc testCase;
65     private StringBuffer JavaDoc reportBuf=new StringBuffer JavaDoc(512);
66     private boolean passed = true;
67     
68     /*-------------------------------------------------------------------*/
69     /** TestCase constructor.
70      * @param testCase the name of the test case
71      */

72     public TestCase(String JavaDoc testCase)
73     {
74         if(log.isDebugEnabled())log.debug("Constructed test case: "+testCase);
75         this.testCase=testCase;
76         tests.addElement(this);
77     }
78     
79     /*-------------------------------------------------------------------*/
80     /** Check a boolean test case.
81      * @param b Boolean to check
82      * @param check Description of this check
83      */

84     public void check(boolean b,String JavaDoc 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     /** Check that string contains a substring.
102      * @return Index of substring
103      */

104     public int checkContains(String JavaDoc string, String JavaDoc subString, String JavaDoc check)
105     {
106         return realCheckContains(string,0,subString,check);
107     }
108     
109     /*-------------------------------------------------------------------*/
110     /** Check that string contains a substring.
111      * @return Index of substring
112      */

113     public int checkContains(String JavaDoc string,
114                              int offset,
115                              String JavaDoc subString, String JavaDoc check)
116     {
117         return realCheckContains(string,offset,subString,check);
118     }
119     
120     /*-------------------------------------------------------------------*/
121     /** Check that string contains a substring.
122      * @return Index of substring
123      */

124     public int realCheckContains(String JavaDoc string,
125                                  int offset,
126                                  String JavaDoc subString, String JavaDoc 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           // do nothing
134
}
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     /** Check that string does not contain a substring.
158      * @return Index of substring
159      */

160     public int checkNotContained(String JavaDoc string, String JavaDoc subString, String JavaDoc check)
161     {
162         return checkNotContained(string,0,subString,check);
163     }
164     
165     /*-------------------------------------------------------------------*/
166     /** Check that string does not contain a substring.
167      * @return Index of substring
168      */

169     public int checkNotContained(String JavaDoc string,
170                                  int offset,
171                                  String JavaDoc subString, String JavaDoc 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     /** Check a pair of objects for equality test case.
195      * @param o1 First object to compare
196      * @param o2 Second object to compare
197      * @param check Description of this check
198      */

199     public void checkEquals(Object JavaDoc o1,Object JavaDoc o2,String JavaDoc check)
200     {
201         commonCheckEquals(o1,o2,check);
202     }
203     
204     /*-------------------------------------------------------------------*/
205     /** Check a a pair of longs for equality.
206      * @param l1 First Long to compare
207      * @param l2 Second Long to compare
208      * @param check Description of this check
209      */

210     public void checkEquals(long l1,long l2,String JavaDoc check)
211     {
212         commonCheckEquals(new Long JavaDoc(l1),new Long JavaDoc(l2),check);
213     }
214     
215     /*-------------------------------------------------------------------*/
216     /** Check a a pair of doubles for equality.
217      * @param d1 First double to compare
218      * @param d2 Second double to compare
219      * @param check Description of this check
220      */

221     public void checkEquals(double d1,double d2,String JavaDoc check)
222     {
223         commonCheckEquals(new Double JavaDoc(d1),new Double JavaDoc(d2),check);
224     }
225     
226     /*-------------------------------------------------------------------*/
227     /** Check a a pair of chars for equality.
228      * @param c1 First char to compare
229      * @param c2 Second char to compare
230      * @param check Description of this check
231      */

232     public void checkEquals(char c1,char c2,String JavaDoc check)
233     {
234         commonCheckEquals(new Character JavaDoc(c1),new Character JavaDoc(c2),check);
235     }
236
237     /*-------------------------------------------------------------------*/
238     /** Check contents of a pair of InputStreams for equality.
239      * @param in1 First InputStream
240      * @param in2 Second InputStream
241      * @param check Description
242      */

243     public void checkEquals(InputStream JavaDoc in1,InputStream JavaDoc in2,String JavaDoc 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 JavaDoc e)
259         {
260             commonCheckEquals(e.toString(),null,check);
261         }
262     }
263     
264    /*-------------------------------------------------------------------*/
265     /** Internal check a pair of objects for equality test case.
266      * @param o1 First object to compare
267      * @param o2 Second object to compare
268      * @param check Description of this check
269      */

270     private void commonCheckEquals(Object JavaDoc o1,Object JavaDoc o2,String JavaDoc check)
271     {
272         if (o1==o2 || ( o1!=null && o1.equals(o2)))
273         {
274           // do nothing
275
}
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     /** Produce test report.
292      *
293      */

294     public static void report()
295     {
296         Enumeration JavaDoc 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