From b155b71c4b7df70993b9fbfc48bbe757339b178e Mon Sep 17 00:00:00 2001 From: mikr Date: Thu, 13 Feb 2020 09:16:08 +0100 Subject: [PATCH] Java-112 Merge duplicate Java read from file articles --- .../readfile/FileOperationsManualTest.java | 134 ------------------ .../readfile/JavaReadFromFileUnitTest.java | 125 ++++++++++++++-- .../src/test/resources/test_read9.in | 1 + 3 files changed, 118 insertions(+), 142 deletions(-) delete mode 100644 core-java-modules/core-java-io/src/test/java/com/baeldung/readfile/FileOperationsManualTest.java create mode 100644 core-java-modules/core-java-io/src/test/resources/test_read9.in diff --git a/core-java-modules/core-java-io/src/test/java/com/baeldung/readfile/FileOperationsManualTest.java b/core-java-modules/core-java-io/src/test/java/com/baeldung/readfile/FileOperationsManualTest.java deleted file mode 100644 index b837c6b4dd..0000000000 --- a/core-java-modules/core-java-io/src/test/java/com/baeldung/readfile/FileOperationsManualTest.java +++ /dev/null @@ -1,134 +0,0 @@ -package com.baeldung.readfile; - -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.hamcrest.CoreMatchers; -import org.hamcrest.Matchers; -import org.junit.Test; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; - -public class FileOperationsManualTest { - - @Test - public void givenFileName_whenUsingClassloader_thenFileData() throws IOException { - String expectedData = "Hello World from fileTest.txt!!!"; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("fileTest.txt").getFile()); - InputStream inputStream = new FileInputStream(file); - String data = readFromInputStream(inputStream); - - assertEquals(expectedData, data.trim()); - } - - @Test - public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException { - String expectedData = "Hello World from fileTest.txt!!!"; - - Class clazz = FileOperationsManualTest.class; - InputStream inputStream = clazz.getResourceAsStream("/fileTest.txt"); - String data = readFromInputStream(inputStream); - - assertEquals(expectedData, data.trim()); - } - - @Test - public void givenFileName_whenUsingJarFile_thenFileData() throws IOException { - String expectedData = "MIT License"; - - Class clazz = Matchers.class; - InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt"); - String data = readFromInputStream(inputStream); - - assertThat(data.trim(), CoreMatchers.containsString(expectedData)); - } - - @Test - public void givenURLName_whenUsingURL_thenFileData() throws IOException { - String expectedData = "Example Domain"; - - URL urlObject = new URL("http://www.example.com/"); - - URLConnection urlConnection = urlObject.openConnection(); - - InputStream inputStream = urlConnection.getInputStream(); - String data = readFromInputStream(inputStream); - - assertThat(data.trim(), CoreMatchers.containsString(expectedData)); - } - - @Test - public void givenFileName_whenUsingFileUtils_thenFileData() throws IOException { - String expectedData = "Hello World from fileTest.txt!!!"; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("fileTest.txt").getFile()); - String data = FileUtils.readFileToString(file, "UTF-8"); - - assertEquals(expectedData, data.trim()); - } - - @Test - public void givenFilePath_whenUsingFilesReadAllBytes_thenFileData() throws IOException, URISyntaxException { - String expectedData = "Hello World from fileTest.txt!!!"; - - Path path = Paths.get(getClass().getClassLoader().getResource("fileTest.txt").toURI()); - - byte[] fileBytes = Files.readAllBytes(path); - String data = new String(fileBytes); - - assertEquals(expectedData, data.trim()); - } - - @Test - public void givenFilePath_whenUsingFilesLines_thenFileData() throws IOException, URISyntaxException { - String expectedData = "Hello World from fileTest.txt!!!"; - - Path path = Paths.get(getClass().getClassLoader().getResource("fileTest.txt").toURI()); - - Stream lines = Files.lines(path); - String data = lines.collect(Collectors.joining("\n")); - lines.close(); - - assertEquals(expectedData, data.trim()); - } - - private String readFromInputStream(InputStream inputStream) throws IOException { - StringBuilder resultStringBuilder = new StringBuilder(); - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { - String line; - while ((line = bufferedReader.readLine()) != null) { - resultStringBuilder.append(line).append("\n"); - } - } - - return resultStringBuilder.toString(); - } - - @Test - public void givenFileName_whenUsingIOUtils_thenFileData() throws IOException { - String expectedData = "This is a content of the file"; - - FileInputStream fis = new FileInputStream("src/test/resources/fileToRead.txt"); - String data = IOUtils.toString(fis, "UTF-8"); - - assertEquals(expectedData, data.trim()); - } -} \ No newline at end of file diff --git a/core-java-modules/core-java-io/src/test/java/com/baeldung/readfile/JavaReadFromFileUnitTest.java b/core-java-modules/core-java-io/src/test/java/com/baeldung/readfile/JavaReadFromFileUnitTest.java index a4ccaad594..4cacac91fb 100644 --- a/core-java-modules/core-java-io/src/test/java/com/baeldung/readfile/JavaReadFromFileUnitTest.java +++ b/core-java-modules/core-java-io/src/test/java/com/baeldung/readfile/JavaReadFromFileUnitTest.java @@ -1,11 +1,15 @@ package com.baeldung.readfile; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.hamcrest.CoreMatchers; +import org.hamcrest.Matchers; import org.junit.Test; -import org.junit.Ignore; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.*; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; @@ -13,14 +17,15 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class JavaReadFromFileUnitTest { - private static final Logger LOG = LoggerFactory.getLogger(JavaReadFromFileUnitTest.class); - @Test public void whenReadWithBufferedReader_thenCorrect() throws IOException { final String expected_value = "Hello world"; @@ -32,6 +37,100 @@ public class JavaReadFromFileUnitTest { assertEquals(expected_value, currentLine); } + @Test + public void givenFileName_whenUsingClassloader_thenFileData() throws IOException { + String expectedData = "Hello World from fileTest.txt!!!"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("fileTest.txt").getFile()); + InputStream inputStream = new FileInputStream(file); + String data = readFromInputStream(inputStream); + + assertEquals(expectedData, data.trim()); + } + + @Test + public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException { + String expectedData = "Hello World from fileTest.txt!!!"; + + Class clazz = JavaReadFromFileUnitTest.class; + InputStream inputStream = clazz.getResourceAsStream("/fileTest.txt"); + String data = readFromInputStream(inputStream); + + assertEquals(expectedData, data.trim()); + } + + @Test + public void givenFileName_whenUsingJarFile_thenFileData() throws IOException { + String expectedData = "BSD License"; + + Class clazz = Matchers.class; + InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt"); + String data = readFromInputStream(inputStream); + + assertThat(data.trim(), CoreMatchers.containsString(expectedData)); + } + + @Test + public void givenURLName_whenUsingURL_thenFileData() throws IOException { + String expectedData = "Example Domain"; + + URL urlObject = new URL("http://www.example.com/"); + + URLConnection urlConnection = urlObject.openConnection(); + + InputStream inputStream = urlConnection.getInputStream(); + String data = readFromInputStream(inputStream); + + assertThat(data.trim(), CoreMatchers.containsString(expectedData)); + } + + @Test + public void givenFileName_whenUsingFileUtils_thenFileData() throws IOException { + String expectedData = "Hello World from fileTest.txt!!!"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("fileTest.txt").getFile()); + String data = FileUtils.readFileToString(file, "UTF-8"); + + assertEquals(expectedData, data.trim()); + } + + @Test + public void givenFilePath_whenUsingFilesReadAllBytes_thenFileData() throws IOException, URISyntaxException { + String expectedData = "Hello World from fileTest.txt!!!"; + + Path path = Paths.get(getClass().getClassLoader().getResource("fileTest.txt").toURI()); + + byte[] fileBytes = Files.readAllBytes(path); + String data = new String(fileBytes); + + assertEquals(expectedData, data.trim()); + } + + @Test + public void givenFilePath_whenUsingFilesLines_thenFileData() throws IOException, URISyntaxException { + String expectedData = "Hello World from fileTest.txt!!!"; + + Path path = Paths.get(getClass().getClassLoader().getResource("fileTest.txt").toURI()); + + Stream lines = Files.lines(path); + String data = lines.collect(Collectors.joining("\n")); + lines.close(); + + assertEquals(expectedData, data.trim()); + } + + @Test + public void givenFileName_whenUsingIOUtils_thenFileData() throws IOException { + String expectedData = "This is a content of the file"; + + FileInputStream fis = new FileInputStream("src/test/resources/test_read9.in"); + String data = IOUtils.toString(fis, "UTF-8"); + + assertEquals(expectedData, data.trim()); + } + @Test public void whenReadWithScanner_thenCorrect() throws IOException { final Scanner scanner = new Scanner(new File("src/test/resources/test_read1.in")); @@ -106,14 +205,12 @@ public class JavaReadFromFileUnitTest { } @Test - @Ignore // TODO public void whenReadUTFEncodedFile_thenCorrect() throws IOException { final String expected_value = "青空"; final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("src/test/resources/test_read7.in"), "UTF-8")); final String currentLine = reader.readLine(); reader.close(); - LOG.debug(currentLine); - + assertEquals(expected_value, currentLine); } @@ -171,4 +268,16 @@ public class JavaReadFromFileUnitTest { assertEquals(expected_value, line); } + private String readFromInputStream(InputStream inputStream) throws IOException { + StringBuilder resultStringBuilder = new StringBuilder(); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { + String line; + while ((line = bufferedReader.readLine()) != null) { + resultStringBuilder.append(line).append("\n"); + } + } + + return resultStringBuilder.toString(); + } + } diff --git a/core-java-modules/core-java-io/src/test/resources/test_read9.in b/core-java-modules/core-java-io/src/test/resources/test_read9.in new file mode 100644 index 0000000000..7be79c9761 --- /dev/null +++ b/core-java-modules/core-java-io/src/test/resources/test_read9.in @@ -0,0 +1 @@ +This is a content of the file