1 /*
2  * Copyright (C) 2019 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.assertThat;
20 
21 import static org.hamcrest.CoreMatchers.endsWith;
22 import static org.junit.Assert.assertTrue;
23 
24 import com.android.ddmlib.Log.LogLevel;
25 import com.android.tradefed.config.ConfigurationException;
26 import com.android.tradefed.config.OptionSetter;
27 import com.android.tradefed.util.FileUtil;
28 
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 import java.io.BufferedReader;
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.InputStreamReader;
41 import java.nio.file.Files;
42 import java.util.List;
43 import java.util.stream.Collectors;
44 
45 /** Unit tests for {@link SimpleFileLogger}. */
46 @RunWith(JUnit4.class)
47 public class SimpleFileLoggerTest {
48 
49     private static final String LOG_TAG = SimpleFileLoggerTest.class.getSimpleName();
50 
51     private File mLogFile;
52     private SimpleFileLogger mLogger;
53 
54     @Before
setUp()55     public void setUp() throws IOException, ConfigurationException {
56         mLogFile = Files.createTempFile(LOG_TAG, null).toFile();
57         mLogger = new SimpleFileLogger();
58         OptionSetter setter = new OptionSetter(mLogger);
59         setter.setOptionValue("path", mLogFile.getAbsolutePath());
60     }
61 
62     @After
tearDown()63     public void tearDown() {
64         if (mLogger != null) {
65             mLogger.closeLog();
66         }
67         FileUtil.deleteFile(mLogFile);
68     }
69 
70     @Test
testPrintLog()71     public void testPrintLog() throws IOException {
72         mLogger.init();
73         mLogger.printLog(LogLevel.DEBUG, LOG_TAG, "debug");
74         mLogger.printLog(LogLevel.INFO, LOG_TAG, "info");
75         mLogger.printLog(LogLevel.WARN, LOG_TAG, "warn");
76         mLogger.printLog(LogLevel.ERROR, LOG_TAG, "error");
77 
78         List<String> lines = readLines(new FileInputStream(mLogFile));
79         assertEquals(4, lines.size());
80         assertThat(lines.get(0), endsWith(String.format("D/%s: %s", LOG_TAG, "debug")));
81         assertThat(lines.get(1), endsWith(String.format("I/%s: %s", LOG_TAG, "info")));
82         assertThat(lines.get(2), endsWith(String.format("W/%s: %s", LOG_TAG, "warn")));
83         assertThat(lines.get(3), endsWith(String.format("E/%s: %s", LOG_TAG, "error")));
84     }
85 
86     @Test
testGetLog()87     public void testGetLog() throws IOException {
88         mLogger.init();
89         mLogger.printLog(LogLevel.INFO, LOG_TAG, "hello world");
90         // getting the log is equivalent to reading the file
91         List<String> expected = readLines(new FileInputStream(mLogFile));
92         List<String> actual = readLines(mLogger.getLog().createInputStream());
93         assertEquals(expected, actual);
94     }
95 
96     @Test
testInit()97     public void testInit() throws IOException {
98         // logging is ignored if not initialized
99         mLogger.printLog(LogLevel.INFO, LOG_TAG, "hello world");
100         List<String> lines = readLines(new FileInputStream(mLogFile));
101         assertTrue(lines.isEmpty());
102     }
103 
104     @Test
testCloseLog()105     public void testCloseLog() throws IOException {
106         mLogger.init();
107         mLogger.closeLog();
108         // logging is ignored if closed
109         mLogger.printLog(LogLevel.INFO, LOG_TAG, "hello world");
110         List<String> lines = readLines(new FileInputStream(mLogFile));
111         assertTrue(lines.isEmpty());
112     }
113 
114     @Test
testClone()115     public void testClone() throws IOException {
116         mLogger.init();
117         mLogger.printLog(LogLevel.DEBUG, LOG_TAG, "original");
118 
119         // clone will append to same log file
120         SimpleFileLogger clone = mLogger.clone();
121         try {
122             clone.init();
123             clone.printLog(LogLevel.DEBUG, LOG_TAG, "clone");
124         } finally {
125             clone.closeLog();
126         }
127 
128         List<String> lines = readLines(new FileInputStream(mLogFile));
129         assertEquals(2, lines.size());
130         assertThat(lines.get(0), endsWith("original"));
131         assertThat(lines.get(1), endsWith("clone"));
132     }
133 
134     // Read input stream as a list of strings.
readLines(InputStream is)135     private static List<String> readLines(InputStream is) {
136         return new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.toList());
137     }
138 }
139