From db25745f05072df2d75152a7a35f6b684ccb1945 Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Tue, 4 Oct 2016 09:40:43 +0200 Subject: [PATCH] BAEL-255 - adding additional read file methods --- .../com/baeldung/file/FileOperationsTest.java | 122 ++++++++++++++---- 1 file changed, 94 insertions(+), 28 deletions(-) diff --git a/core-java-8/src/test/java/com/baeldung/file/FileOperationsTest.java b/core-java-8/src/test/java/com/baeldung/file/FileOperationsTest.java index b1476b6360..9f0781ca02 100644 --- a/core-java-8/src/test/java/com/baeldung/file/FileOperationsTest.java +++ b/core-java-8/src/test/java/com/baeldung/file/FileOperationsTest.java @@ -1,17 +1,24 @@ package com.baeldung.file; -import org.hamcrest.Matchers; -import org.junit.Assert; -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.URL; import java.net.URLConnection; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.function.Consumer; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.containsString; +import org.apache.commons.io.FileUtils; +import org.hamcrest.CoreMatchers; +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; public class FileOperationsTest { @@ -20,34 +27,22 @@ public class FileOperationsTest { String expectedData = "Hello World from fileTest.txt!!!"; ClassLoader classLoader = getClass().getClassLoader(); - InputStream inputStream = classLoader.getResourceAsStream("fileTest.txt"); + File file = new File(classLoader.getResource("fileTest.txt").getFile()); + InputStream inputStream = new FileInputStream(file); String data = readFromInputStream(inputStream); - - Assert.assertThat(data, containsString(expectedData)); -} - - @Test - public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException { - String expectedData = "Hello World from fileTest.txt!!!"; - - Class clazz = FileOperationsTest.class; - InputStream inputStream = clazz.getResourceAsStream("/fileTest.txt"); - String data = readFromInputStream(inputStream); - - Assert.assertThat(data, containsString(expectedData)); + + Assert.assertEquals(expectedData, data.trim()); } @Test - public void givenURLName_whenUsingURL_thenFileData() throws IOException { - String expectedData = "Baeldung"; - - URL urlObject = new URL("http://www.baeldung.com/"); - URLConnection urlConnection = urlObject.openConnection(); - - InputStream inputStream = urlConnection.getInputStream(); + public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException { + String expectedData = "Hello World from fileTest_2.txt!!!"; + + Class clazz = FileOperationsTest.class; + InputStream inputStream = clazz.getResourceAsStream("/fileTest_2.txt"); String data = readFromInputStream(inputStream); - Assert.assertThat(data, containsString(expectedData)); + Assert.assertEquals(expectedData, data.trim()); } @Test @@ -58,9 +53,80 @@ public class FileOperationsTest { InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt"); String data = readFromInputStream(inputStream); - Assert.assertThat(data, containsString(expectedData)); + Assert.assertThat(data.trim(), CoreMatchers.containsString(expectedData)); } + /*@Test + public void givenFileName_whenUsingJarFile_thenFileData() throws IOException { + String expectedData = "BSD License"; + + Class clazz = Matchers.class; + ClassLoader classLoader = clazz.getClassLoader(); + InputStream inputStream = classLoader.getResourceAsStream("LICENSE.txt"); + String data = readFromInputStream(inputStream); + + Assert.assertThat(data.trim(), CoreMatchers.containsString(expectedData)); + }*/ + + @Test + public void givenURLName_whenUsingURL_thenFileData() throws IOException { + String expectedData = "Baeldung"; + + URL urlObject = new URL("http://www.baeldung.com/"); + + URLConnection urlConnection = urlObject.openConnection(); + + InputStream inputStream = urlConnection.getInputStream(); + String data = readFromInputStream(inputStream); + + Assert.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); + + Assert.assertEquals(expectedData, data.trim()); + } + + @Test + public void givenFilePath_whenUsingFilesReadAllBytes_thenFileData() throws IOException { + String expectedData = "Hello World from fileTest.txt!!!"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("fileTest.txt").getFile()); + Path path = Paths.get(file.getAbsolutePath()); + + byte[] fileBytes = Files.readAllBytes(path); + String data = new String(fileBytes); + + Assert.assertEquals(expectedData, data.trim()); + } + + @Test + public void givenFilePath_whenUsingFilesLines_thenFileData() throws IOException { + String expectedData = "Hello World from fileTest.txt!!!"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("fileTest.txt").getFile()); + Path path = Paths.get(file.getAbsolutePath()); + + StringBuilder data = new StringBuilder(); + Stream lines = Files.lines(path); + lines.forEach(new Consumer() { + @Override + public void accept(String line) { + data.append(line).append("\n"); + } + }); + + Assert.assertEquals(expectedData, data.toString().trim()); + } + private String readFromInputStream(InputStream inputStream) throws IOException { InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader);