Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mortbay/util/TestCase.java


1   // ========================================================================
2   // Copyright (c) 1997 MortBay Consulting, Sydney
3   // $Id: TestCase.java,v 1.6 2003/09/18 13:29:27 gregwilkins Exp $
4   // ========================================================================
5   
6   package org.mortbay.util;
7   
8   import java.io.InputStream;
9   import java.util.Enumeration;
10  import java.util.Vector;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  
15  /*-------------------------------------------------------------------*/
16  /** Test Harness and report.
17   * Test Harness for production of standard test reports:
18   *
19   * <pre>
20   *      Test t1 = new Test("All_Pass");
21   *      Test t2 = new Test("All_Fail");
22   *
23   *      t1.check(true,"Boolean check that passes");
24   *      t2.check(false,"Boolean check that fails");
25   *      t1.checkEquals("Foo","Foo","Object comparison that passes");
26   *      t2.checkEquals("Foo","Bar","Object comparison that fails");
27   *      t1.checkEquals(1,1,"Long comparison that passes");
28   *      t2.checkEquals(1,2,"Long comparison that fails");
29   *      t1.checkEquals(1.1,1.1,"Double comparison that passes");
30   *      t2.checkEquals(1.1,2.2,"Double comparison that fails");
31   *      t1.checkEquals('a','a',"Char comparison that passes");
32   *      t2.checkEquals('a','b',"Char comparison that fails");
33   *      
34   *      Test.report();
35   * </pre>
36   *
37   * @see org.mortbay.util.Code
38   * @version $Id: TestCase.java,v 1.6 2003/09/18 13:29:27 gregwilkins Exp $
39   * @author Greg Wilkins
40   */
41  public class TestCase
42  {
43      private static Log log = LogFactory.getLog(TestCase.class);
44  
45      /*-------------------------------------------------------------------*/
46      private static Vector tests = new Vector();
47      private static final String fail = "FAIL";
48      private static final char[] spaces = "                                                                                 ".toCharArray();
49      
50      static final String SelfFailTest =
51          "org.mortbay.util.TestCase all fail";
52      
53      /*-------------------------------------------------------------------*/
54      private String testCase;
55      private StringBuffer reportBuf=new StringBuffer(512);
56      private boolean passed = true;
57      
58      /*-------------------------------------------------------------------*/
59      /** TestCase constructor.
60       *  @param testCase   the name of the test case
61       */
62      public TestCase(String testCase)
63      {
64          if(log.isDebugEnabled())log.debug("Constructed test case: "+testCase);
65          this.testCase=testCase;
66          tests.addElement(this);
67      }
68      
69      /*-------------------------------------------------------------------*/
70      /** Check a boolean test case.
71       *  @param b        Boolean to check
72       *  @param check    Description of this check
73       */
74      public void check(boolean b,String check)
75      {
76          if (!b)
77          {
78              reportBuf.append(testCase+" : "+check+" - ");
79              passed=false;
80              reportBuf.append(fail);
81              reportBuf.append('\n');
82              reportBuf.append(spaces,0,testCase.length()+3);
83              reportBuf.append("check!=true");
84              if(log.isDebugEnabled())log.debug(check+" FAILED");
85          }
86          reportBuf.append('\n');
87      }
88      
89      
90      /*-------------------------------------------------------------------*/
91      /** Check that string contains a substring.
92       *  @return Index of substring
93       */
94      public int checkContains(String string, String subString, String check)
95      {
96          return realCheckContains(string,0,subString,check);
97      }
98      
99      /*-------------------------------------------------------------------*/
100     /** Check that string contains a substring.
101      *  @return Index of substring
102      */
103     public int checkContains(String string,
104                              int offset,
105                              String subString, String check)
106     {
107         return realCheckContains(string,offset,subString,check);
108     }
109     
110     /*-------------------------------------------------------------------*/
111     /** Check that string contains a substring.
112      *  @return Index of substring
113      */
114     public int realCheckContains(String string,
115                                  int offset,
116                                  String subString, String check)
117     {
118         int index=-1;
119         if ((string==null && subString==null)
120             || (string!=null && (subString==null ||
121                                  (index=string.indexOf(subString,offset))>=0)))
122         {
123           // do nothing
124         }
125         else
126         {
127             reportBuf.append(testCase+" : "+check+" - ");
128             passed=false;
129             reportBuf.append(fail);
130             reportBuf.append('\n');
131             reportBuf.append(spaces,0,testCase.length()+3);
132             reportBuf.append('"' + subString + "\" not contained in \"" );
133             
134             if (offset<string.length())
135                 reportBuf.append(string.substring(offset));
136             else
137                 reportBuf.append("string<offset:"+offset+":'"+string+"'");
138             reportBuf.append("\"");
139             if(log.isDebugEnabled())log.debug(check+" FAILED: "+reportBuf.toString());
140             reportBuf.append('\n');
141         }
142         return index;
143     }
144     
145  
146     /*-------------------------------------------------------------------*/
147     /** Check that string does not contain a substring.
148      *  @return Index of substring
149      */
150     public int checkNotContained(String string, String subString, String check)
151     {
152         return checkNotContained(string,0,subString,check);
153     }
154     
155     /*-------------------------------------------------------------------*/
156     /** Check that string does not contain a substring.
157      *  @return Index of substring
158      */
159     public int checkNotContained(String string,
160                                  int offset,
161                                  String subString, String check)
162     {
163         int index=-1;
164         if ((string==null && subString==null)
165             || (string!=null && (subString==null ||
166                                  (index=string.indexOf(subString,offset))>=0)))
167         {
168             reportBuf.append(testCase+" : "+check+" - ");
169             passed=false;
170             reportBuf.append(fail);
171             reportBuf.append('\n');
172             reportBuf.append(spaces,0,testCase.length()+3);
173             reportBuf.append('"' + subString + "\" IS contained in \"" +
174                              string.substring(offset) + '"');
175             if(log.isDebugEnabled())log.debug(check+" FAILED");
176             reportBuf.append('\n');
177         }
178         return index;
179     }
180     
181  
182     
183     /*-------------------------------------------------------------------*/
184     /** Check a pair of objects for equality test case.
185      *  @param o1       First object to compare
186      *  @param o2       Second object to compare
187      *  @param check    Description of this check
188      */
189     public void checkEquals(Object o1,Object o2,String check)
190     {
191         commonCheckEquals(o1,o2,check);
192     }
193     
194     /*-------------------------------------------------------------------*/
195     /** Check a a pair of longs for equality.
196      *  @param l1       First Long to compare
197      *  @param l2       Second Long to compare
198      *  @param check    Description of this check
199      */
200     public void checkEquals(long l1,long l2,String check)
201     {
202         commonCheckEquals(new Long(l1),new Long(l2),check);
203     }
204     
205     /*-------------------------------------------------------------------*/
206     /** Check a a pair of doubles for equality.
207      *  @param d1       First double to compare
208      *  @param d2       Second double to compare
209      *  @param check    Description of this check
210      */
211     public void checkEquals(double d1,double d2,String check)
212     {
213         commonCheckEquals(new Double(d1),new Double(d2),check);
214     }
215     
216     /*-------------------------------------------------------------------*/
217     /** Check a a pair of chars for equality.
218      *  @param c1       First char to compare
219      *  @param c2       Second char to compare
220      *  @param check    Description of this check
221      */
222     public void checkEquals(char c1,char c2,String check)
223     {
224         commonCheckEquals(new Character(c1),new Character(c2),check);
225     }
226 
227     /*-------------------------------------------------------------------*/
228     /** Check contents of a pair of InputStreams for equality.
229      * @param in1 First InputStream
230      * @param in2 Second InputStream
231      * @param check Description
232      */
233     public void checkEquals(InputStream in1,InputStream in2,String check)
234     {
235         int c1;
236         int c2;
237         try{
238             while ((c1=in1.read())==(c2=in2.read()))
239             {
240                 if (c1==-1)
241                 {
242                     commonCheckEquals(null,null,check);
243                     return;
244                 }
245             }
246             commonCheckEquals(""+c1,""+c2,check);
247         }
248         catch(Exception e)
249         {
250             commonCheckEquals(e.toString(),null,check);
251         }
252     }
253     
254    /*-------------------------------------------------------------------*/
255     /** Internal check a pair of objects for equality test case.
256      *  @param o1       First object to compare
257      *  @param o2       Second object to compare
258      *  @param check    Description of this check
259      */
260     private void commonCheckEquals(Object o1,Object o2,String check)
261     {
262         if (o1==o2 || ( o1!=null && o1.equals(o2)))
263         {
264           // do nothing
265         }
266         else
267         {
268             reportBuf.append(testCase+" : "+check+" - ");
269             passed=false;
270             reportBuf.append(fail);
271             reportBuf.append('\n');
272             reportBuf.append(spaces,0,testCase.length()+3);
273             reportBuf.append(((o1!=null)?(o1.toString()):"null") + " != " +
274                              ((o2!=null)?(o2.toString()):"null"));
275             if(log.isDebugEnabled())log.debug(3+check+" FAILED");
276             reportBuf.append('\n');
277         }
278     }
279 
280     /*-------------------------------------------------------------------*/
281     /** Produce test report.
282      *  
283      */
284     public static void report()
285     {
286         Enumeration e = tests.elements();
287         while (e.hasMoreElements())
288         {
289             TestCase t = (TestCase) e.nextElement();
290             if (! t.passed) {
291                 System.err.print("\nTest Case: "+t.testCase);
292                 System.err.println("  - FAILED");
293                 System.err.println(t.reportBuf.toString());
294             }
295         }
296 
297         System.err.println("\nTEST SUMMARY:");
298         e = tests.elements();
299         boolean failed=false;
300         while (e.hasMoreElements())
301         {
302             TestCase t = (TestCase) e.nextElement();
303             if (!t.passed)
304             {
305                 if (!t.testCase.equals(SelfFailTest))
306                 {
307                     System.err.print("Test Case: "+t.testCase);
308                     System.err.println("  - FAILED");
309                     failed=true;
310                 }
311             }
312         }
313         if (failed)
314             System.exit(1);
315         System.exit(0);
316     }
317 }