CVS: junit/src/org/junit/experimental/theories/runner TheoryMethod.java, NONE, 1.1 ConcreteFunction.java, NONE, 1.1 Function.java, NONE, 1.1 TheoryContainerReference.java, 1.1, 1.2

David Saff <[email protected]> Thu, 12 Jul 2007 10:08:53 -0700
Newsgroups gmane.comp.java.junit.devel
Message-ID <[email protected]>
Update of /cvsroot/junit/junit/src/org/junit/experimental/theories/runner
In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv26134/src/org/junit/experimental/theories/runner

Modified Files:
	TheoryContainerReference.java 
Added Files:
	TheoryMethod.java ConcreteFunction.java Function.java 
Log Message:
Re-organize theory packages

--- NEW FILE: TheoryMethod.java ---
/**
 * 
 */
package org.junit.experimental.theories.runner;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Assume.AssumptionViolatedException;
import org.junit.experimental.theories.methods.api.Theory;
import org.junit.internal.runners.TestClass;
import org.junit.internal.runners.TestMethod;

public class TheoryMethod extends TestMethod {
	private final Method fMethod;

	private List<AssumptionViolatedException> fInvalidParameters= new ArrayList<AssumptionViolatedException>();

	public TheoryMethod(Method method, TestClass testClass) {
		super(method, testClass);
		fMethod= method;
	}

	@Override
	public void invoke(Object test) throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
		TheoryContainerReference container= new TheoryContainerReference(
				test);

		ConcreteFunction function= new ConcreteFunction(test, fMethod);

		int runCount= 0;
		try {
			runCount+= container.runWithParameters(this,
					new ArrayList<Object>(), function.signatures());
		} catch (Throwable e) {
			throw new InvocationTargetException(e);
		}
		if (runCount == 0)
			Assert
					.fail("Never found parameters that satisfied method.  Violated assumptions: "
							+ fInvalidParameters);
	}

	public boolean nullsOk() {
		return fMethod.getAnnotation(Theory.class).nullsAccepted();
	}

	public Method getMethod() {
		return fMethod;
	}

	public void addAssumptionFailure(AssumptionViolatedException e) {
		fInvalidParameters.add(e);
	}
}
--- NEW FILE: ConcreteFunction.java ---
/**
 * 
 */
package org.junit.experimental.theories.runner;

import java.lang.reflect.Method;

public class ConcreteFunction extends Function {
	protected final Object target;
	protected final Method method;

	public ConcreteFunction(Object target, Method method) {
		this.target = target;
		this.method = method;
	}

	@Override public Object getTarget() {
		return target;
	}

	@Override public Method getMethod() {
		return method;
	}

	@Override public boolean equals(Object obj) {
		Function function = (Function) obj;

		return targetIs(function.getTarget())
				&& method.equals(function.getMethod());
	}

	private boolean targetIs(Object otherTarget) {
		if (target == null)
			return otherTarget == null;
		return target.equals(otherTarget);
	}

	@Override public String toString() {
		return String.format("%s.%s", target, method);
	}
}
--- NEW FILE: Function.java ---
package org.junit.experimental.theories.runner;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;

import org.junit.experimental.theories.methods.api.ParameterSignature;


public abstract class Function {
	public abstract Method getMethod();

	public abstract Object getTarget();

	public Object invoke(Object... paramArray) throws Throwable {
		getMethod().setAccessible(true);
		Object[] modParams = modifyParameters(paramArray);
		if (modParams.length != parameterTypes().length)
			throw new IllegalArgumentException(String.format(
					"Wrong number of parameters, passing %s to %s", Arrays
							.asList(modParams), getMethod()));
		try {
			return getMethod().invoke(getTarget(), modParams);
		} catch (InvocationTargetException e) {
			throw e.getTargetException();
		}
	}

	public Throwable exceptionThrown(Object... paramArray) {
		try {
			invoke(paramArray);
		} catch(Throwable t) {
			return t;
		}
		
		return null;
	}

	protected Object[] modifyParameters(Object... paramArray) {
		return paramArray;
	}

	public Type[] parameterTypes() {
		return getMethod().getGenericParameterTypes();
	}

	public Object[] emptyParameterArray() {
		return new Object[parameterTypes().length];
	}

	public Function curryWith(final Object[] curriedParameters) {
		return new Function() {

			@Override public Method getMethod() {
				return Function.this.getMethod();
			}

			@Override public Object getTarget() {
				return Function.this.getTarget();
			}

			@Override protected Object[] modifyParameters(Object... paramArray) {
				ArrayList<Object> list = new ArrayList<Object>();
				list.addAll(Arrays.asList(curriedParameters));
				list.addAll(Arrays.asList(paramArray));
				return list.toArray();
			}
		};
	}

	public ArrayList<ParameterSignature> signatures() {
		return ParameterSignature.signatures(getMethod());
	}
}

Index: TheoryContainerReference.java
===================================================================
RCS file: /cvsroot/junit/junit/src/org/junit/experimental/theories/runner/TheoryContainerReference.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- TheoryContainerReference.java	2 Jul 2007 18:11:15 -0000	1.1
+++ TheoryContainerReference.java	12 Jul 2007 17:08:21 -0000	1.2
@@ -11,10 +11,8 @@
 import java.util.List;
 
 import org.junit.Assume.AssumptionViolatedException;
-import org.junit.experimental.theories.javamodel.api.ConcreteFunction;
 import org.junit.experimental.theories.methods.api.ParameterSignature;
 import org.junit.experimental.theories.methods.api.ParameterSupplier;
-import org.junit.experimental.theories.runner.api.Theories.TheoryMethod;
 
 public class TheoryContainerReference {
 	private final Object container;


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/