View Javadoc
1   package org.junit.runner;
2   
3   import static org.hamcrest.CoreMatchers.containsString;
4   import static org.hamcrest.CoreMatchers.hasItems;
5   import static org.hamcrest.CoreMatchers.is;
6   import static org.hamcrest.MatcherAssert.assertThat;
7   
8   import java.util.List;
9   
10  import org.junit.Rule;
11  import org.junit.Test;
12  import org.junit.experimental.categories.IncludeCategories;
13  import org.junit.rules.ExpectedException;
14  import org.junit.runner.manipulation.Filter;
15  
16  public class JUnitCommandLineParseResultTest {
17      @Rule
18      public ExpectedException expectedException = ExpectedException.none();
19  
20      private final JUnitCommandLineParseResult jUnitCommandLineParseResult = new JUnitCommandLineParseResult();
21  
22      @Test
23      public void shouldStopParsingOptionsUponDoubleHyphenArg() throws Exception {
24          String[] restOfArgs = jUnitCommandLineParseResult.parseOptions(
25                  "--0", "--1", "--", "--2", "--3");
26  
27          assertThat(restOfArgs, is(new String[]{"--2", "--3"}));
28      }
29  
30      @Test
31      public void shouldParseFilterArgWithEqualsSyntax() throws Exception {
32          String value= IncludeCategories.class.getName() + "=" + DummyCategory0.class.getName();
33          jUnitCommandLineParseResult.parseOptions("--filter=" + value);
34  
35          List<String> specs= jUnitCommandLineParseResult.getFilterSpecs();
36  
37          assertThat(specs, hasItems(value));
38      }
39  
40      @Test
41      public void shouldCreateFailureUponBaldFilterOptionNotFollowedByValue() {
42          jUnitCommandLineParseResult.parseOptions("--filter");
43  
44          Runner runner = jUnitCommandLineParseResult.createRequest(new Computer()).getRunner();
45          Description description = runner.getDescription().getChildren().get(0);
46  
47          assertThat(description.toString(), containsString("initializationError"));
48      }
49  
50      @Test
51      public void shouldParseFilterArgInWhichValueIsASeparateArg() throws Exception {
52          String value= IncludeCategories.class.getName() + "=" + DummyCategory0.class.getName();
53          jUnitCommandLineParseResult.parseOptions("--filter", value);
54  
55          List<String> specs= jUnitCommandLineParseResult.getFilterSpecs();
56  
57          assertThat(specs, hasItems(value));
58      }
59  
60      @Test
61      public void shouldStopParsingOptionsUponNonOption() throws Exception {
62          String[] restOfArgs = jUnitCommandLineParseResult.parseOptions(new String[]{
63                  "--0", "--1", "2", "3"
64          });
65  
66          assertThat(restOfArgs, is(new String[]{"2", "3"}));
67      }
68  
69      @Test
70      public void shouldCreateFailureUponUnknownOption() throws Exception {
71          String unknownOption = "--unknown-option";
72          jUnitCommandLineParseResult.parseOptions(new String[]{
73                  unknownOption
74          });
75  
76          Runner runner = jUnitCommandLineParseResult.createRequest(new Computer()).getRunner();
77          Description description = runner.getDescription().getChildren().get(0);
78  
79          assertThat(description.toString(), containsString("initializationError"));
80      }
81  
82      @Test
83      public void shouldCreateFailureUponUncreatedFilter() throws Exception {
84          jUnitCommandLineParseResult.parseOptions(new String[]{
85                  "--filter=" + FilterFactoryStub.class.getName()
86          });
87  
88          Runner runner = jUnitCommandLineParseResult.createRequest(new Computer()).getRunner();
89          Description description = runner.getDescription().getChildren().get(0);
90  
91          assertThat(description.toString(), containsString("initializationError"));
92      }
93  
94      @Test
95      public void shouldCreateFailureUponUnfoundFilterFactory() throws Exception {
96          String nonExistentFilterFactory = "NonExistentFilterFactory";
97          jUnitCommandLineParseResult.parseOptions(new String[]{
98                  "--filter=" + nonExistentFilterFactory
99          });
100 
101         Runner runner = jUnitCommandLineParseResult.createRequest(new Computer()).getRunner();
102         Description description = runner.getDescription().getChildren().get(0);
103 
104         assertThat(description.toString(), containsString("initializationError"));
105     }
106 
107     @Test
108     public void shouldAddToClasses() {
109         jUnitCommandLineParseResult.parseParameters(new String[]{
110                 DummyTest.class.getName()
111         });
112 
113         List<Class<?>> classes = jUnitCommandLineParseResult.getClasses();
114         Class<?> testClass = classes.get(0);
115 
116         assertThat(testClass.getName(), is(DummyTest.class.getName()));
117     }
118 
119     @Test
120     public void shouldCreateFailureUponUnknownTestClass() throws Exception {
121         String unknownTestClass = "UnknownTestClass";
122         jUnitCommandLineParseResult.parseParameters(new String[]{
123                 unknownTestClass
124         });
125 
126         Runner runner = jUnitCommandLineParseResult.createRequest(new Computer()).getRunner();
127         Description description = runner.getDescription().getChildren().get(0);
128 
129         assertThat(description.toString(), containsString("initializationError"));
130     }
131 
132     public static class FilterFactoryStub implements FilterFactory {
133         public Filter createFilter(FilterFactoryParams params) throws FilterNotCreatedException {
134             throw new FilterNotCreatedException(new Exception("stub"));
135         }
136     }
137 
138     public static interface DummyCategory0 {
139     }
140 
141     public static class DummyTest {
142         @Test
143         public void dummyTest() {
144         }
145     }
146 }