// Path: telemetry-agent/src/test/java/com/yammer/telemetry/agent/test/SimpleServlet.java
// public class SimpleServlet extends HttpServlet {
// ////    currently need this, should we?
// //    @Override
// //    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// //        super.service(req, resp);
// //    }
// 
//     @Override
//     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//         resp.getWriter().print("foof");
//     }
// 
//     @Override
//     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//         resp.setStatus(201);
//     }
// }
// 
// Path: telemetry-test-framework/src/main/java/com/yammer/telemetry/test/TelemetryTestHelpers.java
// public static void runTransformed(Class<?> clazz, ClassInstrumentationHandler... handlers) throws Exception {
//     Method[] methods = clazz.getDeclaredMethods();
//     Map<Method, Throwable> testFailures = new HashMap<>();
//     int ran = 0;
// 
//     for (Method method : methods) {
//         if (method.isAnnotationPresent(TransformedTest.class)) {
//             try {
//                 ran++;
//                 runTransformed(clazz, method.getName(), handlers);
//             } catch (Exception e) {
//                 //noinspection ThrowableResultOfMethodCallIgnored
//                 testFailures.put(method, unwrap(e));
//             }
//         }
//     }
// 
//     if (!testFailures.isEmpty()) {
//         StringWriter builder = new StringWriter();
//         PrintWriter writer = new PrintWriter(builder);
//         writer.println("Transformed tests failed:");
//         for (Map.Entry<Method, Throwable> entry : testFailures.entrySet()) {
//             writer.printf("%s:%n%s%n%n", entry.getKey(), entry.getValue());
//             //noinspection ThrowableResultOfMethodCallIgnored
//             entry.getValue().printStackTrace(writer);
//             writer.println();
//         }
//         fail(builder.toString());
//     }
// 
//     if (ran == 0) {
//         fail("No tests were found within '" + clazz.getName() + "' that were annotated as '" + TransformedTest.class.getName() + "'");
//     }
// }


import com.google.common.base.Optional;
import com.yammer.telemetry.agent.test.SimpleServlet;
import com.yammer.telemetry.test.TransformedTest;
import com.yammer.telemetry.tracing.*;
import javassist.ClassPool;
import javassist.CtClass;
import org.junit.After;
import org.junit.Test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.math.BigInteger;
import java.security.Principal;
import java.util.*;
import static com.yammer.telemetry.test.TelemetryTestHelpers.runTransformed;
import static org.junit.Assert.*;
    @Test
    public void testNothingForNonHttpServletClasses() throws Exception {
        ClassPool cp = ClassPool.getDefault();
        CtClass ctClass = cp.get("java.lang.String");
        assertFalse(handler.transformed(ctClass, cp));
    }

    @Test
    public void testTransformsHttpServletClasses() throws Exception {
        ClassPool cp = ClassPool.getDefault();
        CtClass ctClass = cp.get("javax.servlet.http.HttpServlet");
        assertTrue(handler.transformed(ctClass, cp));
    }

    @Test
    public void testTransformsHttpServletSubclassesThatOverrideService() throws Exception {
        ClassPool cp = ClassPool.getDefault();
        CtClass ctClass = cp.get("com.sun.jersey.spi.container.servlet.ServletContainer");
        assertTrue(handler.transformed(ctClass, cp));
    }

    @Test
    public void testNothingForHttpServletSubclassesWithoutServiceMethodOverride() throws Exception {
        ClassPool cp = ClassPool.getDefault();
        CtClass ctClass = cp.get("com.yammer.dropwizard.tasks.TaskServlet");
        assertFalse(handler.transformed(ctClass, cp));
    }

    @Test
    public void testRunTransformedTests() throws Exception {
// Path: telemetry-agent/src/test/java/com/yammer/telemetry/agent/test/SimpleServlet.java
// public class SimpleServlet extends HttpServlet {
// ////    currently need this, should we?
// //    @Override
// //    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// //        super.service(req, resp);
// //    }
// 
//     @Override
//     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//         resp.getWriter().print("foof");
//     }
// 
//     @Override
//     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//         resp.setStatus(201);
//     }
// }
// 
// Path: telemetry-test-framework/src/main/java/com/yammer/telemetry/test/TelemetryTestHelpers.java
// public static void runTransformed(Class<?> clazz, ClassInstrumentationHandler... handlers) throws Exception {
//     Method[] methods = clazz.getDeclaredMethods();
//     Map<Method, Throwable> testFailures = new HashMap<>();
//     int ran = 0;
// 
//     for (Method method : methods) {
//         if (method.isAnnotationPresent(TransformedTest.class)) {
//             try {
//                 ran++;
//                 runTransformed(clazz, method.getName(), handlers);
//             } catch (Exception e) {
//                 //noinspection ThrowableResultOfMethodCallIgnored
//                 testFailures.put(method, unwrap(e));
//             }
//         }
//     }
// 
//     if (!testFailures.isEmpty()) {
//         StringWriter builder = new StringWriter();
//         PrintWriter writer = new PrintWriter(builder);
//         writer.println("Transformed tests failed:");
//         for (Map.Entry<Method, Throwable> entry : testFailures.entrySet()) {
//             writer.printf("%s:%n%s%n%n", entry.getKey(), entry.getValue());
//             //noinspection ThrowableResultOfMethodCallIgnored
//             entry.getValue().printStackTrace(writer);
//             writer.println();
//         }
//         fail(builder.toString());
//     }
// 
//     if (ran == 0) {
//         fail("No tests were found within '" + clazz.getName() + "' that were annotated as '" + TransformedTest.class.getName() + "'");
//     }
// }


// Path: telemetry-agent/src/test/java/com/yammer/telemetry/agent/handlers/HttpServletClassHandlerTest.java
import com.google.common.base.Optional;
import com.yammer.telemetry.agent.test.SimpleServlet;
import com.yammer.telemetry.test.TransformedTest;
import com.yammer.telemetry.tracing.*;
import javassist.ClassPool;
import javassist.CtClass;
import org.junit.After;
import org.junit.Test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.math.BigInteger;
import java.security.Principal;
import java.util.*;
import static com.yammer.telemetry.test.TelemetryTestHelpers.runTransformed;
import static org.junit.Assert.*;

    @Test
    public void testNothingForNonHttpServletClasses() throws Exception {
        ClassPool cp = ClassPool.getDefault();
        CtClass ctClass = cp.get("java.lang.String");
        assertFalse(handler.transformed(ctClass, cp));
    }

    @Test
    public void testTransformsHttpServletClasses() throws Exception {
        ClassPool cp = ClassPool.getDefault();
        CtClass ctClass = cp.get("javax.servlet.http.HttpServlet");
        assertTrue(handler.transformed(ctClass, cp));
    }

    @Test
    public void testTransformsHttpServletSubclassesThatOverrideService() throws Exception {
        ClassPool cp = ClassPool.getDefault();
        CtClass ctClass = cp.get("com.sun.jersey.spi.container.servlet.ServletContainer");
        assertTrue(handler.transformed(ctClass, cp));
    }

    @Test
    public void testNothingForHttpServletSubclassesWithoutServiceMethodOverride() throws Exception {
        ClassPool cp = ClassPool.getDefault();
        CtClass ctClass = cp.get("com.yammer.dropwizard.tasks.TaskServlet");
        assertFalse(handler.transformed(ctClass, cp));
    }

    @Test
    public void testRunTransformedTests() throws Exception {
        runTransformed(TransformedTests.class, handler);