diff --git a/core-java/src/test/java/org/baeldung/java/CoreJavaUnitTest.java b/core-java/src/test/java/org/baeldung/java/CoreJavaCollectionsUnitTest.java similarity index 98% rename from core-java/src/test/java/org/baeldung/java/CoreJavaUnitTest.java rename to core-java/src/test/java/org/baeldung/java/CoreJavaCollectionsUnitTest.java index fb6e39f768..7dc6b7aab0 100644 --- a/core-java/src/test/java/org/baeldung/java/CoreJavaUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/CoreJavaCollectionsUnitTest.java @@ -10,7 +10,7 @@ import org.junit.Test; import com.google.common.collect.ImmutableList; -public class CoreJavaUnitTest { +public class CoreJavaCollectionsUnitTest { // tests - diff --git a/core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java b/core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java new file mode 100644 index 0000000000..60bf6ee9a0 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java @@ -0,0 +1,14 @@ +package org.baeldung.java; + +import org.junit.Test; + +public class CoreJavaIoUnitTest { + + // tests - + + @Test + public final void whenIteratingAFile_thenCorrect() { + // + } + +} diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicLiveTest.java index d11af39d34..10f0b9e149 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicLiveTest.java @@ -12,7 +12,6 @@ import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; @@ -63,7 +62,7 @@ public class HttpClientBasicLiveTest { @Test public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException { response = instance.execute(new HttpGet(SAMPLE_URL)); - int statusCode = response.getStatusLine().getStatusCode(); + final int statusCode = response.getStatusLine().getStatusCode(); assertThat(statusCode, equalTo(HttpStatus.SC_OK)); } @@ -83,11 +82,4 @@ public class HttpClientBasicLiveTest { assertThat(bodyAsString, notNullValue()); } - // tests - non-GET - - @Test - public final void whenExecutingBasicRequest_thenNoExceptions() throws ClientProtocolException, IOException { - instance.execute(new HttpPost(SAMPLE_URL)); - } - } diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicPostLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicPostLiveTest.java new file mode 100644 index 0000000000..3dd4a988b0 --- /dev/null +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicPostLiveTest.java @@ -0,0 +1,73 @@ +package org.baeldung.httpclient; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.auth.AuthenticationException; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.ClientProtocolException; +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.auth.BasicScheme; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class HttpClientBasicPostLiveTest { + + private static final String SAMPLE_URL = "http://www.github.com"; + + private CloseableHttpClient instance; + + private CloseableHttpResponse response; + + @Before + public final void before() { + instance = HttpClientBuilder.create().build(); + } + + @After + public final void after() throws IllegalStateException, IOException { + if (response == null) { + return; + } + + try { + final HttpEntity entity = response.getEntity(); + if (entity != null) { + final InputStream instream = entity.getContent(); + instream.close(); + } + } finally { + response.close(); + } + } + + // tests - non-GET + + @Test + public final void whenExecutingPostRequest_thenNoExceptions() throws ClientProtocolException, IOException { + instance.execute(new HttpPost(SAMPLE_URL)); + } + + @Test + public final void whenExecutingPostRequestWithBody_thenNoExceptions() throws ClientProtocolException, IOException { + final HttpPost request = new HttpPost(SAMPLE_URL); + request.setEntity(new StringEntity("in the body of the POST")); + instance.execute(request); + } + + @Test + public final void givenAuth_whenExecutingPostRequestWithBody_thenNoExceptions() throws ClientProtocolException, IOException, AuthenticationException { + final HttpPost request = new HttpPost(SAMPLE_URL); + request.setEntity(new StringEntity("in the body of the POST")); + final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("username", "password"); + request.addHeader(new BasicScheme().authenticate(creds, request, null)); + instance.execute(request); + } + +}