001: package org.junit.experimental.test.theories;
002:
003: import static org.hamcrest.CoreMatchers.is;
004: import static org.hamcrest.CoreMatchers.not;
005: import static org.hamcrest.CoreMatchers.nullValue;
006: import static org.junit.Assert.assertThat;
007: import static org.junit.matchers.Each.each;
008: import static org.junit.matchers.StringContains.containsString;
009:
010: import java.lang.reflect.Method;
011: import java.util.ArrayList;
012: import java.util.Date;
013: import java.util.List;
014:
015: import org.hamcrest.Matcher;
016: import org.junit.Test;
017: import org.junit.experimental.theories.DataPoint;
018: import org.junit.experimental.theories.ParameterSignature;
019: import org.junit.experimental.theories.PotentialParameterValue;
020: import org.junit.experimental.theories.Theories;
021: import org.junit.experimental.theories.Theory;
022: import org.junit.runner.JUnitCore;
023: import org.junit.runner.RunWith;
024: import org.junit.runner.notification.Failure;
025:
026: public class DataPointMethodTest {
027: @RunWith(Theories.class)
028: public static class HasDataPointMethod {
029: @DataPoint
030: public int oneHundred() {
031: return 100;
032: }
033:
034: @Theory
035: public void allIntsOk(int x) {
036:
037: }
038: }
039:
040: @RunWith(Theories.class)
041: public static class HasUglyDataPointMethod {
042: @DataPoint
043: public int oneHundred() {
044: return 100;
045: }
046:
047: @DataPoint
048: public int oneUglyHundred() {
049: throw new RuntimeException();
050: }
051:
052: @Theory
053: public void allIntsOk(int x) {
054:
055: }
056: }
057:
058: @Test
059: public void pickUpDataPointMethods() {
060: assertThat(failures(HasDataPointMethod.class), empty());
061: }
062:
063: @Test
064: public void ignoreExceptionsFromDataPointMethods() {
065: assertThat(failures(HasUglyDataPointMethod.class), empty());
066: }
067:
068: @RunWith(Theories.class)
069: public static class DataPointMethodReturnsMutableObject {
070: @DataPoint
071: public List<Object> empty() {
072: return new ArrayList<Object>();
073: }
074:
075: public static int ONE = 1;
076:
077: public static int TWO = 2;
078:
079: @Theory
080: public void everythingsEmpty(List<Object> first, int number) {
081: assertThat(first.size(), is(0));
082: first.add("a");
083: }
084: }
085:
086: @Test
087: public void mutableObjectsAreCreatedAfresh() {
088: assertThat(failures(DataPointMethodReturnsMutableObject.class),
089: empty());
090: }
091:
092: @RunWith(Theories.class)
093: public static class HasDateMethod {
094: @DataPoint
095: public int oneHundred() {
096: return 100;
097: }
098:
099: public Date notADataPoint() {
100: return new Date();
101: }
102:
103: @Theory
104: public void allIntsOk(int x) {
105:
106: }
107:
108: @Theory
109: public void onlyStringsOk(String s) {
110:
111: }
112:
113: @Theory
114: public void onlyDatesOk(Date d) {
115:
116: }
117: }
118:
119: @Test
120: public void ignoreDataPointMethodsWithWrongTypes() throws Exception {
121: assertThat(potentialValues(
122: HasDateMethod.class.getMethod("onlyStringsOk",
123: String.class)).toString(),
124: not(containsString("100")));
125: }
126:
127: @Test
128: public void ignoreDataPointMethodsWithoutAnnotation()
129: throws Throwable {
130: assertThat(potentialValues(
131: HasDateMethod.class
132: .getMethod("onlyDatesOk", Date.class)).size(),
133: is(0));
134: }
135:
136: private List<PotentialParameterValue> potentialValues(Method method)
137: throws Exception {
138: return ParameterSignature.signatures(method).get(0)
139: .getPotentialValues(new HasDateMethod());
140: }
141:
142: private List<Failure> failures(Class<?> type) {
143: return JUnitCore.runClasses(type).getFailures();
144: }
145:
146: private Matcher<Iterable<Failure>> empty() {
147: Matcher<Failure> nullValue = nullValue();
148: return each(nullValue);
149: }
150: }
|