You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by jo...@apache.org on 2019/02/09 14:31:47 UTC
[struts] branch master updated: Fix compile issue of post order
test example with latest http client version
This is an automated email from the ASF dual-hosted git repository.
jogep pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git
The following commit(s) were added to refs/heads/master by this push:
new bbe46e9 Fix compile issue of post order test example with latest http client version
bbe46e9 is described below
commit bbe46e92f09686bd541384facbcee422bc77c656
Author: Johannes Geppert <jo...@apache.org>
AuthorDate: Sat Feb 9 15:32:02 2019 +0100
Fix compile issue of post order test example with latest http client version
---
.../apache/struts2/rest/example/PostOrderTest.java | 101 +++++++++------------
1 file changed, 42 insertions(+), 59 deletions(-)
diff --git a/apps/rest-showcase/src/test/java/it/org/apache/struts2/rest/example/PostOrderTest.java b/apps/rest-showcase/src/test/java/it/org/apache/struts2/rest/example/PostOrderTest.java
index 80f48ae..333aa28 100644
--- a/apps/rest-showcase/src/test/java/it/org/apache/struts2/rest/example/PostOrderTest.java
+++ b/apps/rest-showcase/src/test/java/it/org/apache/struts2/rest/example/PostOrderTest.java
@@ -18,15 +18,16 @@
*/
package it.org.apache.struts2.rest.example;
+import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import net.sourceforge.jwebunit.junit.WebTestCase;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
-import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
-
public class PostOrderTest extends WebTestCase {
public void setUp() throws Exception {
@@ -72,68 +73,50 @@ public class PostOrderTest extends WebTestCase {
}
public void testPostOrderInXml() throws IOException {
- HttpClient client = new HttpClient();
- PostMethod method = null;
- try {
- method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.xml");
- method.setRequestEntity(new StringRequestEntity("<org.apache.struts2.rest.example.Order>\n" +
- "<clientName>Test3</clientName>\n" +
- "<amount>3342</amount>\n" +
- "</org.apache.struts2.rest.example.Order>"));
- client.executeMethod(method);
- assertEquals(201, method.getStatusCode());
- assertTrue(method.getResponseHeader("Location").getValue().startsWith(ParameterUtils.getBaseUrl()+"/orders/"));
- } finally {
- method.releaseConnection();
- }
+ CloseableHttpClient client = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.xml");
+ httpPost.setEntity(new StringEntity("<org.apache.struts2.rest.example.Order>\n" +
+ "<clientName>Test3</clientName>\n" +
+ "<amount>3342</amount>\n" +
+ "</org.apache.struts2.rest.example.Order>"));
+ CloseableHttpResponse response = client.execute(httpPost);
+ assertEquals(201, response.getStatusLine().getStatusCode());
+ assertTrue(response.getHeaders("Location")[0].getValue().startsWith(ParameterUtils.getBaseUrl() + "/orders/"));
+ client.close();
}
public void testPostOrderInXmlWithBadData() throws IOException {
- HttpClient client = new HttpClient();
- PostMethod method = null;
- try {
- method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.xml");
- method.setRequestEntity(new StringRequestEntity("<org.apache.struts2.rest.example.Order>\n" +
- "<amount>3342</amount>\n" +
- "</org.apache.struts2.rest.example.Order>"));
- client.executeMethod(method);
- assertEquals(400, method.getStatusCode());
- String response = method.getResponseBodyAsString();
- assertTrue(response.contains("<string>The client name is empty"));
- assertNull(method.getResponseHeader("Location"));
- } finally {
- method.releaseConnection();
- }
+ CloseableHttpClient client = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.xml");
+ httpPost.setEntity(new StringEntity("<org.apache.struts2.rest.example.Order>\n" +
+ "<amount>3342</amount>\n" +
+ "</org.apache.struts2.rest.example.Order>"));
+ CloseableHttpResponse response = client.execute(httpPost);
+ assertEquals(400, response.getStatusLine().getStatusCode());
+ assertTrue(response.toString().contains("<string>The client name is empty"));
+ assertNull(response.getHeaders("Location"));
+ client.close();
}
public void testPostOrderInJson() throws IOException {
- HttpClient client = new HttpClient();
- PostMethod method = null;
- try {
- method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.json");
- method.setRequestEntity(new StringRequestEntity("{\"amount\":33,\"clientName\":\"Test4\"}"));
- client.executeMethod(method);
- assertEquals(201, method.getStatusCode());
- assertTrue(method.getResponseHeader("Location").getValue().startsWith(ParameterUtils.getBaseUrl()+"/orders/"));
- } finally {
- method.releaseConnection();
- }
+ CloseableHttpClient client = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.json");
+ httpPost.setEntity(new StringEntity("{\"amount\":33,\"clientName\":\"Test4\"}"));
+ CloseableHttpResponse response = client.execute(httpPost);
+ assertEquals(201, response.getStatusLine().getStatusCode());
+ assertTrue(response.getHeaders("Location")[0].getValue().startsWith(ParameterUtils.getBaseUrl() + "/orders/"));
+ client.close();
}
public void testPostOrderInJsonWithBadData() throws IOException {
- HttpClient client = new HttpClient();
- PostMethod method = null;
- try {
- method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.json");
- method.setRequestEntity(new StringRequestEntity("{\"amount\":33}"));
- client.executeMethod(method);
- String response = method.getResponseBodyAsString();
- assertEquals(400, method.getStatusCode());
-
- assertEquals("{\"actionErrors\":[],\"fieldErrors\":{\"clientName\":[\"The client name is empty\"]}}", response);
- assertNull(method.getResponseHeader("Location"));
- } finally {
- method.releaseConnection();
- }
+ CloseableHttpClient client = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.json");
+ httpPost.setEntity(new StringEntity("{\"amount\":33}"));
+ CloseableHttpResponse response = client.execute(httpPost);
+ assertEquals(400, response.getStatusLine().getStatusCode());
+ assertTrue(response.toString()
+ .contains("{\"actionErrors\":[],\"fieldErrors\":{\"clientName\":[\"The client name is empty\"]}}"));
+ assertNull(response.getHeaders("Location"));
+ client.close();
}
}
\ No newline at end of file