1 /* 2 * Copyright (C) 2013 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.log; 17 18 import com.android.tradefed.util.IEmail; 19 import com.android.tradefed.util.IEmail.Message; 20 21 import junit.framework.TestCase; 22 23 import org.easymock.EasyMock; 24 25 import java.io.IOException; 26 import java.util.ArrayList; 27 import java.util.Collection; 28 29 /** 30 * Unit tests for {@link TerribleFailureEmailHandler}. 31 */ 32 public class TerribleFailureEmailHandlerTest extends TestCase { 33 private IEmail mMockEmail; 34 private TerribleFailureEmailHandler mWtfEmailHandler; 35 private final static String MOCK_HOST_NAME = "myhostname.mydomain.com"; 36 private long mCurrentTimeMillis; 37 38 @Override setUp()39 protected void setUp() throws Exception { 40 super.setUp(); 41 mMockEmail = EasyMock.createMock(IEmail.class); 42 mWtfEmailHandler = new TerribleFailureEmailHandler(mMockEmail) { 43 @Override 44 protected String getLocalHostName() { 45 return MOCK_HOST_NAME; 46 } 47 48 @Override 49 protected long getCurrentTimeMillis() { 50 return mCurrentTimeMillis; 51 } 52 }; 53 mCurrentTimeMillis = System.currentTimeMillis(); 54 } 55 56 /** 57 * Test normal success case for {@link TerribleFailureEmailHandler#onTerribleFailure(String, Throwable)}. 58 * @throws IOException 59 */ testOnTerribleFailure()60 public void testOnTerribleFailure() throws IllegalArgumentException, IOException { 61 mMockEmail.send(EasyMock.<Message>anyObject()); 62 EasyMock.replay(mMockEmail); 63 mWtfEmailHandler.addDestination("user@domain.com"); 64 boolean retValue = mWtfEmailHandler.onTerribleFailure("something terrible happened", null); 65 EasyMock.verify(mMockEmail); 66 assertTrue(retValue); 67 } 68 69 /** 70 * Test that onTerribleFailure catches IllegalArgumentException when Mailer 71 * state is incorrect 72 */ testOnTerribleFailure_catchesIllegalArgumentException()73 public void testOnTerribleFailure_catchesIllegalArgumentException() { 74 try { 75 mMockEmail.send(EasyMock.<Message> anyObject()); 76 } catch (IOException e) { 77 fail("IOException escaped the method under test - should never happen"); 78 } 79 EasyMock.expectLastCall().andThrow(new IllegalArgumentException("Mailer state illegal")); 80 EasyMock.replay(mMockEmail); 81 82 mWtfEmailHandler.addDestination("user@domain.com"); 83 boolean retValue = mWtfEmailHandler.onTerribleFailure("something terrible happened", null); 84 assertFalse(retValue); 85 } 86 87 /** 88 * Test that onTerribleFailure catches IOException 89 */ testOnTerribleFailure_catchesIOException()90 public void testOnTerribleFailure_catchesIOException() { 91 try { 92 mMockEmail.send(EasyMock.<Message> anyObject()); 93 } catch (IOException e) { 94 fail("IOException escaped the method under test - should never happen"); 95 } 96 EasyMock.expectLastCall().andThrow(new IOException("Mailer had an IO Exception")); 97 EasyMock.replay(mMockEmail); 98 99 mWtfEmailHandler.addDestination("user@domain.com"); 100 boolean retValue = mWtfEmailHandler.onTerribleFailure("something terrible happened", null); 101 assertFalse(retValue); 102 } 103 104 /** 105 * Test that no email is attempted to be sent when there is no destination set 106 */ testOnTerribleFailure_emptyDestinations()107 public void testOnTerribleFailure_emptyDestinations() { 108 boolean retValue = mWtfEmailHandler.onTerribleFailure("something terrible happened", null); 109 assertFalse(retValue); 110 } 111 112 /** 113 * Test that no email is attempted to be sent if it is too adjacent to the previous failure. 114 */ testOnTerribleFailure_adjacentFailures()115 public void testOnTerribleFailure_adjacentFailures() throws IllegalArgumentException, 116 IOException { 117 mMockEmail.send(EasyMock.<Message>anyObject()); 118 mWtfEmailHandler.setMinEmailInterval(60000); 119 120 EasyMock.replay(mMockEmail); 121 mWtfEmailHandler.addDestination("user@domain.com"); 122 boolean retValue = mWtfEmailHandler.onTerribleFailure("something terrible happened", null); 123 assertTrue(retValue); 124 mCurrentTimeMillis += 30000; 125 retValue = mWtfEmailHandler.onTerribleFailure("something terrible happened again", null); 126 assertFalse(retValue); 127 EasyMock.verify(mMockEmail); 128 } 129 130 /** 131 * Test that the second email is attempted to be sent if it is not adjacent to the previous 132 * failure. 133 */ testOnTerribleFailure_notAdjacentFailures()134 public void testOnTerribleFailure_notAdjacentFailures() throws IllegalArgumentException, 135 IOException { 136 mMockEmail.send(EasyMock.<Message>anyObject()); 137 mMockEmail.send(EasyMock.<Message>anyObject()); 138 mWtfEmailHandler.setMinEmailInterval(60000); 139 140 EasyMock.replay(mMockEmail); 141 mWtfEmailHandler.addDestination("user@domain.com"); 142 boolean retValue = mWtfEmailHandler.onTerribleFailure("something terrible happened", null); 143 assertTrue(retValue); 144 mCurrentTimeMillis += 90000; 145 retValue = mWtfEmailHandler.onTerribleFailure("something terrible happened again", null); 146 assertTrue(retValue); 147 EasyMock.verify(mMockEmail); 148 } 149 150 /** 151 * Test that the generated email message actually contains the sender and 152 * destination email addresses. 153 */ testGenerateEmailMessage()154 public void testGenerateEmailMessage() { 155 Collection<String> destinations = new ArrayList<String>(); 156 String sender = "alerter@email.address.com"; 157 String destA = "a@email.address.com"; 158 String destB = "b@email.address.com"; 159 destinations.add(destB); 160 destinations.add(destA); 161 162 mWtfEmailHandler.setSender(sender); 163 mWtfEmailHandler.addDestination(destA); 164 mWtfEmailHandler.addDestination(destB); 165 Message msg = mWtfEmailHandler.generateEmailMessage("something terrible happened", 166 new Throwable("hello")); 167 assertEquals(msg.getSender(), sender); 168 assertTrue(msg.getTo().equals(destinations)); 169 } 170 171 /** 172 * Test normal success case for 173 * {@link TerribleFailureEmailHandler#generateEmailSubject()}. 174 */ testGenerateEmailSubject()175 public void testGenerateEmailSubject() { 176 assertEquals("WTF happened to tradefed on " + MOCK_HOST_NAME, 177 mWtfEmailHandler.generateEmailSubject()); 178 } 179 } 180