Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.compatibility.common.tradefed.util;
     17 
     18 import com.android.tradefed.config.Option;
     19 import com.android.tradefed.config.Option.Importance;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import java.util.ArrayList;
     24 import java.util.Arrays;
     25 import java.util.List;
     26 import java.util.Set;
     27 
     28 /**
     29  * Unit tests for {@link OptionHelper}
     30  */
     31 public class OptionHelperTest extends TestCase {
     32 
     33     private static final String TEST_CLASS = "test-class";
     34     private static final String TEST_CLASS_SHORTNAME = "c";
     35     private static final String TEST_FILTER = "test-filter";
     36     private static final String TEST_LOGPATH = "test-logpath";
     37     private static final String TEST_NAME = "test-name";
     38     private static final String TEST_SUITE = "test-suite";
     39     private static final String TEST_SUITE_SHORTNAME = "s";
     40 
     41     @Option(name = TEST_CLASS,
     42             shortName = 'c',
     43             importance = Importance.ALWAYS)
     44     private String mTestClass = null;
     45 
     46     @Option(name = TEST_NAME,
     47             importance = Importance.ALWAYS)
     48     private String mTestName = null;
     49 
     50     @Option(name = TEST_SUITE,
     51             shortName = 's',
     52             importance = Importance.ALWAYS)
     53     private String mTestSuite = null;
     54 
     55     @Option(name = TEST_FILTER,
     56             importance = Importance.ALWAYS)
     57     private List<String> mFilters = new ArrayList<>();
     58 
     59     @Option(name = TEST_LOGPATH,
     60             importance = Importance.ALWAYS)
     61     private String mLogPath = null;
     62 
     63     public void testGetOptionNames() throws Exception {
     64         Set<String> optionNames = OptionHelper.getOptionNames(this);
     65         List<String> expectedNames = Arrays.asList(TEST_CLASS, TEST_NAME, TEST_SUITE,
     66                 TEST_LOGPATH);
     67         assertEquals("Missing option names", true, optionNames.containsAll(expectedNames));
     68         assertEquals("Expected five elements", 5, optionNames.size());
     69     }
     70 
     71     public void testGetOptionShortNames() throws Exception {
     72         Set<String> optionShortNames = OptionHelper.getOptionShortNames(this);
     73         List<String> expectedShortNames = Arrays.asList(TEST_CLASS_SHORTNAME, TEST_SUITE_SHORTNAME);
     74         assertEquals("Missing option shortnames", true,
     75             optionShortNames.containsAll(expectedShortNames));
     76         assertEquals("Expected two elements", 2, optionShortNames.size());
     77     }
     78 
     79     public void testGetValidCliArgs() throws Exception {
     80         String fakeTestClass = "FooTestCases";
     81         String fakeTestMethod = "android.foo.footestsuite.RealTest#testSuperReal";
     82         List<String> noValidNames = new ArrayList<String>();
     83         List<String> validSubset = Arrays.asList("--" + TEST_CLASS, "fooclass",
     84             "-" + TEST_SUITE_SHORTNAME, "foosuite");
     85         List<String> allValidNames = Arrays.asList("--" + TEST_CLASS, "fooclass",
     86             "-" + TEST_SUITE_SHORTNAME, "foosuite:foo-key=fooval", "--" + TEST_NAME, "footest",
     87             "--" + TEST_LOGPATH, "path/to/log-directory/");
     88 
     89         List<String> validQuoteSubset = Arrays.asList("-" + TEST_CLASS_SHORTNAME, fakeTestClass,
     90             "--" + TEST_NAME + " " + fakeTestMethod, "--" + TEST_FILTER, fakeTestClass + " "
     91             + fakeTestMethod);
     92         String[] inputArray = {"foocts ", "-", TEST_CLASS_SHORTNAME, " ", fakeTestClass, " \"--",
     93             TEST_NAME, "=", fakeTestMethod, "\" -z \"FAKE1 FAKE2\" --", TEST_FILTER, " \"",
     94             fakeTestClass, " ", fakeTestMethod + "\""};
     95         String inputString = String.join("", inputArray);
     96 
     97         assertEquals("Expected no valid names", noValidNames,
     98             OptionHelper.getValidCliArgs("test --foo -b", this));
     99         assertEquals("Expected one long name and one short name", validSubset,
    100             OptionHelper.getValidCliArgs("test --" + TEST_CLASS + " fooclass -b fake"
    101                 + " -s foosuite", this));
    102         assertEquals("Expected two long names and one short name", allValidNames,
    103             OptionHelper.getValidCliArgs("test --" + TEST_CLASS + " fooclass -b fake"
    104                 + " -s foosuite:foo-key=fooval --" + TEST_NAME + " footest --" + TEST_LOGPATH
    105                 + " path/to/log-directory/", this));
    106         assertEquals("Expected matching arrays", validQuoteSubset,
    107             OptionHelper.getValidCliArgs(inputString, this));
    108     }
    109 
    110 }
    111