001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: JenaTestBase.java,v 1.26 2008/01/02 12:09:47 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.test;
008:
009: import java.lang.reflect.*;
010: import junit.framework.*;
011: import java.util.*;
012:
013: import com.hp.hpl.jena.util.CollectionFactory;
014: import com.hp.hpl.jena.util.iterator.*;
015:
016: /**
017: A basis for Jena test cases which provides assertFalse and assertDiffer.
018: Often the logic of the names is clearer than using a negation (well, Chris
019: thinks so anyway).
020:
021: @author kers
022: */
023: public class JenaTestBase extends TestCase {
024: public JenaTestBase(String name) {
025: super (name);
026: }
027:
028: /**
029: Does what it says on the can.
030: */
031: public void testToSilenceJunitComplaintsAboutNoTestMethods() {
032: }
033:
034: /**
035: assert that the two objects must be unequal according to .equals().
036: @param title a labelling string for the assertion failure text
037: @param x an object to test; the subject of a .equals()
038: @param y the other object; the argument of the .equals()
039: */
040: public static void assertDiffer(String title, Object x, Object y) {
041: if (x == null ? y == null : x.equals(y))
042: fail((title == null ? "objects should be different, but both were: "
043: : title)
044: + x);
045: }
046:
047: /**
048: assert that the two objects must be unequal according to .equals().
049: @param x an object to test; the subject of a .equals()
050: @param y the other object; the argument of the .equals()
051: */
052: public static void assertDiffer(Object x, Object y) {
053: assertDiffer(null, x, y);
054: }
055:
056: /**
057: assert that the object <code>x</code> must be of the class
058: <code>expected</code>.
059: */
060: public static void assertInstanceOf(Class expected, Object x) {
061: if (x == null)
062: fail("expected instance of " + expected + ", but had null");
063: if (!expected.isInstance(x))
064: fail("expected instance of " + expected
065: + ", but had instance of " + x.getClass());
066: }
067:
068: /**
069: Answer a Set formed from the elements of the List <code>L</code>.
070: */
071: public static Set listToSet(List L) {
072: return CollectionFactory.createHashedSet(L);
073: }
074:
075: /**
076: Answer a List of the substrings of <code>s</code> that are separated
077: by spaces.
078: */
079: protected static List listOfStrings(String s) {
080: List result = new ArrayList();
081: StringTokenizer st = new StringTokenizer(s);
082: while (st.hasMoreTokens())
083: result.add(st.nextToken());
084: return result;
085: }
086:
087: /**
088: Answer a Set of the substrings of <code>s</code> that are separated
089: by spaces.
090: */
091: protected static Set setOfStrings(String s) {
092: Set result = new HashSet();
093: StringTokenizer st = new StringTokenizer(s);
094: while (st.hasMoreTokens())
095: result.add(st.nextToken());
096: return result;
097: }
098:
099: /**
100: Answer a list containing the single object <code>x</code>.
101: */
102: protected static List listOfOne(Object x) {
103: List result = new ArrayList();
104: result.add(x);
105: return result;
106: }
107:
108: /**
109: Answer a Set containing the single object <code>x</code>.
110: */
111: protected static Set setOfOne(Object x) {
112: Set result = new HashSet();
113: result.add(x);
114: return result;
115: }
116:
117: /**
118: Answer a fresh list which is the concatenation of <code>L</code> then
119: <code>R</code>. Neither <code>L</code> nor <code>R</code> is updated.
120: */
121: public static List append(List L, List R) {
122: List result = new ArrayList(L);
123: result.addAll(R);
124: return result;
125: }
126:
127: /**
128: Answer an iterator over the space-separated substrings of <code>s</code>.
129: */
130: protected static ExtendedIterator iteratorOfStrings(String s) {
131: return WrappedIterator.create(listOfStrings(s).iterator());
132: }
133:
134: /**
135: Do nothing; a way of notating that a test has succeeded, useful in the
136: body of a catch-block to silence excessively [un]helpful disgnostics.
137: */
138: public static void pass() {
139: }
140:
141: /**
142: Answer the constructor of the class <code>c</code> which takes arguments
143: of the type(s) in <code>args</code>, or <code>null</code> if there
144: isn't one.
145: */
146: public static Constructor getConstructor(Class c, Class[] args) {
147: try {
148: return c.getConstructor(args);
149: } catch (NoSuchMethodException e) {
150: return null;
151: }
152: }
153:
154: /**
155: Answer true iff the method <code>m</code> is a public method which fits
156: the pattern of being a test method, ie, test*() returning void.
157: */
158: public static boolean isPublicTestMethod(Method m) {
159: return Modifier.isPublic(m.getModifiers()) && isTestMethod(m);
160: }
161:
162: /**
163: Answer true iff the method <code>m</code> has a name starting "test",
164: takes no arguments, and returns void; must catch junit tests, in other
165: words.
166: */
167: public static boolean isTestMethod(Method m) {
168: return m.getName().startsWith("test")
169: && m.getParameterTypes().length == 0
170: && m.getReturnType().equals(Void.TYPE);
171: }
172:
173: /**
174: Answer true iff <code>subClass</code> is the same class as
175: <code>superClass</code>, if its superclass <i>is</i> <code>superClass</code>,
176: or if one of its interfaces hasAsInterface that class.
177: */
178: public static boolean hasAsParent(Class subClass, Class super Class) {
179: if (subClass == super Class
180: || subClass.getSuperclass() == super Class)
181: return true;
182: Class[] is = subClass.getInterfaces();
183: for (int i = 0; i < is.length; i += 1)
184: if (hasAsParent(is[i], super Class))
185: return true;
186: return false;
187: }
188:
189: /**
190: Fail unless <code>subClass</code> has <code>superClass</code> as a
191: parent, either a superclass or an implemented (directly or not) interface.
192: */
193: public static void assertHasParent(Class subClass, Class super Class) {
194: if (hasAsParent(subClass, super Class) == false)
195: fail("" + subClass + " should have " + super Class
196: + " as a parent");
197: }
198: }
199:
200: /*
201: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
202: All rights reserved.
203:
204: Redistribution and use in source and binary forms, with or without
205: modification, are permitted provided that the following conditions
206: are met:
207:
208: 1. Redistributions of source code must retain the above copyright
209: notice, this list of conditions and the following disclaimer.
210:
211: 2. Redistributions in binary form must reproduce the above copyright
212: notice, this list of conditions and the following disclaimer in the
213: documentation and/or other materials provided with the distribution.
214:
215: 3. The name of the author may not be used to endorse or promote products
216: derived from this software without specific prior written permission.
217:
218: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
219: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
220: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
221: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
222: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
224: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
225: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
226: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
228: */
|