1 /* 2 * Copyright (C) 2016 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.*; 19 20 import com.android.ddmlib.testrunner.TestIdentifier; 21 import com.android.tradefed.config.OptionSetter; 22 import com.android.tradefed.invoker.InvocationContext; 23 import com.android.tradefed.util.FileUtil; 24 import com.android.tradefed.util.SubprocessTestResultsParser; 25 26 import org.easymock.EasyMock; 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 import java.io.File; 33 import java.util.Collections; 34 35 /** 36 * Unit Tests for {@link SubprocessResultsReporter} 37 */ 38 @RunWith(JUnit4.class) 39 public class SubprocessResultsReporterTest { 40 41 private SubprocessResultsReporter mReporter; 42 43 @Before 44 public void setUp() { 45 mReporter = new SubprocessResultsReporter(); 46 } 47 48 /** 49 * Test that when none of the option for reporting is set, nothing happen. 50 */ 51 @Test 52 public void testPrintEvent_Inop() { 53 TestIdentifier testId = new TestIdentifier("com.fakeclass", "faketest"); 54 mReporter.testStarted(testId); 55 mReporter.testFailed(testId, "fake failure"); 56 mReporter.testEnded(testId, Collections.emptyMap()); 57 mReporter.printEvent(null, null); 58 } 59 60 /** 61 * Test that when a report file is specified it logs event to it. 62 */ 63 @Test 64 public void testPrintEvent_printToFile() throws Exception { 65 OptionSetter setter = new OptionSetter(mReporter); 66 File tmpReportFile = FileUtil.createTempFile("subprocess-reporter", "unittest"); 67 try { 68 setter.setOptionValue("subprocess-report-file", tmpReportFile.getAbsolutePath()); 69 mReporter.testRunStarted("TEST", 5); 70 mReporter.testRunEnded(100, Collections.emptyMap()); 71 String content = FileUtil.readStringFromFile(tmpReportFile); 72 assertEquals("TEST_RUN_STARTED {\"testCount\":5,\"runName\":\"TEST\"}\n" 73 + "TEST_RUN_ENDED {\"time\":100}\n", content); 74 } finally { 75 FileUtil.deleteFile(tmpReportFile); 76 } 77 } 78 79 /** 80 * Test that when the specified report file is not writable we throw an exception. 81 */ 82 @Test 83 public void testPrintEvent_nonWritableFile() throws Exception { 84 OptionSetter setter = new OptionSetter(mReporter); 85 File tmpReportFile = FileUtil.createTempFile("subprocess-reporter", "unittest"); 86 try { 87 tmpReportFile.setWritable(false); 88 setter.setOptionValue("subprocess-report-file", tmpReportFile.getAbsolutePath()); 89 mReporter.testRunStarted("TEST", 5); 90 fail("Should have thrown an exception."); 91 } catch (RuntimeException expected) { 92 assertEquals(String.format("report file: %s is not writable", 93 tmpReportFile.getAbsolutePath()), expected.getMessage()); 94 } finally { 95 FileUtil.deleteFile(tmpReportFile); 96 } 97 } 98 99 /** 100 * Test that events sent through the socket reporting part are received on the other hand. 101 */ 102 @Test 103 public void testPrintEvent_printToSocket() throws Exception { 104 TestIdentifier testId = new TestIdentifier("com.fakeclass", "faketest"); 105 ITestInvocationListener mMockListener = EasyMock.createMock(ITestInvocationListener.class); 106 SubprocessTestResultsParser receiver = 107 new SubprocessTestResultsParser(mMockListener, true, new InvocationContext()); 108 try { 109 OptionSetter setter = new OptionSetter(mReporter); 110 setter.setOptionValue("subprocess-report-port", 111 Integer.toString(receiver.getSocketServerPort())); 112 // mirror calls between receiver and sender. 113 mMockListener.testIgnored(testId); 114 mMockListener.testAssumptionFailure(testId, "fake trace"); 115 mMockListener.testRunFailed("no reason"); 116 mMockListener.invocationFailed((Throwable)EasyMock.anyObject()); 117 EasyMock.replay(mMockListener); 118 mReporter.testIgnored(testId); 119 mReporter.testAssumptionFailure(testId, "fake trace"); 120 mReporter.testRunFailed("no reason"); 121 mReporter.invocationFailed(new Throwable()); 122 mReporter.close(); 123 receiver.joinReceiver(500); 124 EasyMock.verify(mMockListener); 125 } finally { 126 receiver.close(); 127 } 128 } 129 } 130