From f22d1073d19416ba5d327149b316babebb8b4b47 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 27 Jan 2019 22:47:27 +0200 Subject: [PATCH] BAEL-2659 - Reading a file in Groovy - changed order of functions --- .../groovy/com/baeldung/file/ReadFile.groovy | 48 +++++++++---------- .../com/baeldung/file/ReadFileUnitTest.groovy | 44 ++++++++--------- 2 files changed, 44 insertions(+), 48 deletions(-) diff --git a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy index fca5bde06f..73208a52c8 100644 --- a/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy +++ b/core-groovy/src/main/groovy/com/baeldung/file/ReadFile.groovy @@ -2,28 +2,6 @@ package com.baeldung.file class ReadFile { - /** - * reads file content in string using File.text - * @param filePath - * @return - */ - String readFileString(String filePath) { - File file = new File(filePath) - String fileContent = file.text - return fileContent - } - - /** - * reads file content in string with encoding using File.getText - * @param filePath - * @return - */ - String readFileStringWithCharset(String filePath) { - File file = new File(filePath) - String utf8Content = file.getText("UTF-8") - return utf8Content - } - /** * reads file content line by line using withReader and reader.readLine * @param filePath @@ -52,10 +30,28 @@ class ReadFile { def lines = file.readLines() return lines } - - public static void main(String[] args) { - def file = new File("../../src") - println file.directorySize + + /** + * reads file content in string using File.text + * @param filePath + * @return + */ + String readFileString(String filePath) { + File file = new File(filePath) + String fileContent = file.text + return fileContent } + /** + * reads file content in string with encoding using File.getText + * @param filePath + * @return + */ + String readFileStringWithCharset(String filePath) { + File file = new File(filePath) + String utf8Content = file.getText("UTF-8") + return utf8Content + } + + } \ No newline at end of file diff --git a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy index c90cc8b960..105a8e157f 100644 --- a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy @@ -10,6 +10,28 @@ class ReadFileUnitTest extends Specification { readFile = new ReadFile() } + def 'Should return number of lines in File using ReadFile.readFileLineByLine given filePath' () { + given: + def filePath = "src/main/resources/fileContent.txt" + when: + def noOfLines = readFile.readFileLineByLine(filePath) + then: + noOfLines + noOfLines instanceof Integer + assert noOfLines, 3 + } + + def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath' () { + given: + def filePath = "src/main/resources/fileContent.txt" + when: + def lines = readFile.readFileInList(filePath) + then: + lines + lines instanceof List + assert lines.size(), 3 + } + def 'Should return file content in string using ReadFile.readFileString given filePath' () { given: def filePath = "src/main/resources/fileContent.txt" @@ -33,26 +55,4 @@ Line 3 : String content""") noOfLines noOfLines instanceof String } - - def 'Should return number of lines in File using ReadFile.readFileLineByLine given filePath' () { - given: - def filePath = "src/main/resources/fileContent.txt" - when: - def noOfLines = readFile.readFileLineByLine(filePath) - then: - noOfLines - noOfLines instanceof Integer - assert noOfLines, 3 - } - - def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath' () { - given: - def filePath = "src/main/resources/fileContent.txt" - when: - def lines = readFile.readFileInList(filePath) - then: - lines - lines instanceof List - assert lines.size(), 3 - } } \ No newline at end of file