• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package junitparams.usage;
2 
3 import java.util.Arrays;
4 import java.util.Date;
5 import java.util.Iterator;
6 import java.util.List;
7 
8 import org.junit.Ignore;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 
12 import junitparams.FileParameters;
13 import junitparams.JUnitParamsRunner;
14 import junitparams.Parameters;
15 import junitparams.ParametersReaderProvidersTest;
16 import junitparams.ParamsConverterTest;
17 import junitparams.converters.Param;
18 import junitparams.custom.combined.CombinedParameters;
19 import junitparams.mappers.CsvWithHeaderMapper;
20 import junitparams.naming.TestCaseName;
21 import junitparams.usage.person_example.PersonMapper;
22 import junitparams.usage.person_example.PersonTest;
23 import junitparams.usage.person_example.PersonType;
24 
25 import static junitparams.JUnitParamsRunner.*;
26 import static org.assertj.core.api.Assertions.*;
27 
28 @RunWith(JUnitParamsRunner.class)
29 public class SamplesOfUsageTest {
30 
31     @Test
32     @Parameters({"AAA,1", "BBB,2"})
paramsInAnnotation(String p1, Integer p2)33     public void paramsInAnnotation(String p1, Integer p2) { }
34 
35     @Test
36     @Parameters({"AAA|1", "BBB|2"})
paramsInAnnotationPipeSeparated(String p1, Integer p2)37     public void paramsInAnnotationPipeSeparated(String p1, Integer p2) { }
38 
39     @Test
40     @Parameters
paramsInDefaultMethod(String p1, Integer p2)41     public void paramsInDefaultMethod(String p1, Integer p2) { }
parametersForParamsInDefaultMethod()42     private Object parametersForParamsInDefaultMethod() {
43         return new Object[]{new Object[]{"AAA", 1}, new Object[]{"BBB", 2}};
44     }
45 
46     @Test
47     @Parameters(method = "named1")
paramsInNamedMethod(String p1, Integer p2)48     public void paramsInNamedMethod(String p1, Integer p2) { }
named1()49     private Object named1() {
50         return new Object[]{"AAA", 1};
51     }
52 
53     @Test
54     @Parameters(method = "named2,named3")
paramsInMultipleMethods(String p1, Integer p2)55     public void paramsInMultipleMethods(String p1, Integer p2) { }
named2()56     private Object named2() {
57         return new Object[]{"AAA", 1};
58     }
named3()59     private Object named3() {
60         return new Object[]{"BBB", 2};
61     }
62 
63     @Test
64     @Parameters(method = "named4")
paramsWithVarargs(String... args)65     public void paramsWithVarargs(String... args) {
66         assertThat(args).isEqualTo(new String[]{"AAA", "BBB"});
67     }
named4()68     private Object named4() { return new Object[]{new String[]{"AAA", "BBB"}}; }
69 
70     @Test
71     @Parameters(source = ParametersReaderProvidersTest.OneIntegerProvider.class)
paramsFromExternalClass(int integer)72     public void paramsFromExternalClass(int integer) { }
73 
74     @Test
75     @Parameters
paramsInCollection(String p1)76     public void paramsInCollection(String p1) { }
parametersForParamsInCollection()77     private List<String> parametersForParamsInCollection() { return Arrays.asList("a"); }
78 
79     @Test
80     @Parameters
paramsInIterator(String p1)81     public void paramsInIterator(String p1) { }
parametersForParamsInIterator()82     private Iterator<String> parametersForParamsInIterator() { return Arrays.asList("a").iterator(); }
83 
84     @Test
85     @Parameters({"SOME_VALUE", "OTHER_VALUE"})
enumsAsParamInAnnotation(PersonType person)86     public void enumsAsParamInAnnotation(PersonType person) { }
87 
88     @Test
89     @Parameters
enumsAsParamsInMethod(PersonType person)90     public void enumsAsParamsInMethod(PersonType person) { }
parametersForEnumsAsParamsInMethod()91     private PersonType[] parametersForEnumsAsParamsInMethod() { return (PersonType[]) new PersonType[] {PersonType.SOME_VALUE}; }
92 
93     @Test
94     @Parameters(source = PersonType.class)
enumAsSource(PersonType personType)95     public void enumAsSource(PersonType personType) {
96     }
97 
98     @Test
99     @Parameters
wrapParamsWithConstructor(PersonTest.Person person)100     public void wrapParamsWithConstructor(PersonTest.Person person) { }
parametersForWrapParamsWithConstructor()101     private Object parametersForWrapParamsWithConstructor() {
102         return new Object[]{new Object[]{"first", 1}, new Object[]{"second", 2}};
103     }
104 
105     // Android-changed: does not work when run on device as it does not have access to the file
106     @Ignore
107     @Test
108     @FileParameters("src/test/resources/test.csv")
loadParamsFromCsv(int age, String name)109     public void loadParamsFromCsv(int age, String name) { }
110 
111     // Android-changed: does not work when run on device as it does not have access to the file
112     @Ignore
113     @Test
114     @FileParameters(value = "src/test/resources/test.csv", mapper = PersonMapper.class)
loadParamsFromAnyFile(PersonTest.Person person)115     public void loadParamsFromAnyFile(PersonTest.Person person) { }
116 
117     @Test
118     @FileParameters("classpath:test.csv")
loadParamsFromClasspath(int age, String name)119     public void loadParamsFromClasspath(int age, String name) { }
120 
121     @Test
122     @FileParameters(value = "classpath:with_header.csv", mapper = CsvWithHeaderMapper.class)
loadParamsFromCsvWithHeader(int id, String name)123     public void loadParamsFromCsvWithHeader(int id, String name) { }
124 
125     @Test
126     @Parameters({ "01.12.2012" })
convertParams(@aramconverter = ParamsConverterTest.SimpleDateConverter.class) Date date)127     public void convertParams(@Param(converter = ParamsConverterTest.SimpleDateConverter.class) Date date) {}
128 
129     @Test
130     @Parameters("please\\, escape commas if you use it here and don't want your parameters to be splitted")
commasInParametersUsage(String phrase)131     public void commasInParametersUsage(String phrase) { }
132 
133     // Android-changed: CTS and AndroidJUnitRunner rely on specific format to test names, changing
134     // them will prevent CTS and AndroidJUnitRunner from working properly; see b/36541809
135     @Ignore
136     @Test
137     @Parameters({ "1,1", "2,2", "3,6" })
138     @TestCaseName("factorial({0}) = {1}")
customNamesForTestCase(int argument, int result)139     public void customNamesForTestCase(int argument, int result) { }
140 
141     // Android-changed: CTS and AndroidJUnitRunner rely on specific format to test names, changing
142     // them will prevent CTS and AndroidJUnitRunner from working properly; see b/36541809
143     @Ignore
144     @Test
145     @Parameters({ "value1, value2", "value3, value4" })
146     @TestCaseName("[{index}] {method}: {params}")
predefinedMacroForTestCaseNames(String param1, String param2)147     public void predefinedMacroForTestCaseNames(String param1, String param2) { }
148 
mixedParameters()149     public Object mixedParameters() {
150         boolean booleanValue = true;
151         int[] primitiveArray = {1, 2, 3};
152         String stringValue = "Test";
153         String[] stringArray = {"one", "two", null};
154         return $(
155                 $(booleanValue, primitiveArray, stringValue, stringArray)
156         );
157     }
158 
159     // Android-changed: CTS and AndroidJUnitRunner rely on specific format to test names, changing
160     // them will prevent CTS and AndroidJUnitRunner from working properly; see b/36541809
161     @Ignore
162     @Test
163     @Parameters(method = "mixedParameters")
164     @TestCaseName("{0}, {1}, {2}, {3}")
usageOfMultipleTypesOfParameters( boolean booleanValue, int[] primitiveArray, String stringValue, String[] stringArray)165     public void usageOfMultipleTypesOfParameters(
166             boolean booleanValue, int[] primitiveArray, String stringValue, String[] stringArray) {
167     }
168 
169     @Test
170     @CombinedParameters({"AAA,BBB", "1|2"})
combineParamsInAnnotation(String p1, Integer p2)171     public void combineParamsInAnnotation(String p1, Integer p2) {}
172 
173 }
174