CVS: junit/src/org/junit/experimental/theories/test/javamodel ConcreteFunctionTest.java, NONE, 1.1 FunctionTest.java, NONE, 1.1

David Saff <[email protected]> Mon, 02 Jul 2007 11:11:00 -0700
Newsgroups gmane.comp.java.junit.devel
Message-ID <[email protected]>
Update of /cvsroot/junit/junit/src/org/junit/experimental/theories/test/javamodel
In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv27902/src/org/junit/experimental/theories/test/javamodel

Added Files:
	ConcreteFunctionTest.java FunctionTest.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: ConcreteFunctionTest.java ---
package org.junit.experimental.theories.test.javamodel;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;

import java.lang.reflect.Method;

import org.junit.experimental.theories.javamodel.api.ConcreteFunction;
import org.junit.experimental.theories.methods.api.Theory;
import org.junit.experimental.theories.runner.api.Theories;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class ConcreteFunctionTest {
	public static Method TO_STRING;

	public static Method WAIT;

	static {
		try {
			TO_STRING= Object.class.getMethod("toString");
			WAIT= Object.class.getMethod("wait");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static ConcreteFunction zeroToString1= new ConcreteFunction(0,
			TO_STRING);

	public static ConcreteFunction zeroToString2= new ConcreteFunction(0,
			TO_STRING);

	public static ConcreteFunction oneToString1= new ConcreteFunction(1,
			TO_STRING);

	public static ConcreteFunction oneFinalize= new ConcreteFunction(1, WAIT);

	public static String ROB_KUTNER= "Rob Kutner";

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

	@Theory
	public void unequalMethodsMeansUnequalFunctions(Method m1, Method m2,
			Object o) {
		assumeThat(m1, not(m2));
		assertThat(new ConcreteFunction(o, m1),
				not(new ConcreteFunction(o, m2)));
	}

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

	@SuppressWarnings("unchecked")
	@Theory
	public void throwsUsefulErrorWhenParameterNumberWrong(String string)
			throws Throwable {
		try {
			new ConcreteFunction(this, TO_STRING).invoke(string);
		} catch (Exception e) {
			assertThat(e, allOf(hasToString(containsString(string)),
					hasToString(containsString(TO_STRING.toString()))));
		}
	}
}

--- NEW FILE: FunctionTest.java ---
package org.junit.experimental.theories.test.javamodel;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.imposterization.FunctionPointer;
import org.junit.experimental.theories.methods.api.TestedOn;

public class FunctionTest {
	public static class HasAnnotation {
		public void something(@TestedOn(ints= { 3 })
		int x) {
		}
	}

	FunctionPointer function= FunctionPointer.pointer();

	@Before
	public void functionPoints() {
		function.calls(new HasAnnotation()).something(4);
	}

	@SuppressWarnings("unchecked")
	@Test
	public void getSupplierAnnotation() {
		assertThat(function.signatures().get(0).getSupplierAnnotation(),
				is(TestedOn.class));
	}

	@Test
	public void thrownReturnsNullIfNormalReturn() {
		assertThat(function.exceptionThrown(4), nullValue());
	}
}


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