• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package junitparams.internal;
2 
3 import org.junit.Before;
4 import org.junit.Ignore;
5 import org.junit.Test;
6 import org.junit.runner.Description;
7 import org.junit.runner.RunWith;
8 import org.junit.runners.model.FrameworkMethod;
9 import org.junit.runners.model.TestClass;
10 
11 import junitparams.JUnitParamsRunner;
12 import junitparams.Parameters;
13 
14 import static org.assertj.core.api.Assertions.assertThat;
15 import static org.junit.Assert.*;
16 
17 @RunWith(JUnitParamsRunner.class)
18 public class TestMethodTest {
19     TestMethod plainTestMethod;
20     TestMethod arrayTestMethod;
21 
22     @Before
setUp()23     public void setUp() throws Exception {
24         plainTestMethod = new TestMethod(new FrameworkMethod(TestMethodTest.class.getMethod("forOthersToWork", new Class[]{String.class})),
25                 new TestClass(this.getClass()));
26         arrayTestMethod = new TestMethod(new FrameworkMethod(TestMethodTest.class.getMethod("forOthersToWorkWithArray",
27                 new Class[]{(new String[]{}).getClass()})),
28                 new TestClass(this.getClass()));
29     }
30 
31     @Test
32     @Parameters({"a","b"})
forOthersToWork(String arg)33     public void forOthersToWork(String arg) throws Exception {
34         assertThat(arg).isIn("a","b");
35     }
36 
37 
38     @Test
39     @Parameters({"a,b","b,a"})
forOthersToWorkWithArray(String... arg)40     public void forOthersToWorkWithArray(String... arg) throws Exception {
41         assertThat(arg).containsOnlyOnce("a","b");
42     }
43 
44     @Test
45     @Ignore
flatTestMethodStructure()46     public void flatTestMethodStructure() throws Exception {
47         System.setProperty("JUnitParams.flat", "true");
48 
49         Description description = plainTestMethod.describableFrameworkMethod().getDescription();
50 
51         assertEquals("for_others_to_work(junitparams.internal.TestMethodTest)", description.getDisplayName());
52         assertTrue(description.getChildren().isEmpty());
53         System.clearProperty("JUnitParams.flat");
54     }
55 
56 
57     // Android-changed: CTS and AndroidJUnitRunner rely on specific format to test names, changing
58     // them will prevent CTS and AndroidJUnitRunner from working properly; see b/36541809
59     @Ignore
60     @Test
hierarchicalTestMethodStructure()61     public void hierarchicalTestMethodStructure() throws Exception {
62         System.clearProperty("JUnitParams.flat");
63         Description description = plainTestMethod.describableFrameworkMethod().getDescription();
64 
65         assertEquals("forOthersToWork", description.getDisplayName());
66         assertEquals("[0] a (forOthersToWork)(junitparams.internal.TestMethodTest)", description.getChildren().get(0).getDisplayName());
67         assertEquals("[1] b (forOthersToWork)(junitparams.internal.TestMethodTest)", description.getChildren().get(1).getDisplayName());
68     }
69 
70     // Android-changed: CTS and AndroidJUnitRunner rely on specific format to test names, changing
71     // them will prevent CTS and AndroidJUnitRunner from working properly; see b/36541809
72     @Ignore
73     @Test
hierarchicalArrayTestMethodStructure()74     public void hierarchicalArrayTestMethodStructure() throws Exception {
75         System.clearProperty("JUnitParams.flat");
76         Description description = arrayTestMethod.describableFrameworkMethod().getDescription();
77 
78         assertEquals("forOthersToWorkWithArray", description.getDisplayName());
79         assertEquals("[0] a,b (forOthersToWorkWithArray)(junitparams.internal.TestMethodTest)",
80                 description.getChildren().get(0).getDisplayName());
81         assertEquals("[1] b,a (forOthersToWorkWithArray)(junitparams.internal.TestMethodTest)",
82                 description.getChildren().get(1).getDisplayName());
83     }
84 
85     @Test
86     @Parameters
testVarargs(String... strings)87     public void testVarargs(String... strings){
88     	assertArrayEquals("Hello world".split(" "), strings);
89     }
90 
parametersForTestVarargs()91     protected Object[] parametersForTestVarargs(){
92         return new Object[]{
93                 new Object[]{"Hello", "world"}
94     	};
95     }
96 
97     @Test
98     @Parameters
testVarargsCustomClass(Pair... pairs)99     public void testVarargsCustomClass(Pair... pairs){
100 		assertEquals(pairs[0].x, pairs[0].y);
101 		assertEquals(pairs[1].x, pairs[1].y);
102 		assertNotEquals(pairs[2].x, pairs[2].y);
103     }
104 
parametersForTestVarargsCustomClass()105     protected Object[] parametersForTestVarargsCustomClass(){
106         return new Object[]{
107                 new Object[]{new Pair(0, 0), new Pair(1, 1), new Pair(2, 3)}
108     	};
109     }
110 
111     @Test
112     @Parameters
testVarargsMoreArgs(int sumOfX, int sumOfY, Pair... pairs)113     public void testVarargsMoreArgs(int sumOfX, int sumOfY, Pair... pairs){
114         int sumOfXFromPairs = 0;
115         int sumOfYFromPairs = 0;
116         for (Pair pair : pairs) {
117             sumOfXFromPairs += pair.x;
118             sumOfYFromPairs += pair.y;
119         }
120         assertEquals(sumOfX, sumOfXFromPairs);
121         assertEquals(sumOfY, sumOfYFromPairs);
122     }
123 
parametersForTestVarargsMoreArgs()124     protected Object parametersForTestVarargsMoreArgs(){
125         return new Object[]{new Object[]{40, 50, new Pair(17, 21), new Pair(12, 18), new Pair(11, 11)}, new Object[]{10, 20, new Pair(3,
126                 15), new Pair(7, 5)}};
127     }
128 
129     @Test
130     @Parameters
testVargsMoreArgsOfTheSameType(Pair sum, Pair... pairs)131     public void testVargsMoreArgsOfTheSameType(Pair sum, Pair... pairs) {
132         assertEquals(sum.x, pairs[0].x + pairs[1].x);
133         assertEquals(sum.y, pairs[0].y + pairs[1].y);
134     }
135 
parametersForTestVargsMoreArgsOfTheSameType()136     protected Object parametersForTestVargsMoreArgsOfTheSameType(){
137         return new Object[]{new Object[]{new Pair(10, 30), new Pair(7, 17), new Pair(3, 13)}, new Object[]{new Pair(20, 40), new Pair(18,
138                 21), new Pair(2, 19)}};
139     }
140 
141     @Test
142     @Parameters(method = "nullArray")
varargsCheckPassesWithNullArray(boolean isNull, String... array)143     public void varargsCheckPassesWithNullArray(boolean isNull, String... array) throws Exception {
144         assertEquals(isNull, array == null);
145     }
146 
nullArray()147     private Object nullArray() {
148         return new Object[] {
149                 new Object[] { false, new String[] { "1", "2" } },
150                 new Object[] { true, null },
151         };
152     }
153 
154     private class Pair{
155     	int x,y;
Pair(int x, int y)156     	public Pair(int x, int y){
157     		this.x = x;
158     		this.y = y;
159     	}
160     }
161 }
162