Skip to content

Commit 0a76200

Browse files
author
eugenp
committed
work on java io tests
1 parent 279b527 commit 0a76200

4 files changed

Lines changed: 72 additions & 24 deletions

File tree

core-java/src/test/java/org/baeldung/java/io/JavaFileIntegrationTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public final void givenUsingCommonsIo_whenCreatingFile_thenCorrect() throws IOEx
3535
FileUtils.touch(new File("src/test/resources/newFile_commonsio.txt"));
3636
}
3737

38+
@Test
39+
public final void givenUsingGuava_whenCreatingFile_thenCorrect() throws IOException {
40+
com.google.common.io.Files.touch(new File("src/test/resources/newFile_guava.txt"));
41+
}
42+
3843
// move a file
3944

4045
@Test
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.baeldung.java.io;
2+
3+
import java.io.IOException;
4+
import java.io.Reader;
5+
import java.io.StringReader;
6+
7+
import org.apache.commons.io.IOUtils;
8+
import org.junit.Test;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import com.google.common.io.CharSource;
13+
import com.google.common.io.CharStreams;
14+
15+
@SuppressWarnings("unused")
16+
public class JavaReaderToXUnitTest {
17+
protected final Logger logger = LoggerFactory.getLogger(getClass());
18+
private static final int DEFAULT_SIZE = 1500000;
19+
20+
// tests - Reader to String
21+
22+
@Test
23+
public void givenUsingPlainJava_whenConvertingReaderIntoString_thenCorrect() throws IOException {
24+
final Reader initialReader = new StringReader("text");
25+
26+
final char[] mediationArray = new char["text".length()];
27+
initialReader.read(mediationArray);
28+
initialReader.close();
29+
final String targetString = new String(mediationArray);
30+
}
31+
32+
@Test
33+
public void givenUsingGuava_whenConvertingReaderIntoString_thenCorrect() throws IOException {
34+
final Reader initialReader = CharSource.wrap("Google Guava v.17.0").openStream();
35+
final String targetString = CharStreams.toString(initialReader);
36+
initialReader.close();
37+
}
38+
39+
@Test
40+
public void givenUsingCommonsIo_whenConvertingReaderIntoString_thenCorrect() throws IOException {
41+
final Reader initialReader = new StringReader("Apache Commons IO 2.4");
42+
final String targetString = IOUtils.toString(initialReader);
43+
initialReader.close();
44+
}
45+
46+
}

httpclient/src/test/java/org/baeldung/httpclient/HttpClientConnectionManagementTest.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
import org.apache.http.util.EntityUtils;
3434
import org.junit.After;
3535
import org.junit.Before;
36-
import org.junit.Ignore;
3736
import org.junit.Test;
3837

39-
4038
public class HttpClientConnectionManagementTest {
4139
private BasicHttpClientConnectionManager basicConnManager;
4240
private HttpClientContext context;
@@ -75,13 +73,12 @@ public final void after() throws IllegalStateException, IOException {
7573
client.close();
7674
if (response != null)
7775
response.close();
78-
7976
}
8077

8178
// tests
8279

8380
@Test
84-
@Ignore
81+
// @Ignore
8582
// 2.1 IN ARTCLE
8683
public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException {
8784
basicConnManager = new BasicHttpClientConnectionManager();
@@ -90,7 +87,7 @@ public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws
9087
}
9188

9289
@Test
93-
@Ignore
90+
// @Ignore
9491
// 2.2 IN ARTICLE
9592
public final void whenOpeningLowLevelConnectionWithSocketTimeout_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
9693
basicConnManager = new BasicHttpClientConnectionManager();
@@ -106,11 +103,11 @@ public final void whenOpeningLowLevelConnectionWithSocketTimeout_thenNoException
106103
}
107104

108105
@Test
109-
@Ignore
106+
// @Ignore
110107
// Example 3.1. TESTER VERSION
111108
public final void WhenTwoConnectionsForTwoRequests_ThenLeaseTwoConnectionsNoExceptions() throws InterruptedException {
112-
get1 = new HttpGet("http://localhost");
113-
get2 = new HttpGet("http://google.com");
109+
get1 = new HttpGet("http://www.petrikainulainen.net/");
110+
get2 = new HttpGet("http://www.baeldung.com/");
114111
poolingConnManager = new PoolingHttpClientConnectionManager();
115112
final CloseableHttpClient client1 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
116113
final CloseableHttpClient client2 = HttpClients.custom().setConnectionManager(poolingConnManager).build();
@@ -119,13 +116,12 @@ public final void WhenTwoConnectionsForTwoRequests_ThenLeaseTwoConnectionsNoExce
119116
thread1.start();
120117
thread1.join();
121118
thread2.start();
122-
assertTrue(poolingConnManager.getTotalStats().getLeased() == 1);
123119
thread2.join(1000);
124120
assertTrue(poolingConnManager.getTotalStats().getLeased() == 2);
125121
}
126122

127123
@Test
128-
@Ignore
124+
// @Ignore
129125
// Example 3.1.ARTICLE VERSION
130126
public final void WhenTwoConnectionsForTwoRequests_ThensNoExceptions() throws InterruptedException {
131127
get1 = new HttpGet("http://localhost");
@@ -142,10 +138,9 @@ public final void WhenTwoConnectionsForTwoRequests_ThensNoExceptions() throws In
142138
}
143139

