CVS: junit/src/org/junit/experimental/test/runner TheoryContainerReferenceTest.java, NONE, 1.1 DataPointMethodTest.java, NONE, 1.1 TheoriesTest.java, NONE, 1.1 ParameterizedAssertionErrorTest.java, NONE, 1.1

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

Added Files:
	TheoryContainerReferenceTest.java DataPointMethodTest.java 
	TheoriesTest.java ParameterizedAssertionErrorTest.java 
Log Message:
Re-organize theory packages

--- NEW FILE: TheoryContainerReferenceTest.java ---
package org.junit.experimental.test.runner;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.experimental.imposterization.FunctionPointer;
import org.junit.experimental.theories.methods.api.DataPoint;
import org.junit.experimental.theories.methods.api.ParameterSignature;
import org.junit.experimental.theories.methods.api.Theory;
import org.junit.experimental.theories.runner.TheoryContainerReference;
import org.junit.experimental.theories.runner.api.Theories;
import org.junit.runner.RunWith;
import static org.junit.matchers.StringContains.containsString;

public class TheoryContainerReferenceTest {
	private FunctionPointer method = new FunctionPointer();
	private final HasDataPointMethod test = new HasDataPointMethod();

	@RunWith(Theories.class)
	public static class HasDataPointMethod {
		@DataPoint public int oneHundred() {
			return 100;
		}

		public Date notADataPoint() {
			return new Date();
		}

		@Theory public void allIntsOk(int x) {

		}

		@Theory public void onlyStringsOk(String s) {

		}

		@Theory public void onlyDatesOk(Date d) {

		}
	}

	@Test public void pickUpDataPointMethods() throws SecurityException,
			InstantiationException, IllegalAccessException {
		method.calls(test).allIntsOk(0);
		assertThat(potentialValues().toString(), containsString("100"));
	}

	@Test public void ignoreDataPointMethodsWithWrongTypes()
			throws SecurityException, InstantiationException,
			IllegalAccessException {
		method.calls(test).onlyStringsOk(null);
		assertThat(potentialValues().toString(), not(containsString("100")));
	}

	@Test public void ignoreDataPointMethodsWithoutAnnotation()
			throws SecurityException, InstantiationException,
			IllegalAccessException {
		method.calls(test).onlyDatesOk(null);
		assertThat(potentialValues().size(), is(0));
	}

	private List<?> potentialValues() throws InstantiationException,
			IllegalAccessException {
		return new TheoryContainerReference(test)
				.getPotentialValues(ParameterSignature.signatures(
						method.getMethod()).get(0));
	}
}

--- NEW FILE: DataPointMethodTest.java ---
package org.junit.experimental.test.runner;

import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.Each.each;

import java.util.List;

import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.experimental.theories.methods.api.DataPoint;
import org.junit.experimental.theories.methods.api.Theory;
import org.junit.experimental.theories.runner.api.Theories;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;

public class DataPointMethodTest {
	@RunWith(Theories.class)
	public static class HasDataPointMethod {
		@DataPoint
		public int oneHundred() {
			return 100;
		}

		@Theory
		public void allIntsOk(int x) {

		}
	}
	
	@RunWith(Theories.class)
	public static class HasUglyDataPointMethod {
		@DataPoint
		public int oneHundred() {
			return 100;
		}

		@DataPoint
		public int oneUglyHundred() {
			throw new RuntimeException();
		}

		@Theory
		public void allIntsOk(int x) {

		}
	}

	@Test
	public void pickUpDataPointMethods() {
		assertThat(failures(HasDataPointMethod.class), empty());
	}

	@Test
	public void ignoreExceptionsFromDataPointMethods() {
		assertThat(failures(HasUglyDataPointMethod.class), empty());
	}

	private List<Failure> failures(Class<?> type) {
		return JUnitCore.runClasses(type).getFailures();
	}

	private Matcher<Iterable<Failure>> empty() {
		Matcher<Failure> nullValue= nullValue();
		return each(nullValue);
	}
}

--- NEW FILE: TheoriesTest.java ---
package org.junit.experimental.test.runner;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
import static org.junit.experimental.results.ResultMatchers.failureCountIs;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
import static org.junit.matchers.StringContains.containsString;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.experimental.results.PrintableResult;
import org.junit.experimental.results.ResultMatchers;
import org.junit.experimental.theories.methods.api.TestedOn;
import org.junit.experimental.theories.methods.api.Theory;
import org.junit.experimental.theories.runner.api.Theories;
import org.junit.internal.runners.TestClass;
import org.junit.runner.RunWith;

@SuppressWarnings("restriction")
@RunWith(Theories.class)
public class TheoriesTest {
	public static String SPACE= " ";

	public static String J= "J";

	public static String NULL_STRING= null;

	public static int ZERO= 0;

	public static int ONE= 1;

	public static int THREE= 3;

	public static int FIVE= 5;

	public static int INDEPENDENCE = 1776;
	
	public static Matcher<Integer> NOT_ZERO= not(0);

	public static Matcher<Integer> IS_ONE= is(1);


	@RunWith(Theories.class)
	public static class HasATheory {
		public static int ONE= 1;

		@Theory
		public void everythingIsZero(int x) {
			assertThat(x, is(0));
		}
	}

	@Test
	public void theoryClassMethodsShowUp() throws Exception {
		assertThat(new Theories(HasATheory.class).getDescription()
				.getChildren().size(), is(1));
	}

	@Test
	public void theoryAnnotationsAreRetained() throws Exception {
		assertThat(new TestClass(HasATheory.class).getAnnotatedMethods(
				Theory.class).size(), is(1));
	}

