All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.java.quickcheck.junit3.CharacteristicsTester Maven / Gradle / Ivy

There is a newer version: 0.6
Show newest version
/*
 *  Licensed to the author under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.java.quickcheck.junit3;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import junit.framework.TestCase;
import net.java.quickcheck.Characteristic;
import net.java.quickcheck.Generator;
import net.java.quickcheck.RunnerFactory;

class CharacteristicsTester extends TestCase {

	private final Method fMethod;
	private final Object fInstance;
	private final int fRuns;
	private final boolean fVerbose;
	private final Generator fGenerator;
	private final Method fSetupMethod;
	private final Method fTearDownMethod;

	CharacteristicsTester(Object testClassInstance,
			Generator generator, Method method, int runs,
			boolean verbose) {

		fInstance = testClassInstance;
		fGenerator = generator;
		fMethod = method;
		fSetupMethod = getMethod(testClassInstance, "setUp");
		fTearDownMethod = getMethod(testClassInstance, "tearDown");
		fRuns = runs;
		fVerbose = verbose;
	}

	@Override
	public int countTestCases() {
		return 1;
	}

	@Override
	public void runTest() {
		new RunnerFactory().createRunner(fGenerator, fRuns, characteristic(), fVerbose).forAll();
	}

	/**
	 * @return
	 */
	private Characteristic characteristic() {
		return new Characteristic() {

			public void setUp() throws Exception {
				if (fSetupMethod != null) {
					fSetupMethod.invoke(fInstance);
				}
			}

			public void specify(Object instance) throws Throwable {
				try {
					fMethod.invoke(fInstance, instance);
				} catch (InvocationTargetException e) {
					// exceptions thrown by characteristic methods may not
					// be part of their signatures so there's a
					// invocation target exception thrown
					throw e.getCause();
				}
			}

			public void tearDown() throws Exception {
				if (fTearDownMethod != null) {
					fTearDownMethod.invoke(fInstance);
				}
			}

			public String name() {
				return getClass().getName();
			}
		};
	}

	@Override
	public String getName() {
		return String.format("%s.%s[%s]", fInstance.getClass().getName(),
				fMethod.getName(), fGenerator.getClass().getName());
	}

	private static Method getMethod(Object newInstance, String methodName) {
		try {
			return newInstance.getClass().getMethod(methodName);
		} catch (NoSuchMethodException e) {
			return null;
		}
	}
}