001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.container.tck;
027:
028: import junit.framework.AssertionFailedError;
029: import junit.framework.TestCase;
030: import org.jicarilla.container.tck.util.InvocationRecorder;
031: import org.jicarilla.container.tck.util.InvocationRecorderProvider;
032: import org.jicarilla.container.tck.util.MemberInvocation;
033:
034: import java.lang.reflect.Constructor;
035: import java.lang.reflect.Member;
036: import java.lang.reflect.Method;
037: import java.util.ArrayList;
038: import java.util.Arrays;
039: import java.util.Iterator;
040: import java.util.List;
041:
042: /**
043: *
044: *
045: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
046: * @version $Id: AbstractTCKTestCase.java,v 1.1 2004/01/18 19:32:05 lsimons Exp $
047: */
048: public class AbstractTCKTestCase extends TestCase {
049: // ----------------------------------------------------------------------
050: // Assert the test contract was followed
051: // ----------------------------------------------------------------------
052: private boolean setupWasCalled = false;
053:
054: public void setUp() throws Exception {
055: super .setUp();
056: setupWasCalled = true;
057: }
058:
059: public final void testSetupWasCalled() {
060: assertTrue(setupWasCalled);
061: }
062:
063: // ----------------------------------------------------------------------
064: // Custom assertions
065: // ----------------------------------------------------------------------
066:
067: protected final InvocationRecorder getLoggerFrom(Object o) {
068: assertNotNull("error in testcase!", o);
069: assertTrue("error in testcase!",
070: o instanceof InvocationRecorderProvider);
071:
072: return ((InvocationRecorderProvider) o).getLogger();
073: }
074:
075: protected final void assertNumberInRange(int start, int end,
076: int test) {
077: if (test < start || test > end)
078: throw new AssertionFailedError("expected a number between"
079: + start + " and " + end + " but it was: " + test);
080: }
081:
082: protected final void assertBetween0And5(int test) {
083: assertNumberInRange(0, 5, test);
084: }
085:
086: protected final void assertConstructorCalled(Object target,
087: Class[] arguments) {
088: final String className = target.getClass().getName();
089:
090: if (findConstructorInvocation(target, arguments) != null) {
091: String msg = "A call to the constructor of (a base class of)"
092: + className + " with ";
093:
094: if (arguments.length == 0)
095: msg += "no arguments ";
096: else {
097: msg += "arguments \n(";
098: msg += " ";
099: for (int i = 0; i < arguments.length; i++) {
100: Class argument = arguments[i];
101: msg += argument.getName();
102: if (i < arguments.length - 1)
103: msg += ", ";
104: else
105: msg += " )\n";
106: }
107: msg += "should have been made, but the constructor with ";
108: }
109:
110: MemberInvocation invocation = findConstructorInvocation(target);
111: Constructor constructor = (Constructor) invocation
112: .getMember();
113: if (constructor == null)
114: msg += "no arguments ";
115: else {
116: Class[] realArguments = constructor.getParameterTypes();
117: msg += "arguments \n(";
118: msg += " ";
119:
120: for (int i = 0; i < realArguments.length; i++) {
121: Class argument = realArguments[i];
122: msg += argument.getName();
123: if (i < realArguments.length - 1)
124: msg += ", ";
125: else
126: msg += " )\n";
127: }
128: }
129: msg += "was called instead!";
130:
131: fail(msg);
132: }
133: }
134:
135: protected final void assertMethodCallInRange(Object target,
136: String methodName, int start, int end) {
137: int test = findMethodInvocation(target, methodName);
138: if (test == -1) {
139: fail("A call to '" + methodName + " on " + target
140: + " should have been made!");
141: }
142:
143: if (test < start || test > end) {
144: String msg = "A call to '" + methodName + " on " + target
145: + " should have been made as the ";
146: if (start == 0)
147: msg += "1st";
148: else if (start == 1)
149: msg += "2nd";
150: else
151: msg += (start + 1) + "th";
152:
153: msg += ", or at some later point, but no later than as the "
154: + end
155: + "th call. But it was called as the "
156: + test
157: + "th" + " call!";
158:
159: fail(msg);
160: }
161: }
162:
163: protected final void assertMethodCalled(Object target,
164: String methodName) {
165: if (findMethodInvocation(target, methodName) == -1) {
166: fail("A call to '" + methodName + " on " + target
167: + " should have been made!");
168: }
169: }
170:
171: protected final void assertMethodCalledOnce(Object target,
172: String methodName) {
173: MemberInvocation[] invocations = findMethodInvocations(target,
174: methodName);
175: if (invocations.length != 1) {
176: fail("A call to '" + methodName + " on " + target
177: + " should have been made only once, but it"
178: + " was called " + invocations.length + " times !");
179: }
180: }
181:
182: protected final void assertMethodCalledOnceWithNonNullArguments(
183: Object target, String methodName) {
184: MemberInvocation[] invocations = findMethodInvocations(target,
185: methodName);
186: if (invocations.length != 1) {
187: fail("A call to '" + methodName + " on " + target
188: + " should have been made only once, but it"
189: + " was called " + invocations.length + " times !");
190: }
191: Object[] args = invocations[0].getArgs();
192: for (int i = 0; i < args.length; i++) {
193: Object arg = args[i];
194:
195: if (arg == null) {
196: String msg = "The ";
197: if (i == 0)
198: msg += "1st";
199: else if (i == 1)
200: msg += "2nd";
201: else
202: msg += (i + 1) + "th";
203:
204: msg += " argument on the call to " + methodName
205: + " called on " + target
206: + " was null, but it shouldn't have been!";
207:
208: fail(msg);
209: }
210: }
211: }
212:
213: protected final void assertMethodCalled(Object target,
214: String methodName, int times) {
215: final InvocationRecorder log = getLoggerFrom(target);
216:
217: MemberInvocation[] invocations = findMethodInvocations(log,
218: methodName);
219: if (invocations.length != 1) {
220: fail("A call to '" + methodName + " on " + target
221: + " should have been made " + times
222: + " times, but it" + " was called "
223: + invocations.length + " times !");
224: }
225: }
226:
227: protected final void assertMethodCallSequence(Object target,
228: String[] methodNames) {
229: assertNotNull("error in testcase!", target);
230: final InvocationRecorder log = getLoggerFrom(target);
231:
232: assertMethodCallSequence(log, methodNames);
233: }
234:
235: protected final void assertMethodCallSequence(
236: InvocationRecorder log, String[] methodNames) {
237: assertNotNull("error in testcase!", log);
238: assertNotNull("error in testcase!", methodNames);
239: assertTrue("error in testcase!", methodNames.length > 1);
240:
241: final MemberInvocation inv = (MemberInvocation) log
242: .getInvocations().get(0);
243: final Object target = inv.getTarget();
244:
245: int count = 0;
246:
247: Iterator it = log.getInvocations().iterator();
248: while (it.hasNext()) {
249: MemberInvocation invocation = (MemberInvocation) it.next();
250: if (((Method) invocation.getMember()).getName().equals(
251: methodNames[0])) {
252: count = 1;
253:
254: for (int i = 1; i < methodNames.length; i++) {
255: String methodName = methodNames[i];
256: invocation = (MemberInvocation) it.next();
257: count++;
258:
259: if (!((Method) invocation.getMember()).getName()
260: .equals(methodName)) {
261: String msg = "A call to "
262: + methodName
263: + " on "
264: + target.getClass().getName()
265: + " should have been made directly after a "
266: + "call to "
267: + methodNames[i - 1]
268: + ", but instead a "
269: + "call to "
270: + ((Method) invocation.getMember())
271: .getName() + " was made!";
272:
273: fail(msg);
274: }
275: }
276: }
277: }
278:
279: if (count < methodNames.length) {
280: fail("A call to " + methodNames[count]
281: + " should have been " + " made after a call to "
282: + methodNames[count - 1] + ", but "
283: + " no call occured at all!");
284: }
285: }
286:
287: // ----------------------------------------------------------------------
288: // Helper methods
289: // ----------------------------------------------------------------------
290:
291: protected final int findMethodInvocation(Object target,
292: String methodName) {
293: final InvocationRecorder log = getLoggerFrom(target);
294:
295: Iterator it = log.getInvocations().iterator();
296: int counter = 0;
297: while (it.hasNext()) {
298: MemberInvocation invocation = (MemberInvocation) it.next();
299: if (((Method) invocation.getMember()).getName().equals(
300: methodName))
301: return counter;
302:
303: counter++;
304: }
305: //fail( "No invocation for method '" + methodName + "' found!" );
306: return -1;
307: }
308:
309: protected final MemberInvocation[] findMethodInvocations(
310: Object target, String methodName) {
311: final InvocationRecorder log = getLoggerFrom(target);
312: final List invocations = new ArrayList();
313:
314: Iterator it = log.getInvocations().iterator();
315: while (it.hasNext()) {
316: MemberInvocation invocation = (MemberInvocation) it.next();
317: if (((Method) invocation.getMember()).getName().equals(
318: methodName))
319: invocations.add(invocation);
320: }
321:
322: return (MemberInvocation[]) invocations
323: .toArray(new MemberInvocation[invocations.size()]);
324: }
325:
326: protected final MemberInvocation findConstructorInvocation(
327: Object target) {
328: return findConstructorInvocation(target, new Class[0]);
329: }
330:
331: protected final MemberInvocation findConstructorInvocation(
332: Object target, Class[] arguments) {
333: final InvocationRecorder log = getLoggerFrom(target);
334:
335: Iterator it = log.getInvocations().iterator();
336: while (it.hasNext()) {
337: MemberInvocation invocation = (MemberInvocation) it.next();
338: Member member = invocation.getMember();
339: if (member instanceof Constructor) {
340: Constructor constructor = (Constructor) member;
341: if (Arrays.equals(arguments, constructor
342: .getParameterTypes()))
343: return invocation;
344: }
345: }
346:
347: return null;
348: }
349: }
|