1 package org.junit.tests.experimental.theories.internal; 2 3 import static org.hamcrest.CoreMatchers.containsString; 4 import static org.hamcrest.CoreMatchers.is; 5 import static org.hamcrest.CoreMatchers.not; 6 import static org.junit.Assert.assertThat; 7 import static org.junit.Assert.assertFalse; 8 import static org.junit.Assume.assumeThat; 9 10 import org.junit.Test; 11 import org.junit.experimental.theories.DataPoint; 12 import org.junit.experimental.theories.Theories; 13 import org.junit.experimental.theories.Theory; 14 import org.junit.experimental.theories.internal.ParameterizedAssertionError; 15 import org.junit.runner.RunWith; 16 17 @RunWith(Theories.class) 18 public class ParameterizedAssertionErrorTest { 19 @DataPoint 20 public static final String METHOD_NAME = "methodName"; 21 22 @DataPoint 23 public static final NullPointerException NULL_POINTER_EXCEPTION = new NullPointerException(); 24 25 @DataPoint 26 public static Object[] NO_OBJECTS = new Object[0]; 27 28 @DataPoint 29 public static ParameterizedAssertionError A = new ParameterizedAssertionError( 30 NULL_POINTER_EXCEPTION, METHOD_NAME); 31 32 @DataPoint 33 public static ParameterizedAssertionError B = new ParameterizedAssertionError( 34 NULL_POINTER_EXCEPTION, METHOD_NAME); 35 36 @DataPoint 37 public static ParameterizedAssertionError B2 = new ParameterizedAssertionError( 38 NULL_POINTER_EXCEPTION, "methodName2"); 39 40 @Theory 41 public void equalParameterizedAssertionErrorsHaveSameToString( 42 ParameterizedAssertionError a, ParameterizedAssertionError b) { 43 assumeThat(a, is(b)); 44 assertThat(a.toString(), is(b.toString())); 45 } 46 47 @Theory 48 public void differentParameterizedAssertionErrorsHaveDifferentToStrings( 49 ParameterizedAssertionError a, ParameterizedAssertionError b) { 50 assumeThat(a, not(b)); 51 assertThat(a.toString(), not(b.toString())); 52 } 53 54 @Theory 55 public void equalsReturnsTrue(Throwable targetException, String methodName, 56 Object[] params) { 57 assertThat( 58 new ParameterizedAssertionError(targetException, methodName, params), 59 is(new ParameterizedAssertionError(targetException, methodName, params))); 60 } 61 62 @Theory 63 public void sameHashCodeWhenEquals(Throwable targetException, String methodName, 64 Object[] params) { 65 ParameterizedAssertionError one = new ParameterizedAssertionError( 66 targetException, methodName, params); 67 ParameterizedAssertionError two = new ParameterizedAssertionError( 68 targetException, methodName, params); 69 assumeThat(one, is(two)); 70 71 assertThat(one.hashCode(), is(two.hashCode())); 72 } 73 74 @Theory(nullsAccepted = false) 75 public void buildParameterizedAssertionError(String methodName, String param) { 76 assertThat(new ParameterizedAssertionError( 77 new RuntimeException(), methodName, param).toString(), 78 containsString(methodName)); 79 } 80 81 @Theory 82 public void isNotEqualToNull(ParameterizedAssertionError a) { 83 assertFalse(a.equals(null)); 84 } 85 86 @Test 87 public void canJoinWhenToStringFails() { 88 assertThat(ParameterizedAssertionError.join(" ", new Object() { 89 @Override 90 public String toString() { 91 throw new UnsupportedOperationException(); 92 } 93 }), is("[toString failed]")); 94 } 95 }