From: David S. <ds...@us...> - 2007-07-02 18:11:21
|
Update of /cvsroot/junit/junit/src/org/junit/experimental/theories/runner In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv27902/src/org/junit/experimental/theories/runner Added Files: ParameterizedAssertionError.java TheoryContainerReference.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: ParameterizedAssertionError.java --- /** * */ package org.junit.experimental.theories.runner; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; public class ParameterizedAssertionError extends RuntimeException { private static final long serialVersionUID = 1L; public ParameterizedAssertionError(Throwable targetException, String methodName, Object... params) { super(String.format("%s(%s)", methodName, join(", ", params)), targetException); } @Override public boolean equals(Object obj) { return toString().equals(obj.toString()); } public static String join(String delimiter, Object... params) { return join(delimiter, Arrays.asList(params)); } public static String join(String delimiter, Collection<Object> values) { StringBuffer buffer = new StringBuffer(); Iterator<Object> iter = values.iterator(); while (iter.hasNext()) { Object next = iter.next(); buffer.append(String.valueOf(next)); if (iter.hasNext()) { buffer.append(delimiter); } } return buffer.toString(); } } --- NEW FILE: TheoryContainerReference.java --- /** * */ package org.junit.experimental.theories.runner; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.junit.Assume.AssumptionViolatedException; import org.junit.experimental.theories.javamodel.api.ConcreteFunction; import org.junit.experimental.theories.methods.api.ParameterSignature; import org.junit.experimental.theories.methods.api.ParameterSupplier; import org.junit.experimental.theories.runner.api.Theories.TheoryMethod; public class TheoryContainerReference { private final Object container; public TheoryContainerReference(Object target) { this.container= target; } public int invoke(TheoryMethod method, Object[] params) throws Throwable { try { getTheoryFunction(method).invoke(params); } catch (AssumptionViolatedException e) { method.addAssumptionFailure(e); return 0; } catch (Throwable e) { if (params.length == 0) throw e; throw new ParameterizedAssertionError(e, method.getMethod() .getName(), params); } return 1; } private ConcreteFunction getTheoryFunction(TheoryMethod method) { return new ConcreteFunction(container, method.getMethod()); } public List<?> getPotentialValues(ParameterSignature sig) throws InstantiationException, IllegalAccessException { return getSupplier(sig).getValues(container, sig); } private ParameterSupplier getSupplier(ParameterSignature sig) throws InstantiationException, IllegalAccessException { ParameterSupplier supplier= getAnnotatedSupplier(sig); if (supplier != null) return supplier; return fieldParameterSupplier(); } private ParameterSupplier getAnnotatedSupplier(ParameterSignature sig) throws InstantiationException, IllegalAccessException { Annotation annotation= sig.getSupplierAnnotation(); if (annotation != null) return sig.getSupplier(annotation).value().newInstance(); return null; } private ParameterSupplier fieldParameterSupplier() { return new ParameterSupplier() { @Override public List<Object> getValues(Object test, ParameterSignature sig) { ArrayList<Object> list= new ArrayList<Object>(); for (Field field : container.getClass().getFields()) { if (sig.canAcceptField(field)) { try { list.add(field.get(test)); } catch (IllegalArgumentException e) { throw new RuntimeException( "unexpected: field from getClass doesn't exist on object"); } catch (IllegalAccessException e) { throw new RuntimeException( "unexpected: getFields returned an inaccessible field"); } } } for (Method method : container.getClass().getMethods()) { if (method.getParameterTypes().length == 0 && sig.canAcceptMethod(method)) { try { list.add(method.invoke(test)); } catch (IllegalArgumentException e) { throw new RuntimeException( "unexpected: argument length is checked"); } catch (IllegalAccessException e) { throw new RuntimeException( "unexpected: getMethods returned an inaccessible method"); } catch (InvocationTargetException e) { // do nothing, just look for more values } } } return list; } }; } public int runWithParameters(TheoryMethod method, List<Object> values, List<ParameterSignature> sigs) throws Throwable { if (sigs.size() == 0) { return invoke(method, values.toArray()); } int count= 0; for (Object value : getPotentialValues(sigs.get(0))) { if (value != null || method.nullsOk()) count+= runWithParameters(method, concat(values, value), sigs .subList(1, sigs.size())); } return count; } private List<Object> concat(List<Object> values, Object value) { ArrayList<Object> list= new ArrayList<Object>(); list.addAll(values); list.add(value); return list; } } |