1 /* 2 * Copyright (C) 2008 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; 17 18 import org.w3c.dom.Document; 19 import org.w3c.dom.Node; 20 import org.w3c.dom.NodeList; 21 import org.xml.sax.SAXException; 22 23 import java.io.File; 24 import java.io.IOException; 25 import java.security.NoSuchAlgorithmException; 26 import java.text.ParseException; 27 import java.util.ArrayList; 28 import java.util.Collection; 29 30 import javax.xml.parsers.DocumentBuilder; 31 import javax.xml.parsers.DocumentBuilderFactory; 32 import javax.xml.parsers.ParserConfigurationException; 33 34 /** 35 * Builder of test session from the test result XML file. 36 */ 37 public class TestSessionLogBuilder extends XMLResourceHandler { 38 private static TestSessionLogBuilder sInstance; 39 40 private DocumentBuilder mDocBuilder; 41 42 public static TestSessionLogBuilder getInstance() 43 throws ParserConfigurationException { 44 if (sInstance == null) { 45 sInstance = new TestSessionLogBuilder(); 46 } 47 48 return sInstance; 49 } 50 51 private TestSessionLogBuilder() throws ParserConfigurationException { 52 mDocBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 53 } 54 55 /** 56 * Create TestSessionLog from the result XML file. 57 * 58 * @param resultFilePath The result file path. 59 * @return TestSessionLog. 60 */ 61 public TestSessionLog build(final String resultFilePath) throws SAXException, IOException, 62 TestPlanNotFoundException, TestNotFoundException, 63 NoSuchAlgorithmException, ParserConfigurationException { 64 65 File file = new File(resultFilePath); 66 if (!file.exists()) { 67 throw new TestPlanNotFoundException(); 68 } 69 70 Document doc = mDocBuilder.parse(file); 71 return loadSessionLog(doc); 72 } 73 74 /** 75 * Load TestSessionLog from a Test result DOM doc. 76 * 77 * @param doc Test result DOM Document. 78 * @return loaded test session log from Test result DOM Document. 79 */ 80 private TestSessionLog loadSessionLog(Document doc) 81 throws NoSuchAlgorithmException, ParserConfigurationException, 82 SAXException, IOException, TestPlanNotFoundException, 83 TestNotFoundException { 84 85 ArrayList<TestPackage> pkgsFromResult = new ArrayList<TestPackage>(); 86 NodeList resultList = doc.getElementsByTagName(TestSessionLog.TAG_TEST_RESULT); 87 88 // currently, there should be just one test result tag in the result file 89 Node resultNode = resultList.item(0); 90 String planName = getStringAttributeValue(resultNode, TestSessionLog.ATTRIBUTE_TESTPLAN); 91 String start = getStringAttributeValue(resultNode, TestSessionLog.ATTRIBUTE_STARTTIME); 92 String end = getStringAttributeValue(resultNode, TestSessionLog.ATTRIBUTE_ENDTIME); 93 String planFilePath = HostConfig.getInstance().getPlanRepository().getPlanPath(planName); 94 TestSession sessionFromPlan = TestSessionBuilder.getInstance().build(planFilePath); 95 96 NodeList pkgList = resultNode.getChildNodes(); 97 for (int i = 0; i < pkgList.getLength(); i++) { 98 Node pkgNode = pkgList.item(i); 99 if (pkgNode.getNodeType() == Document.ELEMENT_NODE 100 && TestSessionLog.TAG_TESTPACKAGE.equals(pkgNode.getNodeName())) { 101 TestPackage pkg = TestSessionBuilder.getInstance().loadPackage(pkgNode, null); 102 if (pkg != null) { 103 pkgsFromResult.add(pkg); 104 } 105 } 106 } 107 108 Collection<TestPackage> pkgsFromPlan = sessionFromPlan.getSessionLog().getTestPackages(); 109 for (TestPackage pkgFromPlan : pkgsFromPlan) { 110 for (TestPackage pkgFromResult : pkgsFromResult) { 111 if (pkgFromPlan.getAppPackageName().equals(pkgFromResult.getAppPackageName())) { 112 Collection<Test> testsFromPlan = pkgFromPlan.getTests(); 113 Collection<Test> testsFromResult = pkgFromResult.getTests(); 114 for (Test testFromPlan : testsFromPlan) { 115 for (Test testFromResult : testsFromResult) { 116 if (testFromPlan.getFullName().equals(testFromResult.getFullName())) { 117 CtsTestResult result = testFromResult.getResult(); 118 testFromPlan.addResult(testFromResult.getResult()); 119 break; 120 } 121 } 122 } 123 break; 124 } 125 } 126 } 127 128 TestSessionLog log = new TestSessionLog(pkgsFromPlan, planName); 129 try { 130 log.setStartTime(HostUtils.dateFromString(start).getTime()); 131 log.setEndTime(HostUtils.dateFromString(end).getTime()); 132 } catch (NullPointerException ignored) { 133 // use default time 134 } catch (ParseException ignored) { 135 // use default time 136 } 137 return log; 138 } 139 } 140