CVS: junit/src/org/junit/experimental/theories/test/runner TheoriesTest.java, NONE, 1.1 DataPointMethodTest.java, NONE, 1.1 ParameterizedAssertionErrorTest.java, NONE, 1.1 TheoryContainerReferenceTest.java, NONE, 1.1
David Saff <[email protected]> Mon, 02 Jul 2007 11:11:13 -0700
| Newsgroups | gmane.comp.java.junit.devel |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/junit/junit/src/org/junit/experimental/theories/test/runner
In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv27902/src/org/junit/experimental/theories/test/runner
Added Files:
TheoriesTest.java DataPointMethodTest.java
ParameterizedAssertionErrorTest.java
TheoryContainerReferenceTest.java
Log Message:
- The hamcrest-core-1.1 library is now included in the JUnit distribution. For more hamcrest matchers, see the hamcrest-library jar from http://code.google.com/p/hamcrest
- The Popper Theory runner (http://popper.tigris.org) has been absorbed into the JUnit project, under the package name org.junit.experimental.theories
- Several additional libraries used in the theories tests have been added in a new testlib directory
- New "assertThat" statement to work with hamcrest matchers:
// same as assertEquals(3, x)
assertThat(x, is(3));
// same as assertNull(y)
assertThat(y, nullValue());
- New feature: assumeThat. A failed assumption will cause the test to pass, without further execution. (The behavior of assumeThat may change in the future to allow richer reporting of tests that are skipped because of failed assumptions)
// pass on any non-Windows system
@Test public void getRootDrive() {
assumeThat(getOsString(), is("Windows"));
getFile("C:\");
// ...
}
- Convenience assumption functions:
// none of these are null
assumeNotNull(a, b, c);
assumeTrue(everythingOk());
try {
getDatabaseConnection();
} catch (Exception e) {
assumeNoException(e);
}
- Documentation fixed for many assertEquals array methods
- Two bugs in numeric equality fixed:
1718905: assertEquals does not compare float correctly
1715326: assertEquals does not compare java.math.BigDecimal properly
- The protocol for overriding JUnit4ClassRunner has changed again. Please see the source for details.
- Extenders can now extend TestMethod to describe the behavior of running methods that do not have a @Test annotation.
- Adding Annotations to Description caused a binary compatibility problem with clients compiled on previous JUnit versions. This has been fixed.
--- NEW FILE: TheoriesTest.java ---
package org.junit.experimental.theories.test.runner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
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)
.getFailures().size(), is(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: DataPointMethodTest.java ---
package org.junit.experimental.theories.test.runner;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.experimental.theories.matchers.api.Each;
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.each(nullValue);
}
}
--- NEW FILE: ParameterizedAssertionErrorTest.java ---
package org.junit.experimental.theories.test.runner;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
import org.hamcrest.Matchers;
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(), Matchers
.containsString(methodName));
}
}
--- NEW FILE: TheoryContainerReferenceTest.java ---
package org.junit.experimental.theories.test.runner;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.util.Date;
import java.util.List;
import org.hamcrest.Matchers;
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;
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(), hasToString(containsString("100")));
}
@Test public void ignoreDataPointMethodsWithWrongTypes()
throws SecurityException, InstantiationException,
IllegalAccessException {
method.calls(test).onlyStringsOk(null);
assertThat(potentialValues().toString(), Matchers
.not(hasToString(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));
}
}
-------------------------------------------------------------------------
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/