From d6e58f6efd01ed4816565892893c401e0fc51138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Walter=20G=C3=B3mez?= Date: Tue, 25 Apr 2017 01:50:02 -0600 Subject: [PATCH] Feature/kotlin interop (#1709) * Add project for hibernate immutable article Add Event entity Add hibernate configuration file Add hibernateutil for configuration Add test to match snippets from article * Update master * Add Kotlin Java Interoperability Add kotlin-reflect dependency * Fix test for customer Remove unused imports --- kotlin/pom.xml | 7 ++++ .../java/com/baeldung/java/ArrayExample.java | 24 ++++++++++++ .../main/java/com/baeldung/java/Customer.java | 24 ++++++++++++ .../kotlin/com/baeldung/kotlin/ArrayTest.kt | 39 +++++++++++++++++++ .../com/baeldung/kotlin/CustomerTest.kt | 23 +++++++++++ 5 files changed, 117 insertions(+) create mode 100644 kotlin/src/main/java/com/baeldung/java/ArrayExample.java create mode 100644 kotlin/src/main/java/com/baeldung/java/Customer.java create mode 100644 kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayTest.kt create mode 100644 kotlin/src/test/kotlin/com/baeldung/kotlin/CustomerTest.kt diff --git a/kotlin/pom.xml b/kotlin/pom.xml index 07fb6863d4..89d9e2b292 100644 --- a/kotlin/pom.xml +++ b/kotlin/pom.xml @@ -26,6 +26,12 @@ ${junit.version} test + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin-reflect.version} + test + @@ -93,6 +99,7 @@ 1.1.1 1.1.1 1.1.1 + 1.1.1 \ No newline at end of file diff --git a/kotlin/src/main/java/com/baeldung/java/ArrayExample.java b/kotlin/src/main/java/com/baeldung/java/ArrayExample.java new file mode 100644 index 0000000000..ef91db517b --- /dev/null +++ b/kotlin/src/main/java/com/baeldung/java/ArrayExample.java @@ -0,0 +1,24 @@ +package com.baeldung.java; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class ArrayExample { + + public int sumValues(int[] nums) { + int res = 0; + + for (int x:nums) { + res += x; + } + + return res; + } + + public void writeList() throws IOException { + File file = new File("E://file.txt"); + FileReader fr = new FileReader(file); + fr.close(); + } +} diff --git a/kotlin/src/main/java/com/baeldung/java/Customer.java b/kotlin/src/main/java/com/baeldung/java/Customer.java new file mode 100644 index 0000000000..0156bf7b44 --- /dev/null +++ b/kotlin/src/main/java/com/baeldung/java/Customer.java @@ -0,0 +1,24 @@ +package com.baeldung.java; + +public class Customer { + + private String firstName; + private String lastName; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + +} diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayTest.kt new file mode 100644 index 0000000000..f7d1c53b13 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayTest.kt @@ -0,0 +1,39 @@ +package com.baeldung.kotlin + +import com.baeldung.java.ArrayExample +import com.baeldung.java.Customer +import org.junit.Test +import kotlin.test.assertEquals + +class ArrayTest { + + @Test + fun givenArray_whenValidateArrayType_thenComplete () { + val ex = ArrayExample() + val numArray = intArrayOf(1, 2, 3) + + assertEquals(ex.sumValues(numArray), 6) + } + + @Test + fun givenCustomer_whenGetSuperType_thenComplete() { + val instance = Customer::class + val supertypes = instance.supertypes + + assertEquals(supertypes[0].toString(), "kotlin.Any") + } + + @Test + fun givenCustomer_whenGetConstructor_thenComplete() { + val instance = Customer::class.java + val constructors = instance.constructors + + assertEquals(constructors.size, 1) + assertEquals(constructors[0].name, "com.baeldung.java.Customer") + } + + fun makeReadFile() { + val ax = ArrayExample() + ax.writeList() + } +} \ No newline at end of file diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/CustomerTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/CustomerTest.kt new file mode 100644 index 0000000000..6395dfcfed --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/CustomerTest.kt @@ -0,0 +1,23 @@ +package com.baeldung.kotlin + +import com.baeldung.java.Customer +import org.junit.Test +import kotlin.test.assertEquals + +class CustomerTest { + + @Test + fun givenCustomer_whenNameAndLastNameAreAssigned_thenComplete() { + val customer = Customer() + + // Setter method is being called + customer.firstName = "Frodo" + customer.lastName = "Baggins" + + // Getter method is being called + assertEquals(customer.firstName, "Frodo") + assertEquals(customer.lastName, "Baggins") + } + +} +