Skip to content

Commit 9f22567

Browse files
amdegregorioashleyfrieze
authored andcommitted
BAEL-2727 Example Code
1 parent cb8bdb3 commit 9f22567

10 files changed

Lines changed: 357 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.io
2+
3+
class Task implements Serializable {
4+
String description
5+
Date startDate
6+
Date dueDate
7+
int status
8+
}
1.11 KB
Loading
34 Bytes
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
First line of text
2+
Second line of text
3+
Third line of text
4+
Fourth line of text
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Line one of output example
2+
Line two of output example
3+
Line three of output example
199 Bytes
Binary file not shown.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.io
2+
3+
import static org.junit.Assert.*
4+
5+
import org.junit.Test
6+
7+
class DataAndObjectsUnitTest {
8+
@Test
9+
void whenUsingWithDataOutputStream_thenDataIsSerializedToAFile() {
10+
String message = 'This is a serialized string'
11+
int length = message.length()
12+
boolean valid = true
13+
new File('src/main/resources/ioData.txt').withDataOutputStream { out ->
14+
out.writeUTF(message)
15+
out.writeInt(length)
16+
out.writeBoolean(valid)
17+
}
18+
19+
String loadedMessage = ""
20+
int loadedLength
21+
boolean loadedValid
22+
23+
new File('src/main/resources/ioData.txt').withDataInputStream { is ->
24+
loadedMessage = is.readUTF()
25+
loadedLength = is.readInt()
26+
loadedValid = is.readBoolean()
27+
}
28+
29+
assertEquals(message, loadedMessage)
30+
assertEquals(length, loadedLength)
31+
assertEquals(valid, loadedValid)
32+
}
33+
34+
@Test
35+
void whenUsingWithObjectOutputStream_thenObjectIsSerializedToFile() {
36+
Task task = new Task(description:'Take out the trash', startDate:new Date(), status:0)
37+
new File('src/main/resources/ioSerializedObject.txt').withObjectOutputStream { out ->
38+
out.writeObject(task)
39+
}
40+
41+
Task taskRead
42+
43+
new File('src/main/resources/ioSerializedObject.txt').withObjectInputStream { is ->
44+
taskRead = is.readObject()
45+
}
46+
47+
assertEquals(task.description, taskRead.description)
48+
assertEquals(task.startDate, taskRead.startDate)
49+
assertEquals(task.status, taskRead.status)
50+
}
51+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.baeldung.io
2+
3+
import static org.junit.Assert.*
4+
import org.junit.Test
5+
6+
class ReadExampleUnitTest {
7+
8+
@Test
9+
void whenUsingEachLine_thenCorrectLinesReturned() {
10+
def expectedList = [
11+
'First line of text',
12+
'Second line of text',
13+
'Third line of text',
14+
'Fourth line of text']
15+
16+
def lines = []
17+
18+
new File('src/main/resources/ioInput.txt').eachLine { line ->
19+
lines.add(line)
20+
}
21+
assertEquals(expectedList, lines)
22+
}
23+
24+
@Test
25+
void whenUsingReadEachLineWithLineNumber_thenCorrectLinesReturned() {
26+
def expectedList = [
27+
'Second line of text',
28+
'Third line of text',
29+
'Fourth line of text']
30+
31+
def lineNoRange = 2..4
32+
def lines = []
33+
34+
new File('src/main/resources/ioInput.txt').eachLine { line, lineNo ->
35+
if (lineNoRange.contains(lineNo)) {
36+
lines.add(line)
37+
}
38+
}
39+
assertEquals(expectedList, lines)
40+
}
41+
42+
@Test
43+
void whenUsingReadEachLineWithLineNumberStartAtZero_thenCorrectLinesReturned() {
44+
def expectedList = [
45+
'Second line of text',
46+
'Third line of text',
47+
'Fourth line of text']
48+
49+
def lineNoRange = 1..3
50+
def lines = []
51+
52+
new File('src/main/resources/ioInput.txt').eachLine(0, { line, lineNo ->
53+
if (lineNoRange.contains(lineNo)) {
54+
lines.add(line)
55+
}
56+
})
57+
assertEquals(expectedList, lines)
58+
}
59+
60+
@Test
61+
void whenUsingWithReader_thenLineCountReturned() {
62+
def expectedCount = 4
63+
def actualCount = 0
64+
new File('src/main/resources/ioInput.txt').withReader { reader ->
65+
while(reader.readLine()) {
66+
actualCount++
67+
}
68+
}
69+
assertEquals(expectedCount, actualCount)
70+
}
71+
72+
@Test
73+
void whenUsingNewReader_thenOutputFileCreated() {
74+
def outputPath = 'src/main/resources/ioOut.txt'
75+
def reader = new File('src/main/resources/ioInput.txt').newReader()
76+
new File(outputPath).append(reader)
77+
reader.close()
78+
def ioOut = new File(outputPath)
79+
assertTrue(ioOut.exists())
80+
ioOut.delete()
81+
}
82+
83+
@Test
84+
void whenUsingWithInputStream_thenCorrectBytesAreReturned() {
85+
def expectedLength = 1139
86+
byte[] data = []
87+
new File("src/main/resources/binaryExample.jpg").withInputStream { stream ->
88+
data = stream.getBytes()
89+
}
90+
assertEquals(expectedLength, data.length)
91+
}
92+
93+
@Test
94+
void whenUsingNewInputStream_thenOutputFileCreated() {
95+
def outputPath = 'src/main/resources/binaryOut.jpg'
96+
def is = new File('src/main/resources/binaryExample.jpg').newInputStream()
97+
new File(outputPath).append(is)
98+
is.close()
99+
def ioOut = new File(outputPath)
100+
assertTrue(ioOut.exists())
101+
ioOut.delete()
102+
}
103+
104+
@Test
105+
void whenUsingCollect_thenCorrectListIsReturned() {
106+
def expectedList = ['First line of text', 'Second line of text', 'Third line of text', 'Fourth line of text']
107+
108+
def actualList = new File('src/main/resources/ioInput.txt').collect {it}
109+
assertEquals(expectedList, actualList)
110+
}
111+
112+
@Test
113+
void whenUsingAsStringArray_thenCorrectArrayIsReturned() {
114+
String[] expectedArray = ['First line of text', 'Second line of text', 'Third line of text', 'Fourth line of text']
115+
116+
def actualArray = new File('src/main/resources/ioInput.txt') as String[]
117+
assertArrayEquals(expectedArray, actualArray)
118+
}
119+
120+
@Test
121+
void whenUsingText_thenCorrectStringIsReturned() {
122+
def ln = System.getProperty('line.separator')
123+
def expectedString = "First line of text${ln}Second line of text${ln}Third line of text${ln}Fourth line of text"
124+
def actualString = new File('src/main/resources/ioInput.txt').text
125+
assertEquals(expectedString.toString(), actualString)
126+
}
127+
128+
@Test
129+
void whenUsingBytes_thenByteArrayIsReturned() {
130+
def expectedLength = 1139
131+
def contents = new File('src/main/resources/binaryExample.jpg').bytes
132+
assertEquals(expectedLength, contents.length)
133+
}
134+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.io
2+
3+
import org.junit.Test
4+
5+
import groovy.io.FileType
6+
import groovy.io.FileVisitResult
7+
8+
class TraverseFileTreeUnitTest {
9+
@Test
10+
void whenUsingEachFile_filesAreListed() {
11+
new File('src/main/resources').eachFile { file ->
12+
println file.name
13+
}
14+
}
15+
16+
@Test(expected = IllegalArgumentException)
17+
void whenUsingEachFileOnAFile_anErrorOccurs() {
18+
new File('src/main/resources/ioInput.txt').eachFile { file ->
19+
println file.name
20+
}
21+
}
22+
23+
@Test
24+
void whenUsingEachFileMatch_filesAreListed() {
25+
new File('src/main/resources').eachFileMatch(~/io.*\.txt/) { file ->
26+
println file.name
27+
}
28+
}
29+
30+
@Test
31+
void whenUsingEachFileRecurse_thenFilesInSubfoldersAreListed() {
32+
new File('src/main').eachFileRecurse(FileType.FILES) { file ->
33+
println "$file.parent $file.name"
34+
}
35+
}
36+
37+
@Test
38+
void whenUsingEachFileRecurse_thenDirsInSubfoldersAreListed() {
39+
new File('src/main').eachFileRecurse(FileType.DIRECTORIES) { file ->
40+
println "$file.parent $file.name"
41+
}
42+
}
43+
44+
@Test
45+
void whenUsingEachDirRecurse_thenDirsAndSubDirsAreListed() {
46+
new File('src/main').eachDirRecurse { dir ->
47+
println "$dir.parent $dir.name"
48+
}
49+
}
50+
51+
@Test
52+
void whenUsingTraverse_thenDirectoryIsTraversed() {
53+
new File('src/main').traverse { file ->
54+
if (file.directory && file.name == 'groovy') {
55+
FileVisitResult.SKIP_SUBTREE
56+
} else {
57+
println "$file.parent - $file.name"
58+
}
59+
}
60+
}
61+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.baeldung.io
2+
3+
import static org.junit.Assert.*
4+
5+
import org.junit.Before
6+
import org.junit.Test
7+
8+
class WriteExampleUnitTest {
9+
@Before
10+
void clearOutputFile() {
11+
new File('src/main/resources/ioOutput.txt').text = ''
12+
new File('src/main/resources/ioBinaryOutput.bin').delete()
13+
}
14+
15+
@Test
16+
void whenUsingWithWriter_thenFileCreated() {
17+
def outputLines = [
18+
'Line one of output example',
19+
'Line two of output example',
20+
'Line three of output example'
21+
]
22+
23+
def outputFileName = 'src/main/resources/ioOutput.txt'
24+
new File(outputFileName).withWriter { writer ->
25+
outputLines.each { line ->
26+
writer.writeLine line
27+
}
28+
}
29+
def writtenLines = new File(outputFileName).collect {it}
30+
assertEquals(outputLines, writtenLines)
31+
}
32+
33+
@Test
34+
void whenUsingNewWriter_thenFileCreated() {
35+
def outputLines = [
36+
'Line one of output example',
37+
'Line two of output example',
38+
'Line three of output example'
39+
]
40+
41+
def outputFileName = 'src/main/resources/ioOutput.txt'
42+
def writer = new File(outputFileName).newWriter()
43+
outputLines.forEach {line ->
44+
writer.writeLine line
45+
}
46+
writer.flush()
47+
writer.close()
48+
49+
def writtenLines = new File(outputFileName).collect {it}
50+
assertEquals(outputLines, writtenLines)
51+
}
52+
53+
@Test
54+
void whenUsingDoubleLessThanOperator_thenFileCreated() {
55+
def outputLines = [
56+
'Line one of output example',
57+
'Line two of output example',
58+
'Line three of output example'
59+
]
60+
61+
def ln = System.getProperty('line.separator')
62+
def outputFileName = 'src/main/resources/ioOutput.txt'
63+
new File(outputFileName) << "Line one of output example${ln}Line two of output example${ln}Line three of output example"
64+
def writtenLines = new File(outputFileName).collect {it}
65+
assertEquals(outputLines.size(), writtenLines.size())
66+
}
67+
68+
@Test
69+
void whenUsingBytes_thenBinaryFileCreated() {
70+
def outputFileName = 'src/main/resources/ioBinaryOutput.bin'
71+
def outputFile = new File(outputFileName)
72+
byte[] outBytes = [44, 88, 22]
73+
outputFile.bytes = outBytes
74+
assertEquals(3, new File(outputFileName).size())
75+
}
76+
77+
@Test
78+
void whenUsingWithOutputStream_thenBinaryFileCreated() {
79+
def outputFileName = 'src/main/resources/ioBinaryOutput.bin'
80+
byte[] outBytes = [44, 88, 22]
81+
new File(outputFileName).withOutputStream { stream ->
82+
stream.write(outBytes)
83+
}
84+
assertEquals(3, new File(outputFileName).size())
85+
}
86+
87+
@Test
88+
void whenUsingNewOutputStream_thenBinaryFileCreated() {
89+
def outputFileName = 'src/main/resources/ioBinaryOutput.bin'
90+
byte[] outBytes = [44, 88, 22]
91+
def os = new File(outputFileName).newOutputStream()
92+
os.write(outBytes)
93+
os.close()
94+
assertEquals(3, new File(outputFileName).size())
95+
}
96+
}

0 commit comments

Comments
 (0)