Skip to content

Commit db25745

Browse files
Sunil Gulabanislavisa-baeldung
authored andcommitted
BAEL-255 - adding additional read file methods
1 parent 28a0a5c commit db25745

1 file changed

Lines changed: 90 additions & 24 deletions

File tree

‎core-java-8/src/test/java/com/baeldung/file/FileOperationsTest.java‎

Lines changed: 90 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package com.baeldung.file;
22

3-
import org.hamcrest.Matchers;
4-
import org.junit.Assert;
5-
import org.junit.Test;
6-
73
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileInputStream;
86
import java.io.IOException;
97
import java.io.InputStream;
108
import java.io.InputStreamReader;
119
import java.net.URL;
1210
import java.net.URLConnection;
13-
14-
import static org.hamcrest.CoreMatchers.containsString;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.util.function.Consumer;
15+
import java.util.stream.Stream;
16+
17+
import org.apache.commons.io.FileUtils;
18+
import org.hamcrest.CoreMatchers;
19+
import org.hamcrest.Matchers;
20+
import org.junit.Assert;
21+
import org.junit.Test;
1522

1623
public class FileOperationsTest {
1724

@@ -20,47 +27,106 @@ public void givenFileName_whenUsingClassloader_thenFileData() throws IOException
2027
String expectedData = "Hello World from fileTest.txt!!!";
2128

2229
ClassLoader classLoader = getClass().getClassLoader();
23-
InputStream inputStream = classLoader.getResourceAsStream("fileTest.txt");
30+
File file = new File(classLoader.getResource("fileTest.txt").getFile());
31+
InputStream inputStream = new FileInputStream(file);
2432
String data = readFromInputStream(inputStream);
25-
26-
Assert.assertThat(data, containsString(expectedData));
27-
}
33+
34+
Assert.assertEquals(expectedData, data.trim());
35+
}
2836

2937
@Test
3038
public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException {
31-
String expectedData = "Hello World from fileTest.txt!!!";
32-
39+
String expectedData = "Hello World from fileTest_2.txt!!!";
40+
3341
Class clazz = FileOperationsTest.class;
34-
InputStream inputStream = clazz.getResourceAsStream("/fileTest.txt");
42+
InputStream inputStream = clazz.getResourceAsStream("/fileTest_2.txt");
3543
String data = readFromInputStream(inputStream);
3644

37-
Assert.assertThat(data, containsString(expectedData));
45+
Assert.assertEquals(expectedData, data.trim());
3846
}
3947

4048
@Test
41-
public void givenURLName_whenUsingURL_thenFileData() throws IOException {
42-
String expectedData = "Baeldung";
43-
44-
URL urlObject = new URL("http://www.baeldung.com/");
45-
URLConnection urlConnection = urlObject.openConnection();
49+
public void givenFileName_whenUsingJarFile_thenFileData() throws IOException {
50+
String expectedData = "BSD License";
4651

47-
InputStream inputStream = urlConnection.getInputStream();
52+
Class clazz = Matchers.class;
53+
InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt");
4854
String data = readFromInputStream(inputStream);
4955

50-
Assert.assertThat(data, containsString(expectedData));
56+
Assert.assertThat(data.trim(), CoreMatchers.containsString(expectedData));
5157
}
5258

53-
@Test
59+
/*@Test
5460
public void givenFileName_whenUsingJarFile_thenFileData() throws IOException {
5561
String expectedData = "BSD License";
5662
5763
Class clazz = Matchers.class;
58-
InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt");
64+
ClassLoader classLoader = clazz.getClassLoader();
65+
InputStream inputStream = classLoader.getResourceAsStream("LICENSE.txt");
5966
String data = readFromInputStream(inputStream);
6067
61-
Assert.assertThat(data, containsString(expectedData));
68+
Assert.assertThat(data.trim(), CoreMatchers.containsString(expectedData));
69+
}*/
70+
71+
@Test
72+
public void givenURLName_whenUsingURL_thenFileData() throws IOException {
73+
String expectedData = "Baeldung";
74+
75+
URL urlObject = new URL("http://www.baeldung.com/");
76+
77+
URLConnection urlConnection = urlObject.openConnection();
78+
79+
InputStream inputStream = urlConnection.getInputStream();
80+
String data = readFromInputStream(inputStream);
81+
82+
Assert.assertThat(data.trim(), CoreMatchers.containsString(expectedData));
6283
}
84+
85+
@Test
86+
public void givenFileName_whenUsingFileUtils_thenFileData() throws IOException {
87+
String expectedData = "Hello World from fileTest.txt!!!";
88+
89+
ClassLoader classLoader = getClass().getClassLoader();
90+
File file = new File(classLoader.getResource("fileTest.txt").getFile());
91+
String data = FileUtils.readFileToString(file);
92+
93+
Assert.assertEquals(expectedData, data.trim());
94+
}
95+
96+
@Test
97+
public void givenFilePath_whenUsingFilesReadAllBytes_thenFileData() throws IOException {
98+
String expectedData = "Hello World from fileTest.txt!!!";
99+
100+
ClassLoader classLoader = getClass().getClassLoader();
101+
File file = new File(classLoader.getResource("fileTest.txt").getFile());
102+
Path path = Paths.get(file.getAbsolutePath());
103+
104+
byte[] fileBytes = Files.readAllBytes(path);
105+
String data = new String(fileBytes);
63106

107+
Assert.assertEquals(expectedData, data.trim());
108+
}
109+
110+
@Test
111+
public void givenFilePath_whenUsingFilesLines_thenFileData() throws IOException {
112+
String expectedData = "Hello World from fileTest.txt!!!";
113+
114+
ClassLoader classLoader = getClass().getClassLoader();
115+
File file = new File(classLoader.getResource("fileTest.txt").getFile());
116+
Path path = Paths.get(file.getAbsolutePath());
117+
118+
StringBuilder data = new StringBuilder();
119+
Stream<String> lines = Files.lines(path);
120+
lines.forEach(new Consumer<String>() {
121+
@Override
122+
public void accept(String line) {
123+
data.append(line).append("\n");
124+
}
125+
});
126+
127+
Assert.assertEquals(expectedData, data.toString().trim());
128+
}
129+
64130
private String readFromInputStream(InputStream inputStream) throws IOException {
65131
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
66132
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

0 commit comments

Comments
 (0)