cleanup work

This commit is contained in:
eugenp 2013-12-26 19:52:19 +02:00
parent 6633f5b9a0
commit cfafcc57b7
4 changed files with 89 additions and 10 deletions

View File

@ -10,7 +10,7 @@ import org.junit.Test;
import com.google.common.collect.ImmutableList;
public class CoreJavaUnitTest {
public class CoreJavaCollectionsUnitTest {
// tests -

View File

@ -0,0 +1,14 @@
package org.baeldung.java;
import org.junit.Test;
public class CoreJavaIoUnitTest {
// tests -
@Test
public final void whenIteratingAFile_thenCorrect() {
//
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}