BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
@@ -0,0 +1,18 @@
package com.baeldung.inputstream
import java.io.InputStream
fun InputStream.readUpToChar(stopChar: Char): String {
val stringBuilder = StringBuilder()
var currentChar = this.read().toChar()
while (currentChar != stopChar) {
stringBuilder.append(currentChar)
currentChar = this.read().toChar()
if (this.available() <= 0) {
stringBuilder.append(currentChar)
break
}
}
return stringBuilder.toString()
}
@@ -0,0 +1,79 @@
package com.baeldung.console
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.Console
import java.io.InputStreamReader
import java.io.PrintStream
import java.util.*
class ConsoleIOUnitTest {
@Test
fun givenText_whenPrint_thenPrintText() {
val expectedTest = "Hello from Kotlin"
val out = ByteArrayOutputStream()
System.setOut(PrintStream(out))
print(expectedTest)
out.flush()
val printedText = String(out.toByteArray())
assertThat(printedText).isEqualTo(expectedTest)
}
@Test
fun givenInput_whenRead_thenReadText() {
val expectedTest = "Hello from Kotlin"
val input = ByteArrayInputStream(expectedTest.toByteArray())
System.setIn(input)
val readText = readLine()
assertThat(readText).isEqualTo(expectedTest)
}
@Test
fun givenInput_whenReadWithScanner_thenReadText() {
val expectedTest = "Hello from Kotlin"
val scanner = Scanner(ByteArrayInputStream(expectedTest.toByteArray()))
val readText = scanner.nextLine()
assertThat(readText).isEqualTo(expectedTest)
}
@Test
fun givenInput_whenReadWithBufferedReader_thenReadText() {
val expectedTest = "Hello from Kotlin"
val reader = BufferedReader(InputStreamReader(ByteArrayInputStream(expectedTest.toByteArray())))
val readText = reader.readLine()
assertThat(readText).isEqualTo(expectedTest)
}
@Test
fun givenInput_whenReadWithConsole_thenReadText() {
val expectedTest = "Hello from Kotlin"
val console = mock(Console::class.java)
`when`(console.readLine()).thenReturn(expectedTest)
val readText = console.readLine()
assertThat(readText).isEqualTo(expectedTest)
}
@AfterEach
fun resetIO() {
System.setOut(System.out)
System.setIn(System.`in`)
}
}
@@ -0,0 +1,72 @@
package com.baeldung.inputstream
import kotlinx.io.core.use
import org.junit.Test
import java.io.BufferedReader
import java.io.File
import kotlin.test.assertEquals
class InputStreamToStringTest {
private val fileName = "src/test/resources/inputstream2string.txt"
private val endOfLine = System.lineSeparator()
private val fileFullContent = "Computer programming can be a hassle$endOfLine" +
"It's like trying to take a defended castle"
@Test
fun whenReadFileWithBufferedReader_thenFullFileContentIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val content = inputStream.bufferedReader().use(BufferedReader::readText)
assertEquals(fileFullContent, content)
}
@Test
fun whenReadFileWithBufferedReaderReadText_thenFullFileContentIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val reader = BufferedReader(inputStream.reader())
var content: String
try {
content = reader.readText()
} finally {
reader.close()
}
assertEquals(fileFullContent, content)
}
@Test
fun whenReadFileWithBufferedReaderManually_thenFullFileContentIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val reader = BufferedReader(inputStream.reader())
val content = StringBuilder()
try {
var line = reader.readLine()
while (line != null) {
content.append(line)
line = reader.readLine()
}
} finally {
reader.close()
}
assertEquals(fileFullContent.replace(endOfLine, ""), content.toString())
}
@Test
fun whenReadFileUpToStopChar_thenPartBeforeStopCharIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val content = inputStream.use { it.readUpToChar(' ') }
assertEquals("Computer", content)
}
@Test
fun whenReadFileWithoutContainingStopChar_thenFullFileContentIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val content = inputStream.use { it.readUpToChar('-') }
assertEquals(fileFullContent, content)
}
}
@@ -0,0 +1,2 @@
Computer programming can be a hassle
It's like trying to take a defended castle
@@ -0,0 +1 @@
mock-maker-inline