update:2021.03.19
fix:重新提交 add:
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
package com.android.uiuios.logging;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Calendar;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for {@link FileLog}
|
||||
*/
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class FileLogTest {
|
||||
|
||||
private File mTempDir;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
int count = 0;
|
||||
do {
|
||||
mTempDir = new File(RuntimeEnvironment.application.getCacheDir(),
|
||||
"log-test-" + (count++));
|
||||
} while (!mTempDir.mkdir());
|
||||
|
||||
FileLog.setDir(mTempDir);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
// Clear existing logs
|
||||
new File(mTempDir, "log-0").delete();
|
||||
new File(mTempDir, "log-1").delete();
|
||||
mTempDir.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrintLog() throws Exception {
|
||||
if (!FileLog.ENABLED) {
|
||||
return;
|
||||
}
|
||||
FileLog.print("Testing", "hoolalala");
|
||||
StringWriter writer = new StringWriter();
|
||||
FileLog.flushAll(new PrintWriter(writer));
|
||||
assertTrue(writer.toString().contains("hoolalala"));
|
||||
|
||||
FileLog.print("Testing", "abracadabra", new Exception("cat! cat!"));
|
||||
writer = new StringWriter();
|
||||
FileLog.flushAll(new PrintWriter(writer));
|
||||
assertTrue(writer.toString().contains("abracadabra"));
|
||||
// Exception is also printed
|
||||
assertTrue(writer.toString().contains("cat! cat!"));
|
||||
|
||||
// Old logs still present after flush
|
||||
assertTrue(writer.toString().contains("hoolalala"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOldFileTruncated() throws Exception {
|
||||
if (!FileLog.ENABLED) {
|
||||
return;
|
||||
}
|
||||
FileLog.print("Testing", "hoolalala");
|
||||
StringWriter writer = new StringWriter();
|
||||
FileLog.flushAll(new PrintWriter(writer));
|
||||
assertTrue(writer.toString().contains("hoolalala"));
|
||||
|
||||
Calendar threeDaysAgo = Calendar.getInstance();
|
||||
threeDaysAgo.add(Calendar.HOUR, -72);
|
||||
new File(mTempDir, "log-0").setLastModified(threeDaysAgo.getTimeInMillis());
|
||||
new File(mTempDir, "log-1").setLastModified(threeDaysAgo.getTimeInMillis());
|
||||
|
||||
FileLog.print("Testing", "abracadabra", new Exception("cat! cat!"));
|
||||
writer = new StringWriter();
|
||||
FileLog.flushAll(new PrintWriter(writer));
|
||||
assertTrue(writer.toString().contains("abracadabra"));
|
||||
// Exception is also printed
|
||||
assertTrue(writer.toString().contains("cat! cat!"));
|
||||
|
||||
// Old logs have been truncated
|
||||
assertFalse(writer.toString().contains("hoolalala"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user