144140
@Test
145-
@Ignore
141+
// @Ignore
146142
// 3.3
147143
public final void whenIncreasingConnectionPool_thenNoEceptions() {
148-
149144
poolingConnManager = new PoolingHttpClientConnectionManager();
150145
poolingConnManager.setMaxTotal(5);
151146
poolingConnManager.setDefaultMaxPerRoute(4);
@@ -154,7 +149,7 @@ public final void whenIncreasingConnectionPool_thenNoEceptions() {
154149
}
155150

156151
@Test
157-
@Ignore
152+
// @Ignore
158153
// 3.4 Tester Version
159154
public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimitNoExceptions() throws InterruptedException, IOException {
160155
final HttpGet get = new HttpGet("http://google.com");
@@ -174,7 +169,7 @@ public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConn
174169
}
175170

176171
@Test
177-
@Ignore
172+
// @Ignore
178173
// 3.4 Article version
179174
public final void whenExecutingSameRequestsInDifferentThreads_thenExxecuteReuqesttNoExceptions() throws InterruptedException {
180175
final HttpGet get = new HttpGet("http://localhost");
@@ -192,7 +187,7 @@ public final void whenExecutingSameRequestsInDifferentThreads_thenExxecuteReuqes
192187
}
193188

194189
@Test
195-
@Ignore
190+
// @Ignore
196191
// 4.1
197192
public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() throws ClientProtocolException, IOException {
198193
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
@@ -222,7 +217,7 @@ public long getKeepAliveDuration(final HttpResponse myResponse, final HttpContex
222217
}
223218

224219
@Test
225-
@Ignore
220+
// @Ignore
226221
// 5.1
227222
public final void GivenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
228223
basicConnManager = new BasicHttpClientConnectionManager();
@@ -248,7 +243,7 @@ public final void GivenBasicHttpClientConnManager_whenConnectionReuse_thenNoExce
248243
}
249244

250245
@Test
251-
@Ignore
246+
// @Ignore
252247
// 5.2 TESTER VERSION
253248
public final void WhenConnectionsNeededGreaterThanMaxTotal_thenReuseConnectionsNoExceptions() throws InterruptedException {
254249
poolingConnManager = new PoolingHttpClientConnectionManager();
@@ -271,9 +266,9 @@ public final void WhenConnectionsNeededGreaterThanMaxTotal_thenReuseConnectionsN
271266
}
272267
}
273268

274-
@Test
275269
// 5.2 ARTICLE VERSION
276-
@Ignore
270+
@Test
271+
// @Ignore
277272
public final void WhenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuseNoExceptions() throws InterruptedException {
278273
final HttpGet get = new HttpGet("http://echo.200please.com");
279274
poolingConnManager = new PoolingHttpClientConnectionManager();
@@ -293,7 +288,7 @@ public final void WhenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandR
293288
}
294289

295290
@Test
296-
@Ignore
291+
// @Ignore
297292
// 6.2.1
298293
public final void whenConfiguringTimeOut_thenNoExceptions() {
299294
route = new HttpRoute(new HttpHost("localhost", 80));
@@ -303,15 +298,15 @@ public final void whenConfiguringTimeOut_thenNoExceptions() {
303298
}
304299

305300
@Test
306-
@Ignore
301+
// @Ignore
307302
// 7.1
308303
public final void whenHttpClientChecksStaleConns_thenNoExceptions() {
309304
poolingConnManager = new PoolingHttpClientConnectionManager();
310305
client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).setConnectionManager(poolingConnManager).build();
311306
}
312307

313308
@Test
314-
@Ignore
309+
// @Ignore
315310
// 7.2 TESTER VERSION
316311
public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConnsNoExceptions() throws InterruptedException, IOException {
317312
poolingConnManager = new PoolingHttpClientConnectionManager();
@@ -334,7 +329,7 @@ public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConnsNoExceptio
334329
}
335330

336331
@Test
337-
@Ignore
332+
// @Ignore
338333
// 7.2 ARTICLE VERSION
339334
public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException, IOException {
340335
final HttpGet get = new HttpGet("http://google.com");
@@ -346,7 +341,7 @@ public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws Interr
346341
}
347342

348343
@Test(expected = IllegalStateException.class)
349-
@Ignore
344+
// @Ignore
350345
// 8.1
351346
public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
352347
route = new HttpRoute(new HttpHost("google.com", 80));
@@ -370,4 +365,5 @@ public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions() t
370365
assertTrue(conn.isOpen());
371366
assertTrue(response.getEntity() == null);
372367
}
368+
373369
}

httpclient/src/test/java/org/baeldung/httpclient/TesterVersion_MultiHttpClientConnThread.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class TesterVersion_MultiHttpClientConnThread extends Thread {
1515
private PoolingHttpClientConnectionManager connManager = null;
1616
private Logger logger;
1717
public int leasedConn;
18+
1819
public TesterVersion_MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) {
1920
this.client = client;
2021
this.get = get;

0 commit comments

Comments
 (0)