Home | History | Annotate | Download | only in local
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 package org.chromium.testing.local;
      6 
      7 import java.util.HashSet;
      8 import java.util.Set;
      9 
     10 /**
     11  *  Parses command line arguments for JunitTestMain.
     12  */
     13 public class JunitTestArgParser {
     14 
     15     private final Set<String> mPackageFilters;
     16     private final Set<Class<?>> mRunnerFilters;
     17     private final Set<String> mGtestFilters;
     18 
     19     public static JunitTestArgParser parse(String[] args) {
     20 
     21         JunitTestArgParser parsed = new JunitTestArgParser();
     22 
     23         for (int i = 0; i < args.length; ++i) {
     24             if (args[i].startsWith("-")) {
     25                 String argName;
     26                 if (args[i].startsWith("-", 1)) {
     27                     argName = args[i].substring(2, args[i].length());
     28                 } else {
     29                     argName = args[i].substring(1, args[i].length());
     30                 }
     31                 try {
     32                     if ("package-filter".equals(argName)) {
     33                         // Read the command line argument after the flag.
     34                         parsed.addPackageFilter(args[++i]);
     35                     } else if ("runner-filter".equals(argName)) {
     36                         // Read the command line argument after the flag.
     37                         parsed.addRunnerFilter(Class.forName(args[++i]));
     38                     } else if ("gtest-filter".equals(argName)) {
     39                         // Read the command line argument after the flag.
     40                         parsed.addGtestFilter(args[++i]);
     41                     } else {
     42                         System.out.println("Ignoring flag: \"" + argName + "\"");
     43                     }
     44                 } catch (ArrayIndexOutOfBoundsException e) {
     45                     System.err.println("No value specified for argument \"" + argName + "\"");
     46                     System.exit(1);
     47                 } catch (ClassNotFoundException e) {
     48                     System.err.println("Class not found. (" + e.toString() + ")");
     49                     System.exit(1);
     50                 }
     51             } else {
     52                 System.out.println("Ignoring argument: \"" + args[i] + "\"");
     53             }
     54         }
     55 
     56         return parsed;
     57     }
     58 
     59     private JunitTestArgParser() {
     60         mPackageFilters = new HashSet<String>();
     61         mRunnerFilters = new HashSet<Class<?>>();
     62         mGtestFilters = new HashSet<String>();
     63     }
     64 
     65     public Set<String> getPackageFilters() {
     66         return mPackageFilters;
     67     }
     68 
     69     public Set<Class<?>> getRunnerFilters() {
     70         return mRunnerFilters;
     71     }
     72 
     73     public Set<String> getGtestFilters() {
     74         return mGtestFilters;
     75     }
     76 
     77     private void addPackageFilter(String packageFilter) {
     78         mPackageFilters.add(packageFilter);
     79     }
     80 
     81     private void addRunnerFilter(Class<?> runnerFilter) {
     82         mRunnerFilters.add(runnerFilter);
     83     }
     84 
     85     private void addGtestFilter(String gtestFilter) {
     86         mGtestFilters.add(gtestFilter);
     87     }
     88 
     89 }
     90 
     91