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
This commit is contained in:
parent
1c53e62641
commit
d6e58f6efd
@ -26,6 +26,12 @@
|
|||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-reflect</artifactId>
|
||||||
|
<version>${kotlin-reflect.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
@ -93,6 +99,7 @@
|
|||||||
<kotlin-maven-plugin.version>1.1.1</kotlin-maven-plugin.version>
|
<kotlin-maven-plugin.version>1.1.1</kotlin-maven-plugin.version>
|
||||||
<kotlin-test-junit.version>1.1.1</kotlin-test-junit.version>
|
<kotlin-test-junit.version>1.1.1</kotlin-test-junit.version>
|
||||||
<kotlin-stdlib.version>1.1.1</kotlin-stdlib.version>
|
<kotlin-stdlib.version>1.1.1</kotlin-stdlib.version>
|
||||||
|
<kotlin-reflect.version>1.1.1</kotlin-reflect.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
24
kotlin/src/main/java/com/baeldung/java/ArrayExample.java
Normal file
24
kotlin/src/main/java/com/baeldung/java/ArrayExample.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
24
kotlin/src/main/java/com/baeldung/java/Customer.java
Normal file
24
kotlin/src/main/java/com/baeldung/java/Customer.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
39
kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayTest.kt
Normal file
39
kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayTest.kt
Normal file
@ -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()
|
||||||
|
}
|
||||||
|
}
|
23
kotlin/src/test/kotlin/com/baeldung/kotlin/CustomerTest.kt
Normal file
23
kotlin/src/test/kotlin/com/baeldung/kotlin/CustomerTest.kt
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user