From b1a0be550912a7b80191fd4618eca1dfa4501c0a Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 11:18:18 +0900 Subject: [PATCH 01/17] add test code and enhance kotlin plugin for supporting kotlin and java reference type both --- .../fixturemonkey/kotlin/KotlinPlugin.kt | 17 +++++-- ...PrimaryConstructorArbitraryIntrospector.kt | 13 ++++-- .../kotlin/test/KotlinPluginTest.kt | 45 +++++++++++++++++++ ...aryConstructorArbitraryIntrospectorTest.kt | 4 ++ ...nstructorArbitraryIntrospectorTestSpecs.kt | 6 +++ 5 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt index 72870acf1..64566b567 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt @@ -20,6 +20,9 @@ package com.navercorp.fixturemonkey.kotlin import com.navercorp.fixturemonkey.api.generator.InterfaceObjectPropertyGenerator import com.navercorp.fixturemonkey.api.generator.ObjectPropertyGenerator +import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector +import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector +import com.navercorp.fixturemonkey.api.introspector.FailoverIntrospector import com.navercorp.fixturemonkey.api.introspector.MatchArbitraryIntrospector import com.navercorp.fixturemonkey.api.matcher.MatcherOperator import com.navercorp.fixturemonkey.api.option.FixtureMonkeyOptionsBuilder @@ -38,15 +41,23 @@ import com.navercorp.fixturemonkey.kotlin.matcher.Matchers.TRIPLE_TYPE_MATCHER import com.navercorp.fixturemonkey.kotlin.property.KotlinPropertyGenerator import com.navercorp.fixturemonkey.kotlin.type.actualType import com.navercorp.fixturemonkey.kotlin.type.cachedKotlin +import java.lang.reflect.Modifier import org.apiguardian.api.API import org.apiguardian.api.API.Status.MAINTAINED -import java.lang.reflect.Modifier @API(since = "0.4.0", status = MAINTAINED) class KotlinPlugin : Plugin { override fun accept(optionsBuilder: FixtureMonkeyOptionsBuilder) { - optionsBuilder.objectIntrospector { PrimaryConstructorArbitraryIntrospector.INSTANCE } - .defaultPropertyGenerator(KotlinPropertyGenerator()) + optionsBuilder.objectIntrospector { + FailoverIntrospector( + listOf( + PrimaryConstructorArbitraryIntrospector.INSTANCE, BeanArbitraryIntrospector.INSTANCE + ) + ) + } + .defaultPropertyGenerator( + KotlinPropertyGenerator() + ) .insertFirstArbitraryObjectPropertyGenerator( MatcherOperator( { it.type.actualType().cachedKotlin().isSealed }, diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt index 97fa7dda7..5b88daa9e 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt @@ -27,6 +27,7 @@ import com.navercorp.fixturemonkey.api.property.Property import com.navercorp.fixturemonkey.api.property.PropertyGenerator import com.navercorp.fixturemonkey.api.type.Types import com.navercorp.fixturemonkey.kotlin.property.KotlinPropertyGenerator +import com.navercorp.fixturemonkey.kotlin.type.isKotlinType import org.apiguardian.api.API import org.apiguardian.api.API.Status.MAINTAINED import org.slf4j.LoggerFactory @@ -43,6 +44,10 @@ import kotlin.reflect.jvm.jvmErasure class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector { override fun introspect(context: ArbitraryGeneratorContext): ArbitraryIntrospectorResult { val type = Types.getActualType(context.resolvedType) + if (!type.isKotlinType()) { + return ArbitraryIntrospectorResult.NOT_INTROSPECTED + } + if (Modifier.isAbstract(type.modifiers)) { return ArbitraryIntrospectorResult.NOT_INTROSPECTED } @@ -81,10 +86,10 @@ class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector { arbitrariesByPropertyName: Map, ): Any? { try { - val parameterKotlinType = parameter.type.jvmErasure - return if (parameterKotlinType.isValue) { - parameterKotlinType.primaryConstructor!!.isAccessible = true - parameterKotlinType.primaryConstructor!!.call(arbitrariesByPropertyName[parameter.name]) + val parameterType = parameter.type.jvmErasure + return if (parameterType.isValue) { + parameterType.primaryConstructor!!.isAccessible = true + parameterType.primaryConstructor!!.call(arbitrariesByPropertyName[parameter.name]) } else { arbitrariesByPropertyName[parameter.name] } diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt new file mode 100644 index 000000000..66d97e8e3 --- /dev/null +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt @@ -0,0 +1,45 @@ +package com.navercorp.fixturemonkey.kotlin.test + +import com.navercorp.fixturemonkey.FixtureMonkey +import com.navercorp.fixturemonkey.kotlin.KotlinPlugin +import com.navercorp.fixturemonkey.kotlin.giveMeOne +import org.assertj.core.api.BDDAssertions +import org.junit.jupiter.api.Test + +/** + * Kotlin plugin test + * + * test for behavior of default introspector using failover introspector + * + * - default introspector is PrimaryConstructorArbitraryIntrospector + * - if it fails, it uses BeanArbitraryIntrospector + * + * this action is for supporting instantiating java class + */ +class KotlinPluginTest { + private val sut: FixtureMonkey = FixtureMonkey.builder() + .plugin(KotlinPlugin()) + .build() + + @Test + fun kotlinClassWithJavaClass() { + + val sut: FixtureMonkey = FixtureMonkey.builder() + .plugin(KotlinPlugin()) + .build() + // when + val actual = sut.giveMeOne() + + BDDAssertions.then(actual).isNotNull + BDDAssertions.then(actual.javaObject).isNotNull + } + + @Test + fun sampleMapValue() { + // when + val actual = sut.giveMeOne() + + BDDAssertions.then(actual).isNotNull + BDDAssertions.then(actual.map).isNotNull + } +} diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTest.kt index 6419ca026..5bf2a5e97 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTest.kt @@ -19,11 +19,15 @@ package com.navercorp.fixturemonkey.kotlin.test import com.navercorp.fixturemonkey.FixtureMonkey +import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector +import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.KotlinPlugin import com.navercorp.fixturemonkey.kotlin.giveMeOne +import com.navercorp.fixturemonkey.kotlin.introspector.PrimaryConstructorArbitraryIntrospector import net.jqwik.api.Property import org.assertj.core.api.BDDAssertions.then import org.assertj.core.api.BDDAssertions.thenNoException +import org.junit.jupiter.api.Test class PrimaryConstructorArbitraryIntrospectorTest { private val sut: FixtureMonkey = FixtureMonkey.builder() diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTestSpecs.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTestSpecs.kt index 11ab9f050..69359f649 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTestSpecs.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTestSpecs.kt @@ -18,6 +18,8 @@ package com.navercorp.fixturemonkey.kotlin.test +import com.navercorp.fixturemonkey.kotlin.spec.JavaObject + class PrimaryConstructor( val intValue: Int, val stringValue: String, @@ -59,3 +61,7 @@ class SecondaryConstructor( interface InterfaceClass { fun test() } + +class KotlinClassWithJavaClass(val javaObject: JavaObject) + +class MapValue(val map: Map) From 2177a848fd090392026c8b3bb315ada56af4a581 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 11:20:02 +0900 Subject: [PATCH 02/17] klint formatting --- .../test/PrimaryConstructorArbitraryIntrospectorTest.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTest.kt index 5bf2a5e97..6419ca026 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTest.kt @@ -19,15 +19,11 @@ package com.navercorp.fixturemonkey.kotlin.test import com.navercorp.fixturemonkey.FixtureMonkey -import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector -import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.KotlinPlugin import com.navercorp.fixturemonkey.kotlin.giveMeOne -import com.navercorp.fixturemonkey.kotlin.introspector.PrimaryConstructorArbitraryIntrospector import net.jqwik.api.Property import org.assertj.core.api.BDDAssertions.then import org.assertj.core.api.BDDAssertions.thenNoException -import org.junit.jupiter.api.Test class PrimaryConstructorArbitraryIntrospectorTest { private val sut: FixtureMonkey = FixtureMonkey.builder() From 6b914d2e783b50664023c0474afbf3b74f472a4c Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 11:24:33 +0900 Subject: [PATCH 03/17] add file documentation and mv kotlin plugin test specs --- .../kotlin/test/KotlinPluginTest.kt | 18 +++++++++++++ .../kotlin/test/KotlinPluginTestSpecs.kt | 25 +++++++++++++++++++ ...nstructorArbitraryIntrospectorTestSpecs.kt | 6 ----- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTestSpecs.kt diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt index 66d97e8e3..1b3af4fb3 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt @@ -1,3 +1,21 @@ +/* + * Fixture Monkey + * + * Copyright (c) 2021-present NAVER Corp. + * + * 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.navercorp.fixturemonkey.kotlin.test import com.navercorp.fixturemonkey.FixtureMonkey diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTestSpecs.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTestSpecs.kt new file mode 100644 index 000000000..644883e98 --- /dev/null +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTestSpecs.kt @@ -0,0 +1,25 @@ +/* + * Fixture Monkey + * + * Copyright (c) 2021-present NAVER Corp. + * + * 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.navercorp.fixturemonkey.kotlin.test + +import com.navercorp.fixturemonkey.kotlin.spec.JavaObject + +class KotlinClassWithJavaClass(val javaObject: JavaObject) + +class MapValue(val map: Map) diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTestSpecs.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTestSpecs.kt index 69359f649..11ab9f050 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTestSpecs.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/PrimaryConstructorArbitraryIntrospectorTestSpecs.kt @@ -18,8 +18,6 @@ package com.navercorp.fixturemonkey.kotlin.test -import com.navercorp.fixturemonkey.kotlin.spec.JavaObject - class PrimaryConstructor( val intValue: Int, val stringValue: String, @@ -61,7 +59,3 @@ class SecondaryConstructor( interface InterfaceClass { fun test() } - -class KotlinClassWithJavaClass(val javaObject: JavaObject) - -class MapValue(val map: Map) From 2f5f902abe7d13d0af9ed0b5aa6d069dc147102c Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 11:52:38 +0900 Subject: [PATCH 04/17] formatting and rollback ununecessary refac --- .../com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt | 4 +--- .../PrimaryConstructorArbitraryIntrospector.kt | 8 ++++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt index 64566b567..80a92472c 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt @@ -55,9 +55,7 @@ class KotlinPlugin : Plugin { ) ) } - .defaultPropertyGenerator( - KotlinPropertyGenerator() - ) + .defaultPropertyGenerator(KotlinPropertyGenerator()) .insertFirstArbitraryObjectPropertyGenerator( MatcherOperator( { it.type.actualType().cachedKotlin().isSealed }, diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt index 5b88daa9e..a56090ea1 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt @@ -86,10 +86,10 @@ class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector { arbitrariesByPropertyName: Map, ): Any? { try { - val parameterType = parameter.type.jvmErasure - return if (parameterType.isValue) { - parameterType.primaryConstructor!!.isAccessible = true - parameterType.primaryConstructor!!.call(arbitrariesByPropertyName[parameter.name]) + val parameterKotlinType = parameter.type.jvmErasure + return if (parameterKotlinType.isValue) { + parameterKotlinType.primaryConstructor!!.isAccessible = true + parameterKotlinType.primaryConstructor!!.call(arbitrariesByPropertyName[parameter.name]) } else { arbitrariesByPropertyName[parameter.name] } From 4fe907456a9cb7bcd491d95e65c610ce95efa5ae Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 12:50:26 +0900 Subject: [PATCH 05/17] refac dbb assertion --- .../fixturemonkey/kotlin/test/KotlinPluginTest.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt index 1b3af4fb3..6b7945c8e 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt @@ -21,7 +21,7 @@ package com.navercorp.fixturemonkey.kotlin.test import com.navercorp.fixturemonkey.FixtureMonkey import com.navercorp.fixturemonkey.kotlin.KotlinPlugin import com.navercorp.fixturemonkey.kotlin.giveMeOne -import org.assertj.core.api.BDDAssertions +import org.assertj.core.api.BDDAssertions.then import org.junit.jupiter.api.Test /** @@ -48,8 +48,8 @@ class KotlinPluginTest { // when val actual = sut.giveMeOne() - BDDAssertions.then(actual).isNotNull - BDDAssertions.then(actual.javaObject).isNotNull + then(actual).isNotNull + then(actual.javaObject).isNotNull } @Test @@ -57,7 +57,7 @@ class KotlinPluginTest { // when val actual = sut.giveMeOne() - BDDAssertions.then(actual).isNotNull - BDDAssertions.then(actual.map).isNotNull + then(actual).isNotNull + then(actual.map).isNotNull } } From 4ad828ee32ae3215de3a9354ffd8eff04c982867 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 13:58:27 +0900 Subject: [PATCH 06/17] add KotlinAndJavaCompositeArbitraryIntrospector and change kotlin plugin api, default introspector --- .../fixturemonkey/kotlin/KotlinPlugin.kt | 15 ++++--- ...inAndJavaCompositeArbitraryIntrospector.kt | 32 +++++++++++++++ ...JavaCompositeArbitraryIntrospectorTest.kt} | 39 +++++++++++++------ ...ompositeArbitraryIntrospectorTestSpecs.kt} | 0 4 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt rename fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/{KotlinPluginTest.kt => KotlinAndJavaCompositeArbitraryIntrospectorTest.kt} (58%) rename fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/{KotlinPluginTestSpecs.kt => KotlinAndJavaCompositeArbitraryIntrospectorTestSpecs.kt} (100%) diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt index 80a92472c..78f982fc0 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt @@ -22,17 +22,20 @@ import com.navercorp.fixturemonkey.api.generator.InterfaceObjectPropertyGenerato import com.navercorp.fixturemonkey.api.generator.ObjectPropertyGenerator import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector +import com.navercorp.fixturemonkey.api.introspector.CompositeArbitraryIntrospector import com.navercorp.fixturemonkey.api.introspector.FailoverIntrospector import com.navercorp.fixturemonkey.api.introspector.MatchArbitraryIntrospector import com.navercorp.fixturemonkey.api.matcher.MatcherOperator import com.navercorp.fixturemonkey.api.option.FixtureMonkeyOptionsBuilder import com.navercorp.fixturemonkey.api.plugin.Plugin +import com.navercorp.fixturemonkey.api.property.JavaBeansPropertyGenerator import com.navercorp.fixturemonkey.kotlin.generator.InterfaceKFunctionPropertyGenerator import com.navercorp.fixturemonkey.kotlin.generator.PairContainerPropertyGenerator import com.navercorp.fixturemonkey.kotlin.generator.PairDecomposedContainerValueFactory import com.navercorp.fixturemonkey.kotlin.generator.TripleContainerPropertyGenerator import com.navercorp.fixturemonkey.kotlin.generator.TripleDecomposedContainerValueFactory import com.navercorp.fixturemonkey.kotlin.instantiator.KotlinInstantiatorProcessor +import com.navercorp.fixturemonkey.kotlin.introspector.KotlinAndJavaCompositeArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.introspector.PairIntrospector import com.navercorp.fixturemonkey.kotlin.introspector.PrimaryConstructorArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.introspector.TripleIntrospector @@ -46,13 +49,15 @@ import org.apiguardian.api.API import org.apiguardian.api.API.Status.MAINTAINED @API(since = "0.4.0", status = MAINTAINED) -class KotlinPlugin : Plugin { +class KotlinPlugin( + private val kotlinIntrospector: ArbitraryIntrospector = PrimaryConstructorArbitraryIntrospector.INSTANCE, + private val javaIntrospector: ArbitraryIntrospector = BeanArbitraryIntrospector.INSTANCE, +) : Plugin { override fun accept(optionsBuilder: FixtureMonkeyOptionsBuilder) { optionsBuilder.objectIntrospector { - FailoverIntrospector( - listOf( - PrimaryConstructorArbitraryIntrospector.INSTANCE, BeanArbitraryIntrospector.INSTANCE - ) + KotlinAndJavaCompositeArbitraryIntrospector( + kotlinIntrospector = kotlinIntrospector, + javaIntrospector = javaIntrospector ) } .defaultPropertyGenerator(KotlinPropertyGenerator()) diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt new file mode 100644 index 000000000..063020fab --- /dev/null +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt @@ -0,0 +1,32 @@ +package com.navercorp.fixturemonkey.kotlin.introspector + +import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext +import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector +import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospectorResult +import com.navercorp.fixturemonkey.api.type.Types +import com.navercorp.fixturemonkey.kotlin.type.isKotlinType +import org.slf4j.LoggerFactory + +class KotlinAndJavaCompositeArbitraryIntrospector( + private val kotlinIntrospector: ArbitraryIntrospector, + private val javaIntrospector: ArbitraryIntrospector, +) : ArbitraryIntrospector { + + override fun introspect(context: ArbitraryGeneratorContext): ArbitraryIntrospectorResult { + val type = Types.getActualType(context.resolvedType) + try { + return if (type.isKotlinType()) { + kotlinIntrospector.introspect(context) + } else { + javaIntrospector.introspect(context) + } + } catch (e: Exception) { + LOGGER.warn("Given type $type is failed to generated due to the exception.", e) + return ArbitraryIntrospectorResult.NOT_INTROSPECTED + } + } + + companion object { + private val LOGGER = LoggerFactory.getLogger(KotlinAndJavaCompositeArbitraryIntrospector::class.java) + } +} diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt similarity index 58% rename from fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt rename to fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt index 6b7945c8e..89e9834ac 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt @@ -19,32 +19,44 @@ package com.navercorp.fixturemonkey.kotlin.test import com.navercorp.fixturemonkey.FixtureMonkey +import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.KotlinPlugin import com.navercorp.fixturemonkey.kotlin.giveMeOne import org.assertj.core.api.BDDAssertions.then import org.junit.jupiter.api.Test /** - * Kotlin plugin test * - * test for behavior of default introspector using failover introspector + * test for behavior of default Kotlin Plugin introspector using kotlin and java composite arbitrary introspector * - * - default introspector is PrimaryConstructorArbitraryIntrospector - * - if it fails, it uses BeanArbitraryIntrospector + * - default kotlin introspector is PrimaryConstructorArbitraryIntrospector + * - default java introspector is BeanArbitraryIntrospector * - * this action is for supporting instantiating java class */ -class KotlinPluginTest { - private val sut: FixtureMonkey = FixtureMonkey.builder() - .plugin(KotlinPlugin()) - .build() - +class KotlinAndJavaCompositeArbitraryIntrospectorTest { @Test fun kotlinClassWithJavaClass() { - + // given val sut: FixtureMonkey = FixtureMonkey.builder() .plugin(KotlinPlugin()) .build() + + // when + val actual = sut.giveMeOne() + + then(actual).isNotNull + then(actual.javaObject).isNotNull + } + + @Test + fun kotlinClassWithJavaClassUsingOtherIntrospector() { + // given + val sut: FixtureMonkey = FixtureMonkey.builder() + .plugin( + KotlinPlugin(javaIntrospector = ConstructorPropertiesArbitraryIntrospector.INSTANCE) + ) + .build() + // when val actual = sut.giveMeOne() @@ -54,6 +66,11 @@ class KotlinPluginTest { @Test fun sampleMapValue() { + // given + val sut: FixtureMonkey = FixtureMonkey.builder() + .plugin(KotlinPlugin()) + .build() + // when val actual = sut.giveMeOne() diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTestSpecs.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTestSpecs.kt similarity index 100% rename from fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinPluginTestSpecs.kt rename to fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTestSpecs.kt From 8194dcf36da3351c24b88172255b26c709474e18 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 14:07:02 +0900 Subject: [PATCH 07/17] formatting --- .../com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt | 3 --- .../KotlinAndJavaCompositeArbitraryIntrospector.kt | 1 - .../introspector/PrimaryConstructorArbitraryIntrospector.kt | 5 ----- 3 files changed, 9 deletions(-) diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt index 78f982fc0..1ba3097b5 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt @@ -22,13 +22,10 @@ import com.navercorp.fixturemonkey.api.generator.InterfaceObjectPropertyGenerato import com.navercorp.fixturemonkey.api.generator.ObjectPropertyGenerator import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector -import com.navercorp.fixturemonkey.api.introspector.CompositeArbitraryIntrospector -import com.navercorp.fixturemonkey.api.introspector.FailoverIntrospector import com.navercorp.fixturemonkey.api.introspector.MatchArbitraryIntrospector import com.navercorp.fixturemonkey.api.matcher.MatcherOperator import com.navercorp.fixturemonkey.api.option.FixtureMonkeyOptionsBuilder import com.navercorp.fixturemonkey.api.plugin.Plugin -import com.navercorp.fixturemonkey.api.property.JavaBeansPropertyGenerator import com.navercorp.fixturemonkey.kotlin.generator.InterfaceKFunctionPropertyGenerator import com.navercorp.fixturemonkey.kotlin.generator.PairContainerPropertyGenerator import com.navercorp.fixturemonkey.kotlin.generator.PairDecomposedContainerValueFactory diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt index 063020fab..55330bd94 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt @@ -11,7 +11,6 @@ class KotlinAndJavaCompositeArbitraryIntrospector( private val kotlinIntrospector: ArbitraryIntrospector, private val javaIntrospector: ArbitraryIntrospector, ) : ArbitraryIntrospector { - override fun introspect(context: ArbitraryGeneratorContext): ArbitraryIntrospectorResult { val type = Types.getActualType(context.resolvedType) try { diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt index a56090ea1..97fa7dda7 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt @@ -27,7 +27,6 @@ import com.navercorp.fixturemonkey.api.property.Property import com.navercorp.fixturemonkey.api.property.PropertyGenerator import com.navercorp.fixturemonkey.api.type.Types import com.navercorp.fixturemonkey.kotlin.property.KotlinPropertyGenerator -import com.navercorp.fixturemonkey.kotlin.type.isKotlinType import org.apiguardian.api.API import org.apiguardian.api.API.Status.MAINTAINED import org.slf4j.LoggerFactory @@ -44,10 +43,6 @@ import kotlin.reflect.jvm.jvmErasure class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector { override fun introspect(context: ArbitraryGeneratorContext): ArbitraryIntrospectorResult { val type = Types.getActualType(context.resolvedType) - if (!type.isKotlinType()) { - return ArbitraryIntrospectorResult.NOT_INTROSPECTED - } - if (Modifier.isAbstract(type.modifiers)) { return ArbitraryIntrospectorResult.NOT_INTROSPECTED } From 52f4a6edc4eae240a8feb27daa135cdde794791c Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 14:27:42 +0900 Subject: [PATCH 08/17] change to jqwik test --- .../test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt index 89e9834ac..1ae387207 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt @@ -22,6 +22,7 @@ import com.navercorp.fixturemonkey.FixtureMonkey import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.KotlinPlugin import com.navercorp.fixturemonkey.kotlin.giveMeOne +import net.jqwik.api.Property import org.assertj.core.api.BDDAssertions.then import org.junit.jupiter.api.Test @@ -34,7 +35,7 @@ import org.junit.jupiter.api.Test * */ class KotlinAndJavaCompositeArbitraryIntrospectorTest { - @Test + @Property fun kotlinClassWithJavaClass() { // given val sut: FixtureMonkey = FixtureMonkey.builder() @@ -48,7 +49,7 @@ class KotlinAndJavaCompositeArbitraryIntrospectorTest { then(actual.javaObject).isNotNull } - @Test + @Property fun kotlinClassWithJavaClassUsingOtherIntrospector() { // given val sut: FixtureMonkey = FixtureMonkey.builder() From c21262da0f721ffbd91627dca17adf52384c9f50 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 14:28:19 +0900 Subject: [PATCH 09/17] change to jqwik test --- .../test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt index 1ae387207..bda1b5d5c 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt @@ -65,7 +65,7 @@ class KotlinAndJavaCompositeArbitraryIntrospectorTest { then(actual.javaObject).isNotNull } - @Test + @Property fun sampleMapValue() { // given val sut: FixtureMonkey = FixtureMonkey.builder() From b98934834ca53edab916686149548680c2352993 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 14:30:17 +0900 Subject: [PATCH 10/17] del unused import --- .../test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt index bda1b5d5c..c0d54f5a1 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt @@ -24,7 +24,6 @@ import com.navercorp.fixturemonkey.kotlin.KotlinPlugin import com.navercorp.fixturemonkey.kotlin.giveMeOne import net.jqwik.api.Property import org.assertj.core.api.BDDAssertions.then -import org.junit.jupiter.api.Test /** * From c12168aa7195cbcc668cbb345e772836d3194fab Mon Sep 17 00:00:00 2001 From: jinia91 Date: Thu, 14 Mar 2024 20:13:24 +0900 Subject: [PATCH 11/17] add file docu for licence --- ...linAndJavaCompositeArbitraryIntrospector.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt index 55330bd94..3268bf9f3 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt @@ -1,3 +1,21 @@ +/* + * Fixture Monkey + * + * Copyright (c) 2021-present NAVER Corp. + * + * 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.navercorp.fixturemonkey.kotlin.introspector import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext From 4433d517a87bae847b556915cdbb3d0fd0cbe9b5 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Sat, 16 Mar 2024 12:42:16 +0900 Subject: [PATCH 12/17] feedback(seongahjo) : refac property name and use exist method --- .../navercorp/fixturemonkey/kotlin/KotlinPlugin.kt | 4 ++-- .../KotlinAndJavaCompositeArbitraryIntrospector.kt | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt index 1ba3097b5..8a76f523b 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt @@ -53,8 +53,8 @@ class KotlinPlugin( override fun accept(optionsBuilder: FixtureMonkeyOptionsBuilder) { optionsBuilder.objectIntrospector { KotlinAndJavaCompositeArbitraryIntrospector( - kotlinIntrospector = kotlinIntrospector, - javaIntrospector = javaIntrospector + kotlinArbitraryIntrospector = kotlinIntrospector, + javaArbitraryIntrospector = javaIntrospector ) } .defaultPropertyGenerator(KotlinPropertyGenerator()) diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt index 3268bf9f3..2fc1ad14f 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt @@ -21,21 +21,21 @@ package com.navercorp.fixturemonkey.kotlin.introspector import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospectorResult -import com.navercorp.fixturemonkey.api.type.Types +import com.navercorp.fixturemonkey.kotlin.type.actualType import com.navercorp.fixturemonkey.kotlin.type.isKotlinType import org.slf4j.LoggerFactory class KotlinAndJavaCompositeArbitraryIntrospector( - private val kotlinIntrospector: ArbitraryIntrospector, - private val javaIntrospector: ArbitraryIntrospector, + private val kotlinArbitraryIntrospector: ArbitraryIntrospector, + private val javaArbitraryIntrospector: ArbitraryIntrospector, ) : ArbitraryIntrospector { override fun introspect(context: ArbitraryGeneratorContext): ArbitraryIntrospectorResult { - val type = Types.getActualType(context.resolvedType) + val type = context.resolvedType.actualType() try { return if (type.isKotlinType()) { - kotlinIntrospector.introspect(context) + kotlinArbitraryIntrospector.introspect(context) } else { - javaIntrospector.introspect(context) + javaArbitraryIntrospector.introspect(context) } } catch (e: Exception) { LOGGER.warn("Given type $type is failed to generated due to the exception.", e) From 612404a0d0da904125e1e80759230ba56d0ad9e6 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Sat, 16 Mar 2024 13:08:22 +0900 Subject: [PATCH 13/17] feedback(seongahjo) : del using kjcompositArbitraryIntrospector in kotlinPlugin --- .../fixturemonkey/kotlin/KotlinPlugin.kt | 15 ++------------- ...KotlinAndJavaCompositeArbitraryIntrospector.kt | 5 +++-- ...inAndJavaCompositeArbitraryIntrospectorTest.kt | 10 ++++++++-- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt index 8a76f523b..6d1f51e20 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt @@ -20,8 +20,6 @@ package com.navercorp.fixturemonkey.kotlin import com.navercorp.fixturemonkey.api.generator.InterfaceObjectPropertyGenerator import com.navercorp.fixturemonkey.api.generator.ObjectPropertyGenerator -import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector -import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector import com.navercorp.fixturemonkey.api.introspector.MatchArbitraryIntrospector import com.navercorp.fixturemonkey.api.matcher.MatcherOperator import com.navercorp.fixturemonkey.api.option.FixtureMonkeyOptionsBuilder @@ -32,7 +30,6 @@ import com.navercorp.fixturemonkey.kotlin.generator.PairDecomposedContainerValue import com.navercorp.fixturemonkey.kotlin.generator.TripleContainerPropertyGenerator import com.navercorp.fixturemonkey.kotlin.generator.TripleDecomposedContainerValueFactory import com.navercorp.fixturemonkey.kotlin.instantiator.KotlinInstantiatorProcessor -import com.navercorp.fixturemonkey.kotlin.introspector.KotlinAndJavaCompositeArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.introspector.PairIntrospector import com.navercorp.fixturemonkey.kotlin.introspector.PrimaryConstructorArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.introspector.TripleIntrospector @@ -46,17 +43,9 @@ import org.apiguardian.api.API import org.apiguardian.api.API.Status.MAINTAINED @API(since = "0.4.0", status = MAINTAINED) -class KotlinPlugin( - private val kotlinIntrospector: ArbitraryIntrospector = PrimaryConstructorArbitraryIntrospector.INSTANCE, - private val javaIntrospector: ArbitraryIntrospector = BeanArbitraryIntrospector.INSTANCE, -) : Plugin { +class KotlinPlugin : Plugin { override fun accept(optionsBuilder: FixtureMonkeyOptionsBuilder) { - optionsBuilder.objectIntrospector { - KotlinAndJavaCompositeArbitraryIntrospector( - kotlinArbitraryIntrospector = kotlinIntrospector, - javaArbitraryIntrospector = javaIntrospector - ) - } + optionsBuilder.objectIntrospector { PrimaryConstructorArbitraryIntrospector.INSTANCE } .defaultPropertyGenerator(KotlinPropertyGenerator()) .insertFirstArbitraryObjectPropertyGenerator( MatcherOperator( diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt index 2fc1ad14f..e01b51567 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/KotlinAndJavaCompositeArbitraryIntrospector.kt @@ -21,13 +21,14 @@ package com.navercorp.fixturemonkey.kotlin.introspector import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospectorResult +import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.type.actualType import com.navercorp.fixturemonkey.kotlin.type.isKotlinType import org.slf4j.LoggerFactory class KotlinAndJavaCompositeArbitraryIntrospector( - private val kotlinArbitraryIntrospector: ArbitraryIntrospector, - private val javaArbitraryIntrospector: ArbitraryIntrospector, + private val kotlinArbitraryIntrospector: ArbitraryIntrospector = PrimaryConstructorArbitraryIntrospector.INSTANCE, + private val javaArbitraryIntrospector: ArbitraryIntrospector = BeanArbitraryIntrospector.INSTANCE, ) : ArbitraryIntrospector { override fun introspect(context: ArbitraryGeneratorContext): ArbitraryIntrospectorResult { val type = context.resolvedType.actualType() diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt index c0d54f5a1..9e898a6de 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt @@ -22,6 +22,7 @@ import com.navercorp.fixturemonkey.FixtureMonkey import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector import com.navercorp.fixturemonkey.kotlin.KotlinPlugin import com.navercorp.fixturemonkey.kotlin.giveMeOne +import com.navercorp.fixturemonkey.kotlin.introspector.KotlinAndJavaCompositeArbitraryIntrospector import net.jqwik.api.Property import org.assertj.core.api.BDDAssertions.then @@ -39,6 +40,7 @@ class KotlinAndJavaCompositeArbitraryIntrospectorTest { // given val sut: FixtureMonkey = FixtureMonkey.builder() .plugin(KotlinPlugin()) + .objectIntrospector(KotlinAndJavaCompositeArbitraryIntrospector()) .build() // when @@ -52,8 +54,11 @@ class KotlinAndJavaCompositeArbitraryIntrospectorTest { fun kotlinClassWithJavaClassUsingOtherIntrospector() { // given val sut: FixtureMonkey = FixtureMonkey.builder() - .plugin( - KotlinPlugin(javaIntrospector = ConstructorPropertiesArbitraryIntrospector.INSTANCE) + .plugin(KotlinPlugin()) + .objectIntrospector( + KotlinAndJavaCompositeArbitraryIntrospector( + javaArbitraryIntrospector = ConstructorPropertiesArbitraryIntrospector.INSTANCE + ) ) .build() @@ -69,6 +74,7 @@ class KotlinAndJavaCompositeArbitraryIntrospectorTest { // given val sut: FixtureMonkey = FixtureMonkey.builder() .plugin(KotlinPlugin()) + .objectIntrospector(KotlinAndJavaCompositeArbitraryIntrospector()) .build() // when From 7d0db754d027476b8742453a9137f00043697ad8 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Sat, 16 Mar 2024 19:10:52 +0900 Subject: [PATCH 14/17] add kotlinAndJavaCompositeArbitraryIntrospector doc --- .../kotlin-plugin/introspectors-for-kotlin.md | 71 +++++++++++++++++++ ...dJavaCompositeArbitraryIntrospectorTest.kt | 8 --- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/docs/content/v1.0.x/docs/plugins/kotlin-plugin/introspectors-for-kotlin.md b/docs/content/v1.0.x/docs/plugins/kotlin-plugin/introspectors-for-kotlin.md index fb5b7bd7e..f2baab99b 100644 --- a/docs/content/v1.0.x/docs/plugins/kotlin-plugin/introspectors-for-kotlin.md +++ b/docs/content/v1.0.x/docs/plugins/kotlin-plugin/introspectors-for-kotlin.md @@ -41,3 +41,74 @@ fun test() { val product: Product = fixtureMonkey.giveMeOne() } ``` + +## KotlinAndJavaCompositeArbitraryIntrospector + +The `KotlinAndJavaCompositeArbitraryIntrospector` is an introspector designed to assist in the creation of Kotlin classes that reference Java classes. + + +**Example Kotlin Class :** +```kotlin +class KotlinClassWithJavaClass(val javaObject: JavaObject) +``` + +**Example Java Class :** +```java +public class JavaObject { + private String value; + private Map map; + + public JavaObject() { + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} +``` + +**Using PrimaryConstructorArbitraryIntrospector :** +```kotlin + fun kotlinClassWithJavaClass() { + // given + val sut: FixtureMonkey = FixtureMonkey.builder() + .plugin(KotlinPlugin()) + .objectIntrospector(KotlinAndJavaCompositeArbitraryIntrospector()) + .build() + + // when + val actual = sut.giveMeOne() + + then(actual).isNotNull + then(actual.javaObject).isNotNull + } +``` + +For Kotlin and Java classes respectively, it uses the PrimaryConstructorArbitraryIntrospector and the BeanArbitraryIntrospector by default. + +If changes are desired, these can be injected as arguments. + +```kotlin + // given + val sut: FixtureMonkey = FixtureMonkey.builder() + .plugin(KotlinPlugin()) + .objectIntrospector( + KotlinAndJavaCompositeArbitraryIntrospector( + kotlinArbitraryIntrospector = PrimaryConstructorArbitraryIntrospector.INSTANCE, + javaArbitraryIntrospector = ConstructorPropertiesArbitraryIntrospector.INSTANCE + ) + ) + .build() +``` diff --git a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt index 9e898a6de..bfd959b4f 100644 --- a/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt +++ b/fixture-monkey-kotlin/src/test/kotlin/com/navercorp/fixturemonkey/kotlin/test/KotlinAndJavaCompositeArbitraryIntrospectorTest.kt @@ -26,14 +26,6 @@ import com.navercorp.fixturemonkey.kotlin.introspector.KotlinAndJavaCompositeArb import net.jqwik.api.Property import org.assertj.core.api.BDDAssertions.then -/** - * - * test for behavior of default Kotlin Plugin introspector using kotlin and java composite arbitrary introspector - * - * - default kotlin introspector is PrimaryConstructorArbitraryIntrospector - * - default java introspector is BeanArbitraryIntrospector - * - */ class KotlinAndJavaCompositeArbitraryIntrospectorTest { @Property fun kotlinClassWithJavaClass() { From cba737c1e9801b7d2373fe85eca7bd02777e8121 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Mon, 18 Mar 2024 14:39:34 +0900 Subject: [PATCH 15/17] formating --- .../kotlin-plugin/introspectors-for-kotlin.md | 16 ++++++++-------- .../fixturemonkey/kotlin/KotlinPlugin.kt | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/content/v1.0.x/docs/plugins/kotlin-plugin/introspectors-for-kotlin.md b/docs/content/v1.0.x/docs/plugins/kotlin-plugin/introspectors-for-kotlin.md index f2baab99b..3bb891e21 100644 --- a/docs/content/v1.0.x/docs/plugins/kotlin-plugin/introspectors-for-kotlin.md +++ b/docs/content/v1.0.x/docs/plugins/kotlin-plugin/introspectors-for-kotlin.md @@ -102,13 +102,13 @@ If changes are desired, these can be injected as arguments. ```kotlin // given - val sut: FixtureMonkey = FixtureMonkey.builder() - .plugin(KotlinPlugin()) - .objectIntrospector( - KotlinAndJavaCompositeArbitraryIntrospector( - kotlinArbitraryIntrospector = PrimaryConstructorArbitraryIntrospector.INSTANCE, - javaArbitraryIntrospector = ConstructorPropertiesArbitraryIntrospector.INSTANCE - ) + val sut: FixtureMonkey = FixtureMonkey.builder() + .plugin(KotlinPlugin()) + .objectIntrospector( + KotlinAndJavaCompositeArbitraryIntrospector( + kotlinArbitraryIntrospector = PrimaryConstructorArbitraryIntrospector.INSTANCE, + javaArbitraryIntrospector = ConstructorPropertiesArbitraryIntrospector.INSTANCE ) - .build() + ) + .build() ``` diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt index 6d1f51e20..72870acf1 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinPlugin.kt @@ -38,9 +38,9 @@ import com.navercorp.fixturemonkey.kotlin.matcher.Matchers.TRIPLE_TYPE_MATCHER import com.navercorp.fixturemonkey.kotlin.property.KotlinPropertyGenerator import com.navercorp.fixturemonkey.kotlin.type.actualType import com.navercorp.fixturemonkey.kotlin.type.cachedKotlin -import java.lang.reflect.Modifier import org.apiguardian.api.API import org.apiguardian.api.API.Status.MAINTAINED +import java.lang.reflect.Modifier @API(since = "0.4.0", status = MAINTAINED) class KotlinPlugin : Plugin { From 2e3ed9c3dcf95fe0597bac9fdca78f085e3d3dd5 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Mon, 18 Mar 2024 15:02:27 +0900 Subject: [PATCH 16/17] java and kotlin composite class benchmark test --- .../kotlin/KotlinJavaCompositeOrderSheet.kt | 34 +++++++++++++++++++ .../kotlin/KotlinObjectGenerationBenchMark.kt | 13 +++++++ 2 files changed, 47 insertions(+) create mode 100644 fixture-monkey-benchmarks/fixture-monkey-benchmark-kotlin/src/jmh/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinJavaCompositeOrderSheet.kt diff --git a/fixture-monkey-benchmarks/fixture-monkey-benchmark-kotlin/src/jmh/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinJavaCompositeOrderSheet.kt b/fixture-monkey-benchmarks/fixture-monkey-benchmark-kotlin/src/jmh/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinJavaCompositeOrderSheet.kt new file mode 100644 index 000000000..6b3d24049 --- /dev/null +++ b/fixture-monkey-benchmarks/fixture-monkey-benchmark-kotlin/src/jmh/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinJavaCompositeOrderSheet.kt @@ -0,0 +1,34 @@ +package com.navercorp.fixturemonkey.kotlin + +import com.navercorp.fixturemonkey.OrderSheet +import java.math.BigDecimal +import java.time.Instant +import javax.validation.constraints.Size + +data class KotlinJavaCompositeOrderSheet( + val id: String, + val backUrl: String?, + val userNo: Long?, + @field:Size(min = 1, max = 1) + val products: List, + val merchantsByMerchantNo: Map.Entry?, + val registeredDateTime: Instant?, + @field:Size(min = 1, max = 1) + val bundleDeliveryFeesByDeliveryGroupKey: Map? +) { + data class OrderSheetMerchant( + val talkInterlockAccountId: String?, + val logeyeRequestId: String?, + val logeyeInflowPathName: String?, + val logeyePayAccumulation: Boolean? + ) + + data class OrderSheetBundleDeliveryFee( + val deliveryFee: BigDecimal?, + val type: BundleType + ) + + enum class BundleType { + MANUALLY, IDENTICAL_PRODUCT + } +} diff --git a/fixture-monkey-benchmarks/fixture-monkey-benchmark-kotlin/src/jmh/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinObjectGenerationBenchMark.kt b/fixture-monkey-benchmarks/fixture-monkey-benchmark-kotlin/src/jmh/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinObjectGenerationBenchMark.kt index c4933626f..c31201273 100644 --- a/fixture-monkey-benchmarks/fixture-monkey-benchmark-kotlin/src/jmh/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinObjectGenerationBenchMark.kt +++ b/fixture-monkey-benchmarks/fixture-monkey-benchmark-kotlin/src/jmh/kotlin/com/navercorp/fixturemonkey/kotlin/KotlinObjectGenerationBenchMark.kt @@ -4,6 +4,7 @@ import com.navercorp.fixturemonkey.OrderSheet import com.navercorp.fixturemonkey.FixtureMonkey import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector import com.navercorp.fixturemonkey.api.type.TypeCache +import com.navercorp.fixturemonkey.kotlin.introspector.KotlinAndJavaCompositeArbitraryIntrospector import java.util.concurrent.TimeUnit import org.openjdk.jmh.annotations.Benchmark import org.openjdk.jmh.annotations.BenchmarkMode @@ -48,4 +49,16 @@ open class KotlinObjectGenerationBenchMark { private fun generateKotlinOrderSheet(fixtureMonkey: FixtureMonkey): List = List(COUNT) { fixtureMonkey.giveMeOne(KotlinOrderSheet::class.java) } + + @Benchmark + fun beanGenerateKotlinJavaCompositeOrderSheetWithFixtureMonkey(blackhole: Blackhole) { + val fixtureMonkey = FixtureMonkey.builder() + .plugin(KotlinPlugin()) + .objectIntrospector(KotlinAndJavaCompositeArbitraryIntrospector()) + .build() + blackhole.consume(generateKotlinJavaCompositeOrderSheet(fixtureMonkey)) + } + + private fun generateKotlinJavaCompositeOrderSheet(fixtureMonkey: FixtureMonkey): List = + List(COUNT) { fixtureMonkey.giveMeOne(KotlinJavaCompositeOrderSheet::class.java) } } From 3f718bcf0df0acc8d19e111f0c9c6876982dab30 Mon Sep 17 00:00:00 2001 From: jinia91 Date: Mon, 18 Mar 2024 15:03:14 +0900 Subject: [PATCH 17/17] change contributor nick name ^ --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 14e484f0a..d5844bc72 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ fun sampleOrder() { * @[sangy515](https://github.com/sangy515) * @[yunseok-jeong0](https://github.com/yunseok-jeong0) * @[wicksome](https://github.com/wicksome) -* @[Wonjin Choi](https://github.com/jinia91) +* @[jinia91](https://github.com/jinia91) * @[songkg7](https://github.com/songkg7) * @[this-is-spear](https://github.com/this-is-spear) * @[donggyunhuh](https://github.com/donggyunhuh)