KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > test > JenaTestBase


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: JenaTestBase.java,v 1.15 2005/02/21 12:18:53 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.test;
8
9 import java.lang.reflect.*;
10 import junit.framework.*;
11 import java.util.*;
12
13 import com.hp.hpl.jena.util.CollectionFactory;
14
15 /**
16     A basis for Jena test cases which provides assertFalse and assertDiffer.
17     Often the logic of the names is clearer than using a negation (well, Chris
18     thinks so anyway).
19     
20     @author kers
21 */

22 public class JenaTestBase extends TestCase
23     {
24     public JenaTestBase( String JavaDoc name )
25         { super( name ); }
26         
27     /**
28         assert that the two objects must be unequal according to .equals().
29         @param title a labelling string for the assertion failure text
30         @param x an object to test; the subject of a .equals()
31         @param y the other object; the argument of the .equals()
32     */

33     public static void assertDiffer( String JavaDoc title, Object JavaDoc x, Object JavaDoc y )
34         {
35         if (x == null ? y == null : x.equals( y ))
36             fail( (title == null ? "objects should be different, but both were: " : title) + x );
37         }
38         
39     /**
40         assert that the two objects must be unequal according to .equals().
41         @param x an object to test; the subject of a .equals()
42         @param y the other object; the argument of the .equals()
43     */

44     public static void assertDiffer( Object JavaDoc x, Object JavaDoc y )
45         { assertDiffer( null, x, y ); }
46         
47     /**
48         Answer a Set formed from the elements of the List <code>L</code>.
49     */

50     public static Set listToSet( List L )
51         { return CollectionFactory.createHashedSet( L ); }
52     
53     /**
54         Do nothing; a way of notating that a test has succeeded, useful in the body of a
55         catch-block to silence excessively [un]helpful disgnostics.
56     */

57     public static void pass()
58         {}
59         
60     /**
61         Answer the constructor of the class <code>c</code> which takes arguments of the
62         type(s) in <code>args</code>, or <code>null</code> if there isn't one.
63      */

64     public static Constructor getConstructor( Class JavaDoc c, Class JavaDoc [] args )
65         {
66         try { return c.getConstructor( args ); }
67         catch (NoSuchMethodException JavaDoc e) { return null; }
68         }
69
70     /**
71         Answer true iff the method <code>m</code> is a public method which fits the
72         pattern of being a test method, ie, test*() returning void.
73      */

74     public static boolean isPublicTestMethod( Method m )
75         { return Modifier.isPublic( m.getModifiers() ) && isTestMethod( m ); }
76      
77     /**
78         Answer true iff the method <code>m</code> has a name starting "test", takes no
79         arguments, and returns void; must catch junit tests, in other words.
80     */

81     public static boolean isTestMethod( Method m )
82         { return
83             m.getName().startsWith( "test" )
84             && m.getParameterTypes().length == 0
85             && m.getReturnType().equals( Void.TYPE ); }
86     
87     /**
88          Answer true iff <code>subClass</code> is the same class as
89          <code>superClass</code>, if its superclass <i>is</i> <code>superClass</code>,
90          or if one of its interfaces hasAsInterface that class.
91     */

92     public boolean hasAsParent( Class JavaDoc subClass, Class JavaDoc superClass )
93         {
94         if (subClass == superClass || subClass.getSuperclass() == superClass) return true;
95         Class JavaDoc [] is = subClass.getInterfaces();
96         for (int i = 0; i < is.length; i += 1) if (hasAsParent( is[i], superClass )) return true;
97         return false;
98         }
99     
100     /**
101          Fail unless <code>subClass</code> has <code>superClass</code> as a
102          parent, either a superclass or an implemented (directly or not) interface.
103     */

104     public void assertHasParent( Class JavaDoc subClass, Class JavaDoc superClass )
105         {
106         if (hasAsParent( subClass, superClass ) == false)
107             fail( "" + subClass + " should have " + superClass + " as a parent" );
108         }
109
110     public List append(List L, List R)
111         { List result = new ArrayList( L );
112         result.addAll( R );
113         return result; }
114
115     }
116
117
118 /*
119     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
120     All rights reserved.
121
122     Redistribution and use in source and binary forms, with or without
123     modification, are permitted provided that the following conditions
124     are met:
125
126     1. Redistributions of source code must retain the above copyright
127        notice, this list of conditions and the following disclaimer.
128
129     2. Redistributions in binary form must reproduce the above copyright
130        notice, this list of conditions and the following disclaimer in the
131        documentation and/or other materials provided with the distribution.
132
133     3. The name of the author may not be used to endorse or promote products
134        derived from this software without specific prior written permission.
135
136     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
137     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
138     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
139     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
140     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
141     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
145     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 */
Popular Tags