1 /*
2  * Copyright (C) 2015 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.compatibility.common.util;
18 
19 import junit.framework.TestCase;
20 
21 /**
22  * Unit tests for {@link CaseResult}
23  */
24 public class CaseResultTest extends TestCase {
25 
26     private static final String CLASS = "android.test.FoorBar";
27     private static final String METHOD_1 = "testBlah1";
28     private static final String METHOD_2 = "testBlah2";
29     private static final String METHOD_3 = "testBlah3";
30     private static final String MESSAGE = "Something small is not alright";
31     private static final String STACK_TRACE = "Something small is not alright\n " +
32             "at four.big.insects.Marley.sing(Marley.java:10)";
33     private CaseResult mResult;
34 
35     @Override
setUp()36     public void setUp() throws Exception {
37         mResult = new CaseResult(CLASS);
38     }
39 
40     @Override
tearDown()41     public void tearDown() throws Exception {
42         mResult = null;
43     }
44 
testAccessors()45     public void testAccessors() throws Exception {
46         assertEquals("Incorrect case name", CLASS, mResult.getName());
47     }
48 
testResultCreation()49     public void testResultCreation() throws Exception {
50         ITestResult testResult = mResult.getOrCreateResult(METHOD_1);
51         // Should create one
52         assertEquals("Expected one result", 1, mResult.getResults().size());
53         assertTrue("Expected test result", mResult.getResults().contains(testResult));
54         // Should not create another one
55         ITestResult testResult2 = mResult.getOrCreateResult(METHOD_1);
56         assertEquals("Expected the same result", testResult, testResult2);
57         assertEquals("Expected one result", 1, mResult.getResults().size());
58     }
59 
testResultReporting()60     public void testResultReporting() throws Exception {
61         ITestResult testResult = mResult.getOrCreateResult(METHOD_1);
62         testResult.failed(STACK_TRACE);
63         assertEquals("Expected status to be set", TestStatus.FAIL, testResult.getResultStatus());
64         assertEquals("Expected message to be set", MESSAGE, testResult.getMessage());
65         assertEquals("Expected stack to be set", STACK_TRACE, testResult.getStackTrace());
66         testResult = mResult.getOrCreateResult(METHOD_2);
67         testResult.passed(null);
68         assertEquals("Expected status to be set", TestStatus.PASS, testResult.getResultStatus());
69         assertEquals("Expected two results", 2, mResult.getResults().size());
70     }
71 
testCountResults()72     public void testCountResults() throws Exception {
73         mResult.getOrCreateResult(METHOD_1).failed(STACK_TRACE);
74         mResult.getOrCreateResult(METHOD_2).failed(STACK_TRACE);
75         mResult.getOrCreateResult(METHOD_3).passed(null);
76         assertEquals("Expected two failures", 2, mResult.countResults(TestStatus.FAIL));
77         assertEquals("Expected one pass", 1, mResult.countResults(TestStatus.PASS));
78     }
79 
testMergeCase()80     public void testMergeCase() throws Exception {
81         mResult.getOrCreateResult(METHOD_1).failed(STACK_TRACE);
82         mResult.getOrCreateResult(METHOD_2).passed(null);
83 
84         // Same case another test and passing results in method 2
85         CaseResult otherResult = new CaseResult(CLASS);
86         otherResult.getOrCreateResult(METHOD_1).passed(null);
87         otherResult.getOrCreateResult(METHOD_2).passed(null);
88         otherResult.getOrCreateResult(METHOD_3).failed(STACK_TRACE);
89 
90         mResult.mergeFrom(otherResult);
91         assertEquals("Expected one result", 3, mResult.getResults().size());
92         assertEquals("Expected one failures", 1, mResult.countResults(TestStatus.FAIL));
93         assertEquals("Expected two pass", 2, mResult.countResults(TestStatus.PASS));
94     }
95 
testMergeCase_passToFail()96      public void testMergeCase_passToFail() throws Exception {
97         mResult.getOrCreateResult(METHOD_1).passed(null);
98 
99         // Same case another test and passing results in method 2
100         CaseResult otherResult = new CaseResult(CLASS);
101         otherResult.getOrCreateResult(METHOD_1).passed(null);
102         otherResult.getOrCreateResult(METHOD_2).passed(null);
103         otherResult.getOrCreateResult(METHOD_3).failed(STACK_TRACE);
104 
105         mResult.mergeFrom(otherResult);
106 
107         assertEquals("Expected one result", 3, mResult.getResults().size());
108         assertEquals("Expected one failures", 1, mResult.countResults(TestStatus.FAIL));
109         assertEquals("Expected two pass", 2, mResult.countResults(TestStatus.PASS));
110     }
111 
testMergeCase_mismatchedModuleName()112     public void testMergeCase_mismatchedModuleName() throws Exception {
113 
114         CaseResult otherResult = new CaseResult(CLASS + "foo");
115         try {
116             mResult.mergeFrom(otherResult);
117             fail("Expected IlleglArgumentException");
118         } catch (IllegalArgumentException expected) {}
119     }
120 }
121