11package com .baeldung .readfile ;
22
3+ import org .apache .commons .io .FileUtils ;
4+ import org .apache .commons .io .IOUtils ;
5+ import org .hamcrest .CoreMatchers ;
6+ import org .hamcrest .Matchers ;
37import org .junit .Test ;
4- import org .junit .Ignore ;
5- import org .slf4j .Logger ;
6- import org .slf4j .LoggerFactory ;
78
89import java .io .*;
10+ import java .net .URISyntaxException ;
11+ import java .net .URL ;
12+ import java .net .URLConnection ;
913import java .nio .ByteBuffer ;
1014import java .nio .channels .FileChannel ;
1115import java .nio .charset .Charset ;
1216import java .nio .file .Files ;
1317import java .nio .file .Path ;
1418import java .nio .file .Paths ;
1519import java .util .Scanner ;
20+ import java .util .stream .Collectors ;
21+ import java .util .stream .Stream ;
1622
23+ import static org .hamcrest .MatcherAssert .assertThat ;
1724import static org .junit .Assert .assertEquals ;
1825import static org .junit .Assert .assertTrue ;
1926
2027public class JavaReadFromFileUnitTest {
2128
22- private static final Logger LOG = LoggerFactory .getLogger (JavaReadFromFileUnitTest .class );
23-
2429 @ Test
2530 public void whenReadWithBufferedReader_thenCorrect () throws IOException {
2631 final String expected_value = "Hello world" ;
@@ -32,6 +37,100 @@ public void whenReadWithBufferedReader_thenCorrect() throws IOException {
3237 assertEquals (expected_value , currentLine );
3338 }
3439
40+ @ Test
41+ public void givenFileName_whenUsingClassloader_thenFileData () throws IOException {
42+ String expectedData = "Hello World from fileTest.txt!!!" ;
43+
44+ ClassLoader classLoader = getClass ().getClassLoader ();
45+ File file = new File (classLoader .getResource ("fileTest.txt" ).getFile ());
46+ InputStream inputStream = new FileInputStream (file );
47+ String data = readFromInputStream (inputStream );
48+
49+ assertEquals (expectedData , data .trim ());
50+ }
51+
52+ @ Test
53+ public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData () throws IOException {
54+ String expectedData = "Hello World from fileTest.txt!!!" ;
55+
56+ Class clazz = JavaReadFromFileUnitTest .class ;
57+ InputStream inputStream = clazz .getResourceAsStream ("/fileTest.txt" );
58+ String data = readFromInputStream (inputStream );
59+
60+ assertEquals (expectedData , data .trim ());
61+ }
62+
63+ @ Test
64+ public void givenFileName_whenUsingJarFile_thenFileData () throws IOException {
65+ String expectedData = "BSD License" ;
66+
67+ Class clazz = Matchers .class ;
68+ InputStream inputStream = clazz .getResourceAsStream ("/LICENSE.txt" );
69+ String data = readFromInputStream (inputStream );
70+
71+ assertThat (data .trim (), CoreMatchers .containsString (expectedData ));
72+ }
73+
74+ @ Test
75+ public void givenURLName_whenUsingURL_thenFileData () throws IOException {
76+ String expectedData = "Example Domain" ;
77+
78+ URL urlObject = new URL ("http://www.example.com/" );
79+
80+ URLConnection urlConnection = urlObject .openConnection ();
81+
82+ InputStream inputStream = urlConnection .getInputStream ();
83+ String data = readFromInputStream (inputStream );
84+
85+ assertThat (data .trim (), CoreMatchers .containsString (expectedData ));
86+ }
87+
88+ @ Test
89+ public void givenFileName_whenUsingFileUtils_thenFileData () throws IOException {
90+ String expectedData = "Hello World from fileTest.txt!!!" ;
91+
92+ ClassLoader classLoader = getClass ().getClassLoader ();
93+ File file = new File (classLoader .getResource ("fileTest.txt" ).getFile ());
94+ String data = FileUtils .readFileToString (file , "UTF-8" );
95+
96+ assertEquals (expectedData , data .trim ());
97+ }
98+
99+ @ Test
100+ public void givenFilePath_whenUsingFilesReadAllBytes_thenFileData () throws IOException , URISyntaxException {
101+ String expectedData = "Hello World from fileTest.txt!!!" ;
102+
103+ Path path = Paths .get (getClass ().getClassLoader ().getResource ("fileTest.txt" ).toURI ());
104+
105+ byte [] fileBytes = Files .readAllBytes (path );
106+ String data = new String (fileBytes );
107+
108+ assertEquals (expectedData , data .trim ());
109+ }
110+
111+ @ Test
112+ public void givenFilePath_whenUsingFilesLines_thenFileData () throws IOException , URISyntaxException {
113+ String expectedData = "Hello World from fileTest.txt!!!" ;
114+
115+ Path path = Paths .get (getClass ().getClassLoader ().getResource ("fileTest.txt" ).toURI ());
116+
117+ Stream <String > lines = Files .lines (path );
118+ String data = lines .collect (Collectors .joining ("\n " ));
119+ lines .close ();
120+
121+ assertEquals (expectedData , data .trim ());
122+ }
123+
124+ @ Test
125+ public void givenFileName_whenUsingIOUtils_thenFileData () throws IOException {
126+ String expectedData = "This is a content of the file" ;
127+
128+ FileInputStream fis = new FileInputStream ("src/test/resources/test_read9.in" );
129+ String data = IOUtils .toString (fis , "UTF-8" );
130+
131+ assertEquals (expectedData , data .trim ());
132+ }
133+
35134 @ Test
36135 public void whenReadWithScanner_thenCorrect () throws IOException {
37136 final Scanner scanner = new Scanner (new File ("src/test/resources/test_read1.in" ));
@@ -106,14 +205,12 @@ public void whenReadTwoFilesWithSequenceInputStream_thenCorrect() throws IOExcep
106205 }
107206
108207 @ Test
109- @ Ignore // TODO
110208 public void whenReadUTFEncodedFile_thenCorrect () throws IOException {
111209 final String expected_value = "青空" ;
112210 final BufferedReader reader = new BufferedReader (new InputStreamReader (new FileInputStream ("src/test/resources/test_read7.in" ), "UTF-8" ));
113211 final String currentLine = reader .readLine ();
114212 reader .close ();
115- LOG .debug (currentLine );
116-
213+
117214 assertEquals (expected_value , currentLine );
118215 }
119216
@@ -171,4 +268,16 @@ public void whenReadLargeFileJava7_thenCorrect() throws IOException {
171268 assertEquals (expected_value , line );
172269 }
173270
271+ private String readFromInputStream (InputStream inputStream ) throws IOException {
272+ StringBuilder resultStringBuilder = new StringBuilder ();
273+ try (BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream ))) {
274+ String line ;
275+ while ((line = bufferedReader .readLine ()) != null ) {
276+ resultStringBuilder .append (line ).append ("\n " );
277+ }
278+ }
279+
280+ return resultStringBuilder .toString ();
281+ }
282+
174283}
0 commit comments