Skip to content

Commit

Permalink
[pinpoint-apm#8965] Apply SharedTestLifeCycle to HttpWebServer
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jun 23, 2022
1 parent 8462707 commit e787630
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,29 @@

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

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.ImportPlugin;
import com.navercorp.pinpoint.test.plugin.JvmVersion;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointConfig;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.navercorp.pinpoint.test.plugin.ImportPlugin;
import com.navercorp.pinpoint.test.plugin.shared.AfterSharedClass;
import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass;

import com.navercorp.pinpoint.test.plugin.shared.SharedTestBeforeAllResult;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingMessage;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.interceptor.MessageSenderInterceptor;
import org.apache.cxf.jaxrs.client.ClientConfiguration;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.message.Message;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Properties;

import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.annotation;
import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.event;

Expand All @@ -53,35 +51,24 @@
@Dependency({"org.apache.cxf:cxf-rt-rs-client:[3.0.0][3.0.16][3.1.0][3.1.16],[3.2.1,)", WebServer.VERSION, PluginITConstants.VERSION})
@ImportPlugin({"com.navercorp.pinpoint:pinpoint-cxf-plugin", "com.navercorp.pinpoint:pinpoint-jdk-http-plugin"})
@PinpointConfig("cxf/pinpoint-cxf-test.config")
@SharedTestLifeCycleClass(HttpWebServer.class)
public class CxfClientIT {

private static WebServer webServer;

private static String ADDRESS;

public static String getADDRESS() {
return ADDRESS;
}

public static void setADDRESS(String ADDRESS) {
CxfClientIT.ADDRESS = ADDRESS;
@SharedTestBeforeAllResult
public static void setBeforeAllResult(Properties beforeAllResult) {
ADDRESS = beforeAllResult.getProperty("ADDRESS");
}

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

@AfterSharedClass
public static void AfterClass() {
webServer = WebServer.cleanup(webServer);
public String getAddress() {
return ADDRESS;
}

@Test
public void test() throws Exception {

String address = getADDRESS();
String address = getAddress();

String json = "{\"id\" : 12345, \"name\" : \"victor\"}";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.navercorp.pinpoint.plugin.cxf;

import com.navercorp.pinpoint.pluginit.utils.WebServer;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycle;

import java.util.Properties;

public class HttpWebServer implements SharedTestLifeCycle {
private WebServer webServer;

@Override
public Properties beforeAll() {
try {
webServer = WebServer.newTestWebServer();

Properties properties = new Properties();
properties.setProperty("ADDRESS", webServer.getCallHttpUrl());
properties.setProperty("HOST_PORT", webServer.getHostAndPort());
return properties;
} catch (Exception e) {
throw new RuntimeException("webserver start error", e);
}
}

@Override
public void afterAll() {
webServer = WebServer.cleanup(webServer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,27 @@
*/
package com.navercorp.pinpoint.plugin.httpclient3;

import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
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.ImportPlugin;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestBeforeAllResult;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
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.Dependency;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.navercorp.pinpoint.test.plugin.shared.AfterSharedClass;
import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass;
import java.util.Properties;

/**
* @author jaehong.kim
Expand All @@ -45,32 +44,20 @@
@PinpointAgent(AgentPath.PATH)
@ImportPlugin("com.navercorp.pinpoint:pinpoint-httpclient3-plugin")
@Dependency({ "commons-httpclient:commons-httpclient:[3.0],[3.0.1],[3.1]", WebServer.VERSION, PluginITConstants.VERSION})
@SharedTestLifeCycleClass(HttpWebServer.class)
public class HttpClientIT {

private static com.navercorp.pinpoint.pluginit.utils.WebServer webServer;

// ---------- For @BeforeSharedClass, @AfterSharedClass //
private static String CALL_URL;
private static String ADDRESS;

public static String getCallUrl() {
return CALL_URL;
@SharedTestBeforeAllResult
public static void setBeforeAllResult(Properties beforeAllResult) {
ADDRESS = beforeAllResult.getProperty("ADDRESS");
}

public static void setCallUrl(String callUrl) {
CALL_URL = callUrl;
public String getAddress() {
return ADDRESS;
}
// ---------- //

@BeforeSharedClass
public static void sharedSetUp() throws Exception {
webServer = WebServer.newTestWebServer();
setCallUrl(webServer.getCallHttpUrl());
}

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

private static final long CONNECTION_TIMEOUT = 10000;
private static final int SO_TIMEOUT = 10000;
Expand All @@ -81,7 +68,7 @@ public void test() {
client.getParams().setConnectionManagerTimeout(CONNECTION_TIMEOUT);
client.getParams().setSoTimeout(SO_TIMEOUT);

GetMethod method = new GetMethod(getCallUrl());
GetMethod method = new GetMethod(getAddress());

// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.navercorp.pinpoint.plugin.httpclient3;

import com.navercorp.pinpoint.pluginit.utils.WebServer;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycle;

import java.util.Properties;

public class HttpWebServer implements SharedTestLifeCycle {
private WebServer webServer;

@Override
public Properties beforeAll() {
try {
webServer = WebServer.newTestWebServer();

Properties properties = new Properties();
properties.setProperty("ADDRESS", webServer.getCallHttpUrl());
properties.setProperty("HOST_PORT", webServer.getHostAndPort());
return properties;
} catch (Exception e) {
throw new RuntimeException("webserver start error", e);
}
}

@Override
public void afterAll() {
webServer = WebServer.cleanup(webServer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import fi.iki.elonen.NanoHTTPD;

import java.io.IOException;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -48,7 +49,7 @@ public Response serve(IHTTPSession session) {
}


public static WebServer newTestWebServer() throws Exception {
public static WebServer newTestWebServer() throws IOException {

final int port = SocketUtils.findAvailableTcpPort(21000);
final WebServer webServer = new WebServer(LOCAL_HOST, port);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.navercorp.pinpoint.plugin.spring.web;

import com.navercorp.pinpoint.pluginit.utils.WebServer;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycle;

import java.util.Properties;

public class HttpWebServer implements SharedTestLifeCycle {
private WebServer webServer;

@Override
public Properties beforeAll() {
try {
webServer = WebServer.newTestWebServer();

Properties properties = new Properties();
properties.setProperty("ADDRESS", webServer.getCallHttpUrl());
properties.setProperty("HOST_PORT", webServer.getHostAndPort());
return properties;
} catch (Exception e) {
throw new RuntimeException("webserver start error", e);
}
}

@Override
public void afterAll() {
webServer = WebServer.cleanup(webServer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointConfig;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import com.navercorp.pinpoint.test.plugin.shared.AfterSharedClass;
import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass;

import com.navercorp.pinpoint.test.plugin.shared.SharedTestBeforeAllResult;
import com.navercorp.pinpoint.test.plugin.shared.SharedTestLifeCycleClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.client.AbstractClientHttpRequest;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.Netty4ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Properties;

import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.annotation;
import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.event;

Expand All @@ -49,32 +50,19 @@
WebServer.VERSION, PluginITConstants.VERSION})
@PinpointConfig("pinpoint-disabled-plugin-test.config")
@ImportPlugin({"com.navercorp.pinpoint:pinpoint-resttemplate-plugin"})
@SharedTestLifeCycleClass(HttpWebServer.class)
public class RestTemplateIT {

private static WebServer webServer;

// ---------- For @BeforeSharedClass, @AfterSharedClass //
private static String CALL_URL;

public static String getCallUrl() {
return CALL_URL;
private static String ADDRESS;
@SharedTestBeforeAllResult
public static void setBeforeAllResult(Properties beforeAllResult) {
ADDRESS = beforeAllResult.getProperty("ADDRESS");
}

public static void setCallUrl(String callUrl) {
CALL_URL = callUrl;
}
// ---------- //

@BeforeSharedClass
public static void sharedSetUp() throws Exception {
webServer = WebServer.newTestWebServer();
setCallUrl(webServer.getCallHttpUrl());
public static String getCallUrl() {
return ADDRESS;
}

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

@Test
public void test1() throws Exception {
Expand Down

0 comments on commit e787630

Please sign in to comment.