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.tradefed.result; 17 18 import com.android.tradefed.util.xml.AbstractXmlParser.ParseException; 19 20 import java.io.StringReader; 21 22 /** 23 * Unit tests for {@link TestResults} parsing. 24 */ 25 public class TestResultsTest extends junit.framework.TestCase { 26 27 private static final String RESULT_START = "<TestResult>"; 28 private static final String RESULT_END = "</TestResult>"; 29 private static final String TEST_PACKAGE_START = 30 "<TestPackage name=\"pkgName\" appPackageName=\"appPkgName\" digest=\"digValue\" >"; 31 private static final String TEST_PACKAGE_END = "</TestPackage>"; 32 33 private static final String TEST_PACKAGE_FULL = 34 RESULT_START +TEST_PACKAGE_START + TEST_PACKAGE_END + RESULT_END; 35 36 private static final String TEST_FULL = 37 RESULT_START + TEST_PACKAGE_START + 38 "<TestSuite name=\"com\" >" + 39 "<TestSuite name=\"example\" >" + 40 "<TestCase name=\"ExampleTest\" >" + 41 "<Test name=\"testExample\" endtime=\"et\" starttime=\"st\" result=\"fail\" >" + 42 "<FailedScene message=\"msg\" >" + 43 "<StackTrace>at ExampleTest.testExample()" + 44 "</StackTrace>" + 45 "</FailedScene>" + 46 "</Test>" + 47 "</TestCase>" + 48 "</TestSuite>" + 49 "</TestSuite>"; 50 51 /** 52 * Test parsing data with no result content 53 */ testParse_empty()54 public void testParse_empty() throws Exception { 55 TestResults parser = new TestResults(); 56 parser.parse(new StringReader("<Empty/>")); 57 assertEquals(0, parser.getPackages().size()); 58 } 59 60 /** 61 * Test parsing data with a single test package 62 */ testParse_package()63 public void testParse_package() throws Exception { 64 TestResults parser = new TestResults(); 65 parser.parse(new StringReader(TEST_PACKAGE_FULL)); 66 assertEquals(1, parser.getPackages().size()); 67 TestPackageResult pkg = parser.getPackages().iterator().next(); 68 assertEquals("pkgName", pkg.getName()); 69 assertEquals("appPkgName", pkg.getAppPackageName()); 70 assertEquals("digValue", pkg.getDigest()); 71 } 72 73 /** 74 * Test parsing not well formed XML data 75 */ testParse_corrupt()76 public void testParse_corrupt() throws Exception { 77 TestResults parser = new TestResults(); 78 // missing TEST_PACKAGE_END 79 try { 80 parser.parse(new StringReader(RESULT_START + TEST_PACKAGE_START + RESULT_END)); 81 fail("ParseException not thrown"); 82 } catch (ParseException e) { 83 // expected 84 } 85 } 86 87 /** 88 * Test parsing a result with a single failed test 89 */ testParse_test()90 public void testParse_test() throws Exception { 91 TestResults parser = new TestResults(); 92 parser.parse(new StringReader(TEST_FULL)); 93 assertEquals(1, parser.getPackages().size()); 94 TestPackageResult pkg = parser.getPackages().iterator().next(); 95 TestSuite comSuite = pkg.getTestSuites().iterator().next(); 96 assertEquals("com", comSuite.getName()); 97 TestSuite exampleSuite = comSuite.getTestSuites().iterator().next(); 98 assertEquals("example", exampleSuite.getName()); 99 TestCase exampleCase = exampleSuite.getTestCases().iterator().next(); 100 assertEquals("ExampleTest", exampleCase.getName()); 101 Test exampleTest = exampleCase.getTests().iterator().next(); 102 assertEquals("testExample", exampleTest.getName()); 103 assertEquals("msg", exampleTest.getMessage()); 104 assertEquals("at ExampleTest.testExample()", exampleTest.getStackTrace()); 105 } 106 } 107