BAEL-2655 Sample code (#6731)
* Upgraded dependencies * Fixed test in Linux Environment * Added I/O samples * FIX End of line reference, previous version was OS dependent * CHORE Added junit5 dependency * UPGRADE gradle version, UPDATE versions * Deleted emtpy lines
This commit is contained in:
parent
062ea56d7a
commit
f6b1967cbe
|
@ -5,7 +5,7 @@ version '1.0-SNAPSHOT'
|
||||||
|
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
ext.kotlin_version = '1.2.41'
|
ext.kotlin_version = '1.3.30'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
@ -26,8 +26,6 @@ sourceCompatibility = 1.8
|
||||||
compileKotlin { kotlinOptions.jvmTarget = "1.8" }
|
compileKotlin { kotlinOptions.jvmTarget = "1.8" }
|
||||||
compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
|
compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
|
||||||
|
|
||||||
kotlin { experimental { coroutines "enable" } }
|
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
jcenter()
|
jcenter()
|
||||||
|
@ -39,10 +37,22 @@ sourceSets {
|
||||||
srcDirs 'com/baeldung/ktor'
|
srcDirs 'com/baeldung/ktor'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
testLogging {
|
||||||
|
events "passed", "skipped", "failed"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile "ch.qos.logback:logback-classic:1.2.1"
|
implementation "ch.qos.logback:logback-classic:1.2.1"
|
||||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}"
|
||||||
|
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
|
||||||
|
testImplementation 'junit:junit:4.12'
|
||||||
|
testImplementation 'org.assertj:assertj-core:3.12.2'
|
||||||
|
testImplementation 'org.mockito:mockito-core:2.27.0'
|
||||||
|
testImplementation "org.jetbrains.kotlin:kotlin-test:${kotlin_version}"
|
||||||
|
testImplementation "org.jetbrains.kotlin:kotlin-test-junit5:${kotlin_version}"
|
||||||
}
|
}
|
Binary file not shown.
|
@ -1,5 +1,5 @@
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
|
|
||||||
|
|
|
@ -1,5 +1,21 @@
|
||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright 2015 the original author or authors.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
#
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
##
|
##
|
||||||
## Gradle start up script for UN*X
|
## Gradle start up script for UN*X
|
||||||
|
@ -28,7 +44,7 @@ APP_NAME="Gradle"
|
||||||
APP_BASE_NAME=`basename "$0"`
|
APP_BASE_NAME=`basename "$0"`
|
||||||
|
|
||||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
DEFAULT_JVM_OPTS=""
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
MAX_FD="maximum"
|
MAX_FD="maximum"
|
||||||
|
|
|
@ -1,3 +1,19 @@
|
||||||
|
@rem
|
||||||
|
@rem Copyright 2015 the original author or authors.
|
||||||
|
@rem
|
||||||
|
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@rem you may not use this file except in compliance with the License.
|
||||||
|
@rem You may obtain a copy of the License at
|
||||||
|
@rem
|
||||||
|
@rem http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
@rem
|
||||||
|
@rem Unless required by applicable law or agreed to in writing, software
|
||||||
|
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@rem See the License for the specific language governing permissions and
|
||||||
|
@rem limitations under the License.
|
||||||
|
@rem
|
||||||
|
|
||||||
@if "%DEBUG%" == "" @echo off
|
@if "%DEBUG%" == "" @echo off
|
||||||
@rem ##########################################################################
|
@rem ##########################################################################
|
||||||
@rem
|
@rem
|
||||||
|
@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
|
||||||
set APP_HOME=%DIRNAME%
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
set DEFAULT_JVM_OPTS=
|
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||||
|
|
||||||
@rem Find java.exe
|
@rem Find java.exe
|
||||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||||
|
|
|
@ -20,9 +20,21 @@
|
||||||
<version>${kotlin.version}</version>
|
<version>${kotlin.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.platform</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-platform-runner</artifactId>
|
<artifactId>junit-jupiter</artifactId>
|
||||||
<version>${junit.platform.version}</version>
|
<version>${junit.jupiter.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>${mockito.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.bytebuddy</groupId>
|
||||||
|
<artifactId>byte-buddy</artifactId>
|
||||||
|
<version>${byte-buddy.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -37,6 +49,12 @@
|
||||||
<version>${kotlin.version}</version>
|
<version>${kotlin.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-test-junit5</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -69,9 +87,10 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<kotlin.version>1.2.71</kotlin.version>
|
<kotlin.version>1.3.30</kotlin.version>
|
||||||
<junit.platform.version>1.1.1</junit.platform.version>
|
<junit.jupiter.version>5.4.2</junit.jupiter.version>
|
||||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
<mockito.version>2.27.0</mockito.version>
|
||||||
|
<byte-buddy.version>1.9.12</byte-buddy.version>
|
||||||
<assertj.version>3.10.0</assertj.version>
|
<assertj.version>3.10.0</assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -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 @@
|
||||||
|
mock-maker-inline
|
|
@ -8,7 +8,8 @@ import kotlin.test.assertEquals
|
||||||
|
|
||||||
class InputStreamToStringTest {
|
class InputStreamToStringTest {
|
||||||
private val fileName = "src/test/resources/inputstream2string.txt"
|
private val fileName = "src/test/resources/inputstream2string.txt"
|
||||||
private val fileFullContent = "Computer programming can be a hassle\r\n" +
|
private val endOfLine = System.lineSeparator()
|
||||||
|
private val fileFullContent = "Computer programming can be a hassle$endOfLine" +
|
||||||
"It's like trying to take a defended castle"
|
"It's like trying to take a defended castle"
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -46,7 +47,7 @@ class InputStreamToStringTest {
|
||||||
} finally {
|
} finally {
|
||||||
reader.close()
|
reader.close()
|
||||||
}
|
}
|
||||||
assertEquals(fileFullContent.replace("\r\n", ""), content.toString())
|
assertEquals(fileFullContent.replace(endOfLine, ""), content.toString())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -202,10 +202,10 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<kotlin.version>1.3.10</kotlin.version>
|
<kotlin.version>1.3.30</kotlin.version>
|
||||||
<kotlinx.version>1.0.0</kotlinx.version>
|
<kotlinx.version>1.0.0</kotlinx.version>
|
||||||
<ktor.io.version>0.9.5</ktor.io.version>
|
<ktor.io.version>0.9.5</ktor.io.version>
|
||||||
<assertj.version>3.11.0</assertj.version>
|
<assertj.version>3.12.0</assertj.version>
|
||||||
<junit.platform.version>1.2.0</junit.platform.version>
|
<junit.platform.version>1.3.2</junit.platform.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
|
Loading…
Reference in New Issue