Skip to content

Commit a25095d

Browse files
authored
Support for KTorm Entity interfaces (#87)
Fixes #76
1 parent 5713982 commit a25095d

File tree

8 files changed

+194
-4
lines changed

8 files changed

+194
-4
lines changed

.editorconfig

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
insert_final_newline = true
7+
max_line_length = 145
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.gradle]
12+
ij_continuation_indent_size = 8
13+
14+
[{*.kt, *.kts}]
15+
ij_continuation_indent_size = 8
16+
ij_kotlin_assignment_wrap = normal
17+
ij_kotlin_call_parameters_new_line_after_left_paren = true
18+
ij_kotlin_call_parameters_right_paren_on_new_line = true
19+
ij_kotlin_call_parameters_wrap = on_every_item
20+
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
21+
ij_kotlin_continuation_indent_for_chained_calls = false
22+
ij_kotlin_continuation_indent_for_expression_bodies = false
23+
ij_kotlin_continuation_indent_in_argument_lists = false
24+
ij_kotlin_continuation_indent_in_elvis = false
25+
ij_kotlin_continuation_indent_in_if_conditions = false
26+
ij_kotlin_continuation_indent_in_parameter_lists = false
27+
ij_kotlin_continuation_indent_in_supertype_lists = false
28+
ij_kotlin_extends_list_wrap = normal
29+
ij_kotlin_if_rparen_on_new_line = true
30+
ij_kotlin_keep_blank_lines_before_right_brace = 0
31+
ij_kotlin_keep_blank_lines_in_code = 1
32+
ij_kotlin_keep_blank_lines_in_declarations = 1
33+
ij_kotlin_method_call_chain_wrap = normal
34+
ij_kotlin_method_parameters_new_line_after_left_paren = true
35+
ij_kotlin_method_parameters_right_paren_on_new_line = true
36+
ij_kotlin_method_parameters_wrap = on_every_item
37+
ij_kotlin_name_count_to_use_star_import = 2147483647
38+
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
39+
ij_kotlin_wrap_expression_body_functions = 1
40+
41+
[*.md]
42+
indent_style = space
43+
indent_size = 4
44+
trim_trailing_whitespace = false
45+
max_line_length = 80

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ language translations, unit/integration tests are welcome.
132132

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

135-
Copyright 2020 Appmattus Limited
135+
Copyright 2021 Appmattus Limited
136136

