Skip to content

Commit

Permalink
Support for KTorm Entity interfaces (#87)
Browse files Browse the repository at this point in the history
Fixes #76
  • Loading branch information
mattmook committed Oct 1, 2021
1 parent 5713982 commit a25095d
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 4 deletions.
45 changes: 45 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
root = true

[*]
charset = utf-8
indent_style = space
insert_final_newline = true
max_line_length = 145
indent_size = 4
trim_trailing_whitespace = true

[*.gradle]
ij_continuation_indent_size = 8

[{*.kt, *.kts}]
ij_continuation_indent_size = 8
ij_kotlin_assignment_wrap = normal
ij_kotlin_call_parameters_new_line_after_left_paren = true
ij_kotlin_call_parameters_right_paren_on_new_line = true
ij_kotlin_call_parameters_wrap = on_every_item
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ij_kotlin_continuation_indent_for_chained_calls = false
ij_kotlin_continuation_indent_for_expression_bodies = false
ij_kotlin_continuation_indent_in_argument_lists = false
ij_kotlin_continuation_indent_in_elvis = false
ij_kotlin_continuation_indent_in_if_conditions = false
ij_kotlin_continuation_indent_in_parameter_lists = false
ij_kotlin_continuation_indent_in_supertype_lists = false
ij_kotlin_extends_list_wrap = normal
ij_kotlin_if_rparen_on_new_line = true
ij_kotlin_keep_blank_lines_before_right_brace = 0
ij_kotlin_keep_blank_lines_in_code = 1
ij_kotlin_keep_blank_lines_in_declarations = 1
ij_kotlin_method_call_chain_wrap = normal
ij_kotlin_method_parameters_new_line_after_left_paren = true
ij_kotlin_method_parameters_right_paren_on_new_line = true
ij_kotlin_method_parameters_wrap = on_every_item
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_wrap_expression_body_functions = 1

[*.md]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false
max_line_length = 80
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ language translations, unit/integration tests are welcome.

link:LICENSE.md[image:https://img.shields.io/badge/License-Apache%202.0-blue.svg[License]]

Copyright 2020 Appmattus Limited
Copyright 2021 Appmattus Limited

Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ object Versions {
const val jodaTime = "2.10.12"
const val multiplatformSettings = "0.7.2"
const val threeTen = "1.5.1"
const val kTorm = "3.4.1"

const val junit4 = "4.13.2"
const val kotest = "4.6.3"
Expand Down
3 changes: 3 additions & 0 deletions fixture/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ dependencies {
compileOnly("org.threeten:threetenbp:${Versions.threeTen}")
testImplementation("org.threeten:threetenbp:${Versions.threeTen}")

compileOnly("org.ktorm:ktorm-core:${Versions.kTorm}")
testImplementation("org.ktorm:ktorm-core:${Versions.kTorm}")

compileOnly(files("${System.getenv("ANDROID_HOME")}/platforms/android-29/android.jar"))

testImplementation("junit:junit:${Versions.junit4}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Appmattus Limited
* Copyright 2021 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,6 +46,7 @@ import com.appmattus.kotlinfixture.resolver.IterableKTypeResolver
import com.appmattus.kotlinfixture.resolver.JodaTimeResolver
import com.appmattus.kotlinfixture.resolver.KFunctionResolver
import com.appmattus.kotlinfixture.resolver.KNamedPropertyResolver
import com.appmattus.kotlinfixture.resolver.KTormResolver
import com.appmattus.kotlinfixture.resolver.KTypeResolver
import com.appmattus.kotlinfixture.resolver.LocaleResolver
import com.appmattus.kotlinfixture.resolver.MapKTypeResolver
Expand Down Expand Up @@ -132,6 +133,7 @@ data class Configuration internal constructor(
FormatResolver(),
CurrencyResolver(),
LocaleResolver(),
KTormResolver(),

ObjectResolver(),
SealedClassResolver(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2021 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.kotlinfixture.resolver

import com.appmattus.kotlinfixture.Context
import com.appmattus.kotlinfixture.Unresolved
import org.ktorm.entity.Entity
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.full.declaredMembers
import kotlin.reflect.full.isSubclassOf

internal class KTormResolver : Resolver {

override fun resolve(context: Context, obj: Any): Any {
if (hasKTorm && obj is KClass<*>) {
if (obj.isSubclassOf(Entity::class) && obj != Entity::class) {
val entity = Entity.create(obj)

obj.declaredMembers.filterIsInstance<KProperty<*>>().forEach {
entity[it.name] = context.resolve(it.returnType)
}

return entity
}
}

return Unresolved.Unhandled
}

companion object {
private val hasKTorm: Boolean by lazy {
try {
Class.forName("org.ktorm.entity.Entity", false, KTormResolver::class.java.classLoader)
true
} catch (expected: ClassNotFoundException) {
false
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Appmattus Limited
* Copyright 2021 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@ import org.junit.Assume.assumeTrue
import org.junit.experimental.runners.Enclosed
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.ktorm.entity.Entity
import java.net.URI
import java.net.URL
import java.text.DateFormat
Expand Down Expand Up @@ -529,7 +530,10 @@ class ComparisonTest {
arrayOf(typeOf<TestSealedClass>(), VALID, UNSUPPORTED, VALID, UNSUPPORTED),

// Abstract class
arrayOf(typeOf<Number>(), VALID, UNSUPPORTED, UNSUPPORTED, UNSUPPORTED)
arrayOf(typeOf<Number>(), VALID, UNSUPPORTED, UNSUPPORTED, UNSUPPORTED),

// KTorm
arrayOf(typeOf<Entity<KTorm>>(), VALID, UNSUPPORTED, IGNORE, UNSUPPORTED)
)
}
}
Expand Down Expand Up @@ -559,3 +563,7 @@ enum class TestEnumClass {
}

data class TestClass(val value: String)

interface KTorm : Entity<KTorm> {
companion object : Entity.Factory<KTorm>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2021 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.kotlinfixture.resolver

import com.appmattus.kotlinfixture.TestContext
import com.appmattus.kotlinfixture.Unresolved
import com.appmattus.kotlinfixture.assertIsRandom
import com.appmattus.kotlinfixture.config.Configuration
import com.appmattus.kotlinfixture.typeOf
import org.ktorm.entity.Entity
import kotlin.test.Test
import kotlin.test.assertTrue

class KTormResolverTest {

val context = TestContext(
Configuration(),
CompositeResolver(KTormResolver(), StringResolver(), PrimitiveResolver(), KTypeResolver())
)

@Test
fun `Unknown class returns Unresolved`() {
val result = context.resolve(Number::class)

assertTrue(result is Unresolved)
}

@Test
fun `Random nullability returned`() {
assertIsRandom {
context.resolve(typeOf<TestEntity?>()) == null
}
}

@Test
fun `Entity populated with random ids`() {
assertIsRandom {
(context.resolve(typeOf<TestEntity>()) as TestEntity).id
}
}

@Test
fun `Entity populated with random names`() {
assertIsRandom {
(context.resolve(typeOf<TestEntity>()) as TestEntity).name
}
}

@Test
fun `Entity class returns Unresolved`() {
val result = context.resolve(typeOf<Entity<TestEntity>>())

assertTrue(result is Unresolved)
}

interface TestEntity : Entity<TestEntity> {
companion object : Entity.Factory<TestEntity>()

val id: Int
var name: String
}
}

0 comments on commit a25095d

Please sign in to comment.