* BAEL-1704: Non-Trivial Work in Kotlin vs Java

* BAEL-1704: Non-Trivial Work in Kotlin vs Java

Renaming one test class

* BAEL-1704: Kotlin vs Java

* BAEL-1704: Kotlin vs Java

The files moved to the guest branch.
This commit is contained in:
Patryk
2018-08-17 15:00:08 +02:00
committed by KevinGilmore
parent 86aaf6f81a
commit 061d1191f1
13 changed files with 214 additions and 1 deletions

14
guest/core-kotlin/.gitignore vendored Normal file
View File

@@ -0,0 +1,14 @@
/bin/
#ignore gradle
.gradle/
#ignore build and generated files
build/
node/
out/
#ignore installed node modules and package lock file
node_modules/
package-lock.json

199
guest/core-kotlin/pom.xml Normal file
View File

@@ -0,0 +1,199 @@
<?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</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>com.stackify</groupId>
<packaging>jar</packaging>
<repositories>
<repository>
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin-stdlib.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</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>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin-reflect.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.spek</groupId>
<artifactId>spek-api</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.spek</groupId>
<artifactId>spek-subject-extension</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.spek</groupId>
<artifactId>spek-junit-platform-engine</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.nhaarman</groupId>
<artifactId>mockito-kotlin</artifactId>
<version>${mockito-kotlin.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.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin-maven-plugin.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin-maven-plugin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
<executions>
<!-- Replacing default-compile as it is treated specially
by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially
by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>junit5</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*Test5.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven-failsafe-plugin.version>2.22.0</maven-failsafe-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin-maven-plugin.version>1.2.60</kotlin-maven-plugin.version>
<kotlin-test-junit.version>1.2.51</kotlin-test-junit.version>
<kotlin-stdlib.version>1.2.51</kotlin-stdlib.version>
<kotlin-reflect.version>1.2.51</kotlin-reflect.version>
<kotlinx.version>0.22.5</kotlinx.version>
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
<commons-math3.version>3.6.1</commons-math3.version>
<junit.platform.version>1.0.0</junit.platform.version>
<junit.vintage.version>5.2.0</junit.vintage.version>
<assertj.version>3.10.0</assertj.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
</properties>
</project>

View File

@@ -0,0 +1,21 @@
package com.baeldung.kotlinvsjava
import org.junit.Test
import kotlin.test.assertEquals
class CompanionObjectTest {
@Test
fun givenAClassWithCompanionObject_whenCallingMethodTheSameAsStaticOne_thenWeGetAResult() {
assertEquals("A", A.returnClassName())
}
}
class A {
companion object {
fun returnClassName(): String {
return "A"
}
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.kotlinvsjava
import org.junit.Test
import kotlin.test.assertEquals
class ConstructorTests {
@Test
fun givenAClassWithPrimaryConstructor_whenCreatingAnInstance_thenWeGetObject() {
var example = Example(1, "Example")
assertEquals(1, example.id)
assertEquals("Example", example.name)
}
}
class Example constructor(val id: Int, var name: String)

View File

@@ -0,0 +1,33 @@
package com.baeldung.kotlinvsjava
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
class DataClassTest {
@Test
fun givenASampleDataClass_whenCallingToStringMethod_thenItReturnsAllProperties() {
val student = Student(1, "John", "Smith")
assertEquals(1, student.id)
assertEquals("John", student.name)
assertEquals("Smith", student.lastName)
assertEquals("Student(id=1, name=John, lastName=Smith)", student.toString())
}
@Test
fun givenASampleDataClass_whenCreatingACopyWithGeneratedFunction_thenItReturnsACopyWithRequestedChanges() {
val student = Student(1, "John", "Smith")
val student2 = student.copy(id = 2, name = "Anne")
assertEquals(2, student2.id)
assertEquals("Anne", student2.name)
assertEquals("Smith", student2.lastName)
assertEquals("Student(id=2, name=Anne, lastName=Smith)", student2.toString())
assertFalse(student.equals(student2))
}
}
data class Student(val id: Int, val name: String, val lastName: String)

View File

@@ -0,0 +1,27 @@
package com.baeldung.kotlinvsjava
import org.junit.Test
import kotlin.test.assertEquals
class DelegationTest {
@Test
fun givenAClassWithDelegation_whenCallDelegatedMethod_thenWeGetAResultDefinedInPassedObject() {
val car = Car(V6Engine())
assertEquals("Vroom", car.makeSound())
}
}
interface Engine {
fun makeSound(): String
}
class V6Engine: Engine {
override fun makeSound(): String {
return "Vroom"
}
}
class Car(e: Engine) : Engine by e

View File

@@ -0,0 +1,34 @@
package com.baeldung.kotlinvsjava
import java.io.IOException
import kotlin.test.Test
import kotlin.test.assertEquals
class ExceptionsTest {
@Test
fun givenATryExpression_whenReturning5InLastExpressionOfTryBlock_thenWeGet5() {
val value: Int = try { 5 } catch (e: IOException) { 6 }
assertEquals(5, value)
}
@Test
fun givenATryExpression_whenReturning6InLastExpressionOfCatchBlock_thenWeGet6() {
val value: Int = try { funThrowingException() } catch (e: IOException) { 6 }
assertEquals(6, value)
}
@org.junit.Test(expected = IllegalArgumentException::class)
fun givenANullString_whenUsingElvisOperator_thenExceptionIsThrown() {
val sampleString: String? = null
sampleString?.length ?: throw IllegalArgumentException("String must not be null")
}
private fun funThrowingException(): Nothing {
throw IOException()
}
}

View File

@@ -0,0 +1,30 @@
package com.baeldung.kotlinvsjava
import org.junit.Test
import kotlin.test.assertEquals
class ExtensionFunctionsTest {
@Test
fun givenAStringWithAnExtensionFunction_whenCallingThatFunction_thenItConcatenatesStrings() {
val sampleString = "ABC"
val concatenatedString = sampleString.appendString("DEF")
assertEquals("ABCDEF", concatenatedString)
}
@Test
fun givenAStringWithAnExtensionProperty_whenReadingProperty_thenItReturnsLengthOfString() {
val sampleString = "ABC"
assertEquals(3, sampleString.size)
}
fun String.appendString(str : String): String {
return plus(str)
}
val String.size: Int
get() = length
}

View File

@@ -0,0 +1,70 @@
package com.baeldung.kotlinvsjava
import org.junit.Test
import kotlin.test.assertEquals
class FunctionsTest {
@Test
fun givenALambdaExpressionConcatenatingString_whenUsingTheFunctionWithAAndBString_thenWeGetAB() {
val concat: (String, String) -> String = { a, b -> a + b }
assertEquals("AB", concat("A","B"))
}
@Test
fun givenAnAnonymousFunctionConcatenatingString_whenUsingTheFunctionWithAAndBString_thenWeGetAB() {
val concat: (String, String) -> String = fun(a: String, b: String): String { return a + b }
assertEquals("AB", concat("A","B"))
}
@Test
fun givenAnPlusMethodOfString_whenUsingTheFunctionWithAAndBString_thenWeGetAB() {
val concat = String::plus
assertEquals("AB", concat("A","B"))
}
@Test
fun givenAStringConstractorAssignedToFunction_whenUsingFunctionReference_thenWeGetNewString() {
val concat = ::String
assertEquals("A", concat().plus("A"))
}
@Test
fun givenAClassImplementingAFunctionType_whenUsingTheFunctionWithAAndBString_thenWeGetAB() {
val concat = StringConcatenation()
assertEquals("AB", concat("A", "B"))
}
@Test
fun givenALambdaExpressionWithReceiver_whenUsingTheFunctionWithReceiver_thenWeGetABC() {
val concat: String.(String, String) -> String = { a, b -> plus(a).plus(b) }
assertEquals("ABC", "A".concat("B", "C"))
}
@Test
fun givenALambdaExpressionWithinLambdaExpression_whenUsingTheFunction_thenWeGetAB() {
val concat: (String) -> ((String) -> String) = { a -> {b -> a + b} }
assertEquals("AB", (concat("A")("B")))
}
@Test
fun given3NestedLambdaExpression_whenUsingTheFunction_thenWeGetABC() {
val concat: (String) -> (String) -> (String) -> String = { a -> {b -> { c -> a + b + c} } }
assertEquals("ABC", concat("A")("B")("C"))
}
}
class StringConcatenation: (String, String) -> String {
override fun invoke(p1: String, p2: String): String {
return p1 + p2
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.kotlinvsjava
import org.junit.Test
import kotlin.math.absoluteValue
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class IsOperatorTest {
@Test
fun givenSampleValue_whenUsingIsOperatorInIfStatement_thenItCastsAutomaticallyToString() {
val value: Any = "string"
if(value is String) {
assertEquals(6, value.length)
}
}
@Test
fun givenSampleValue_whenUsingIsOperatorWithAndOperator_thenItCastsAutomaticallyToString() {
val value: Any = "string"
assertTrue(value is String && value.length == 6)
}
@Test
fun givenSampleValue_whenUsingWithWhenOperator_thenItCastsAutomaticallyToString() {
val value: Any = "string"
when(value) {
is String -> assertEquals(6, value.length)
is Int -> assertEquals(6, value.absoluteValue)
}
}
}

View File

@@ -0,0 +1,67 @@
package com.baeldung.kotlinvsjava
import kotlin.test.Test
import java.lang.NullPointerException
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
class NullSafetyTest {
@Test
fun givenStringAndNull_whenUsingSafeCallOperatorWithLengthMethod_thenReturnsLengthForStringAndNullForNull() {
val stringValue: String? = "string"
val nullValue: String? = null
assertNotNull(stringValue?.length)
assertNull(nullValue?.length)
}
@Test(expected = NullPointerException::class)
fun givenNullReference_whenUsingTheNotNullAssertionOperator_thenItThrowsNullPointerException() {
val stringValue: String? = "string"
val nullValue: String? = null
assertNotNull(stringValue!!.length)
nullValue!!.length
}
@Test
fun givenStringAndNull_whenUsingElvisOperator_thenItTestsAgainstNullAndReturnsTheProperValue() {
val stringValue: String? = "string"
val nullValue: String? = null
val shouldBeLength: Int = stringValue?.length ?: -1
val souldBeMinusOne: Int = nullValue?.length ?: -1
assertEquals(6, shouldBeLength)
assertEquals(-1, souldBeMinusOne)
}
@Test
fun givenString_whenCastingToInt_thenItReturnsNull() {
val stringValue: String? = "string"
val intValue: Int? = stringValue as? Int
assertNull(intValue)
}
@Test
fun givenCollectionWithNulls_whenFilterNonNull_thenItReturnsCollectionWithoutNulls() {
val list: List<String?> = listOf("a", "b", null)
val nonNullList = list.filterNotNull()
assertEquals(2, nonNullList.size)
assertEquals(nonNullList, listOf("a", "b"))
}
@Test
fun givenCollectionWithNulls_whenLetWithSafeCallOperator_thenItOmitsNulls() {
val list: List<String?> = listOf("a", "b", null)
for(elem in list) {
elem?.let { assertNotNull(it) }
}
}
}

View File

@@ -0,0 +1,61 @@
package com.baeldung.kotlinvsjava
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class OperatorsOverloadingTest {
@Test
fun givenThePlaneClassWithOverloadedIncrementationOperator_whenCallingTheOperator_thenItIncreasesSpeed(){
var plane = Plane(0.0)
plane++
assertEquals(50.0, plane.currentSpeed)
}
@Test
fun givenThePlaneClassWithOverloadedMinusOperator_whenCallingTheOperator_thenItDecreaseSpeed(){
var plane = Plane(1000.0)
plane - 500.0
assertEquals(500.0, plane.currentSpeed)
}
@Test
fun givenThePlaneClassWithOverloadedInvokeOperator_whenCallingTheOperator_thenItSetSpeed(){
var plane = Plane(0.0)
plane(150.0)
assertEquals(150.0, plane.currentSpeed)
}
@Test
fun given2PlaneObjectWithOverloadedComparisonOperator_whenCallingTheOperator_thenItComparesSpeedValues(){
var plane = Plane(0.0)
var plane2 = Plane(150.0)
assertTrue(plane < (plane2))
}
}
class Plane(var currentSpeed: Double) {
operator fun inc(): Plane {
currentSpeed += 50.0
return this
}
operator fun minus(number: Double) {
currentSpeed = if(currentSpeed < number) 0.0 else currentSpeed - number
}
operator fun invoke(speed: Double) {
currentSpeed = speed
}
operator fun compareTo(plane: Plane): Int {
return currentSpeed.compareTo(plane.currentSpeed)
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.kotlinvsjava
import org.junit.Test
import java.math.BigDecimal
import kotlin.test.assertEquals
class PropertiesTest {
@Test
fun givenASampleClassWithValAndVarProperties_whenSettingPrice_thenWeGetZeroOrOne() {
val product = Product()
product.price = BigDecimal(10)
val product2 = Product()
product2.price = null
assertEquals("empty", product.id)
assertEquals("empty", product2.id)
assertEquals(BigDecimal(10), product.price)
assertEquals(BigDecimal(1), product2.price)
}
}
class Product {
val id: String? = "empty"
var price: BigDecimal? = BigDecimal.ZERO
set(value) = if(value == null) { field = BigDecimal.ONE} else { field = value }
}