View Javadoc
1   package org.junit.runner;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.List;
6   
7   import org.junit.internal.Classes;
8   import org.junit.runner.FilterFactory.FilterNotCreatedException;
9   import org.junit.runner.manipulation.Filter;
10  import org.junit.runners.model.InitializationError;
11  
12  class JUnitCommandLineParseResult {
13      private final List<String> filterSpecs = new ArrayList<String>();
14      private final List<Class<?>> classes = new ArrayList<Class<?>>();
15      private final List<Throwable> parserErrors = new ArrayList<Throwable>();
16  
17      /**
18       * Do not use. Testing purposes only.
19       */
20      JUnitCommandLineParseResult() {}
21  
22      /**
23       * Returns filter specs parsed from command line.
24       */
25      public List<String> getFilterSpecs() {
26          return Collections.unmodifiableList(filterSpecs);
27      }
28  
29      /**
30       * Returns test classes parsed from command line.
31       */
32      public List<Class<?>> getClasses() {
33          return Collections.unmodifiableList(classes);
34      }
35  
36      /**
37       * Parses the arguments.
38       *
39       * @param args Arguments
40       */
41      public static JUnitCommandLineParseResult parse(String[] args) {
42          JUnitCommandLineParseResult result = new JUnitCommandLineParseResult();
43  
44          result.parseArgs(args);
45  
46          return result;
47      }
48  
49      private void parseArgs(String[] args) {
50          parseParameters(parseOptions(args));
51      }
52  
53      String[] parseOptions(String... args) {
54          for (int i = 0; i != args.length; ++i) {
55              String arg = args[i];
56  
57              if (arg.equals("--")) {
58                  return copyArray(args, i + 1, args.length);
59              } else if (arg.startsWith("--")) {
60                  if (arg.startsWith("--filter=") || arg.equals("--filter")) {
61                      String filterSpec;
62                      if (arg.equals("--filter")) {
63                          ++i;
64  
65                          if (i < args.length) {
66                              filterSpec = args[i];
67                          } else {
68                              parserErrors.add(new CommandLineParserError(arg + " value not specified"));
69                              break;
70                          }
71                      } else {
72                          filterSpec = arg.substring(arg.indexOf('=') + 1);
73                      }
74  
75                      filterSpecs.add(filterSpec);
76                  } else {
77                      parserErrors.add(new CommandLineParserError("JUnit knows nothing about the " + arg + " option"));
78                  }
79              } else {
80                  return copyArray(args, i, args.length);
81              }
82          }
83  
84          return new String[]{};
85      }
86  
87      private String[] copyArray(String[] args, int from, int to) {
88          ArrayList<String> result = new ArrayList<String>();
89  
90          for (int j = from; j != to; ++j) {
91              result.add(args[j]);
92          }
93  
94          return result.toArray(new String[result.size()]);
95      }
96  
97      void parseParameters(String[] args) {
98          for (String arg : args) {
99              try {
100                 classes.add(Classes.getClass(arg));
101             } catch (ClassNotFoundException e) {
102                 parserErrors.add(new IllegalArgumentException("Could not find class [" + arg + "]", e));
103             }
104         }
105     }
106 
107     private Request errorReport(Throwable cause) {
108         return Request.errorReport(JUnitCommandLineParseResult.class, cause);
109     }
110 
111     /**
112      * Creates a {@link Request}.
113      *
114      * @param computer {@link Computer} to be used.
115      */
116     public Request createRequest(Computer computer) {
117         if (parserErrors.isEmpty()) {
118             Request request = Request.classes(
119                     computer, classes.toArray(new Class<?>[classes.size()]));
120             return applyFilterSpecs(request);
121         } else {
122             return errorReport(new InitializationError(parserErrors));
123         }
124     }
125 
126     private Request applyFilterSpecs(Request request) {
127         try {
128             for (String filterSpec : filterSpecs) {
129                 Filter filter = FilterFactories.createFilterFromFilterSpec(
130                         request, filterSpec);
131                 request = request.filterWith(filter);
132             }
133             return request;
134         } catch (FilterNotCreatedException e) {
135             return errorReport(e);
136         }
137     }
138 
139     /**
140      * Exception used if there's a problem parsing the command line.
141      */
142     public static class CommandLineParserError extends Exception {
143         private static final long serialVersionUID= 1L;
144 
145         public CommandLineParserError(String message) {
146             super(message);
147         }
148     }
149 }