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.log; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertNotEquals; 20 21 import com.android.ddmlib.Log.LogLevel; 22 import com.android.tradefed.config.ConfigurationException; 23 import com.android.tradefed.config.OptionSetter; 24 import com.android.tradefed.result.InputStreamSource; 25 import com.android.tradefed.util.StreamUtil; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 import java.io.BufferedReader; 32 import java.io.IOException; 33 import java.io.InputStreamReader; 34 35 /** Unit tests for {@link FileLogger}. */ 36 @RunWith(JUnit4.class) 37 public class FileLoggerTest { 38 39 private static final String LOG_TAG = "FileLoggerTest"; 40 41 /** 42 * Test logging to a logger. 43 * 44 * @throws ConfigurationException if unable to create log file 45 * @throws IOException if unable to read from the file 46 * @throws SecurityException if unable to delete the log file on cleanup 47 */ 48 @Test 49 public void testLogToLogger() throws ConfigurationException, IOException, SecurityException { 50 String Text1 = "The quick brown fox jumps over the lazy doggie."; 51 String Text2 = "Betty Botter bought some butter, 'But,' she said, 'this butter's bitter.'"; 52 String Text3 = "Wolf zombies quickly spot the jinxed grave."; 53 BufferedReader logFileReader = null; 54 FileLogger logger = new FileLogger(); 55 InputStreamSource logSource = null; 56 57 try { 58 logger.init(); 59 // Write 3 lines of text to the log... 60 logger.printLog(LogLevel.INFO, LOG_TAG, Text1); 61 String expectedText1 = LogUtil.getLogFormatString(LogLevel.INFO, LOG_TAG, Text1).trim(); 62 logger.printLog(LogLevel.VERBOSE, LOG_TAG, Text2); 63 String expectedText2 = 64 LogUtil.getLogFormatString(LogLevel.VERBOSE, LOG_TAG, Text2).trim(); 65 logger.printLog(LogLevel.ASSERT, LOG_TAG, Text3); 66 String expectedText3 = 67 LogUtil.getLogFormatString(LogLevel.ASSERT, LOG_TAG, Text3).trim(); 68 // Verify the 3 lines we logged 69 logSource = logger.getLog(); 70 logFileReader = new BufferedReader(new InputStreamReader( 71 logSource.createInputStream())); 72 73 String actualLogString = logFileReader.readLine().trim(); 74 assertEquals(trimTimestamp(expectedText1), trimTimestamp(actualLogString)); 75 76 actualLogString = logFileReader.readLine().trim(); 77 assertEquals(trimTimestamp(expectedText2), trimTimestamp(actualLogString)); 78 79 actualLogString = logFileReader.readLine().trim(); 80 assertEquals(trimTimestamp(expectedText3), trimTimestamp(actualLogString)); 81 } 82 finally { 83 StreamUtil.close(logFileReader); 84 StreamUtil.cancel(logSource); 85 logger.closeLog(); 86 } 87 } 88 89 /** 90 * Remove the timestamp at the beginning of the log message. 91 * 92 * @param message log message with leading timestamp. 93 * @return a {@link String} of message without leading timestamp. 94 */ 95 private String trimTimestamp(String message) { 96 // The log level character is prefixed to the log tag. For example: 97 // 04-11 10:30:[50] I/FileLoggerTest: The quick brown fox jumps. 98 int startIndex = message.indexOf(LOG_TAG) - 2; 99 return message.substring(startIndex); 100 } 101 102 /** 103 * Test behavior when {@link FileLogger#getLog()} is called after {@link FileLogger#closeLog()}. 104 */ 105 @Test 106 public void testGetLog_afterClose() throws Exception { 107 FileLogger logger = new FileLogger(); 108 logger.init(); 109 logger.closeLog(); 110 // expect this to be silently handled 111 logger.getLog(); 112 } 113 114 /** 115 * Test that no unexpected Exceptions occur if {@link FileLogger#printLog(LogLevel, String, 116 * String)} is called after {@link FileLogger#closeLog()} 117 */ 118 @Test 119 public void testCloseLog() throws Exception { 120 FileLogger logger = new FileLogger(); 121 logger.init(); 122 // test the package-private methods to capture any exceptions that occur 123 logger.doCloseLog(); 124 logger.writeToLog("test2"); 125 } 126 127 /** 128 * Test behavior when {@link FileLogger#getLog()} is called when {@link FileLogger#init()} has 129 * not been called. 130 */ 131 @Test 132 public void testGetLog_NoInit() { 133 FileLogger logger = new FileLogger(); 134 // expect this to be silently handled 135 logger.getLog(); 136 } 137 138 /** 139 * Test that the {@link FileLogger#clone()} properly copy all the options from the original 140 * object. 141 */ 142 @Test 143 public void testClone() throws Exception { 144 FileLogger logger = new FileLogger(); 145 OptionSetter setter = new OptionSetter(logger); 146 setter.setOptionValue("max-log-size", "500"); 147 setter.setOptionValue("log-level", "INFO"); 148 FileLogger clone = (FileLogger) logger.clone(); 149 assertEquals(LogLevel.INFO, clone.getLogLevel()); 150 // We now have 2 distinct objects 151 assertNotEquals(logger, clone); 152 assertEquals(logger.getMaxLogSizeMbytes(), clone.getMaxLogSizeMbytes()); 153 } 154 } 155