11package com .baeldung .file ;
22
3- import org .apache .commons .io .FileUtils ;
4- import org .hamcrest .CoreMatchers ;
5- import org .hamcrest .Matchers ;
6- import org .junit .Assert ;
7- import org .junit .Test ;
8-
93import java .io .BufferedReader ;
104import java .io .File ;
115import java .io .FileInputStream ;
2014import java .nio .file .Paths ;
2115import java .util .stream .Stream ;
2216
23- public class FileOperationsTest {
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 ;
22+
23+ public class FileOperationsUnitTest {
2424
2525 @ Test
2626 public void givenFileName_whenUsingClassloader_thenFileData () throws IOException {
@@ -30,15 +30,15 @@ public void givenFileName_whenUsingClassloader_thenFileData() throws IOException
3030 File file = new File (classLoader .getResource ("fileTest.txt" ).getFile ());
3131 InputStream inputStream = new FileInputStream (file );
3232 String data = readFromInputStream (inputStream );
33-
33+
3434 Assert .assertEquals (expectedData , data .trim ());
3535 }
3636
3737 @ Test
3838 public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData () throws IOException {
3939 String expectedData = "Hello World from fileTest.txt!!!" ;
40-
41- Class clazz = FileOperationsTest .class ;
40+
41+ Class clazz = FileOperationsUnitTest .class ;
4242 InputStream inputStream = clazz .getResourceAsStream ("/fileTest.txt" );
4343 String data = readFromInputStream (inputStream );
4444
@@ -69,44 +69,44 @@ public void givenURLName_whenUsingURL_thenFileData() throws IOException {
6969
7070 Assert .assertThat (data .trim (), CoreMatchers .containsString (expectedData ));
7171 }
72-
72+
7373 @ Test
7474 public void givenFileName_whenUsingFileUtils_thenFileData () throws IOException {
7575 String expectedData = "Hello World from fileTest.txt!!!" ;
76-
76+
7777 ClassLoader classLoader = getClass ().getClassLoader ();
7878 File file = new File (classLoader .getResource ("fileTest.txt" ).getFile ());
7979 String data = FileUtils .readFileToString (file );
80-
80+
8181 Assert .assertEquals (expectedData , data .trim ());
8282 }
83-
83+
8484 @ Test
8585 public void givenFilePath_whenUsingFilesReadAllBytes_thenFileData () throws IOException , URISyntaxException {
8686 String expectedData = "Hello World from fileTest.txt!!!" ;
8787
8888 Path path = Paths .get (getClass ().getClassLoader ().getResource ("fileTest.txt" ).toURI ());
89-
89+
9090 byte [] fileBytes = Files .readAllBytes (path );
9191 String data = new String (fileBytes );
9292
9393 Assert .assertEquals (expectedData , data .trim ());
9494 }
95-
95+
9696 @ Test
9797 public void givenFilePath_whenUsingFilesLines_thenFileData () throws IOException , URISyntaxException {
9898 String expectedData = "Hello World from fileTest.txt!!!" ;
9999
100100 Path path = Paths .get (getClass ().getClassLoader ().getResource ("fileTest.txt" ).toURI ());
101-
101+
102102 StringBuilder data = new StringBuilder ();
103103 Stream <String > lines = Files .lines (path );
104104 lines .forEach (line -> data .append (line ).append ("\n " ));
105105 lines .close ();
106-
106+
107107 Assert .assertEquals (expectedData , data .toString ().trim ());
108108 }
109-
109+
110110 private String readFromInputStream (InputStream inputStream ) throws IOException {
111111 StringBuilder resultStringBuilder = new StringBuilder ();
112112 try (BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream ))) {
0 commit comments