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