From 0a76200ab4df8717b5fc7ab05ec601cf5de4fa07 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 6 Jul 2014 11:37:59 +0300 Subject: [PATCH] work on java io tests --- .../java/io/JavaFileIntegrationTest.java | 5 ++ .../java/io/JavaReaderToXUnitTest.java | 46 +++++++++++++++++++ .../HttpClientConnectionManagementTest.java | 44 ++++++++---------- ...sterVersion_MultiHttpClientConnThread.java | 1 + 4 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 core-java/src/test/java/org/baeldung/java/io/JavaReaderToXUnitTest.java diff --git a/core-java/src/test/java/org/baeldung/java/io/JavaFileIntegrationTest.java b/core-java/src/test/java/org/baeldung/java/io/JavaFileIntegrationTest.java index cc0014dc7c..c139e34afb 100644 --- a/core-java/src/test/java/org/baeldung/java/io/JavaFileIntegrationTest.java +++ b/core-java/src/test/java/org/baeldung/java/io/JavaFileIntegrationTest.java @@ -35,6 +35,11 @@ public class JavaFileIntegrationTest { FileUtils.touch(new File("src/test/resources/newFile_commonsio.txt")); } + @Test + public final void givenUsingGuava_whenCreatingFile_thenCorrect() throws IOException { + com.google.common.io.Files.touch(new File("src/test/resources/newFile_guava.txt")); + } + // move a file @Test diff --git a/core-java/src/test/java/org/baeldung/java/io/JavaReaderToXUnitTest.java b/core-java/src/test/java/org/baeldung/java/io/JavaReaderToXUnitTest.java new file mode 100644 index 0000000000..4e8b97f381 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/io/JavaReaderToXUnitTest.java @@ -0,0 +1,46 @@ +package org.baeldung.java.io; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.io.CharSource; +import com.google.common.io.CharStreams; + +@SuppressWarnings("unused") +public class JavaReaderToXUnitTest { + protected final Logger logger = LoggerFactory.getLogger(getClass()); + private static final int DEFAULT_SIZE = 1500000; + + // tests - Reader to String + + @Test + public void givenUsingPlainJava_whenConvertingReaderIntoString_thenCorrect() throws IOException { + final Reader initialReader = new StringReader("text"); + + final char[] mediationArray = new char["text".length()]; + initialReader.read(mediationArray); + initialReader.close(); + final String targetString = new String(mediationArray); + } + + @Test + public void givenUsingGuava_whenConvertingReaderIntoString_thenCorrect() throws IOException { + final Reader initialReader = CharSource.wrap("Google Guava v.17.0").openStream(); + final String targetString = CharStreams.toString(initialReader); + initialReader.close(); + } + + @Test + public void givenUsingCommonsIo_whenConvertingReaderIntoString_thenCorrect() throws IOException { + final Reader initialReader = new StringReader("Apache Commons IO 2.4"); + final String targetString = IOUtils.toString(initialReader); + initialReader.close(); + } + +} diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientConnectionManagementTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientConnectionManagementTest.java index 37f7b07145..2a923585b6 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientConnectionManagementTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientConnectionManagementTest.java @@ -33,10 +33,8 @@ import org.apache.http.protocol.HttpRequestExecutor; import org.apache.http.util.EntityUtils; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; - public class HttpClientConnectionManagementTest { private BasicHttpClientConnectionManager basicConnManager; private HttpClientContext context; @@ -75,13 +73,12 @@ public class HttpClientConnectionManagementTest { client.close(); if (response != null) response.close(); - } // tests @Test - @Ignore + // @Ignore // 2.1 IN ARTCLE public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException { basicConnManager = new BasicHttpClientConnectionManager(); @@ -90,7 +87,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 2.2 IN ARTICLE public final void whenOpeningLowLevelConnectionWithSocketTimeout_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException { basicConnManager = new BasicHttpClientConnectionManager(); @@ -106,11 +103,11 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // Example 3.1. TESTER VERSION public final void WhenTwoConnectionsForTwoRequests_ThenLeaseTwoConnectionsNoExceptions() throws InterruptedException { - get1 = new HttpGet("http://localhost"); - get2 = new HttpGet("http://google.com"); + get1 = new HttpGet("http://www.petrikainulainen.net/"); + get2 = new HttpGet("http://www.baeldung.com/"); poolingConnManager = new PoolingHttpClientConnectionManager(); final CloseableHttpClient client1 = HttpClients.custom().setConnectionManager(poolingConnManager).build(); final CloseableHttpClient client2 = HttpClients.custom().setConnectionManager(poolingConnManager).build(); @@ -119,13 +116,12 @@ public class HttpClientConnectionManagementTest { thread1.start(); thread1.join(); thread2.start(); - assertTrue(poolingConnManager.getTotalStats().getLeased() == 1); thread2.join(1000); assertTrue(poolingConnManager.getTotalStats().getLeased() == 2); } @Test - @Ignore + // @Ignore // Example 3.1.ARTICLE VERSION public final void WhenTwoConnectionsForTwoRequests_ThensNoExceptions() throws InterruptedException { get1 = new HttpGet("http://localhost"); @@ -142,10 +138,9 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 3.3 public final void whenIncreasingConnectionPool_thenNoEceptions() { - poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager.setMaxTotal(5); poolingConnManager.setDefaultMaxPerRoute(4); @@ -154,7 +149,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 3.4 Tester Version public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimitNoExceptions() throws InterruptedException, IOException { final HttpGet get = new HttpGet("http://google.com"); @@ -174,7 +169,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 3.4 Article version public final void whenExecutingSameRequestsInDifferentThreads_thenExxecuteReuqesttNoExceptions() throws InterruptedException { final HttpGet get = new HttpGet("http://localhost"); @@ -192,7 +187,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 4.1 public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() throws ClientProtocolException, IOException { final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() { @@ -222,7 +217,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 5.1 public final void GivenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException { basicConnManager = new BasicHttpClientConnectionManager(); @@ -248,7 +243,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 5.2 TESTER VERSION public final void WhenConnectionsNeededGreaterThanMaxTotal_thenReuseConnectionsNoExceptions() throws InterruptedException { poolingConnManager = new PoolingHttpClientConnectionManager(); @@ -271,9 +266,9 @@ public class HttpClientConnectionManagementTest { } } - @Test // 5.2 ARTICLE VERSION - @Ignore + @Test + // @Ignore public final void WhenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuseNoExceptions() throws InterruptedException { final HttpGet get = new HttpGet("http://echo.200please.com"); poolingConnManager = new PoolingHttpClientConnectionManager(); @@ -293,7 +288,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 6.2.1 public final void whenConfiguringTimeOut_thenNoExceptions() { route = new HttpRoute(new HttpHost("localhost", 80)); @@ -303,7 +298,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 7.1 public final void whenHttpClientChecksStaleConns_thenNoExceptions() { poolingConnManager = new PoolingHttpClientConnectionManager(); @@ -311,7 +306,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 7.2 TESTER VERSION public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConnsNoExceptions() throws InterruptedException, IOException { poolingConnManager = new PoolingHttpClientConnectionManager(); @@ -334,7 +329,7 @@ public class HttpClientConnectionManagementTest { } @Test - @Ignore + // @Ignore // 7.2 ARTICLE VERSION public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException, IOException { final HttpGet get = new HttpGet("http://google.com"); @@ -346,7 +341,7 @@ public class HttpClientConnectionManagementTest { } @Test(expected = IllegalStateException.class) - @Ignore + // @Ignore // 8.1 public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException { route = new HttpRoute(new HttpHost("google.com", 80)); @@ -370,4 +365,5 @@ public class HttpClientConnectionManagementTest { assertTrue(conn.isOpen()); assertTrue(response.getEntity() == null); } + } diff --git a/httpclient/src/test/java/org/baeldung/httpclient/TesterVersion_MultiHttpClientConnThread.java b/httpclient/src/test/java/org/baeldung/httpclient/TesterVersion_MultiHttpClientConnThread.java index f71ac7462e..131761b072 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/TesterVersion_MultiHttpClientConnThread.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/TesterVersion_MultiHttpClientConnThread.java @@ -15,6 +15,7 @@ public class TesterVersion_MultiHttpClientConnThread extends Thread { private PoolingHttpClientConnectionManager connManager = null; private Logger logger; public int leasedConn; + public TesterVersion_MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) { this.client = client; this.get = get;