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 */
16package com.android.cts.tradefed.result;
17
18import com.android.ddmlib.testrunner.TestIdentifier;
19import com.android.ddmlib.testrunner.ITestRunListener.TestFailure;
20import com.android.tradefed.result.XmlResultReporter;
21import com.android.tradefed.targetsetup.BuildInfo;
22import com.android.tradefed.util.FileUtil;
23
24import java.io.ByteArrayOutputStream;
25import java.io.File;
26import java.io.IOException;
27import java.io.OutputStream;
28import java.util.Collections;
29import java.util.Map;
30
31import junit.framework.TestCase;
32
33/**
34 * Unit tests for {@link XmlResultReporter}.
35 */
36public class CtsXmlResultReporterTest extends TestCase {
37
38    private CtsXmlResultReporter mResultReporter;
39    private ByteArrayOutputStream mOutputStream;
40    private File mReportDir;
41
42    /**
43     * {@inheritDoc}
44     */
45    @Override
46    protected void setUp() throws Exception {
47        super.setUp();
48
49        mOutputStream = new ByteArrayOutputStream();
50        mResultReporter = new CtsXmlResultReporter() {
51            @Override
52            OutputStream createOutputResultStream(File reportDir) throws IOException {
53                return mOutputStream;
54            }
55
56            @Override
57            String getTimestamp() {
58                return "ignore";
59            }
60        };
61        // TODO: use mock file dir instead
62        mReportDir = FileUtil.createTempDir("foo");
63        mResultReporter.setReportDir(mReportDir);
64    }
65
66    @Override
67    protected void tearDown() throws Exception {
68        if (mReportDir != null) {
69            FileUtil.recursiveDelete(mReportDir);
70        }
71        super.tearDown();
72    }
73
74    /**
75     * A simple test to ensure expected output is generated for test run with no tests.
76     */
77    public void testEmptyGeneration() {
78        final String expectedOutput = "<?xml version='1.0' encoding='UTF-8' standalone='no' ?>" +
79            "<?xml-stylesheet type=\"text/xsl\" href=\"cts_result.xsl\"?>" +
80            "<TestResult testPlan=\"unknown\" profile=\"unknown\" starttime=\"ignore\" endtime=\"ignore\" version=\"2.0\"> " +
81            "<Summary failed=\"0\" notExecuted=\"0\" timeout=\"0\" omitted=\"0\" pass=\"0\" total=\"0\" />" +
82            "</TestResult>";
83        mResultReporter.invocationStarted(new BuildInfo(1, "test", "test"));
84        mResultReporter.invocationEnded(1);
85        assertEquals(expectedOutput, getOutput());
86    }
87
88    /**
89     * A simple test to ensure expected output is generated for test run with a single passed test.
90     */
91    public void testSinglePass() {
92        Map<String, String> emptyMap = Collections.emptyMap();
93        final TestIdentifier testId = new TestIdentifier("com.foo.FooTest", "testFoo");
94        mResultReporter.invocationStarted(new BuildInfo());
95        mResultReporter.testRunStarted("run", 1);
96        mResultReporter.testStarted(testId);
97        mResultReporter.testEnded(testId, emptyMap);
98        mResultReporter.testRunEnded(3000, emptyMap);
99        mResultReporter.invocationEnded(1);
100        String output =  getOutput();
101        // TODO: consider doing xml based compare
102        assertTrue(output.contains(
103                "<Summary failed=\"0\" notExecuted=\"0\" timeout=\"0\" omitted=\"0\" pass=\"1\" total=\"1\" />"));
104        assertTrue(output.contains("<TestPackage name=\"run\" runTime=\"3s\" digest=\"\" " +
105                "failed=\"0\" notExecuted=\"0\" timeout=\"0\" omitted=\"0\" pass=\"1\" total=\"1\">"));
106        assertTrue(output.contains(String.format("<TestCase name=\"%s\">", testId.getClassName())));
107
108        final String testCaseTag = String.format(
109                "<Test name=\"%s\" result=\"pass\" />", testId.getTestName());
110        assertTrue(output.contains(testCaseTag));
111    }
112
113    /**
114     * A simple test to ensure expected output is generated for test run with a single failed test.
115     */
116    public void testSingleFail() {
117        Map<String, String> emptyMap = Collections.emptyMap();
118        final TestIdentifier testId = new TestIdentifier("FooTest", "testFoo");
119        final String trace = "this is a trace\nmore trace";
120        mResultReporter.invocationStarted(new BuildInfo());
121        mResultReporter.testRunStarted("run", 1);
122        mResultReporter.testStarted(testId);
123        mResultReporter.testFailed(TestFailure.FAILURE, testId, trace);
124        mResultReporter.testEnded(testId, emptyMap);
125        mResultReporter.testRunEnded(3, emptyMap);
126        mResultReporter.invocationEnded(1);
127        String output =  getOutput();
128        System.out.print(getOutput());
129        // TODO: consider doing xml based compare
130        assertTrue(output.contains(
131                "<Summary failed=\"1\" notExecuted=\"0\" timeout=\"0\" omitted=\"0\" pass=\"0\" total=\"1\" />"));
132        final String failureTag =
133                "<FailedScene message=\"this is a trace\">this is a tracemore trace";
134        assertTrue(output.contains(failureTag));
135    }
136
137    /**
138     * Gets the output produced, stripping it of extraneous whitespace characters.
139     */
140    private String getOutput() {
141        String output = mOutputStream.toString();
142        // ignore newlines and tabs whitespace
143        output = output.replaceAll("[\\r\\n\\t]", "");
144        // replace two ws chars with one
145        return output.replaceAll("  ", " ");
146    }
147}
148