TestPackageResult.java revision 65cd6eff9303eebf0a57c68b42e8a9e44b8771ae
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 */ 16package com.android.cts.tradefed.result; 17 18import com.android.ddmlib.testrunner.TestIdentifier; 19import com.android.tradefed.log.LogUtil.CLog; 20import com.android.tradefed.result.TestResult; 21 22import org.kxml2.io.KXmlSerializer; 23import org.xmlpull.v1.XmlPullParser; 24import org.xmlpull.v1.XmlPullParserException; 25 26import java.io.IOException; 27import java.util.Collection; 28import java.util.Collections; 29import java.util.LinkedList; 30import java.util.List; 31 32/** 33 * Data structure for a CTS test package result. 34 * <p/> 35 * Provides methods to serialize to XML. 36 */ 37class TestPackageResult extends AbstractXmlPullParser { 38 39 static final String TAG = "TestPackage"; 40 private static final String DIGEST_ATTR = "digest"; 41 private static final String APP_PACKAGE_NAME_ATTR = "appPackageName"; 42 private static final String NAME_ATTR = "name"; 43 private static final String ns = CtsXmlResultReporter.ns; 44 private static final String SIGNATURE_TEST_PKG = "android.tests.sigtest"; 45 46 private String mAppPackageName; 47 private String mName; 48 private String mDigest; 49 50 private TestSuite mSuiteRoot = new TestSuite(null); 51 52 public void setAppPackageName(String appPackageName) { 53 mAppPackageName = appPackageName; 54 } 55 56 public String getAppPackageName() { 57 return mAppPackageName; 58 } 59 60 public void setName(String name) { 61 mName = name; 62 } 63 64 public String getName() { 65 return mName; 66 } 67 68 public void setDigest(String digest) { 69 mDigest = digest; 70 } 71 72 public String getDigest() { 73 return mDigest; 74 } 75 76 /** 77 * Return the {@link TestSuite}s 78 */ 79 public Collection<TestSuite> getTestSuites() { 80 return mSuiteRoot.getTestSuites(); 81 } 82 83 /** 84 * Adds a test result to this test package 85 * 86 * @param testId 87 * @param testResult 88 */ 89 public void insertTest(TestIdentifier testId, TestResult testResult) { 90 List<String> classNameSegments = new LinkedList<String>(); 91 Collections.addAll(classNameSegments, testId.getClassName().split("\\.")); 92 if (classNameSegments.size() <= 0) { 93 CLog.e("Unrecognized package name format for test class '%s'", 94 testId.getClassName()); 95 } else { 96 String testCaseName = classNameSegments.remove(classNameSegments.size()-1); 97 mSuiteRoot.insertTest(classNameSegments, testCaseName, testId.getTestName(), 98 testResult); 99 } 100 } 101 102 /** 103 * Serialize this object and all its contents to XML. 104 * 105 * @param serializer 106 * @throws IOException 107 */ 108 public void serialize(KXmlSerializer serializer) throws IOException { 109 serializer.startTag(ns, TAG); 110 serializer.attribute(ns, NAME_ATTR, mName); 111 serializer.attribute(ns, APP_PACKAGE_NAME_ATTR, mAppPackageName); 112 serializer.attribute(ns, DIGEST_ATTR, getDigest()); 113 if (mName.equals(SIGNATURE_TEST_PKG)) { 114 serializer.attribute(ns, "signatureCheck", "true"); 115 } 116 mSuiteRoot.serialize(serializer); 117 serializer.endTag(ns, TAG); 118 } 119 120 /** 121 * Populates this class with package result data parsed from XML. 122 * 123 * @param parser the {@link XmlPullParser}. Expected to be pointing at start 124 * of TestPackage tag 125 */ 126 @Override 127 void parse(XmlPullParser parser) throws XmlPullParserException, IOException { 128 if (!parser.getName().equals(TAG)) { 129 throw new XmlPullParserException(String.format( 130 "invalid XML: Expected %s tag but received %s", TAG, parser.getName())); 131 } 132 setAppPackageName(getAttribute(parser, APP_PACKAGE_NAME_ATTR)); 133 setName(getAttribute(parser, NAME_ATTR)); 134 setDigest(getAttribute(parser, DIGEST_ATTR)); 135 int eventType = parser.getEventType(); 136 while (eventType != XmlPullParser.END_DOCUMENT) { 137 if (eventType == XmlPullParser.START_TAG && parser.getName().equals(TestSuite.TAG)) { 138 TestSuite suite = new TestSuite(); 139 suite.parse(parser); 140 mSuiteRoot.insertSuite(suite); 141 } 142 if (eventType == XmlPullParser.END_TAG && parser.getName().equals(TAG)) { 143 return; 144 } 145 eventType = parser.next(); 146 } 147 } 148} 149