/*
 * Copyright 2018 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.plugin.jdk.http;

import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;


import com.navercorp.pinpoint.pluginit.utils.AgentPath;
import com.navercorp.pinpoint.pluginit.utils.PluginITConstants;
import com.navercorp.pinpoint.pluginit.utils.WebServer;
import com.navercorp.pinpoint.test.plugin.Dependency;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.test.plugin.JvmVersion;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;

/**
 * @author Jongho Moon
 *
 */
@RunWith(PinpointPluginTestSuite.class)
@PinpointAgent(AgentPath.PATH)
@JvmVersion({7, 8})
@Dependency({ WebServer.VERSION, PluginITConstants.VERSION})
public class HttpURLConnectionIT {

    private static WebServer webServer;

    @BeforeClass
    public static void BeforeClass() throws Exception {
        webServer = WebServer.newTestWebServer();
    }

    @AfterClass
    public static void AfterClass() throws Exception {
        webServer = WebServer.cleanup(webServer);
    }

    @Test
    public void test() throws Exception {
        URL url = new URL(webServer.getCallHttpUrl());
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.getHeaderFields();
        
        PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
        verifier.printMethod();
        
        Class<?> targetClass = Class.forName("sun.net.www.protocol.http.HttpURLConnection");
        Method getInputStream = targetClass.getMethod("getInputStream");
        
        String destinationId = webServer.getHostAndPort();
        String httpUrl = webServer.getCallHttpUrl();
        verifier.verifyTraceCount(2);
        verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", getInputStream, null, null, destinationId, annotation("http.url", httpUrl)));
    }

    @Test
    public void testConnectTwice() throws Exception {
        URL url = new URL(webServer.getCallHttpUrl());
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        
        connection.connect();
        connection.getInputStream();
        
        PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
        verifier.printMethod();
        
        Class<?> targetClass = Class.forName("sun.net.www.protocol.http.HttpURLConnection");
        Method connect = targetClass.getMethod("connect");

        String destinationId = webServer.getHostAndPort();
        String httpUrl = webServer.getCallHttpUrl();
        verifier.verifyTraceCount(2);
        verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", connect, null, null, destinationId, annotation("http.url", httpUrl)));
    }

    @Test
    public void testConnecting() throws Exception {

        URL url = new URL("http://no.such.url");
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();

        Exception expected1 = null;
        try {
            connection.connect();
        } catch (UnknownHostException e) {
            expected1 = e;
        } finally {
            connection.disconnect();
        }

        Exception expected2 = null;
        HttpURLConnection connection2 = (HttpURLConnection)url.openConnection();
        try {
            connection2.connect();
        } catch (UnknownHostException e) {
            expected2 = e;
        } finally {
            connection2.disconnect();
        }

        PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
        verifier.printMethod();
        
        Class<?> targetClass = Class.forName("sun.net.www.protocol.http.HttpURLConnection");
        Method getInputStream = targetClass.getMethod("connect");
        
        verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", getInputStream, expected1, null, null, "no.such.url", annotation("http.url", "http://no.such.url")));
        
        verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", getInputStream, expected2, null, null, "no.such.url", annotation("http.url", "http://no.such.url")));
        
        verifier.verifyTraceCount(0);
    }
}

@@ -4091,182 +4091,44 @@
    %7D
- finally %7B%0A            connection.disconnect();%0A        %7D%0A%0A        Exception expected2 = null;%0A        HttpURLConnection connection2 = (HttpURLConnection)url.openConnection()
+%0A%0A        Exception expected2 = null
 ;%0A  
@@ -4161,17 +4161,16 @@
 nnection
-2
 .connect
@@ -4256,68 +4256,11 @@
    %7D
- finally %7B%0A            connection2.disconnect();%0A        %7D
 %0A%0A
+%0A
     
@@ -4361,32 +4361,24 @@
 ntMethod();%0A
-        
 %0A        Cla
@@ -4521,32 +4521,24 @@
 %22connect%22);%0A
-        
 %0A        ver
@@ -4683,33 +4683,24 @@
 ch.url%22)));%0A
-        %0A
         veri
@@ -4848,24 +4848,25 @@
 rl%22)));%0A
+%0A
         
 %0A       
@@ -4849,32 +4849,55 @@
 l%22)));%0A%0A        
+verifier.printMethod();
 %0A        verifie

Fix it-test failure