diff --git a/pom.xml b/pom.xml index 7f544d500..27c504924 100644 --- a/pom.xml +++ b/pom.xml @@ -155,6 +155,7 @@ com/a/eye/skywalking/trace/proto/*.class + com/a/eye/skywalking/sniffer/mock/**/*.class diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/EasyLogger.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/EasyLogger.java index e449fbf0a..ac8ee73b8 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/EasyLogger.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/EasyLogger.java @@ -1,7 +1,6 @@ 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)); } } diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/SyncFileWriter.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/SyncFileWriter.java index 579e098ba..99d081143 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/SyncFileWriter.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/SyncFileWriter.java @@ -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)); } } } diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/LoggingUtil.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/ThrowableFormatter.java similarity index 80% rename from skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/LoggingUtil.java rename to skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/ThrowableFormatter.java index dbfc3102c..429e63d49 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/LoggingUtil.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/ThrowableFormatter.java @@ -1,4 +1,4 @@ -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(); diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/logging/STDOutWriterTest.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/logging/STDOutWriterTest.java new file mode 100644 index 000000000..31a5c2a54 --- /dev/null +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/logging/STDOutWriterTest.java @@ -0,0 +1,52 @@ +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); + } +} diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/logging/ThrowableFormatterTest.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/logging/ThrowableFormatterTest.java new file mode 100644 index 000000000..048b2ae5f --- /dev/null +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/logging/ThrowableFormatterTest.java @@ -0,0 +1,18 @@ +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]); + } +} diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/AllObjectDefaultMethodsMatchTest.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/AllObjectDefaultMethodsMatchTest.java new file mode 100644 index 000000000..091190871 --- /dev/null +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/AllObjectDefaultMethodsMatchTest.java @@ -0,0 +1,12 @@ +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(){ + } +}