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 
17 package com.android.cts.tradefed.result;
18 
19 import com.android.tradefed.util.xml.AbstractXmlParser.ParseException;
20 
21 import java.io.Reader;
22 import java.io.StringReader;
23 
24 import junit.framework.TestCase;
25 
26 /**
27  * Unit tests for {@link TestSummaryXml}.
28  */
29 public class TestSummaryXmlTest extends TestCase {
30 
31     static final String TEST_DATA =
32         "<TestResult>" +
33             "<Summary failed=\"1\" notExecuted=\"2\" pass=\"3\" timeout=\"4\"/>" +
34         "</TestResult>";
35 
36     static final String MISSING_DATA =
37         "<TestResult>" +
38             "<Foo failed=\"1\" notExecuted=\"2\" pass=\"3\" timeout=\"4\"/>" +
39         "</TestResult>";
40 
testConstructor()41     public void testConstructor()  {
42         TestSummaryXml result = new TestSummaryXml(1, "2011-11-01");
43         assertEquals(1, result.getId());
44         assertEquals("2011-11-01", result.getTimestamp());
45     }
46 
47     /**
48      * Simple test for parsing summary data
49      */
testParse()50     public void testParse() throws ParseException  {
51         TestSummaryXml result = new TestSummaryXml(1, "2011-11-01");
52         result.parse(getStringAsReader(TEST_DATA));
53         // expect failed and timeout to be summed
54         assertEquals(5, result.getNumFailed());
55         assertEquals(2, result.getNumIncomplete());
56         assertEquals(3, result.getNumPassed());
57     }
58 
59     /**
60      *  Test data where Summary tag is missing
61      */
testParse_missing()62     public void testParse_missing() {
63         TestSummaryXml result = new TestSummaryXml(1, "2011-11-01");
64         try {
65             result.parse(getStringAsReader(MISSING_DATA));
66             fail("ParseException not thrown");
67         } catch (ParseException e) {
68             // expected
69         }
70     }
71 
getStringAsReader(String input)72     private Reader getStringAsReader(String input) {
73         return new StringReader(input);
74     }
75 }
76