From: David S. <ds...@us...> - 2007-07-02 18:11:08
|
Update of /cvsroot/junit/junit/src/org/junit/experimental/imposterization In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv27902/src/org/junit/experimental/imposterization Added Files: FunctionPointer.java ThrownMatcher.java PopperImposterizer.java AssumePassing.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: FunctionPointer.java --- package org.junit.experimental.imposterization; import java.lang.reflect.Method; import org.jmock.api.Invocation; import org.jmock.api.Invokable; import org.junit.experimental.theories.javamodel.api.Function; public class FunctionPointer extends Function { public static FunctionPointer pointer() { return new FunctionPointer(); } private Method method; private Object target; @SuppressWarnings("unchecked") public <T> T calls(T target) { this.target = target; Invokable invokable = new Invokable() { public Object invoke(Invocation invocation) throws Throwable { Method invokedMethod = invocation.getInvokedMethod(); if (!invokedMethod.getName().equals("finalize")) method = invokedMethod; return null; } }; return (T) new PopperImposterizer(invokable).imposterize(target.getClass()); } @Override public Method getMethod() { return method; } @Override public Object getTarget() { return target; } } --- NEW FILE: ThrownMatcher.java --- /** * */ package org.junit.experimental.imposterization; import org.hamcrest.Matcher; import org.hamcrest.Matchers; import org.jmock.api.Invocation; import org.jmock.api.Invokable; import org.junit.Assert; public class ThrownMatcher { public static class IncorrectThrownException extends RuntimeException { public IncorrectThrownException(Matcher<?> matcher, Throwable thrown) { super("Expected: " + matcher.toString(), thrown); } private static final long serialVersionUID = 1L; } private final Matcher<?> matcher; private ThrownMatcher(final Matcher<?> matcher) { this.matcher = matcher; } @SuppressWarnings("unchecked") public <T> T when(final T target) { return (T) createWhenObject(target); } private <T> Object createWhenObject(final T target) { return new PopperImposterizer(new Invokable() { public Object invoke(Invocation invocation) throws Throwable { try { invocation.applyTo(target); } catch (Throwable thrown) { if (!matcher.matches(thrown)) { throw new IncorrectThrownException(matcher, thrown); } return null; } Assert.assertThat(null, matcher); return null; } }).imposterize(target.getClass()); } public static ThrownMatcher assertReturnsNormally() { return assertThrownException(Matchers.is(((Object) null))); } public static ThrownMatcher assertThrownException(Matcher<?> theMatcher) { return new ThrownMatcher(theMatcher); } } --- NEW FILE: PopperImposterizer.java --- /** * */ package org.junit.experimental.imposterization; import org.jmock.api.Imposteriser; import org.jmock.api.Invokable; import org.jmock.lib.legacy.ClassImposteriser; public class PopperImposterizer { private final Invokable invokable; public PopperImposterizer(Invokable invokable) { this.invokable = invokable; } @SuppressWarnings("unchecked") public <T> T imposterize(Class<T> type) { Imposteriser imposterizer = ClassImposteriser.INSTANCE; if (type.getName().contains("EnhancerByCGLIB")) { return (T) imposterizer.imposterise(invokable, type.getSuperclass(), type.getInterfaces()); } else { return imposterizer.imposterise(invokable, type); } } } --- NEW FILE: AssumePassing.java --- /** * */ package org.junit.experimental.imposterization; import static org.hamcrest.CoreMatchers.nullValue; import org.jmock.api.Invocation; import org.jmock.api.Invokable; import org.junit.Assume.AssumptionViolatedException; public class AssumePassing { /** * This will stay here forever, but dependencies do not add long-term value. * The compile error will remind you to remove references when done with * dependency */ @Deprecated public static <T> T assumePasses(final Class<T> type) { return new PopperImposterizer(new Invokable() { public Object invoke(Invocation invocation) throws Throwable { try { invocation.applyTo(type.newInstance()); } catch (AssumptionViolatedException e) { return null; } catch (Throwable thrown) { throw new AssumptionViolatedException(thrown, nullValue()); } return null; } }).imposterize(type); } } |