1 /* 2 * Copyright (C) 2011 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.cts.xmlgenerator; 17 18 import java.io.InputStream; 19 import java.util.Collection; 20 import java.util.HashMap; 21 import java.util.Map; 22 import java.util.Scanner; 23 24 /** 25 * Parser of test lists that are in the format of: 26 * 27 * suite:android.holo.cts 28 * case:HoloTest 29 * test:testHolo 30 * test:testHoloDialog[:timeout_value] 31 */ 32 class TestListParser { 33 parse(InputStream input)34 public Collection<TestSuite> parse(InputStream input) { 35 Map<String, TestSuite> suiteMap = new HashMap<String, TestSuite>(); 36 TestSuite currentSuite = null; 37 TestCase currentCase = null; 38 Scanner scanner = null; 39 try { 40 scanner = new Scanner(input); 41 while(scanner.hasNextLine()) { 42 String line = scanner.nextLine(); 43 String[] tokens = line.split(":"); 44 if (tokens.length < 2) { 45 continue; 46 } 47 48 String key = tokens[0]; 49 String value = tokens[1]; 50 if ("suite".equals(key)) { 51 currentSuite = handleSuite(suiteMap, value); 52 } else if ("case".equals(key)) { 53 currentCase = handleCase(currentSuite, value); 54 } else if ("test".equals(key)) { 55 int timeout = -1; 56 if (tokens.length == 3) { 57 timeout = Integer.parseInt(tokens[2]); 58 } 59 handleTest(currentCase, value, timeout); 60 } 61 } 62 } finally { 63 if (scanner != null) { 64 scanner.close(); 65 } 66 } 67 return suiteMap.values(); 68 } 69 handleSuite(Map<String, TestSuite> suiteMap, String fullSuite)70 private TestSuite handleSuite(Map<String, TestSuite> suiteMap, String fullSuite) { 71 String[] suites = fullSuite.split("\\."); 72 int numSuites = suites.length; 73 TestSuite lastSuite = null; 74 75 for (int i = 0; i < numSuites; i++) { 76 String name = suites[i]; 77 if (lastSuite != null) { 78 if (lastSuite.hasSuite(name)) { 79 lastSuite = lastSuite.getSuite(name); 80 } else { 81 TestSuite newSuite = new TestSuite(name); 82 lastSuite.addSuite(newSuite); 83 lastSuite = newSuite; 84 } 85 } else if (suiteMap.containsKey(name)) { 86 lastSuite = suiteMap.get(name); 87 } else { 88 lastSuite = new TestSuite(name); 89 suiteMap.put(name, lastSuite); 90 } 91 } 92 93 return lastSuite; 94 } 95 handleCase(TestSuite suite, String caseName)96 private TestCase handleCase(TestSuite suite, String caseName) { 97 TestCase testCase = new TestCase(caseName); 98 suite.addCase(testCase); 99 return testCase; 100 } 101 handleTest(TestCase testCase, String test, int timeout)102 private void handleTest(TestCase testCase, String test, int timeout) { 103 testCase.addTest(test, timeout); 104 } 105 } 106