[BAEL-19882] - Move articles out of core-kotlin part1
This commit is contained in:
5
core-kotlin-modules/README.md
Normal file
5
core-kotlin-modules/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
## Core Kotlin
|
||||
|
||||
This module contains articles about core Kotlin.
|
||||
|
||||
|
||||
8
core-kotlin-modules/core-kotlin-annotations/README.md
Normal file
8
core-kotlin-modules/core-kotlin-annotations/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
## Core Kotlin Annotations
|
||||
|
||||
This module contains articles about core Kotlin annotations.
|
||||
|
||||
### Relevant articles:
|
||||
- [Kotlin Annotations](https://www.baeldung.com/kotlin-annotations)
|
||||
- [Guide to Kotlin @JvmField](https://www.baeldung.com/kotlin-jvm-field-annotation)
|
||||
- [Guide to JVM Platform Annotations in Kotlin](https://www.baeldung.com/kotlin-jvm-annotations)
|
||||
41
core-kotlin-modules/core-kotlin-annotations/pom.xml
Normal file
41
core-kotlin-modules/core-kotlin-annotations/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-kotlin-annotations</artifactId>
|
||||
<name>core-kotlin-annotations</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-kotlin-modules</groupId>
|
||||
<artifactId>core-kotlin-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<kotlin.version>1.3.30</kotlin.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.annotations
|
||||
|
||||
@Target(AnnotationTarget.FIELD)
|
||||
annotation class Positive
|
||||
|
||||
@Target(AnnotationTarget.FIELD)
|
||||
annotation class AllowedNames(val names: Array<String>)
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.baeldung.annotations
|
||||
|
||||
class Item(@Positive val amount: Float, @AllowedNames(["Alice", "Bob"]) val name: String)
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.annotations
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val item = Item(amount = 1.0f, name = "Bob")
|
||||
val validator = Validator()
|
||||
println("Is instance valid? ${validator.isValid(item)}")
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.annotations
|
||||
|
||||
/**
|
||||
* Naive annotation-based validator.
|
||||
* @author A.Shcherbakov
|
||||
*/
|
||||
class Validator() {
|
||||
|
||||
/**
|
||||
* Return true if every item's property annotated with @Positive is positive and if
|
||||
* every item's property annotated with @AllowedNames has a value specified in that annotation.
|
||||
*/
|
||||
fun isValid(item: Item): Boolean {
|
||||
val fields = item::class.java.declaredFields
|
||||
for (field in fields) {
|
||||
field.isAccessible = true
|
||||
for (annotation in field.annotations) {
|
||||
val value = field.get(item)
|
||||
if (field.isAnnotationPresent(Positive::class.java)) {
|
||||
val amount = value as Float
|
||||
if (amount < 0) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if (field.isAnnotationPresent(AllowedNames::class.java)) {
|
||||
val allowedNames = field.getAnnotation(AllowedNames::class.java)?.names
|
||||
val name = value as String
|
||||
allowedNames?.let {
|
||||
if (!it.contains(name)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.jvmannotations
|
||||
|
||||
import java.util.*
|
||||
|
||||
interface Document {
|
||||
|
||||
@JvmDefault
|
||||
fun getTypeDefault() = "document"
|
||||
|
||||
fun getType() = "document"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.jvmannotations;
|
||||
|
||||
public class HtmlDocument implements Document {
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "HTML";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
@file:JvmName("MessageHelper")
|
||||
@file:JvmMultifileClass //used
|
||||
package com.baeldung.jvmannotations
|
||||
|
||||
import java.util.*
|
||||
|
||||
@JvmName("getMyUsername")
|
||||
fun getMyName() : String {
|
||||
return "myUserId"
|
||||
}
|
||||
|
||||
object MessageBroker {
|
||||
@JvmStatic
|
||||
var totalMessagesSent = 0
|
||||
|
||||
const val maxMessageLength = 0
|
||||
|
||||
@JvmStatic
|
||||
fun clearAllMessages() {
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
@Throws(Exception::class)
|
||||
fun findMessages(sender : String, type : String = "text", maxResults : Int = 10) : List<Message> {
|
||||
if(sender.isEmpty()) {
|
||||
throw Exception()
|
||||
}
|
||||
return ArrayList()
|
||||
}
|
||||
}
|
||||
|
||||
class Message {
|
||||
|
||||
// this would cause a compilation error since sender is immutable
|
||||
// @set:JvmName("setSender")
|
||||
val sender = "myself"
|
||||
|
||||
// this works as name is overridden
|
||||
@JvmName("getSenderName")
|
||||
fun getSender() : String = "from:$sender"
|
||||
|
||||
@get:JvmName("getReceiverName")
|
||||
@set:JvmName("setReceiverName")
|
||||
var receiver : String = ""
|
||||
|
||||
@get:JvmName("getContent")
|
||||
@set:JvmName("setContent")
|
||||
var text = ""
|
||||
|
||||
// generates a warning
|
||||
@get:JvmName("getId")
|
||||
private val id = 0
|
||||
|
||||
@get:JvmName("hasAttachment")
|
||||
var hasAttachment = true
|
||||
|
||||
var isEncrypted = true
|
||||
|
||||
fun setReceivers(receiverNames : List<String>) {
|
||||
}
|
||||
|
||||
@JvmName("setReceiverIds")
|
||||
fun setReceivers(receiverNames : List<Int>) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("MessageHelper") //applies to all top level functions / variables / constants
|
||||
package com.baeldung.jvmannotations
|
||||
|
||||
fun convert(message: Message) {
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.jvmannotations
|
||||
|
||||
import java.util.*
|
||||
class TextDocument : Document {
|
||||
override fun getType() = "text"
|
||||
|
||||
fun transformList(list : List<Number>) : List<Number> {
|
||||
return list.filter { n -> n.toInt() > 1 }
|
||||
}
|
||||
|
||||
fun transformListInverseWildcards(list : List<@JvmSuppressWildcards Number>) : List<@JvmWildcard Number> {
|
||||
return list.filter { n -> n.toInt() > 1 }
|
||||
}
|
||||
|
||||
var list : List<@JvmWildcard Any> = ArrayList()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.jvmannotations
|
||||
|
||||
import java.util.*
|
||||
|
||||
class XmlDocument(d : Document) : Document by d
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.jvmfield
|
||||
|
||||
class JvmSample(text:String) {
|
||||
@JvmField
|
||||
val sampleText:String = text
|
||||
}
|
||||
|
||||
class CompanionSample {
|
||||
companion object {
|
||||
@JvmField val MAX_LIMIT = 20
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.annotations
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.assertFalse
|
||||
|
||||
class ValidationTest {
|
||||
|
||||
@Test
|
||||
fun whenAmountIsOneAndNameIsAlice_thenTrue() {
|
||||
assertTrue(Validator().isValid(Item(1f, "Alice")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenAmountIsOneAndNameIsBob_thenTrue() {
|
||||
assertTrue(Validator().isValid(Item(1f, "Bob")))
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun whenAmountIsMinusOneAndNameIsAlice_thenFalse() {
|
||||
assertFalse(Validator().isValid(Item(-1f, "Alice")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenAmountIsMinusOneAndNameIsBob_thenFalse() {
|
||||
assertFalse(Validator().isValid(Item(-1f, "Bob")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenAmountIsOneAndNameIsTom_thenFalse() {
|
||||
assertFalse(Validator().isValid(Item(1f, "Tom")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenAmountIsMinusOneAndNameIsTom_thenFalse() {
|
||||
assertFalse(Validator().isValid(Item(-1f, "Tom")))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.range
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
import com.baeldung.jvmannotations.*;
|
||||
|
||||
class DocumentTest {
|
||||
|
||||
@Test
|
||||
fun testDefaultMethod() {
|
||||
|
||||
val myDocument = TextDocument()
|
||||
val myTextDocument = XmlDocument(myDocument)
|
||||
|
||||
assertEquals("text", myDocument.getType())
|
||||
assertEquals("text", myTextDocument.getType())
|
||||
assertEquals("document", myTextDocument.getTypeDefault())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.jvmfield
|
||||
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class JvmSampleTest {
|
||||
|
||||
var sample = ""
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
sample = JvmSample("Hello!").sampleText
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenField_whenCheckValue_thenMatchesValue() {
|
||||
assertTrue(sample == "Hello!")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenStaticVariable_whenCheckValue_thenMatchesValue() {
|
||||
// Sample when is treated as a static variable
|
||||
assertTrue(CompanionSample.MAX_LIMIT == 20)
|
||||
}
|
||||
}
|
||||
9
core-kotlin-modules/core-kotlin-io/README.md
Normal file
9
core-kotlin-modules/core-kotlin-io/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
## Core Kotlin I/O
|
||||
|
||||
This module contains articles about core Kotlin I/O.
|
||||
|
||||
### Relevant articles:
|
||||
- [InputStream to String in Kotlin](https://www.baeldung.com/kotlin-inputstream-to-string)
|
||||
- [Console I/O in Kotlin](https://www.baeldung.com/kotlin-console-io)
|
||||
- [Reading from a File in Kotlin](https://www.baeldung.com/kotlin-read-file)
|
||||
- [Writing to a File in Kotlin](https://www.baeldung.com/kotlin-write-file)
|
||||
54
core-kotlin-modules/core-kotlin-io/pom.xml
Normal file
54
core-kotlin-modules/core-kotlin-io/pom.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-kotlin-io</artifactId>
|
||||
<name>core-kotlin-io</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-kotlin-modules</groupId>
|
||||
<artifactId>core-kotlin-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<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>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter.version>5.4.2</junit-jupiter.version>
|
||||
<mockito.version>2.27.0</mockito.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import java.io.File
|
||||
|
||||
class FileReader {
|
||||
|
||||
fun readFileLineByLineUsingForEachLine(fileName: String) = File(fileName).forEachLine { println(it) }
|
||||
|
||||
fun readFileAsLinesUsingUseLines(fileName: String): List<String> = File(fileName)
|
||||
.useLines { it.toList() }
|
||||
|
||||
fun readFileAsLinesUsingBufferedReader(fileName: String): List<String> = File(fileName).bufferedReader().readLines()
|
||||
|
||||
fun readFileAsLinesUsingReadLines(fileName: String): List<String> = File(fileName).readLines()
|
||||
|
||||
fun readFileAsTextUsingInputStream(fileName: String) =
|
||||
File(fileName).inputStream().readBytes().toString(Charsets.UTF_8)
|
||||
|
||||
fun readFileDirectlyAsText(fileName: String): String = File(fileName).readText(Charsets.UTF_8)
|
||||
|
||||
fun readFileUsingGetResource(fileName: String) = this::class.java.getResource(fileName).readText(Charsets.UTF_8)
|
||||
|
||||
fun readFileAsLinesUsingGetResourceAsStream(fileName: String) = this::class.java.getResourceAsStream(fileName).bufferedReader().readLines()
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import java.io.File
|
||||
|
||||
class FileWriter {
|
||||
|
||||
fun writeFileUsingPrintWriter(fileName: String, fileContent: String) =
|
||||
File(fileName).printWriter().use { out -> out.print(fileContent) }
|
||||
|
||||
fun writeFileUsingBufferedWriter(fileName: String, fileContent: String) =
|
||||
File(fileName).bufferedWriter().use { out -> out.write(fileContent) }
|
||||
|
||||
fun writeFileDirectly(fileName: String, fileContent: String) =
|
||||
File(fileName).writeText(fileContent)
|
||||
|
||||
fun writeFileDirectlyAsBytes(fileName: String, fileContent: String) =
|
||||
File(fileName).writeBytes(fileContent.toByteArray())
|
||||
|
||||
}
|
||||
@@ -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,67 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
internal class FileReaderTest {
|
||||
|
||||
private val fileName = "src/test/resources/Kotlin.in"
|
||||
|
||||
private val fileReader = FileReader()
|
||||
|
||||
@Test
|
||||
fun whenReadFileLineByLineUsingForEachLine_thenCorrect() {
|
||||
fileReader.readFileLineByLineUsingForEachLine(fileName)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsLinesUsingUseLines_thenCorrect() {
|
||||
val lines = fileReader.readFileAsLinesUsingUseLines(fileName)
|
||||
|
||||
assertTrue { lines.contains("1. Concise") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsLinesUsingBufferedReader_thenCorrect() {
|
||||
val lines = fileReader.readFileAsLinesUsingBufferedReader(fileName)
|
||||
|
||||
assertTrue { lines.contains("2. Safe") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsLinesUsingReadLines_thenCorrect() {
|
||||
val lines = fileReader.readFileAsLinesUsingReadLines(fileName)
|
||||
|
||||
assertTrue { lines.contains("3. Interoperable") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsTextUsingInputStream_thenCorrect() {
|
||||
val text = fileReader.readFileAsTextUsingInputStream(fileName)
|
||||
|
||||
assertTrue { text.contains("4. Tool-friendly") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadDirectlyAsText_thenCorrect() {
|
||||
val text = fileReader.readFileDirectlyAsText(fileName)
|
||||
|
||||
assertTrue { text.contains("Hello to Kotlin") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileAsTextUsingGetResource_thenCorrect() {
|
||||
val text = fileReader.readFileUsingGetResource("/Kotlin.in")
|
||||
|
||||
assertTrue { text.contains("1. Concise") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileUsingGetResourceAsStream_thenCorrect() {
|
||||
val lines = fileReader.readFileAsLinesUsingGetResourceAsStream("/Kotlin.in")
|
||||
|
||||
assertTrue { lines.contains("3. Interoperable") }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.filesystem
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
internal class FileWriterTest {
|
||||
|
||||
private val fileName = "src/test/resources/Kotlin.out"
|
||||
|
||||
private val fileContent = "Kotlin\nConcise, Safe, Interoperable, Tool-friendly"
|
||||
|
||||
private val fileWriter = FileWriter()
|
||||
|
||||
@Test
|
||||
fun whenWrittenWithPrintWriter_thenCorrect() {
|
||||
fileWriter.writeFileUsingPrintWriter(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenWrittenWithBufferedWriter_thenCorrect() {
|
||||
fileWriter.writeFileUsingBufferedWriter(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenWrittenDirectly_thenCorrect() {
|
||||
fileWriter.writeFileDirectly(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenWrittenDirectlyAsBytes_thenCorrect() {
|
||||
fileWriter.writeFileDirectlyAsBytes(fileName, fileContent)
|
||||
|
||||
assertEquals(fileContent, File(fileName).readText())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
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,5 @@
|
||||
Hello to Kotlin. Its:
|
||||
1. Concise
|
||||
2. Safe
|
||||
3. Interoperable
|
||||
4. Tool-friendly
|
||||
@@ -0,0 +1,2 @@
|
||||
Kotlin
|
||||
Concise, Safe, Interoperable, Tool-friendly
|
||||
@@ -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
|
||||
9
core-kotlin-modules/core-kotlin-strings/README.md
Normal file
9
core-kotlin-modules/core-kotlin-strings/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
## Core Kotlin Strings
|
||||
|
||||
This module contains articles about core Kotlin strings.
|
||||
|
||||
### Relevant articles:
|
||||
- [Generate a Random Alphanumeric String in Kotlin](https://www.baeldung.com/kotlin-random-alphanumeric-string)
|
||||
- [String Comparison in Kotlin](https://www.baeldung.com/kotlin-string-comparison)
|
||||
- [Concatenate Strings in Kotlin](https://www.baeldung.com/kotlin-concatenate-strings)
|
||||
- [Kotlin String Templates](https://www.baeldung.com/kotlin-string-template)
|
||||
24
core-kotlin-modules/core-kotlin-strings/pom.xml
Normal file
24
core-kotlin-modules/core-kotlin-strings/pom.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-kotlin-strings</artifactId>
|
||||
<name>core-kotlin-strings</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-kotlin-modules</groupId>
|
||||
<artifactId>core-kotlin-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.baeldung.stringtemplates
|
||||
|
||||
/**
|
||||
* Example of a useful function defined in Kotlin String class
|
||||
*/
|
||||
fun padExample(): String {
|
||||
return "Hello".padEnd(10, '!')
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a simple string template usage
|
||||
*/
|
||||
fun simpleTemplate(n: Int): String {
|
||||
val message = "n = $n"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a string template with a simple expression
|
||||
*/
|
||||
fun templateWithExpression(n: Int): String {
|
||||
val message = "n + 1 = ${n + 1}"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a string template with expression containing some logic
|
||||
*/
|
||||
fun templateWithLogic(n: Int): String {
|
||||
val message = "$n is ${if (n > 0) "positive" else "not positive"}"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of nested string templates
|
||||
*/
|
||||
fun nestedTemplates(n: Int): String {
|
||||
val message = "$n is ${if (n > 0) "positive" else if (n < 0) "negative and ${if (n % 2 == 0) "even" else "odd"}" else "zero"}"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of joining array's element into a string with a default separator
|
||||
*/
|
||||
fun templateJoinArray(): String {
|
||||
val numbers = listOf(1, 1, 2, 3, 5, 8)
|
||||
val message = "first Fibonacci numbers: ${numbers.joinToString()}"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of escaping the dollar sign
|
||||
*/
|
||||
fun notAStringTemplate(): String {
|
||||
val message = "n = \$n"
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a simple triple quoted string
|
||||
*/
|
||||
fun showFilePath(): String {
|
||||
val path = """C:\Repository\read.me"""
|
||||
return path
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a multiline string
|
||||
*/
|
||||
fun showMultiline(): String {
|
||||
val receipt = """Item 1: $1.00
|
||||
Item 2: $0.50"""
|
||||
return receipt
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a multiline string with indentation
|
||||
*/
|
||||
fun showMultilineIndent(): String {
|
||||
val receipt = """Item 1: $1.00
|
||||
>Item 2: $0.50""".trimMargin(">")
|
||||
return receipt
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a triple quoted string with a not-working escape sequence
|
||||
*/
|
||||
fun showTripleQuotedWrongEscape(): String {
|
||||
val receipt = """Item 1: $1.00\nItem 2: $0.50"""
|
||||
return receipt
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of a triple quoted string with a correctly working escape sequence
|
||||
*/
|
||||
|
||||
fun showTripleQuotedCorrectEscape(): String {
|
||||
val receipt = """Item 1: $1.00${"\n"}Item 2: $0.50"""
|
||||
return receipt
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println(padExample())
|
||||
println(simpleTemplate(10))
|
||||
println(templateWithExpression(5))
|
||||
println(templateWithLogic(7))
|
||||
println(nestedTemplates(-5))
|
||||
println(templateJoinArray())
|
||||
println(notAStringTemplate())
|
||||
println(showFilePath())
|
||||
println(showMultiline())
|
||||
println(showMultilineIndent())
|
||||
println(showTripleQuotedWrongEscape())
|
||||
println(showTripleQuotedCorrectEscape())
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.randomstring
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils
|
||||
import org.junit.Before
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.security.SecureRandom
|
||||
import java.util.concurrent.ThreadLocalRandom
|
||||
import kotlin.experimental.and
|
||||
import kotlin.streams.asSequence
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
const val STRING_LENGTH = 10
|
||||
const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+"
|
||||
|
||||
class RandomStringUnitTest {
|
||||
private val charPool : List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
|
||||
|
||||
@Test
|
||||
fun givenAStringLength_whenUsingJava_thenReturnAlphanumericString() {
|
||||
var randomString = ThreadLocalRandom.current()
|
||||
.ints(STRING_LENGTH.toLong(), 0, charPool.size)
|
||||
.asSequence()
|
||||
.map(charPool::get)
|
||||
.joinToString("")
|
||||
|
||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||
assertEquals(STRING_LENGTH, randomString.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenAStringLength_whenUsingKotlin_thenReturnAlphanumericString() {
|
||||
var randomString = (1..STRING_LENGTH).map { i -> kotlin.random.Random.nextInt(0, charPool.size) }
|
||||
.map(charPool::get)
|
||||
.joinToString("")
|
||||
|
||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||
assertEquals(STRING_LENGTH, randomString.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenAStringLength_whenUsingApacheCommon_thenReturnAlphanumericString() {
|
||||
var randomString = RandomStringUtils.randomAlphanumeric(STRING_LENGTH)
|
||||
|
||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||
assertEquals(STRING_LENGTH, randomString.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenAStringLength_whenUsingRandomForBytes_thenReturnAlphanumericString() {
|
||||
val random = SecureRandom()
|
||||
val bytes = ByteArray(STRING_LENGTH)
|
||||
random.nextBytes(bytes)
|
||||
|
||||
var randomString = (0..bytes.size - 1).map { i ->
|
||||
charPool.get((bytes[i] and 0xFF.toByte() and (charPool.size-1).toByte()).toInt())
|
||||
}.joinToString("")
|
||||
|
||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||
assertEquals(STRING_LENGTH, randomString.length)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.stringcomparison
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class StringComparisonUnitTest {
|
||||
|
||||
@Test
|
||||
fun `compare using equals operator`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val firstCapitalized = "KOTLIN"
|
||||
assertTrue { first == second }
|
||||
assertFalse { first == firstCapitalized }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compare using referential equals operator`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val copyOfFirst = buildString { "kotlin" }
|
||||
assertTrue { first === second }
|
||||
assertFalse { first === copyOfFirst }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compare using equals method`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val firstCapitalized = "KOTLIN"
|
||||
assertTrue { first.equals(second) }
|
||||
assertFalse { first.equals(firstCapitalized) }
|
||||
assertTrue { first.equals(firstCapitalized, true) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compare using compare method`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val firstCapitalized = "KOTLIN"
|
||||
assertTrue { first.compareTo(second) == 0 }
|
||||
assertTrue { first.compareTo(firstCapitalized) == 32 }
|
||||
assertTrue { firstCapitalized.compareTo(first) == -32 }
|
||||
assertTrue { first.compareTo(firstCapitalized, true) == 0 }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.stringconcatenation
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class StringConcatenationTest {
|
||||
|
||||
@Test
|
||||
fun givenTwoStrings_concatenateWithTemplates_thenEquals() {
|
||||
val a = "Hello"
|
||||
val b = "Baeldung"
|
||||
val c = "$a $b"
|
||||
|
||||
assertEquals("Hello Baeldung", c)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenTwoStrings_concatenateWithPlusOperator_thenEquals() {
|
||||
val a = "Hello"
|
||||
val b = "Baeldung"
|
||||
val c = a + " " + b
|
||||
|
||||
assertEquals("Hello Baeldung", c)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenTwoStrings_concatenateWithStringBuilder_thenEquals() {
|
||||
val a = "Hello"
|
||||
val b = "Baeldung"
|
||||
|
||||
val builder = StringBuilder()
|
||||
builder.append(a).append(" ").append(b)
|
||||
|
||||
val c = builder.toString()
|
||||
|
||||
assertEquals("Hello Baeldung", c)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenTwoStrings_concatenateWithPlusMethod_thenEquals() {
|
||||
val a = "Hello"
|
||||
val b = "Baeldung"
|
||||
val c = a.plus(" ").plus(b)
|
||||
|
||||
assertEquals("Hello Baeldung", c)
|
||||
}
|
||||
|
||||
}
|
||||
56
core-kotlin-modules/pom.xml
Normal file
56
core-kotlin-modules/pom.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.core-kotlin-modules</groupId>
|
||||
<artifactId>core-kotlin-modules</artifactId>
|
||||
<name>core-kotlin-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-kotlin</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../parent-kotlin</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>core-kotlin-annotations</module>
|
||||
<module>core-kotlin-io</module>
|
||||
<module>core-kotlin-strings</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>test-compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<jvmTarget>1.8</jvmTarget>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<kotlin.version>1.3.30</kotlin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user