01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.pluto.util;
18:
19: import java.lang.reflect.Method;
20: import java.lang.reflect.InvocationTargetException;
21:
22: import org.jmock.MockObjectTestCase;
23:
24: /**
25: * Test Class
26: *
27: * @version 1.0
28: * @since June 1, 2005
29: */
30: public abstract class PlutoTestCase extends MockObjectTestCase {
31:
32: public void setUp() throws Exception {
33: System.setProperty("org.apache.commons.logging.Log",
34: "org.apache.commons.logging.impl.SimpleLog");
35: System.setProperty(
36: "org.apache.commons.logging.simplelog.defaultlog",
37: "ERROR");
38: }
39:
40: protected void assertException(Object target, String methodName,
41: Object[] parameters, Class exceptionType) {
42: Class[] parameterClasses = new Class[parameters.length];
43: for (int i = 0; i < parameters.length; i++) {
44: parameterClasses[i] = parameters[i] == null ? Object.class
45: : parameters[i].getClass();
46: }
47: assertException(target, methodName, parameterClasses,
48: parameters, exceptionType);
49: }
50:
51: protected void assertException(Object target, String methodName,
52: Class[] parameterClasses, Object[] parameters,
53: Class exceptionType) {
54: try {
55: Class targetClass = target.getClass();
56: Method method = targetClass.getMethod(methodName,
57: parameterClasses);
58: method.invoke(target, parameters);
59: } catch (InvocationTargetException ite) {
60: Throwable t = ite.getTargetException();
61: if (!t.getClass().equals(exceptionType)) {
62: fail("Incorrect Exception thrown. Expected: "
63: + exceptionType.getName() + ", recieved "
64: + t.getClass().getName());
65: }
66: } catch (Throwable t) {
67: fail("Invalid Test. Reflection invocation and setup failed.");
68: }
69: }
70:
71: protected void assertContains(String message,
72: String expectedSubstring, String testString) {
73: if (testString.indexOf(expectedSubstring) < 0) {
74: fail(message);
75: }
76: }
77: }
|