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 static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertTrue; 20 21 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 22 import com.android.tradefed.testtype.DeviceTestCase; 23 24 import junit.framework.AssertionFailedError; 25 import junit.framework.TestCase; 26 27 import org.easymock.Capture; 28 import org.easymock.EasyMock; 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 import java.lang.annotation.ElementType; 35 import java.lang.annotation.Inherited; 36 import java.lang.annotation.Retention; 37 import java.lang.annotation.RetentionPolicy; 38 import java.lang.annotation.Target; 39 import java.util.HashMap; 40 41 /** Unit tests for {@link JUnitToInvocationResultForwarder}. */ 42 @RunWith(JUnit4.class) 43 public class JUnitToInvocationResultForwarderTest { 44 45 private ITestInvocationListener mListener; 46 private JUnitToInvocationResultForwarder mForwarder; 47 48 @Before setUp()49 public void setUp() throws Exception { 50 mListener = EasyMock.createMock(ITestInvocationListener.class); 51 mForwarder = new JUnitToInvocationResultForwarder(mListener); 52 } 53 54 /** Inherited test annotation to ensure we properly collected it. */ 55 @Target(ElementType.METHOD) 56 @Inherited 57 @Retention(RetentionPolicy.RUNTIME) 58 public @interface MyCustomAnnotation {} 59 60 /** Base test class with a test method annotated with an inherited annotation. */ 61 public class BaseTestClass extends TestCase { 62 @MyCustomAnnotation testbaseWithAnnotation()63 public void testbaseWithAnnotation() {} 64 } 65 66 /** Extension of the base test class. */ 67 public class InheritingClass extends BaseTestClass {} 68 69 /** 70 * Test method for {@link JUnitToInvocationResultForwarder#addFailure(junit.framework.Test, 71 * AssertionFailedError)}. 72 */ 73 @Test testAddFailure()74 public void testAddFailure() { 75 final AssertionFailedError a = new AssertionFailedError(); 76 mListener.testFailed( 77 EasyMock.eq(new TestDescription(DeviceTestCase.class.getName(), "testAddFailure")), 78 (String) EasyMock.anyObject()); 79 EasyMock.replay(mListener); 80 DeviceTestCase test = new DeviceTestCase(); 81 test.setName("testAddFailure"); 82 mForwarder.addFailure(test, a); 83 EasyMock.verify(mListener); 84 } 85 86 /** Test method for {@link JUnitToInvocationResultForwarder#endTest(junit.framework.Test)}. */ 87 @Test testEndTest()88 public void testEndTest() { 89 HashMap<String, Metric> emptyMap = new HashMap<>(); 90 mListener.testEnded( 91 EasyMock.eq(new TestDescription(DeviceTestCase.class.getName(), "testEndTest")), 92 EasyMock.eq(emptyMap)); 93 DeviceTestCase test = new DeviceTestCase(); 94 test.setName("testEndTest"); 95 EasyMock.replay(mListener); 96 mForwarder.endTest(test); 97 EasyMock.verify(mListener); 98 } 99 100 /** Test method for {@link JUnitToInvocationResultForwarder#startTest(junit.framework.Test)}. */ 101 @Test testStartTest()102 public void testStartTest() { 103 mListener.testStarted( 104 EasyMock.eq(new TestDescription(DeviceTestCase.class.getName(), "testStartTest"))); 105 DeviceTestCase test = new DeviceTestCase(); 106 test.setName("testStartTest"); 107 EasyMock.replay(mListener); 108 mForwarder.startTest(test); 109 EasyMock.verify(mListener); 110 } 111 112 /** 113 * Test method for {@link JUnitToInvocationResultForwarder#startTest(junit.framework.Test)} when 114 * the test method is inherited. 115 */ 116 @Test testStartTest_annotations()117 public void testStartTest_annotations() { 118 Capture<TestDescription> capture = new Capture<>(); 119 mListener.testStarted(EasyMock.capture(capture)); 120 InheritingClass test = new InheritingClass(); 121 test.setName("testbaseWithAnnotation"); 122 EasyMock.replay(mListener); 123 mForwarder.startTest(test); 124 EasyMock.verify(mListener); 125 TestDescription desc = capture.getValue(); 126 assertEquals(InheritingClass.class.getName(), desc.getClassName()); 127 assertEquals("testbaseWithAnnotation", desc.getTestName()); 128 assertEquals(1, desc.getAnnotations().size()); 129 // MyCustomAnnotation is inherited 130 assertTrue(desc.getAnnotations().iterator().next() instanceof MyCustomAnnotation); 131 } 132 } 133