BAEL-2727 Example Code
This commit is contained in:
parent
cb8bdb3f39
commit
9f22567d4f
8
core-groovy/src/main/groovy/com/baeldung/io/Task.groovy
Normal file
8
core-groovy/src/main/groovy/com/baeldung/io/Task.groovy
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
class Task implements Serializable {
|
||||||
|
String description
|
||||||
|
Date startDate
|
||||||
|
Date dueDate
|
||||||
|
int status
|
||||||
|
}
|
BIN
core-groovy/src/main/resources/binaryExample.jpg
Normal file
BIN
core-groovy/src/main/resources/binaryExample.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
BIN
core-groovy/src/main/resources/ioData.txt
Normal file
BIN
core-groovy/src/main/resources/ioData.txt
Normal file
Binary file not shown.
4
core-groovy/src/main/resources/ioInput.txt
Normal file
4
core-groovy/src/main/resources/ioInput.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
First line of text
|
||||||
|
Second line of text
|
||||||
|
Third line of text
|
||||||
|
Fourth line of text
|
3
core-groovy/src/main/resources/ioOutput.txt
Normal file
3
core-groovy/src/main/resources/ioOutput.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Line one of output example
|
||||||
|
Line two of output example
|
||||||
|
Line three of output example
|
BIN
core-groovy/src/main/resources/ioSerializedObject.txt
Normal file
BIN
core-groovy/src/main/resources/ioSerializedObject.txt
Normal file
Binary file not shown.
@ -0,0 +1,51 @@
|
|||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class DataAndObjectsUnitTest {
|
||||||
|
@Test
|
||||||
|
void whenUsingWithDataOutputStream_thenDataIsSerializedToAFile() {
|
||||||
|
String message = 'This is a serialized string'
|
||||||
|
int length = message.length()
|
||||||
|
boolean valid = true
|
||||||
|
new File('src/main/resources/ioData.txt').withDataOutputStream { out ->
|
||||||
|
out.writeUTF(message)
|
||||||
|
out.writeInt(length)
|
||||||
|
out.writeBoolean(valid)
|
||||||
|
}
|
||||||
|
|
||||||
|
String loadedMessage = ""
|
||||||
|
int loadedLength
|
||||||
|
boolean loadedValid
|
||||||
|
|
||||||
|
new File('src/main/resources/ioData.txt').withDataInputStream { is ->
|
||||||
|
loadedMessage = is.readUTF()
|
||||||
|
loadedLength = is.readInt()
|
||||||
|
loadedValid = is.readBoolean()
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(message, loadedMessage)
|
||||||
|
assertEquals(length, loadedLength)
|
||||||
|
assertEquals(valid, loadedValid)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithObjectOutputStream_thenObjectIsSerializedToFile() {
|
||||||
|
Task task = new Task(description:'Take out the trash', startDate:new Date(), status:0)
|
||||||
|
new File('src/main/resources/ioSerializedObject.txt').withObjectOutputStream { out ->
|
||||||
|
out.writeObject(task)
|
||||||
|
}
|
||||||
|
|
||||||
|
Task taskRead
|
||||||
|
|
||||||
|
new File('src/main/resources/ioSerializedObject.txt').withObjectInputStream { is ->
|
||||||
|
taskRead = is.readObject()
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(task.description, taskRead.description)
|
||||||
|
assertEquals(task.startDate, taskRead.startDate)
|
||||||
|
assertEquals(task.status, taskRead.status)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,134 @@
|
|||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class ReadExampleUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachLine_thenCorrectLinesReturned() {
|
||||||
|
def expectedList = [
|
||||||
|
'First line of text',
|
||||||
|
'Second line of text',
|
||||||
|
'Third line of text',
|
||||||
|
'Fourth line of text']
|
||||||
|
|
||||||
|
def lines = []
|
||||||
|
|
||||||
|
new File('src/main/resources/ioInput.txt').eachLine { line ->
|
||||||
|
lines.add(line)
|
||||||
|
}
|
||||||
|
assertEquals(expectedList, lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingReadEachLineWithLineNumber_thenCorrectLinesReturned() {
|
||||||
|
def expectedList = [
|
||||||
|
'Second line of text',
|
||||||
|
'Third line of text',
|
||||||
|
'Fourth line of text']
|
||||||
|
|
||||||
|
def lineNoRange = 2..4
|
||||||
|
def lines = []
|
||||||
|
|
||||||
|
new File('src/main/resources/ioInput.txt').eachLine { line, lineNo ->
|
||||||
|
if (lineNoRange.contains(lineNo)) {
|
||||||
|
lines.add(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(expectedList, lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingReadEachLineWithLineNumberStartAtZero_thenCorrectLinesReturned() {
|
||||||
|
def expectedList = [
|
||||||
|
'Second line of text',
|
||||||
|
'Third line of text',
|
||||||
|
'Fourth line of text']
|
||||||
|
|
||||||
|
def lineNoRange = 1..3
|
||||||
|
def lines = []
|
||||||
|
|
||||||
|
new File('src/main/resources/ioInput.txt').eachLine(0, { line, lineNo ->
|
||||||
|
if (lineNoRange.contains(lineNo)) {
|
||||||
|
lines.add(line)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
assertEquals(expectedList, lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithReader_thenLineCountReturned() {
|
||||||
|
def expectedCount = 4
|
||||||
|
def actualCount = 0
|
||||||
|
new File('src/main/resources/ioInput.txt').withReader { reader ->
|
||||||
|
while(reader.readLine()) {
|
||||||
|
actualCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(expectedCount, actualCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingNewReader_thenOutputFileCreated() {
|
||||||
|
def outputPath = 'src/main/resources/ioOut.txt'
|
||||||
|
def reader = new File('src/main/resources/ioInput.txt').newReader()
|
||||||
|
new File(outputPath).append(reader)
|
||||||
|
reader.close()
|
||||||
|
def ioOut = new File(outputPath)
|
||||||
|
assertTrue(ioOut.exists())
|
||||||
|
ioOut.delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithInputStream_thenCorrectBytesAreReturned() {
|
||||||
|
def expectedLength = 1139
|
||||||
|
byte[] data = []
|
||||||
|
new File("src/main/resources/binaryExample.jpg").withInputStream { stream ->
|
||||||
|
data = stream.getBytes()
|
||||||
|
}
|
||||||
|
assertEquals(expectedLength, data.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingNewInputStream_thenOutputFileCreated() {
|
||||||
|
def outputPath = 'src/main/resources/binaryOut.jpg'
|
||||||
|
def is = new File('src/main/resources/binaryExample.jpg').newInputStream()
|
||||||
|
new File(outputPath).append(is)
|
||||||
|
is.close()
|
||||||
|
def ioOut = new File(outputPath)
|
||||||
|
assertTrue(ioOut.exists())
|
||||||
|
ioOut.delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingCollect_thenCorrectListIsReturned() {
|
||||||
|
def expectedList = ['First line of text', 'Second line of text', 'Third line of text', 'Fourth line of text']
|
||||||
|
|
||||||
|
def actualList = new File('src/main/resources/ioInput.txt').collect {it}
|
||||||
|
assertEquals(expectedList, actualList)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingAsStringArray_thenCorrectArrayIsReturned() {
|
||||||
|
String[] expectedArray = ['First line of text', 'Second line of text', 'Third line of text', 'Fourth line of text']
|
||||||
|
|
||||||
|
def actualArray = new File('src/main/resources/ioInput.txt') as String[]
|
||||||
|
assertArrayEquals(expectedArray, actualArray)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingText_thenCorrectStringIsReturned() {
|
||||||
|
def ln = System.getProperty('line.separator')
|
||||||
|
def expectedString = "First line of text${ln}Second line of text${ln}Third line of text${ln}Fourth line of text"
|
||||||
|
def actualString = new File('src/main/resources/ioInput.txt').text
|
||||||
|
assertEquals(expectedString.toString(), actualString)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingBytes_thenByteArrayIsReturned() {
|
||||||
|
def expectedLength = 1139
|
||||||
|
def contents = new File('src/main/resources/binaryExample.jpg').bytes
|
||||||
|
assertEquals(expectedLength, contents.length)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
import groovy.io.FileType
|
||||||
|
import groovy.io.FileVisitResult
|
||||||
|
|
||||||
|
class TraverseFileTreeUnitTest {
|
||||||
|
@Test
|
||||||
|
void whenUsingEachFile_filesAreListed() {
|
||||||
|
new File('src/main/resources').eachFile { file ->
|
||||||
|
println file.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException)
|
||||||
|
void whenUsingEachFileOnAFile_anErrorOccurs() {
|
||||||
|
new File('src/main/resources/ioInput.txt').eachFile { file ->
|
||||||
|
println file.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachFileMatch_filesAreListed() {
|
||||||
|
new File('src/main/resources').eachFileMatch(~/io.*\.txt/) { file ->
|
||||||
|
println file.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachFileRecurse_thenFilesInSubfoldersAreListed() {
|
||||||
|
new File('src/main').eachFileRecurse(FileType.FILES) { file ->
|
||||||
|
println "$file.parent $file.name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachFileRecurse_thenDirsInSubfoldersAreListed() {
|
||||||
|
new File('src/main').eachFileRecurse(FileType.DIRECTORIES) { file ->
|
||||||
|
println "$file.parent $file.name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingEachDirRecurse_thenDirsAndSubDirsAreListed() {
|
||||||
|
new File('src/main').eachDirRecurse { dir ->
|
||||||
|
println "$dir.parent $dir.name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingTraverse_thenDirectoryIsTraversed() {
|
||||||
|
new File('src/main').traverse { file ->
|
||||||
|
if (file.directory && file.name == 'groovy') {
|
||||||
|
FileVisitResult.SKIP_SUBTREE
|
||||||
|
} else {
|
||||||
|
println "$file.parent - $file.name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
package com.baeldung.io
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class WriteExampleUnitTest {
|
||||||
|
@Before
|
||||||
|
void clearOutputFile() {
|
||||||
|
new File('src/main/resources/ioOutput.txt').text = ''
|
||||||
|
new File('src/main/resources/ioBinaryOutput.bin').delete()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithWriter_thenFileCreated() {
|
||||||
|
def outputLines = [
|
||||||
|
'Line one of output example',
|
||||||
|
'Line two of output example',
|
||||||
|
'Line three of output example'
|
||||||
|
]
|
||||||
|
|
||||||
|
def outputFileName = 'src/main/resources/ioOutput.txt'
|
||||||
|
new File(outputFileName).withWriter { writer ->
|
||||||
|
outputLines.each { line ->
|
||||||
|
writer.writeLine line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
def writtenLines = new File(outputFileName).collect {it}
|
||||||
|
assertEquals(outputLines, writtenLines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingNewWriter_thenFileCreated() {
|
||||||
|
def outputLines = [
|
||||||
|
'Line one of output example',
|
||||||
|
'Line two of output example',
|
||||||
|
'Line three of output example'
|
||||||
|
]
|
||||||
|
|
||||||
|
def outputFileName = 'src/main/resources/ioOutput.txt'
|
||||||
|
def writer = new File(outputFileName).newWriter()
|
||||||
|
outputLines.forEach {line ->
|
||||||
|
writer.writeLine line
|
||||||
|
}
|
||||||
|
writer.flush()
|
||||||
|
writer.close()
|
||||||
|
|
||||||
|
def writtenLines = new File(outputFileName).collect {it}
|
||||||
|
assertEquals(outputLines, writtenLines)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingDoubleLessThanOperator_thenFileCreated() {
|
||||||
|
def outputLines = [
|
||||||
|
'Line one of output example',
|
||||||
|
'Line two of output example',
|
||||||
|
'Line three of output example'
|
||||||
|
]
|
||||||
|
|
||||||
|
def ln = System.getProperty('line.separator')
|
||||||
|
def outputFileName = 'src/main/resources/ioOutput.txt'
|
||||||
|
new File(outputFileName) << "Line one of output example${ln}Line two of output example${ln}Line three of output example"
|
||||||
|
def writtenLines = new File(outputFileName).collect {it}
|
||||||
|
assertEquals(outputLines.size(), writtenLines.size())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingBytes_thenBinaryFileCreated() {
|
||||||
|
def outputFileName = 'src/main/resources/ioBinaryOutput.bin'
|
||||||
|
def outputFile = new File(outputFileName)
|
||||||
|
byte[] outBytes = [44, 88, 22]
|
||||||
|
outputFile.bytes = outBytes
|
||||||
|
assertEquals(3, new File(outputFileName).size())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingWithOutputStream_thenBinaryFileCreated() {
|
||||||
|
def outputFileName = 'src/main/resources/ioBinaryOutput.bin'
|
||||||
|
byte[] outBytes = [44, 88, 22]
|
||||||
|
new File(outputFileName).withOutputStream { stream ->
|
||||||
|
stream.write(outBytes)
|
||||||
|
}
|
||||||
|
assertEquals(3, new File(outputFileName).size())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingNewOutputStream_thenBinaryFileCreated() {
|
||||||
|
def outputFileName = 'src/main/resources/ioBinaryOutput.bin'
|
||||||
|
byte[] outBytes = [44, 88, 22]
|
||||||
|
def os = new File(outputFileName).newOutputStream()
|
||||||
|
os.write(outBytes)
|
||||||
|
os.close()
|
||||||
|
assertEquals(3, new File(outputFileName).size())
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user