137137
Licensed under the Apache License, Version 2.0 (the "License"); you may
138138
not use this file except in compliance with the License. You may obtain

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ object Versions {
3333
const val jodaTime = "2.10.12"
3434
const val multiplatformSettings = "0.7.2"
3535
const val threeTen = "1.5.1"
36+
const val kTorm = "3.4.1"
3637

3738
const val junit4 = "4.13.2"
3839
const val kotest = "4.6.3"

fixture/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ dependencies {
3636
compileOnly("org.threeten:threetenbp:${Versions.threeTen}")
3737
testImplementation("org.threeten:threetenbp:${Versions.threeTen}")
3838

39+
compileOnly("org.ktorm:ktorm-core:${Versions.kTorm}")
40+
testImplementation("org.ktorm:ktorm-core:${Versions.kTorm}")
41+
3942
compileOnly(files("${System.getenv("ANDROID_HOME")}/platforms/android-29/android.jar"))
4043

4144
testImplementation("junit:junit:${Versions.junit4}")

fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/Configuration.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Appmattus Limited
2+
* Copyright 2021 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ import com.appmattus.kotlinfixture.resolver.IterableKTypeResolver
4646
import com.appmattus.kotlinfixture.resolver.JodaTimeResolver
4747
import com.appmattus.kotlinfixture.resolver.KFunctionResolver
4848
import com.appmattus.kotlinfixture.resolver.KNamedPropertyResolver
49+
import com.appmattus.kotlinfixture.resolver.KTormResolver
4950
import com.appmattus.kotlinfixture.resolver.KTypeResolver
5051
import com.appmattus.kotlinfixture.resolver.LocaleResolver
5152
import com.appmattus.kotlinfixture.resolver.MapKTypeResolver
@@ -132,6 +133,7 @@ data class Configuration internal constructor(
132133
FormatResolver(),
133134
CurrencyResolver(),
134135
LocaleResolver(),
136+
KTormResolver(),
135137

136138
ObjectResolver(),
137139
SealedClassResolver(),
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2021 Appmattus Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.appmattus.kotlinfixture.resolver
18+
19+
import com.appmattus.kotlinfixture.Context
20+
import com.appmattus.kotlinfixture.Unresolved
21+
import org.ktorm.entity.Entity
22+
import kotlin.reflect.KClass
23+
import kotlin.reflect.KProperty
24+
import kotlin.reflect.full.declaredMembers
25+
import kotlin.reflect.full.isSubclassOf
26+
27+
internal class KTormResolver : Resolver {
28+
29+
override fun resolve(context: Context, obj: Any): Any {
30+
if (hasKTorm && obj is KClass<*>) {
31+
if (obj.isSubclassOf(Entity::class) && obj != Entity::class) {
32+
val entity = Entity.create(obj)
33+
34+
obj.declaredMembers.filterIsInstance<KProperty<*>>().forEach {
35+
entity[it.name] = context.resolve(it.returnType)
36+
}
37+
38+
return entity
39+
}
40+
}
41+
42+
return Unresolved.Unhandled
43+
}
44+
45+
companion object {
46+
private val hasKTorm: Boolean by lazy {
47+
try {
48+
Class.forName("org.ktorm.entity.Entity", false, KTormResolver::class.java.classLoader)
49+
true
50+
} catch (expected: ClassNotFoundException) {
51+
false
52+
}
53+
}
54+
}
55+
}

fixture/src/test/kotlin/com/appmattus/kotlinfixture/ComparisonTest.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Appmattus Limited
2+
* Copyright 2021 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ import org.junit.Assume.assumeTrue
2929
import org.junit.experimental.runners.Enclosed
3030
import org.junit.runner.RunWith
3131
import org.junit.runners.Parameterized
32+
import org.ktorm.entity.Entity
3233
import java.net.URI
3334
import java.net.URL
3435
import java.text.DateFormat
@@ -529,7 +530,10 @@ class ComparisonTest {
529530
arrayOf(typeOf<TestSealedClass>(), VALID, UNSUPPORTED, VALID, UNSUPPORTED),
530531

531532
// Abstract class
532-
arrayOf(typeOf<Number>(), VALID, UNSUPPORTED, UNSUPPORTED, UNSUPPORTED)
533+
arrayOf(typeOf<Number>(), VALID, UNSUPPORTED, UNSUPPORTED, UNSUPPORTED),
534+
535+
// KTorm
536+
arrayOf(typeOf<Entity<KTorm>>(), VALID, UNSUPPORTED, IGNORE, UNSUPPORTED)
533537
)
534538
}
535539
}
@@ -559,3 +563,7 @@ enum class TestEnumClass {
559563
}
560564

561565
data class TestClass(val value: String)
566+
567+
interface KTorm : Entity<KTorm> {
568+
companion object : Entity.Factory<KTorm>()
569+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2021 Appmattus Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.appmattus.kotlinfixture.resolver
18+
19+
import com.appmattus.kotlinfixture.TestContext
20+
import com.appmattus.kotlinfixture.Unresolved
21+
import com.appmattus.kotlinfixture.assertIsRandom
22+
import com.appmattus.kotlinfixture.config.Configuration
23+
import com.appmattus.kotlinfixture.typeOf
24+
import org.ktorm.entity.Entity
25+
import kotlin.test.Test
26+
import kotlin.test.assertTrue
27+
28+
class KTormResolverTest {
29+
30+
val context = TestContext(
31+
Configuration(),
32+
CompositeResolver(KTormResolver(), StringResolver(), PrimitiveResolver(), KTypeResolver())
33+
)
34+
35+
@Test
36+
fun `Unknown class returns Unresolved`() {
37+
val result = context.resolve(Number::class)
38+
39+
assertTrue(result is Unresolved)
40+
}
41+
42+
@Test
43+
fun `Random nullability returned`() {
44+
assertIsRandom {
45+
context.resolve(typeOf<TestEntity?>()) == null
46+
}
47+
}
48+
49+
@Test
50+
fun `Entity populated with random ids`() {
51+
assertIsRandom {
52+
(context.resolve(typeOf<TestEntity>()) as TestEntity).id
53+
}
54+
}
55+
56+
@Test
57+
fun `Entity populated with random names`() {
58+
assertIsRandom {
59+
(context.resolve(typeOf<TestEntity>()) as TestEntity).name
60+
}
61+
}
62+
63+
@Test
64+
fun `Entity class returns Unresolved`() {
65+
val result = context.resolve(typeOf<Entity<TestEntity>>())
66+
67+
assertTrue(result is Unresolved)
68+
}
69+
70+
interface TestEntity : Entity<TestEntity> {
71+
companion object : Entity.Factory<TestEntity>()
72+
73+
val id: Int
74+
var name: String
75+
}
76+
}

0 commit comments

Comments
 (0)