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 junit.framework.TestCase; 20 21 import java.io.ByteArrayInputStream; 22 import java.io.PrintWriter; 23 import java.util.ArrayList; 24 import java.util.HashMap; 25 import java.util.Scanner; 26 27 public class XmlPlanGeneratorTest extends TestCase { 28 29 private static final String JAR = "out/host/linux-x86/framework/compatibility-xml-plan-generator_v2.jar"; 30 private static final String PACKAGE_NAME = "com.android.test"; 31 private static final String NAME = "ValidTest"; 32 private static final String VALID_RESULT = 33 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 34 "<TestPackage appPackageName=\"com.android.test\" name=\"ValidTest\" version=\"1.0\">" + 35 "<TestSuite name=\"com\">" + 36 "<TestSuite name=\"android\">" + 37 "<TestSuite name=\"test\">" + 38 "<TestCase name=\"ValidTest\">" + 39 "<Test name=\"testA\" />" + 40 "</TestCase>" + 41 "</TestSuite>" + 42 "</TestSuite>" + 43 "</TestSuite>" + 44 "</TestPackage>"; 45 46 private static final String VALID = 47 "suite:com.android.test\n" + 48 "case:ValidTest\n" + 49 "test:testA\n"; 50 51 private static final String INVALID_A = ""; 52 53 private static final String INVALID_B = 54 "suite:com.android.test\n" + 55 "case:InvalidTest\n"; 56 57 private static final String INVALID_C = 58 "uh oh"; 59 60 private static final String INVALID_D = 61 "test:testA\n" + 62 "case:InvalidTest\n" + 63 "suite:com.android.test\n"; 64 65 private static final String INVALID_E = 66 "suite:com.android.test\n" + 67 "test:testA\n" + 68 "case:InvalidTest\n"; 69 testValid()70 public void testValid() throws Exception { 71 assertEquals(VALID_RESULT, runGenerator(VALID)); 72 } 73 testInvalidA()74 public void testInvalidA() throws Exception { 75 assertNull(runGenerator(INVALID_A)); 76 } 77 testInvalidB()78 public void testInvalidB() throws Exception { 79 assertNull(runGenerator(INVALID_B)); 80 } 81 testTestListParserInvalidFormat()82 public void testTestListParserInvalidFormat() throws Exception { 83 runTestListParser(INVALID_C); 84 } 85 testTestListParserSuiteExpected()86 public void testTestListParserSuiteExpected() throws Exception { 87 runTestListParser(INVALID_D); 88 } 89 testTestListParserCaseExpected()90 public void testTestListParserCaseExpected() throws Exception { 91 runTestListParser(INVALID_E); 92 } 93 runGenerator(String input)94 private static String runGenerator(String input) throws Exception { 95 ArrayList<String> args = new ArrayList<String>(); 96 args.add("java"); 97 args.add("-jar"); 98 args.add(JAR); 99 args.add("-p"); 100 args.add(PACKAGE_NAME); 101 args.add("-n"); 102 args.add(NAME); 103 104 final Process p = new ProcessBuilder(args).start(); 105 final PrintWriter out = new PrintWriter(p.getOutputStream()); 106 out.print(input); 107 out.flush(); 108 out.close(); 109 final StringBuilder output = new StringBuilder(); 110 final Scanner in = new Scanner(p.getInputStream()); 111 while (in.hasNextLine()) { 112 output.append(in.nextLine()); 113 } 114 int ret = p.waitFor(); 115 if (ret == 0) { 116 return output.toString(); 117 } 118 return null; 119 } 120 runTestListParser(String input)121 private static void runTestListParser(String input) throws Exception { 122 try { 123 final ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes()); 124 final HashMap<String, TestSuite> suites = TestListParser.parse(in); 125 fail(); 126 } catch (RuntimeException e) {} 127 } 128 } 129