BAEL-2659 - Reading a file in Groovy - added more examples and unit tests

This commit is contained in:
Anshul Bansal 2019-01-29 17:27:52 +02:00
parent f22d1073d1
commit 1f4d1839f4
4 changed files with 52 additions and 1 deletions

View File

@ -53,5 +53,41 @@ class ReadFile {
return utf8Content
}
/**
* reads content of binary file and returns byte array
* @param filePath
* @return
*/
byte[] readBinaryFile(String filePath) {
File file = new File(filePath)
byte[] binaryContent = file.bytes
return binaryContent
}
/**
* More Examples of reading a file
* @return
*/
def moreExamples() {
def list = new File("src/main/resources/fileContent.txt").collect {it}
def array = new File("src/main/resources/fileContent.txt") as String[]
new File("src/main/resources/fileContent.txt").eachLine { line ->
println line
}
def is = new File("src/main/resources/fileContent.txt").newInputStream()
is.eachLine {
println it
}
is.close()
new File("src/main/resources/fileContent.txt").withInputStream { stream ->
stream.eachLine { line ->
println line
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 B

View File

@ -0,0 +1,5 @@
ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ
ᛋᚳᛖᚪᛚ᛫ᚦᛖᚪᚻ᛫ᛗᚪᚾᚾᚪ᛫ᚷᛖᚻᚹᛦᛚᚳ᛫ᛗᛁᚳᛚᚢᚾ᛫ᚻᛦᛏ᛫ᛞᚫᛚᚪᚾ
ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ
¥ · £ · € · $ · ¢ · ₡ · ₢ · ₣ · ₤ · ₥ · ₦ · ₧ · ₨ · ₩ · ₪ · ₫ · ₭ · ₮ · ₯ · ₹

View File

@ -48,11 +48,21 @@ Line 3 : String content""")
def 'Should return UTF-8 encoded file content in string using ReadFile.readFileStringWithCharset given filePath' () {
given:
def filePath = "src/main/resources/fileContent.txt"
def filePath = "src/main/resources/utf8Content.html"
when:
def noOfLines = readFile.readFileStringWithCharset(filePath)
then:
noOfLines
noOfLines instanceof String
}
def 'Should return binary file content in byte arry using ReadFile.readBinaryFile given filePath' () {
given:
def filePath = "src/main/resources/sample.png"
when:
def noOfLines = readFile.readBinaryFile(filePath)
then:
noOfLines
noOfLines instanceof byte[]
}
}