提交 6e2ccfab 编写于 作者: wu-sheng's avatar wu-sheng

Add a test case about System.out/err.

上级 a5f2aa1f
......@@ -155,6 +155,7 @@
<instrumentation>
<excludes>
<exclude>com/a/eye/skywalking/trace/proto/*.class</exclude>
<exclude>com/a/eye/skywalking/sniffer/mock/**/*.class</exclude>
</excludes>
</instrumentation>
</configuration>
......
package com.a.eye.skywalking.api.logging;
import com.a.eye.skywalking.api.util.LoggingUtil;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
......@@ -32,7 +31,7 @@ public class EasyLogger implements ILog {
}
if (e != null) {
WriterFactory.getLogWriter().writeError(LoggingUtil.fetchThrowableStack(e));
WriterFactory.getLogWriter().writeError(ThrowableFormatter.format(e));
}
}
......
......@@ -3,7 +3,6 @@ package com.a.eye.skywalking.api.logging;
import com.a.eye.skywalking.api.conf.Config;
import com.a.eye.skywalking.api.util.LoggingUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
......@@ -97,15 +96,15 @@ public class SyncFileWriter implements IWriter {
try {
File file = new File(Config.Logging.LOG_DIR_NAME, Config.Logging.SYSTEM_ERROR_LOG_FILE_NAME);
fileOutputStream = new FileOutputStream(file, true);
fileOutputStream.write(("Failed to init sync File Writer.\n" + LoggingUtil.fetchThrowableStack(e)).getBytes());
fileOutputStream.write(("Failed to init sync File Writer.\n" + ThrowableFormatter.format(e)).getBytes());
} catch (Exception e1) {
System.err.print(LoggingUtil.fetchThrowableStack(e1));
System.err.print(ThrowableFormatter.format(e1));
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e1) {
System.err.print(LoggingUtil.fetchThrowableStack(e1));
System.err.print(ThrowableFormatter.format(e1));
}
}
}
......
package com.a.eye.skywalking.api.util;
package com.a.eye.skywalking.api.logging;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
......@@ -6,8 +6,8 @@ import java.io.IOException;
/**
* Created by xin on 16-6-24.
*/
public class LoggingUtil {
public static String fetchThrowableStack(Throwable e) {
public class ThrowableFormatter {
public static String format(Throwable e) {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
e.printStackTrace(new java.io.PrintWriter(buf, true));
String expMessage = buf.toString();
......
package com.a.eye.skywalking.api.logging;
import java.io.PrintStream;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
/**
* Created by wusheng on 2017/2/28.
*/
public class STDOutWriterTest {
private static PrintStream outRef;
private static PrintStream errRef;
@BeforeClass
public static void initAndHoldOut(){
outRef = System.out;
errRef = System.err;
}
@Test
public void testWrite(){
PrintStream mockStream = Mockito.mock(PrintStream.class);
System.setOut(mockStream);
STDOutWriter writer = new STDOutWriter();
writer.write("hello");
Mockito.verify(mockStream,times(1)).println(anyString());
}
@Test
public void testWriteError(){
PrintStream mockStream = Mockito.mock(PrintStream.class);
System.setErr(mockStream);
STDOutWriter writer = new STDOutWriter();
writer.writeError("hello");
Mockito.verify(mockStream,times(1)).println(anyString());
}
@AfterClass
public static void reset(){
System.setOut(outRef);
System.setErr(errRef);
}
}
package com.a.eye.skywalking.api.logging;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by wusheng on 2017/2/28.
*/
public class ThrowableFormatterTest {
@Test
public void testFormat(){
NullPointerException exception = new NullPointerException();
String formatLines = ThrowableFormatter.format(exception);
String[] lines = formatLines.split("\n");
Assert.assertEquals("java.lang.NullPointerException", lines[0]);
Assert.assertEquals("\tat com.a.eye.skywalking.api.logging.ThrowableFormatterTest.testFormat(ThrowableFormatterTest.java:12)", lines[1]);
}
}
package com.a.eye.skywalking.api.plugin.bytebuddy;
import org.junit.Test;
/**
* Created by wusheng on 2017/2/28.
*/
public class AllObjectDefaultMethodsMatchTest {
@Test
public void testMatches(){
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册