/**
 * Copyright 2014 NAVER Corp.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.navercorp.pinpoint.test.plugin;

import java.io.OutputStream;
import java.io.PrintWriter;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

/**
 * @author Jongho Moon
 *
 */
public class PinpointPluginTestRunListener extends RunListener implements PinpointPluginTestConstants {
    private final PrintWriter out;
    
    public PinpointPluginTestRunListener(OutputStream out) {
        this(new PrintWriter(out));
    }
    
    public PinpointPluginTestRunListener(PrintWriter out) {
        this.out = out;
    }

    @Override
    public void testRunStarted(Description description) throws Exception {
        out.println(JUNIT_OUTPUT_DELIMITER + "testRunStarted");
        out.flush();
    }

    @Override
    public void testRunFinished(Result result) throws Exception {
        out.println(JUNIT_OUTPUT_DELIMITER + "testRunFinished");
        out.flush();
    }

    @Override
    public void testStarted(Description description) throws Exception {
        out.println(JUNIT_OUTPUT_DELIMITER + "testStarted" + JUNIT_OUTPUT_DELIMITER + description.getDisplayName());
        out.flush();
    }

    @Override
    public void testFinished(Description description) throws Exception {
        out.println(JUNIT_OUTPUT_DELIMITER + "testFinished" + JUNIT_OUTPUT_DELIMITER + description.getDisplayName());
        out.flush();
    }

    @Override
    public void testFailure(Failure failure) throws Exception {
        out.println(JUNIT_OUTPUT_DELIMITER + "testFailure" + JUNIT_OUTPUT_DELIMITER + failureToString(failure));
        out.flush();
    }

    @Override
    public void testAssumptionFailure(Failure failure) {
        out.println(JUNIT_OUTPUT_DELIMITER + "testAssumptionFailure" + JUNIT_OUTPUT_DELIMITER + failureToString(failure));
        out.flush();
    }

    @Override
    public void testIgnored(Description description) throws Exception {
        out.println(JUNIT_OUTPUT_DELIMITER + "testIgnored" + JUNIT_OUTPUT_DELIMITER + description.getDisplayName());
        out.flush();
    }

    private String failureToString(Failure failure) {
        StringBuilder builder = new StringBuilder();

        builder.append(failure.getTestHeader());
        builder.append(JUNIT_OUTPUT_DELIMITER);
        builder.append(failure.getException().getClass().getName());
        builder.append(JUNIT_OUTPUT_DELIMITER);
        builder.append(failure.getMessage());
        builder.append(JUNIT_OUTPUT_DELIMITER);

        for (StackTraceElement e : failure.getException().getStackTrace()) {
            builder.append(e.getClassName());
            builder.append(',');
            builder.append(e.getMethodName());
            builder.append(',');
            builder.append(e.getFileName());
            builder.append(',');
            builder.append(e.getLineNumber());

            builder.append(JUNIT_OUTPUT_DELIMITER);
        }

        return builder.toString();
    }
}

@@ -682,22 +682,95 @@
 io.Print
-Writer
+Stream;%0Aimport java.io.PrintWriter;%0Aimport java.io.UnsupportedEncodingException
 ;%0A%0Aimpor
@@ -1068,16 +1068,64 @@
 tants %7B%0A
+    public static final String UTF_8 = %22UTF-8%22;%0A
     priv
@@ -1143,24 +1143,20 @@
 rint
-Writer
+Stream
  out;%0A
-    
 %0A   
@@ -1230,28 +1230,18 @@
 his(
-new PrintWriter(out)
+out, UTF_8
 );%0A 
@@ -1249,13 +1249,8 @@
   %7D%0A
-    %0A
     
@@ -1290,58 +1290,277 @@
 ner(
-PrintWriter out) %7B%0A        this.out = out;
+OutputStream out, String encoding) %7B%0A        try %7B%0A            this.out = new PrintStream(out, true, encoding);%0A        %7D catch (UnsupportedEncodingException ex) %7B%0A            throw new RuntimeException(%22encoding error. error:%22 + ex.getMessage(), ex);%0A        %7D
 %0A    %7D%0A%0A
     
@@ -1547,32 +1547,33 @@
        %7D%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -1709,37 +1709,16 @@
 rted%22);%0A
-        out.flush();%0A
     %7D%0A%0A 
@@ -1861,37 +1861,16 @@
 shed%22);%0A
-        out.flush();%0A
     %7D%0A%0A 
@@ -2071,37 +2071,16 @@
 ame());%0A
-        out.flush();%0A
     %7D%0A%0A 
@@ -2283,37 +2283,16 @@
 ame());%0A
-        out.flush();%0A
     %7D%0A%0A 
@@ -2481,37 +2481,16 @@
 lure));%0A
-        out.flush();%0A
     %7D%0A%0A 
@@ -2682,37 +2682,16 @@
 lure));%0A
-        out.flush();%0A
     %7D%0A%0A 
@@ -2896,29 +2896,8 @@
 ));%0A
-        out.flush();%0A
     
@@ -3003,16 +3003,18 @@
 Builder(
+64
 );%0A%0A    

fix findbug - set default encoding=UTF-8