	@Test
	public void canRunTheories() throws Exception {
		assertThat(PrintableResult.testResult(HasATheory.class),
				hasSingleFailureContaining("Expected"));
	}
	
	@RunWith(Theories.class)
	public static class HasATwoParameterTheory {
		public static int ONE= 1;

		@Theory
		public void everythingIsZero(int x, int y) {
			assertThat(x, is(y));
		}
	}

	@Test
	public void canRunTwoParameterTheories() throws Exception {
		assertThat(PrintableResult.testResult(HasATwoParameterTheory.class),
				ResultMatchers.isSuccessful());
	}
	
	@RunWith(Theories.class)
	public static class DoesntUseParams {
		public static int ONE= 1;

		@Theory
		public void everythingIsZero(int x, int y) {
			assertThat(2, is(3));
		}
	}

	@Test
	public void reportBadParams() throws Exception {
		assertThat(PrintableResult.testResult(DoesntUseParams.class),
				hasSingleFailureContaining("everythingIsZero(1, 1)"));
	}

	@RunWith(Theories.class)
	public static class NullsOK {
		public static String NULL= null;

		public static String A= "A";

		@Theory
		public void everythingIsA(String a) {
			assertThat(a, is("A"));
		}
	}

	@Test
	public void nullsUsedUnlessProhibited() throws Exception {
		assertThat(PrintableResult.testResult(NullsOK.class),
				hasSingleFailureContaining("null"));
	}

	@RunWith(Theories.class)
	public static class ParameterAnnotations {
		@Theory
		public void everythingIsOne(@TestedOn(ints= { 1 })
		int number) {
			assertThat(number, is(1));
		}
	}

	@Test
	public void testedOnLimitsParameters() throws Exception {
		assertThat(PrintableResult.testResult(ParameterAnnotations.class),
				ResultMatchers.isSuccessful());
	}

	@RunWith(Theories.class)
	public static class HonorExpectedException {
		@Test(expected= NullPointerException.class)
		public void shouldThrow() {

		}
	}
	
	@Test
	public void honorExpected() throws Exception {
		assertThat(PrintableResult.testResult(HonorExpectedException.class)
				.getFailures().size(), is(1));
	}


	@RunWith(Theories.class)
	public static class HonorTimeout {
		@Test(timeout = 5)
		public void shouldStop() throws InterruptedException {
			while(true) {
				Thread.sleep(1000);
			}
		}
	}
	
	@Test
	public void honorTimeout() throws Exception {
		assertThat(PrintableResult.testResult(HonorTimeout.class), failureCountIs(1));
	}
	
	@RunWith(Theories.class)
	public static class AssumptionsFail {
		public static int DATA= 0;

		public static Matcher<Integer> MATCHER= null;

		@Theory
		public void nonZeroIntsAreFun(int x) {
			assumeThat(x, MATCHER);
		}
	}

	@Theory
	public void showFailedAssumptionsWhenNoParametersFound(int data,
			Matcher<Integer> matcher) throws Exception {
		assumeThat(data, not(matcher));
		AssumptionsFail.DATA= data;
		AssumptionsFail.MATCHER= matcher;

		String result= PrintableResult.testResult(AssumptionsFail.class)
				.toString();
		assertThat(result, containsString(matcher.toString()));
		assertThat(result, containsString("" + data));
	}
}

--- NEW FILE: ParameterizedAssertionErrorTest.java ---
package org.junit.experimental.test.runner;

import static org.junit.matchers.StringContains.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
import org.junit.experimental.theories.methods.api.Theory;
import org.junit.experimental.theories.runner.ParameterizedAssertionError;
import org.junit.experimental.theories.runner.api.Theories;
import org.junit.runner.RunWith;


@RunWith(Theories.class)
public class ParameterizedAssertionErrorTest {
	public static final String METHOD_NAME= "methodName";

	public static final NullPointerException NULL_POINTER_EXCEPTION= new NullPointerException();

	public static Object[] NO_OBJECTS= new Object[0];

	public static ParameterizedAssertionError A= new ParameterizedAssertionError(
			NULL_POINTER_EXCEPTION, METHOD_NAME);

	public static ParameterizedAssertionError B= new ParameterizedAssertionError(
			NULL_POINTER_EXCEPTION, METHOD_NAME);

	public static ParameterizedAssertionError B2= new ParameterizedAssertionError(
			NULL_POINTER_EXCEPTION, "methodName2");

	@Theory
	public void equalParameterizedAssertionErrorsHaveSameToString(
			ParameterizedAssertionError a, ParameterizedAssertionError b) {
		assumeThat(a, is(b));
		assertThat(a.toString(), is(b.toString()));
	}

	@Theory
	public void differentParameterizedAssertionErrorsHaveDifferentToStrings(
			ParameterizedAssertionError a, ParameterizedAssertionError b) {
		assumeThat(a, not(b));
		assertThat(a.toString(), not(b.toString()));
	}

	@Theory
	public void equalsReturnsTrue(Throwable targetException, String methodName,
			Object[] params) {
		assertThat(new ParameterizedAssertionError(targetException, methodName,
				params), is(new ParameterizedAssertionError(targetException,
				methodName, params)));
	}

	@SuppressWarnings("unchecked")
	@Theory(nullsAccepted= false)
	public void buildParameterizedAssertionError(String methodName, String param) {
		assertThat(new ParameterizedAssertionError(new RuntimeException(),
				methodName, param).toString(), containsString(methodName));
	}
}


-------------------------------------------------------------------------
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/