1 /* 2 * Copyright (C) 2010 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.tradefed.result; 17 18 import com.android.ddmlib.testrunner.TestIdentifier; 19 import com.android.tradefed.build.BuildInfo; 20 import com.android.tradefed.invoker.IInvocationContext; 21 import com.android.tradefed.invoker.InvocationContext; 22 23 import junit.framework.TestCase; 24 25 import java.io.ByteArrayOutputStream; 26 import java.io.InputStream; 27 import java.util.Collections; 28 import java.util.Map; 29 30 /** 31 * Unit tests for {@link XmlResultReporter}. 32 */ 33 public class XmlResultReporterTest extends TestCase { 34 private static final String PATH = "path"; 35 private static final String URL = "url"; 36 37 private XmlResultReporter mResultReporter; 38 private ByteArrayOutputStream mOutputStream; 39 private ILogSaver mMockLogSaver; 40 41 class MockLogSaver implements ILogSaver { 42 @Override 43 public LogFile saveLogData(String dataName, LogDataType dataType, 44 InputStream dataStream) { 45 return new LogFile(PATH, URL, dataType.isCompressed(), dataType.isText()); 46 } 47 48 @Override 49 public LogFile saveLogDataRaw(String dataName, String ext, InputStream dataStream) { 50 return new LogFile(PATH, URL, false, false); 51 } 52 53 @Override 54 public LogFile getLogReportDir() { 55 return new LogFile(PATH, URL, false, false); 56 } 57 58 @Override 59 public void invocationStarted(IInvocationContext context) { 60 // Ignore 61 } 62 63 @Override 64 public void invocationEnded(long elapsedTime) { 65 // Ignore 66 } 67 } 68 69 /** 70 * {@inheritDoc} 71 */ 72 @Override 73 protected void setUp() throws Exception { 74 super.setUp(); 75 76 mMockLogSaver = new MockLogSaver(); 77 78 mResultReporter = new XmlResultReporter() { 79 @Override 80 ByteArrayOutputStream createOutputStream() { 81 mOutputStream = new ByteArrayOutputStream(); 82 return mOutputStream; 83 } 84 85 @Override 86 String getTimestamp() { 87 return "ignore"; 88 } 89 }; 90 mResultReporter.setLogSaver(mMockLogSaver); 91 } 92 93 @Override 94 protected void tearDown() throws Exception { 95 super.tearDown(); 96 } 97 98 /** 99 * A simple test to ensure expected output is generated for test run with no tests. 100 */ 101 public void testEmptyGeneration() { 102 final String expectedOutput = "<?xml version='1.0' encoding='UTF-8' ?>" + 103 "<testsuite name=\"test\" tests=\"0\" failures=\"0\" errors=\"0\" time=\"1\" " + 104 "timestamp=\"ignore\" hostname=\"localhost\"> " + 105 "<properties />" + 106 "</testsuite>"; 107 IInvocationContext context = new InvocationContext(); 108 context.addDeviceBuildInfo("fakeDevice", new BuildInfo("1", "test")); 109 context.setTestTag("test"); 110 mResultReporter.invocationStarted(context); 111 mResultReporter.invocationEnded(1); 112 assertEquals(expectedOutput, getOutput()); 113 } 114 115 /** 116 * A simple test to ensure expected output is generated for test run with a single passed test. 117 */ 118 public void testSinglePass() { 119 Map<String, String> emptyMap = Collections.emptyMap(); 120 final TestIdentifier testId = new TestIdentifier("FooTest", "testFoo"); 121 IInvocationContext context = new InvocationContext(); 122 context.addDeviceBuildInfo("fakeDevice", new BuildInfo()); 123 context.setTestTag("stub"); 124 mResultReporter.invocationStarted(context); 125 mResultReporter.testRunStarted("run", 1); 126 mResultReporter.testStarted(testId); 127 mResultReporter.testEnded(testId, emptyMap); 128 mResultReporter.testRunEnded(3, emptyMap); 129 mResultReporter.invocationEnded(1); 130 String output = getOutput(); 131 // TODO: consider doing xml based compare 132 assertTrue(output.contains("tests=\"1\" failures=\"0\" errors=\"0\"")); 133 final String testCaseTag = String.format("<testcase name=\"%s\" classname=\"%s\"", 134 testId.getTestName(), testId.getClassName()); 135 assertTrue(output.contains(testCaseTag)); 136 } 137 138 /** 139 * A simple test to ensure expected output is generated for test run with a single failed test. 140 */ 141 public void testSingleFail() { 142 Map<String, String> emptyMap = Collections.emptyMap(); 143 final TestIdentifier testId = new TestIdentifier("FooTest", "testFoo"); 144 final String trace = "this is a trace"; 145 IInvocationContext context = new InvocationContext(); 146 context.addDeviceBuildInfo("fakeDevice", new BuildInfo()); 147 context.setTestTag("stub"); 148 mResultReporter.invocationStarted(context); 149 mResultReporter.testRunStarted("run", 1); 150 mResultReporter.testStarted(testId); 151 mResultReporter.testFailed(testId, trace); 152 mResultReporter.testEnded(testId, emptyMap); 153 mResultReporter.testRunEnded(3, emptyMap); 154 mResultReporter.invocationEnded(1); 155 String output = getOutput(); 156 // TODO: consider doing xml based compare 157 assertTrue(output.contains("tests=\"1\" failures=\"1\" errors=\"0\"")); 158 final String testCaseTag = String.format("<testcase name=\"%s\" classname=\"%s\"", 159 testId.getTestName(), testId.getClassName()); 160 assertTrue(output.contains(testCaseTag)); 161 final String failureTag = String.format("<failure>%s</failure>", trace); 162 assertTrue(output.contains(failureTag)); 163 } 164 165 /** 166 * Gets the output produced, stripping it of extraneous whitespace characters. 167 */ 168 private String getOutput() { 169 String output = mOutputStream.toString(); 170 // ignore newlines and tabs whitespace 171 output = output.replaceAll("[\\r\\n\\t]", ""); 172 // replace two ws chars with one 173 return output.replaceAll(" ", " "); 174 } 175 } 176