Merge pull request #951 from tomekl007/BAEL-382_kotlin

BAEL-382 kotlin
This commit is contained in:
pedja4 2017-01-17 12:52:22 +01:00 committed by GitHub
commit 32ad05a43e
8 changed files with 205 additions and 0 deletions

65
kotlin/pom.xml Normal file
View File

@ -0,0 +1,65 @@
<?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</groupId>
<artifactId>kotlin</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin-stdlib.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<version>${kotlin-test-junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin-maven-plugin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<junit.version>4.12</junit.version>
<kotlin-test-junit.version>1.0.4</kotlin-test-junit.version>
<kotlin-stdlib.version>1.0.4</kotlin-stdlib.version>
<kotlin-maven-plugin.version>1.0.4</kotlin-maven-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,5 @@
package com.baeldung.kotlin
fun main(args: Array<String>){
println("hello word")
}

View File

@ -0,0 +1,14 @@
package com.baeldung.kotlin
open class Item(val id: String, val name: String = "unknown_name") {
open fun getIdOfItem(): String {
return id
}
}
class ItemWithCategory(id: String, name: String, val categoryId: String) : Item(id, name) {
override fun getIdOfItem(): String {
return id + name
}
}

View File

@ -0,0 +1,66 @@
package com.baeldung.kotlin
import java.util.*
class ItemService {
fun findItemNameForId(id: String): Item? {
val itemId = UUID.randomUUID().toString()
return Item(itemId, "name-$itemId")
}
}
class ItemManager(val categoryId: String, val dbConnection: String) {
var email = ""
constructor(categoryId: String, dbConnection: String, email: String)
: this(categoryId, dbConnection) {
this.email = email
}
fun isFromSpecificCategory(catId: String): Boolean {
return categoryId == catId
}
fun makeAnalyisOfCategory(catId: String): Unit {
val result = if (catId == "100") "Yes" else "No"
println(result)
}
}
fun main(args: Array<String>) {
val numbers = arrayOf("first", "second", "third", "fourth")
var concat = ""
for (n in numbers) {
concat += n
}
var sum = 0
for (i in 2..9) {
sum += i
}
val firstName = "Tom"
val secondName = "Mary"
val concatOfNames = "$firstName + $secondName"
println("Names: $concatOfNames")
val itemManager = ItemManager("cat_id", "db://connection")
val result = "function result: ${itemManager.isFromSpecificCategory("1")}"
println(result)
val number = 2
if (number < 10) {
println("number less that 10")
} else if (number > 10) {
println("number is greater that 10")
}
val name = "John"
when (name) {
"John" -> println("Hi man")
"Alice" -> println("Hi lady")
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.kotlin
import java.util.concurrent.ThreadLocalRandom
class ListExtension {
fun <T> List<T>.random(): T? {
if (this.isEmpty()) return null
return get(ThreadLocalRandom.current().nextInt(count()))
}
fun <T> getRandomElementOfList(list: List<T>): T? {
return list.random()
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.kotlin
import org.junit.Test
import kotlin.test.assertNotNull
class ItemServiceTest {
@Test
fun givenItemId_whenGetForOptionalItem_shouldMakeActionOnNonNullValue() {
//given
val id = "item_id"
val itemService = ItemService()
//when
val result = itemService.findItemNameForId(id)
//then
assertNotNull(result?.let { it -> it.id })
assertNotNull(result!!.id)
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.kotlin
import com.baeldung.kotlin.ListExtension
import org.junit.Test
import kotlin.test.assertTrue
class ListExtensionTest {
@Test
fun givenList_whenExecuteExtensionFunctionOnList_shouldReturnRandomElementOfList(){
//given
val elements = listOf("a", "b", "c")
//when
val result = ListExtension().getRandomElementOfList(elements)
//then
assertTrue(elements.contains(result))
}
}

View File

@ -68,6 +68,8 @@
<module>jsoup</module>
<module>junit5</module>
<module>kotlin</module>
<module>log-mdc</module>
<module>log4j</module>
<module>log4j2</module>