1package junitparams.internal;
2
3import org.junit.Before;
4import org.junit.Ignore;
5import org.junit.Test;
6import org.junit.runner.Description;
7import org.junit.runner.RunWith;
8import org.junit.runners.model.FrameworkMethod;
9import org.junit.runners.model.TestClass;
10
11import junitparams.JUnitParamsRunner;
12import junitparams.Parameters;
13
14import static org.assertj.core.api.Assertions.assertThat;
15import static org.junit.Assert.*;
16
17@RunWith(JUnitParamsRunner.class)
18public class TestMethodTest {
19    TestMethod plainTestMethod;
20    TestMethod arrayTestMethod;
21
22    @Before
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"})
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"})
40    public void forOthersToWorkWithArray(String... arg) throws Exception {
41        assertThat(arg).containsOnlyOnce("a","b");
42    }
43
44    @Test
45    @Ignore
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
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
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
87    public void testVarargs(String... strings){
88    	assertArrayEquals("Hello world".split(" "), strings);
89    }
90
91    protected Object[] parametersForTestVarargs(){
92        return new Object[]{
93                new Object[]{"Hello", "world"}
94    	};
95    }
96
97    @Test
98    @Parameters
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
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
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
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
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
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")
143    public void varargsCheckPassesWithNullArray(boolean isNull, String... array) throws Exception {
144        assertEquals(isNull, array == null);
145    }
146
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;
156    	public Pair(int x, int y){
157    		this.x = x;
158    		this.y = y;
159    	}
160    }
161}
162