testtype cleanup.

Rename TestCaseRepo to TestPackageRepo, and add a ITestPan.getName method

Bug 5171576

Change-Id: Ib4725f36179794fc0883c97a7830f7cc2c1a7e6b
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/CtsTest.java b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/CtsTest.java
index 060f292..5b5d464 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/CtsTest.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/CtsTest.java
@@ -305,7 +305,7 @@
     private List<TestPackage> buildTestsToRun() {
         List<TestPackage> testPkgList = new LinkedList<TestPackage>();
         try {
-            ITestCaseRepo testRepo = createTestCaseRepo();
+            ITestPackageRepo testRepo = createTestCaseRepo();
             Collection<ITestPackageDef> testPkgDefs = getTestPackagesToRun(testRepo);
 
             for (ITestPackageDef testPkgDef : testPkgDefs) {
@@ -343,15 +343,14 @@
      * @throws ParseException
      * @throws FileNotFoundException
      */
-    private Collection<ITestPackageDef> getTestPackagesToRun(ITestCaseRepo testRepo)
+    private Collection<ITestPackageDef> getTestPackagesToRun(ITestPackageRepo testRepo)
             throws ParseException, FileNotFoundException {
         // use LinkedHashSet to have predictable iteration order
         Set<ITestPackageDef> testPkgDefs = new LinkedHashSet<ITestPackageDef>();
         if (mPlanName != null) {
             Log.i(LOG_TAG, String.format("Executing CTS test plan %s", mPlanName));
-            String ctsPlanRelativePath = String.format("%s.xml", mPlanName);
-            File ctsPlanFile = new File(mCtsBuild.getTestPlansDir(), ctsPlanRelativePath);
-            ITestPlan parser = createPlan();
+            File ctsPlanFile = mCtsBuild.getTestPlanFile(mPlanName);
+            ITestPlan parser = createPlan(mPlanName);
             parser.parse(createXmlStream(ctsPlanFile));
             for (String uri : parser.getTestUris()) {
                 if (!mExcludedPackageNames.contains(uri)) {
@@ -442,12 +441,12 @@
     }
 
     /**
-     * Factory method for creating a {@link ITestCaseRepo}.
+     * Factory method for creating a {@link ITestPackageRepo}.
      * <p/>
      * Exposed for unit testing
      */
-    ITestCaseRepo createTestCaseRepo() {
-        return new TestCaseRepo(mCtsBuild.getTestCasesDir());
+    ITestPackageRepo createTestCaseRepo() {
+        return new TestPackageRepo(mCtsBuild.getTestCasesDir());
     }
 
     /**
@@ -455,8 +454,8 @@
      * <p/>
      * Exposed for unit testing
      */
-    ITestPlan createPlan() {
-        return new TestPlan();
+    ITestPlan createPlan(String planName) {
+        return new TestPlan(planName);
     }
 
     /**
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestCaseRepo.java b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestPackageRepo.java
similarity index 97%
rename from tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestCaseRepo.java
rename to tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestPackageRepo.java
index f3930c9..53451f1 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestCaseRepo.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestPackageRepo.java
@@ -22,7 +22,7 @@
 /**
  * Interface for accessing tests from the CTS repository.
  */
-public interface ITestCaseRepo {
+public interface ITestPackageRepo {
 
     /**
      * Get a {@link TestPackageDef} given a uri
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestPlan.java b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestPlan.java
index ea6b7a1..191cefd 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestPlan.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/ITestPlan.java
@@ -61,10 +61,24 @@
     public void addExcludedTest(String uri, TestIdentifier testToExclude);
 
     /**
+     * Adds the list of excluded tests for given package
+     *
+     * @param pkgUri
+     * @param excludedTests
+     */
+    public void addExcludedTests(String uri, Collection<TestIdentifier> excludedTests);
+
+    /**
      * Serialize the contents of this test plan.
      *
      * @param xmlOutStream the {@link OutputStream} to serialize test plan contents to
      * @throws IOException
      */
     public void serialize(OutputStream xmlOutStream) throws IOException;
+
+    /**
+     * @return the test plan name
+     */
+    public String getName();
+
 }
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestCaseRepo.java b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestPackageRepo.java
similarity index 94%
rename from tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestCaseRepo.java
rename to tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestPackageRepo.java
index 6b2a00d..77fa996 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestCaseRepo.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestPackageRepo.java
@@ -32,9 +32,9 @@
 import java.util.Map;
 
 /**
- * Retrieves CTS test case definitions from the repository.
+ * Retrieves CTS test package definitions from the repository.
  */
-public class TestCaseRepo implements ITestCaseRepo {
+public class TestPackageRepo implements ITestPackageRepo {
 
     private static final String LOG_TAG = "TestCaseRepo";
 
@@ -44,11 +44,11 @@
     private Map<String, TestPackageDef> mTestMap;
 
     /**
-     * Creates a {@link TestCaseRepo}, initialized from provided repo files
+     * Creates a {@link TestPackageRepo}, initialized from provided repo files
      *
      * @param testCaseDir directory containing all test case definition xml and build files
      */
-    public TestCaseRepo(File testCaseDir) {
+    public TestPackageRepo(File testCaseDir) {
         mTestCaseDir = testCaseDir;
         mTestMap = new Hashtable<String, TestPackageDef>();
         parse(mTestCaseDir);
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestPlan.java b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestPlan.java
index 4072774..3639a9c 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestPlan.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/TestPlan.java
@@ -49,6 +49,8 @@
     private static final String EXCLUDE_ATTR = "exclude";
     private static final String URI_ATTR = "uri";
 
+    private final String mName;
+
     /**
      * SAX callback object. Handles parsing data from the xml tags.
      */
@@ -91,7 +93,8 @@
         }
     }
 
-    TestPlan() {
+    public TestPlan(String name) {
+        mName = name;
         // Uses a LinkedHashMap to have predictable iteration order
         mUriExcludedTestsMap = new LinkedHashMap<String, TestFilter>();
     }
@@ -100,6 +103,14 @@
      * {@inheritDoc}
      */
     @Override
+    public String getName() {
+        return mName;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
     public Collection<String> getTestUris() {
         return mUriExcludedTestsMap.keySet();
     }
@@ -145,6 +156,19 @@
      * {@inheritDoc}
      */
     @Override
+    public void addExcludedTests(String uri, Collection<TestIdentifier> excludedTests) {
+        TestFilter filter = mUriExcludedTestsMap.get(uri);
+        if (filter != null) {
+            filter.getExcludedTests().addAll(excludedTests);
+        } else {
+            throw new IllegalArgumentException(String.format("Could not find package %s", uri));
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
     public void serialize(OutputStream stream) throws IOException {
         KXmlSerializer serializer = new KXmlSerializer();
         serializer.setOutput(stream, "UTF-8");
diff --git a/tools/tradefed-host/tests/src/com/android/cts/tradefed/testtype/CtsTestTest.java b/tools/tradefed-host/tests/src/com/android/cts/tradefed/testtype/CtsTestTest.java
index 086adeb..b2c7a96 100644
--- a/tools/tradefed-host/tests/src/com/android/cts/tradefed/testtype/CtsTestTest.java
+++ b/tools/tradefed-host/tests/src/com/android/cts/tradefed/testtype/CtsTestTest.java
@@ -42,7 +42,7 @@
     private static final String PACKAGE_NAME = "test-uri";
     /** the test fixture under test, with all external dependencies mocked out */
     private CtsTest mCtsTest;
-    private ITestCaseRepo mMockRepo;
+    private ITestPackageRepo mMockRepo;
     private ITestPlan mMockPlan;
     private ITestDevice mMockDevice;
     private ITestInvocationListener mMockListener;
@@ -58,7 +58,7 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        mMockRepo = EasyMock.createMock(ITestCaseRepo.class);
+        mMockRepo = EasyMock.createMock(ITestPackageRepo.class);
         mMockPlan = EasyMock.createMock(ITestPlan.class);
         mMockDevice = EasyMock.createMock(ITestDevice.class);
         mMockListener = EasyMock.createNiceMock(ITestInvocationListener.class);
@@ -68,12 +68,12 @@
 
         mCtsTest = new CtsTest() {
             @Override
-            ITestCaseRepo createTestCaseRepo() {
+            ITestPackageRepo createTestCaseRepo() {
                 return mMockRepo;
             }
 
             @Override
-            ITestPlan createPlan() {
+            ITestPlan createPlan(String planName) {
                 return mMockPlan;
             }
 
diff --git a/tools/tradefed-host/tests/src/com/android/cts/tradefed/testtype/TestPlanTest.java b/tools/tradefed-host/tests/src/com/android/cts/tradefed/testtype/TestPlanTest.java
index 44b2f6b..18d4776 100644
--- a/tools/tradefed-host/tests/src/com/android/cts/tradefed/testtype/TestPlanTest.java
+++ b/tools/tradefed-host/tests/src/com/android/cts/tradefed/testtype/TestPlanTest.java
@@ -65,13 +65,20 @@
                     EXCLUDE_TEST_CLASS) +
         "</TestPlan>";
 
+    private TestPlan mPlan;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mPlan = new TestPlan("plan");
+    }
+
     /**
      * Simple test for parsing a plan containing two uris
      */
     public void testParse() throws ParseException  {
-        TestPlan plan = new TestPlan();
-        plan.parse(getStringAsStream(TEST_DATA));
-        assertTestData(plan);
+        mPlan.parse(getStringAsStream(TEST_DATA));
+        assertTestData(mPlan);
     }
 
     /**
@@ -92,10 +99,9 @@
      * Test parsing a plan containing a single excluded test
      */
     public void testParse_exclude() throws ParseException  {
-        TestPlan plan = new TestPlan();
-        plan.parse(getStringAsStream(TEST_EXCLUDED_DATA));
-        assertEquals(1, plan.getTestUris().size());
-        TestFilter filter = plan.getExcludedTestFilter(TEST_URI1);
+        mPlan.parse(getStringAsStream(TEST_EXCLUDED_DATA));
+        assertEquals(1, mPlan.getTestUris().size());
+        TestFilter filter = mPlan.getExcludedTestFilter(TEST_URI1);
         assertTrue(filter.getExcludedTests().contains(new TestIdentifier(EXCLUDE_TEST_CLASS,
                 EXCLUDE_TEST_METHOD)));
     }
@@ -104,9 +110,8 @@
      * Test parsing a plan containing multiple excluded tests
      */
     public void testParse_multiExclude() throws ParseException  {
-        TestPlan plan = new TestPlan();
-        plan.parse(getStringAsStream(TEST_MULTI_EXCLUDED_DATA));
-        assertMultiExcluded(plan);
+        mPlan.parse(getStringAsStream(TEST_MULTI_EXCLUDED_DATA));
+        assertMultiExcluded(mPlan);
     }
 
     /**
@@ -126,10 +131,9 @@
      * Test parsing a plan containing an excluded class
      */
     public void testParse_classExclude() throws ParseException  {
-        TestPlan plan = new TestPlan();
-        plan.parse(getStringAsStream(TEST_CLASS_EXCLUDED_DATA));
-        assertEquals(1, plan.getTestUris().size());
-        TestFilter filter = plan.getExcludedTestFilter(TEST_URI1);
+        mPlan.parse(getStringAsStream(TEST_CLASS_EXCLUDED_DATA));
+        assertEquals(1, mPlan.getTestUris().size());
+        TestFilter filter = mPlan.getExcludedTestFilter(TEST_URI1);
         assertTrue(filter.getExcludedClasses().contains(EXCLUDE_TEST_CLASS));
     }
 
@@ -138,9 +142,8 @@
      * @throws IOException
      */
     public void testSerialize_empty() throws ParseException, IOException  {
-        TestPlan plan = new TestPlan();
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-        plan.serialize(outStream);
+        mPlan.serialize(outStream);
         assertTrue(outStream.toString().contains(EMPTY_DATA));
     }
 
@@ -149,12 +152,11 @@
      * @throws IOException
      */
     public void testSerialize_packages() throws ParseException, IOException  {
-        TestPlan plan = new TestPlan();
-        plan.addPackage(TEST_URI1);
-        plan.addPackage(TEST_URI2);
+        mPlan.addPackage(TEST_URI1);
+        mPlan.addPackage(TEST_URI2);
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-        plan.serialize(outStream);
-        TestPlan parsedPlan = new TestPlan();
+        mPlan.serialize(outStream);
+        TestPlan parsedPlan = new TestPlan("parsed");
         parsedPlan.parse(getStringAsStream(outStream.toString()));
         // parsedPlan should contain same contents as TEST_DATA
         assertTestData(parsedPlan);
@@ -164,15 +166,14 @@
      * Test serializing and deserializing plan with multiple excluded tests
      */
     public void testSerialize_multiExclude() throws ParseException, IOException  {
-        TestPlan plan = new TestPlan();
-        plan.addPackage(TEST_URI1);
-        plan.addExcludedTest(TEST_URI1, new TestIdentifier(EXCLUDE_TEST_CLASS,
+        mPlan.addPackage(TEST_URI1);
+        mPlan.addExcludedTest(TEST_URI1, new TestIdentifier(EXCLUDE_TEST_CLASS,
                 EXCLUDE_TEST_METHOD));
-        plan.addExcludedTest(TEST_URI1, new TestIdentifier(EXCLUDE_TEST_CLASS,
+        mPlan.addExcludedTest(TEST_URI1, new TestIdentifier(EXCLUDE_TEST_CLASS,
                 EXCLUDE_TEST_METHOD2));
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-        plan.serialize(outStream);
-        TestPlan parsedPlan = new TestPlan();
+        mPlan.serialize(outStream);
+        TestPlan parsedPlan = new TestPlan("parsed");
         parsedPlan.parse(getStringAsStream(outStream.toString()));
         // parsedPlan should contain same contents as TEST_DATA
         assertMultiExcluded(parsedPlan);