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 package com.android.cts.tradefed.result; 17 18 import com.android.cts.tradefed.result.TestLog.TestLogType; 19 20 import org.kxml2.io.KXmlSerializer; 21 import org.xmlpull.v1.XmlPullParser; 22 import org.xmlpull.v1.XmlPullParserException; 23 import org.xmlpull.v1.XmlPullParserFactory; 24 25 import junit.framework.TestCase; 26 27 import java.io.StringReader; 28 import java.io.StringWriter; 29 30 /** Tests for {@link TestLog}. */ 31 public class TestLogTest extends TestCase { 32 testTestLogType_fromDataName()33 public void testTestLogType_fromDataName() { 34 assertNull(TestLogType.fromDataName(null)); 35 assertNull(TestLogType.fromDataName("")); 36 assertNull(TestLogType.fromDataName("kmsg-foo_bar_test")); 37 38 assertEquals(TestLogType.LOGCAT, 39 TestLogType.fromDataName("logcat-foo_bar_test")); 40 assertEquals(TestLogType.BUGREPORT, 41 TestLogType.fromDataName("bug-foo_bar_test")); 42 } 43 testTestLogType_getAttrValue()44 public void testTestLogType_getAttrValue() { 45 assertEquals("logcat", TestLogType.LOGCAT.getAttrValue()); 46 assertEquals("bugreport", TestLogType.BUGREPORT.getAttrValue()); 47 } 48 testFromDataName()49 public void testFromDataName() { 50 TestLog log = TestLog.fromDataName("logcat-baz_test", "http://logs/baz_test"); 51 assertEquals(TestLogType.LOGCAT, log.getLogType()); 52 assertEquals("http://logs/baz_test", log.getUrl()); 53 } 54 testFromDataName_unrecognizedDataName()55 public void testFromDataName_unrecognizedDataName() { 56 assertNull(TestLog.fromDataName("kmsg-baz_test", null)); 57 } 58 testFromDataName_nullDataName()59 public void testFromDataName_nullDataName() { 60 assertNull(TestLog.fromDataName(null, "http://logs/baz_test")); 61 } 62 testFromDataName_nullUrl()63 public void testFromDataName_nullUrl() { 64 assertNull(TestLog.fromDataName("logcat-bar_test", null)); 65 } 66 testFromDataName_allNull()67 public void testFromDataName_allNull() { 68 assertNull(TestLog.fromDataName(null, null)); 69 } 70 testFromXml()71 public void testFromXml() throws Exception { 72 TestLog log = TestLog.fromXml(newXml("<TestLog type=\"logcat\" url=\"http://logs/baz_test\">")); 73 assertEquals(TestLogType.LOGCAT, log.getLogType()); 74 assertEquals("http://logs/baz_test", log.getUrl()); 75 } 76 testFromXml_unrecognizedType()77 public void testFromXml_unrecognizedType() throws Exception { 78 assertNull(TestLog.fromXml(newXml("<TestLog type=\"kmsg\" url=\"http://logs/baz_test\">"))); 79 } 80 testFromXml_noTypeAttribute()81 public void testFromXml_noTypeAttribute() throws Exception { 82 assertNull(TestLog.fromXml(newXml("<TestLog url=\"http://logs/baz_test\">"))); 83 } 84 testFromXml_noUrlAttribute()85 public void testFromXml_noUrlAttribute() throws Exception { 86 assertNull(TestLog.fromXml(newXml("<TestLog type=\"bugreport\">"))); 87 } 88 testFromXml_allNull()89 public void testFromXml_allNull() throws Exception { 90 assertNull(TestLog.fromXml(newXml("<TestLog>"))); 91 } 92 testSerialize()93 public void testSerialize() throws Exception { 94 KXmlSerializer serializer = new KXmlSerializer(); 95 StringWriter writer = new StringWriter(); 96 serializer.setOutput(writer); 97 98 TestLog log = TestLog.of(TestLogType.LOGCAT, "http://logs/foo/bar"); 99 log.serialize(serializer); 100 assertEquals("<TestLog type=\"logcat\" url=\"http://logs/foo/bar\" />", writer.toString()); 101 } 102 testIsTag()103 public void testIsTag() { 104 assertTrue(TestLog.isTag("TestLog")); 105 assertFalse(TestLog.isTag("TestResult")); 106 } 107 newXml(String xml)108 private XmlPullParser newXml(String xml) throws Exception { 109 XmlPullParserFactory factory = org.xmlpull.v1.XmlPullParserFactory.newInstance(); 110 XmlPullParser parser = factory.newPullParser(); 111 parser.setInput(new StringReader(xml)); 112 113 // Move the parser from the START_DOCUMENT stage to the START_TAG of the data. 114 parser.next(); 115 116 return parser; 117 } 118 } 119