From c0cc59ee4832b8447c7876f529fe53db66a5a7a2 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 4 Jan 2014 17:03:29 +0200 Subject: [PATCH] additional examples --- .../httpclient/HttpClientAuthLiveTest.java | 72 +++++++++++++++++++ .../JacksonPrettyPrintUnitTest.java} | 4 +- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java rename jackson/src/test/java/org/baeldung/jackson/{try1/JacksonTryUnitTest.java => sandbox/JacksonPrettyPrintUnitTest.java} (94%) diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java new file mode 100644 index 0000000000..beb5abbeea --- /dev/null +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java @@ -0,0 +1,72 @@ +package org.baeldung.httpclient; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpStatus; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.BasicCredentialsProvider; +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 HttpClientAuthLiveTest { + + 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 + + // simple request - response + + @Test + public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException { + final CredentialsProvider provider = new BasicCredentialsProvider(); + final AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); + final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "user1Pass"); + + provider.setCredentials(scope, credentials); + + instance = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); + + response = instance.execute(new HttpGet("http://localhost:8080/spring-security-mvc-basic-auth/homepage.html")); + + final int statusCode = response.getStatusLine().getStatusCode(); + assertThat(statusCode, equalTo(HttpStatus.SC_OK)); + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/try1/JacksonTryUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/sandbox/JacksonPrettyPrintUnitTest.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/try1/JacksonTryUnitTest.java rename to jackson/src/test/java/org/baeldung/jackson/sandbox/JacksonPrettyPrintUnitTest.java index 50e470dc29..a2bff89320 100644 --- a/jackson/src/test/java/org/baeldung/jackson/try1/JacksonTryUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/sandbox/JacksonPrettyPrintUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.try1; +package org.baeldung.jackson.sandbox; import java.io.IOException; import java.nio.ByteBuffer; @@ -13,7 +13,7 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; -public class JacksonTryUnitTest { +public class JacksonPrettyPrintUnitTest { @Test public final void whenDeserializing_thenCorrect() throws JsonParseException, JsonMappingException, IOException {