Home | History | Annotate | Download | only in xmlgenerator
      1 /*
      2  * Copyright (C) 2014 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 
     17 package com.android.compatibility.common.xmlgenerator;
     18 
     19 import java.io.InputStream;
     20 import java.util.HashMap;
     21 import java.util.Scanner;
     22 
     23 /**
     24  * Parser of test lists in the form;
     25  *
     26  * suite:android.sample
     27  * case:SampleTest
     28  * test:testA
     29  * test:testB
     30  * suite:android.sample.ui
     31  * case:SampleUiTest
     32  * test:testA
     33  * test:testB
     34  */
     35 public class TestListParser {
     36 
     37     private TestListParser() {}
     38 
     39     public static HashMap<String, TestSuite> parse(InputStream input) {
     40         final HashMap<String, TestSuite> suites = new HashMap<String, TestSuite>();
     41         TestSuite currentSuite = null;
     42         TestCase currentCase = null;
     43         Scanner in = null;
     44         try {
     45             in = new Scanner(input);
     46             while (in.hasNextLine()) {
     47                 final String line = in.nextLine();
     48                 final String[] parts = line.split(":");
     49                 if (parts.length != 2) {
     50                     throw new RuntimeException("Invalid Format: " + line);
     51                 }
     52                 final String key = parts[0];
     53                 final String value = parts[1];
     54                 if (currentSuite == null) {
     55                     if (!"suite".equals(key)) {
     56                         throw new RuntimeException("TestSuite Expected");
     57                     }
     58                     final String[] names = value.split("\\.");
     59                     for (int i = 0; i < names.length; i++) {
     60                         final String name = names[i];
     61                         if (currentSuite != null) {
     62                             if (currentSuite.hasTestSuite(name)) {
     63                                 currentSuite = currentSuite.getTestSuite(name);
     64                             } else {
     65                                 final TestSuite newSuite = new TestSuite(name);
     66                                 currentSuite.addTestSuite(newSuite);
     67                                 currentSuite = newSuite;
     68                             }
     69                         } else if (suites.containsKey(name)) {
     70                             currentSuite = suites.get(name);
     71                         } else {
     72                             currentSuite = new TestSuite(name);
     73                             suites.put(name, currentSuite);
     74                         }
     75                     }
     76                 } else if (currentCase == null) {
     77                     if (!"case".equals(key)) {
     78                         throw new RuntimeException("TestCase Expected");
     79                     }
     80                     currentCase = new TestCase(value);
     81                     currentSuite.addTestCase(currentCase);
     82                 } else {
     83                     if (!"test".equals(key)) {
     84                         throw new RuntimeException("Test Expected");
     85                     }
     86                     currentCase.addTest(new Test(value));
     87                 }
     88             }
     89         } finally {
     90             if (in != null) {
     91                 in.close();
     92             }
     93         }
     94         return suites;
     95     }
     96 }
     97