View Javadoc

1   package junitour;
2   
3   import junit.framework.AssertionFailedError;
4   import java.util.WeakHashMap;
5   
6   /**
7    * This Error is based on JUnits AssertionException which is thrown
8    * when any assert-check fails.
9    * It's an extension to mark the special case of an incomplete test.
10   *
11   * <pre>
12   *
13   * $Log$
14   * Revision 1.4  2005/07/31 03:16:04  hostlows
15   * using Java 5 generics for making code simpler: less casts, simpler for-loops
16   *
17   * Revision 1.3  2004/07/08 01:47:50  hostlows
18   * cleanup, new build, license changed to mozilla license...
19   *
20   * Revision 1.2  2003/11/25 21:37:23  hostlows
21   * made it ant-compatible
22   *
23   * Revision 1.1.1.1  2003/11/12 23:32:13  hostlows
24   * Initial import.
25   *
26   * Revision 1.1  2003/10/30 07:13:54  hostlows
27   * First release, which can run as a TextUI-Testrunner and
28   * produces an enhanced output: additionally prints the count of
29   * incomplete Tests! It handles examination of incomplete Testcase over
30   * different classloaders now!
31   *
32   *
33   * </pre>
34   * @author $Author: hostlows $, created by  <a href="mailto:hostlows@users.sf.net">Robert Hostlowsky</a>
35   * @version $Revision: 53 $, $Date: 2005-07-31 05:16:04 +0200 (So, 31 Jul 2005) $
36   */
37  
38  public class UnitTestIncompleteError extends AssertionFailedError implements UnitTestIncomplete {
39    private static WeakHashMap<Class,Boolean> clazzCache = new WeakHashMap<Class,Boolean>(200);
40  
41    /** default constructor */
42    public UnitTestIncompleteError() {
43      super();
44    }
45  
46    public UnitTestIncompleteError(final String message) {
47      super(message);
48    }
49  
50    public UnitTestIncompleteError(final String message, final Throwable cause) {
51      super(message);
52      super.initCause(cause);
53    }
54  
55    public UnitTestIncompleteError(final Throwable cause) {
56      super();
57      super.initCause(cause);
58    }
59  
60    public static boolean isTestIncompleteError(final Throwable t) {
61      if (t == null) return false;
62  
63      debug ("testing ["+t+"] of class ["+t.getClass()+"] which implements these interfaces ["+t.getClass().getInterfaces()+"]");
64  
65      final Class aClass = t.getClass();
66      final Boolean cachedClazzCheck = clazzCache.get(aClass);
67      if( cachedClazzCheck!=null ) //must check this?
68        return cachedClazzCheck;
69  
70      if (UnitTestIncompleteError.class.getName().equals(aClass.getName())) {
71        return true;
72      }
73      if (UnitTestIncompleteError.class.isInstance(t)) {
74        return true;
75      }
76      if (UnitTestIncomplete.class.isInstance(t)) {
77        return true;
78      }
79  
80      if (UnitTestIncomplete.class.isAssignableFrom(aClass)) {
81        return true;
82      }
83  
84      //check the class's base interfaces
85      final Class[] interfaces = aClass.getInterfaces();
86      if( checkIncomplete(interfaces) ) {
87        clazzCache.put(aClass, Boolean.TRUE);
88        return true;
89      }
90      clazzCache.put(aClass, Boolean.FALSE);
91      return false;
92    }
93  
94    private static void debug(String s) {
95      System.err.println("UnitTestIncomplete:"+s);
96    }
97  
98    /** helper for internal checks
99     *
100    * @param interfaces an array of base-interfaces
101    * @return true, if any of the given interfaces is based on the
102    * UnitTestIncomplete Marker interface
103    */
104   private static boolean checkIncomplete(Class[] interfaces) {
105     if( interfaces==null ) return false;
106 
107     for (int i = 0; i < interfaces.length; i++) {
108       Class anInterface = interfaces[i];
109       if (anInterface.getName().equals(UnitTestIncomplete.class.getName())) {
110 //            System.err.println("Match: interface ["+anInterface+"]");
111         return true;
112       }
113     }
114     return false;
115   }
116 
117 }