1 package org.junit.tests.experimental.theories.runner; 2 3 import static org.hamcrest.CoreMatchers.is; 4 import static org.hamcrest.CoreMatchers.not; 5 import static org.hamcrest.CoreMatchers.notNullValue; 6 import static org.junit.Assert.assertThat; 7 import static org.junit.experimental.results.PrintableResult.testResult; 8 import static org.junit.experimental.results.ResultMatchers.isSuccessful; 9 10 import org.junit.Test; 11 import org.junit.experimental.results.ResultMatchers; 12 import org.junit.experimental.theories.DataPoint; 13 import org.junit.experimental.theories.DataPoints; 14 import org.junit.experimental.theories.Theories; 15 import org.junit.experimental.theories.Theory; 16 import org.junit.experimental.theories.suppliers.TestedOn; 17 import org.junit.runner.JUnitCore; 18 import org.junit.runner.RunWith; 19 20 public class WithExtendedParameterSources { 21 @RunWith(Theories.class) 22 public static class ParameterAnnotations { 23 @Theory 24 public void everythingIsOne(@TestedOn(ints = {1}) 25 int number) { 26 assertThat(number, is(1)); 27 } 28 } 29 30 @Test 31 public void testedOnLimitsParameters() throws Exception { 32 assertThat(testResult(ParameterAnnotations.class), ResultMatchers 33 .isSuccessful()); 34 } 35 36 @RunWith(Theories.class) 37 public static class ShouldFilterOutNullSingleDataPoints { 38 39 @DataPoint 40 public static String A = "a"; 41 42 @DataPoint 43 public static String NULL = null; 44 45 @Theory(nullsAccepted = false) 46 public void allStringsAreNonNull(String s) { 47 assertThat(s, notNullValue()); 48 } 49 } 50 51 @Test 52 public void shouldFilterOutNullSingleDataPoints() { 53 assertThat(testResult(ShouldFilterOutNullSingleDataPoints.class), isSuccessful()); 54 } 55 56 @RunWith(Theories.class) 57 public static class ShouldFilterOutNullElementsFromDataPointArrays { 58 @DataPoints 59 public static String[] SOME_NULLS = { "non-null", null }; 60 61 @Theory(nullsAccepted = false) 62 public void allStringsAreNonNull(String s) { 63 assertThat(s, notNullValue()); 64 } 65 } 66 67 @Test 68 public void shouldFilterOutNullElementsFromDataPointArrays() { 69 assertThat(testResult(ShouldFilterOutNullElementsFromDataPointArrays.class), isSuccessful()); 70 } 71 72 @RunWith(Theories.class) 73 public static class ShouldRejectTheoriesWithOnlyDisallowedNullData { 74 @DataPoints 75 public static String value = null; 76 77 @Theory(nullsAccepted = false) 78 public void allStringsAreNonNull(String s) { 79 } 80 } 81 82 @Test 83 public void ShouldRejectTheoriesWithOnlyDisallowedNullData() { 84 assertThat(testResult(ShouldRejectTheoriesWithOnlyDisallowedNullData.class), not(isSuccessful())); 85 } 86 87 @RunWith(Theories.class) 88 public static class DataPointArrays { 89 public static String log = ""; 90 91 @DataPoints 92 public static String[] STRINGS = new String[]{"A", "B"}; 93 94 @Theory 95 public void addToLog(String string) { 96 log += string; 97 } 98 } 99 100 @Test 101 public void getDataPointsFromArray() { 102 DataPointArrays.log = ""; 103 JUnitCore.runClasses(DataPointArrays.class); 104 assertThat(DataPointArrays.log, is("AB")); 105 } 106 107 @RunWith(Theories.class) 108 public static class DataPointArrayMethod { 109 public static String log = ""; 110 111 @DataPoints 112 public static String[] STRINGS() { 113 return new String[]{"A", "B"}; 114 } 115 116 ; 117 118 @Theory 119 public void addToLog(String string) { 120 log += string; 121 } 122 } 123 124 @Test 125 public void getDataPointsFromArrayMethod() { 126 DataPointArrayMethod.log = ""; 127 JUnitCore.runClasses(DataPointArrayMethod.class); 128 assertThat(DataPointArrayMethod.log, is("AB")); 129 } 130 131 @RunWith(Theories.class) 132 public static class DataPointMalformedArrayMethods { 133 public static String log = ""; 134 135 @DataPoints 136 public static String[] STRINGS() { 137 return new String[]{"A", "B"}; 138 } 139 140 ; 141 142 @DataPoints 143 public static String STRING() { 144 return "C"; 145 } 146 147 @DataPoints 148 public static int[] INTS() { 149 return new int[]{1, 2, 3}; 150 } 151 152 @Theory 153 public void addToLog(String string) { 154 log += string; 155 } 156 } 157 158 @Test 159 public void getDataPointsFromArrayMethodInSpiteOfMalformedness() { 160 DataPointArrayMethod.log = ""; 161 JUnitCore.runClasses(DataPointArrayMethod.class); 162 assertThat(DataPointArrayMethod.log, is("AB")); 163 } 164 165 @RunWith(Theories.class) 166 public static class DataPointArrayToBeUsedForWholeParameter { 167 public static String log = ""; 168 169 @DataPoint 170 public static String[] STRINGS = new String[]{"A", "B"}; 171 172 @Theory 173 public void addToLog(String[] strings) { 174 log += strings[0]; 175 } 176 } 177 178 @Test 179 public void dataPointCanBeArray() { 180 DataPointArrayToBeUsedForWholeParameter.log = ""; 181 JUnitCore.runClasses(DataPointArrayToBeUsedForWholeParameter.class); 182 assertThat(DataPointArrayToBeUsedForWholeParameter.log, is("A")); 183 } 184 }