001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.tests.harness.util;
011:
012: import java.lang.reflect.Method;
013: import java.util.ArrayList;
014:
015: /**
016: * <code>CallHistory</code> is used to record the invocation
017: * of methods within a target object. This is useful during
018: * lifecycle testing for an object.
019: * <p>
020: * To use <code>CallHistory</code> ..
021: * <ol>
022: * <li>Create a CallHistory in the target or pass one in.</li>
023: * <li>Invoke some test scenario. </li>
024: * <li>If a method is called on the target record the invocation
025: * in the call history</li>
026: * <li>Verify the call history after the test scenario is
027: * complete.</li>
028: * </ol>
029: * </p><p>
030: * Each <code>CallHistory</code> has a target which is used to
031: * verify the method names passed to the history. If an invalid
032: * name is passed an <code>IllegalArgumentException</code> will
033: * be thrown.
034: * </p>
035: */
036: public class CallHistory {
037: private ArrayList methodList;
038:
039: private Class classType;
040:
041: /**
042: * Creates a new call history for an object.
043: *
044: * @param target the call history target.
045: */
046: public CallHistory(Object target) {
047: methodList = new ArrayList();
048: classType = target.getClass();
049: }
050:
051: /**
052: * Throws an exception if the method name is invalid
053: * for the given target class.
054: */
055: private void testMethodName(String methodName) {
056: Method[] methods = classType.getMethods();
057: for (int i = 0; i < methods.length; i++)
058: if (methods[i].getName().equals(methodName))
059: return;
060: throw new IllegalArgumentException("Target class ("
061: + classType.getName() + ") does not contain method: "
062: + methodName);
063: }
064:
065: /**
066: * Adds a method name to the call history.
067: *
068: * @param methodName the name of a method
069: */
070: public void add(String methodName) {
071: testMethodName(methodName);
072: methodList.add(methodName);
073: }
074:
075: /**
076: * Clears the call history.
077: */
078: public void clear() {
079: methodList.clear();
080: }
081:
082: /**
083: * Returns whether a list of methods have been called in
084: * order.
085: *
086: * @param testNames an array of the method names in the order they are expected
087: * @return <code>true</code> if the methods were called in order
088: */
089: public boolean verifyOrder(String[] testNames)
090: throws IllegalArgumentException {
091: int testIndex = 0;
092: int testLength = testNames.length;
093: if (testLength == 0)
094: return true;
095: for (int nX = 0; nX < methodList.size(); nX++) {
096: String methodName = (String) methodList.get(nX);
097: String testName = testNames[testIndex];
098: testMethodName(testName);
099: if (testName.equals(methodName))
100: ++testIndex;
101: if (testIndex >= testLength)
102: return true;
103: }
104: return false;
105: }
106:
107: /**
108: * Returns whether a method has been called.
109: *
110: * @param methodName a method name
111: * @return <code>true</code> if the method was called
112: */
113: public boolean contains(String methodName) {
114: testMethodName(methodName);
115: return methodList.contains(methodName);
116: }
117:
118: /**
119: * Returns whether a list of methods were called.
120: *
121: * @param methodNames a list of methods
122: * @return <code>true</code> if the methods were called
123: */
124: public boolean contains(String[] methodNames) {
125: for (int i = 0; i < methodNames.length; i++) {
126: testMethodName(methodNames[i]);
127: if (!methodList.contains(methodNames[i]))
128: return false;
129: }
130: return true;
131: }
132:
133: /**
134: * Returns whether the list of methods called is empty.
135: *
136: * @return <code>true</code> iff the list of methods is empty
137: */
138: public boolean isEmpty() {
139: return methodList.isEmpty();
140: }
141:
142: /**
143: * Prints the call history to the console.
144: */
145: public void printToConsole() {
146: for (int i = 0; i < methodList.size(); i++)
147: System.out.println(methodList.get(i));
148: }
149: }
|