1 /* 2 * Copyright (C) 2018 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.cts.releaseparser; 18 19 import com.android.cts.releaseparser.ReleaseProto.*; 20 21 import org.xml.sax.Attributes; 22 import org.xml.sax.SAXException; 23 import org.xml.sax.helpers.DefaultHandler; 24 25 import java.io.BufferedReader; 26 import java.io.File; 27 import java.io.InputStream; 28 import java.io.InputStreamReader; 29 import java.util.ArrayList; 30 import java.util.Enumeration; 31 import java.util.List; 32 import java.util.zip.ZipEntry; 33 import java.util.zip.ZipFile; 34 35 import javax.xml.parsers.SAXParser; 36 import javax.xml.parsers.SAXParserFactory; 37 38 public class TestSuiteTradefedParser extends JarParser { 39 private static final String TEST_SUITE_INFO_PROPERTIES_FILE = "test-suite-info.properties"; 40 private static final String KNOWN_FAILURES_XML_FILE = "-known-failures.xml"; 41 private static final String EXCLUDE_FILTER_TAG = "compatibility:exclude-filter"; 42 private static final String NAME_TAG = "name"; 43 private static final String VALUE_TAG = "value"; 44 45 private Entry.EntryType mType; 46 private List<String> mKnownFailureList; 47 private String mName; 48 private String mFullname; 49 private String mBuildNumber; 50 private String mTargetArch; 51 private String mVersion; 52 TestSuiteTradefedParser(File file)53 public TestSuiteTradefedParser(File file) { 54 super(file); 55 } 56 57 @Override getType()58 public Entry.EntryType getType() { 59 if (mType == null) { 60 parseFile(); 61 } 62 return mType; 63 } 64 getName()65 public String getName() { 66 if (mType == null) { 67 parseFile(); 68 } 69 return mName; 70 } 71 getFullName()72 public String getFullName() { 73 if (mType == null) { 74 parseFile(); 75 } 76 return mFullname; 77 } 78 getBuildNumber()79 public String getBuildNumber() { 80 if (mType == null) { 81 parseFile(); 82 } 83 return mBuildNumber; 84 } 85 getTargetArch()86 public String getTargetArch() { 87 if (mType == null) { 88 parseFile(); 89 } 90 return mTargetArch; 91 } 92 getVersion()93 public String getVersion() { 94 if (mType == null) { 95 parseFile(); 96 } 97 return mVersion; 98 } 99 getKnownFailureList()100 public List<String> getKnownFailureList() { 101 if (mKnownFailureList == null) { 102 mKnownFailureList = new ArrayList<String>(); 103 praseKnownFailure(); 104 } 105 return mKnownFailureList; 106 } 107 praseKnownFailure()108 private void praseKnownFailure() { 109 try { 110 ZipFile zip = new ZipFile(getFile()); 111 try { 112 Enumeration<? extends ZipEntry> entries = zip.entries(); 113 while (entries.hasMoreElements()) { 114 ZipEntry entry = entries.nextElement(); 115 116 if (entry.getName().endsWith(KNOWN_FAILURES_XML_FILE)) { 117 SAXParserFactory spf = SAXParserFactory.newInstance(); 118 spf.setNamespaceAware(false); 119 SAXParser saxParser = spf.newSAXParser(); 120 InputStream xmlStream = zip.getInputStream(entry); 121 KnownFailuresXmlHandler kfXmlHandler = 122 new KnownFailuresXmlHandler(); 123 saxParser.parse(xmlStream, kfXmlHandler); 124 xmlStream.close(); 125 } 126 } 127 } finally { 128 zip.close(); 129 } 130 } catch (Exception e) { 131 System.err.println(String.format("Cannot praseKnownFailure %s", e.getMessage())); 132 } 133 } 134 135 private class KnownFailuresXmlHandler extends DefaultHandler { 136 @Override startElement(String uri, String localName, String name, Attributes attributes)137 public void startElement(String uri, String localName, String name, Attributes attributes) 138 throws SAXException { 139 super.startElement(uri, localName, name, attributes); 140 if (EXCLUDE_FILTER_TAG.equals(attributes.getValue(NAME_TAG))) { 141 String kfFilter = attributes.getValue(VALUE_TAG).replace(' ', '.'); 142 mKnownFailureList.add(kfFilter); 143 } 144 } 145 } 146 parseFile()147 private void parseFile() { 148 try { 149 ZipFile zip = new ZipFile(getFile()); 150 try { 151 Enumeration<? extends ZipEntry> entries = zip.entries(); 152 while (entries.hasMoreElements()) { 153 ZipEntry entry = entries.nextElement(); 154 155 if (entry.getName().equals(TEST_SUITE_INFO_PROPERTIES_FILE)) { 156 InputStream inStream = zip.getInputStream(entry); 157 InputStreamReader isReader = new InputStreamReader(inStream, "UTF-8"); 158 BufferedReader bfReader = new BufferedReader(isReader); 159 String ln; 160 while((ln = bfReader.readLine()) != null) { 161 String[] tokens = ln.split(" = "); 162 switch (tokens[0]) { 163 case "build_number": 164 mBuildNumber = tokens[1]; 165 break; 166 case "target_arch": 167 mTargetArch = tokens[1]; 168 break; 169 case "name": 170 mName = tokens[1]; 171 break; 172 case "fullname": 173 mFullname = tokens[1]; 174 break; 175 case "version": 176 mVersion = tokens[1]; 177 break; 178 } 179 } 180 inStream.close(); 181 isReader.close(); 182 bfReader.close(); 183 } 184 } 185 mType = Entry.EntryType.TEST_SUITE_TRADEFED; 186 } finally { 187 zip.close(); 188 } 189 } catch (Exception e) { 190 System.err.println( 191 String.format("Cannot %s %s", TEST_SUITE_INFO_PROPERTIES_FILE, e.getMessage())); 192 mType = super.getType(); 193 } 194 } 195 } 196