From aa89fb8079420d2b0de3f336d47c6699668be243 Mon Sep 17 00:00:00 2001 From: "ah.jo" Date: Thu, 14 Nov 2024 18:50:07 +0900 Subject: [PATCH] Fix setting recursive implementations of self reference object (#1086) --- .../v1.1.x-kor/release-notes/_index.md | 6 ++ docs/content/v1.1.x/release-notes/_index.md | 6 ++ .../tests/java/InterfaceTestSpecs.java | 64 +++++++++++++++++++ .../fixturemonkey/tests/java/JavaTest.java | 37 +++++++++++ .../fixturemonkey/tree/ObjectNode.java | 8 +-- .../fixturemonkey/tree/TraverseContext.java | 14 ++-- 6 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/InterfaceTestSpecs.java diff --git a/docs/content/v1.1.x-kor/release-notes/_index.md b/docs/content/v1.1.x-kor/release-notes/_index.md index 8c8266cf8..c7a9d1e05 100644 --- a/docs/content/v1.1.x-kor/release-notes/_index.md +++ b/docs/content/v1.1.x-kor/release-notes/_index.md @@ -5,6 +5,12 @@ menu: docs: weight: 100 --- +sectionStart +## v.1.1.2 +Fix setting recursive implementations of self reference object + +sectionEnd + sectionStart ## v.1.1.1 Fix set a recursive object. diff --git a/docs/content/v1.1.x/release-notes/_index.md b/docs/content/v1.1.x/release-notes/_index.md index 177f7b9ab..9c6810a54 100644 --- a/docs/content/v1.1.x/release-notes/_index.md +++ b/docs/content/v1.1.x/release-notes/_index.md @@ -5,6 +5,12 @@ menu: docs: weight: 100 --- +sectionStart +## v.1.1.2 +Fix setting recursive implementations of self reference object + +sectionEnd + sectionStart ## v.1.1.1 Fix set a recursive object. diff --git a/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/InterfaceTestSpecs.java b/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/InterfaceTestSpecs.java new file mode 100644 index 000000000..42f6f2d11 --- /dev/null +++ b/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/InterfaceTestSpecs.java @@ -0,0 +1,64 @@ +/* + * 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.tests.java; + +import java.util.List; + +import lombok.Value; + +class InterfaceTestSpecs { + public interface InterfaceObject { + Object getObject(); + } + + @Value + public static class InterfaceStringObject implements InterfaceObject { + String object; + + @Override + public String getObject() { + return this.object; + } + } + + @Value + public static class InterfaceIntegerObject implements InterfaceObject { + Integer object; + + @Override + public Integer getObject() { + return object; + } + } + + @Value + public static class InterfaceListObject implements InterfaceObject { + List values; + + @Override + public List getObject() { + return values; + } + } + + @Value + public static class InterfaceWrapperObject { + InterfaceObject value; + } +} diff --git a/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/JavaTest.java b/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/JavaTest.java index bedb5a155..d27e03af6 100644 --- a/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/JavaTest.java +++ b/fixture-monkey-tests/java-tests/src/test/java/com/navercorp/fixturemonkey/tests/java/JavaTest.java @@ -61,6 +61,7 @@ import com.navercorp.fixturemonkey.api.introspector.FailoverIntrospector; import com.navercorp.fixturemonkey.api.introspector.FieldReflectionArbitraryIntrospector; import com.navercorp.fixturemonkey.api.lazy.LazyArbitrary; +import com.navercorp.fixturemonkey.api.plugin.InterfacePlugin; import com.navercorp.fixturemonkey.api.type.TypeReference; import com.navercorp.fixturemonkey.customizer.InnerSpec; import com.navercorp.fixturemonkey.customizer.Values; @@ -89,6 +90,11 @@ import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveListObject; import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveMapObject; import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveObject; +import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceIntegerObject; +import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceListObject; +import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceObject; +import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceStringObject; +import com.navercorp.fixturemonkey.tests.java.InterfaceTestSpecs.InterfaceWrapperObject; import com.navercorp.fixturemonkey.tests.java.MutableJavaTestSpecs.ConstantObject; import com.navercorp.fixturemonkey.tests.java.NestedClassTestSpecs.Inner; import com.navercorp.fixturemonkey.tests.java.NoArgsConstructorJavaTestSpecs.NestedObject; @@ -1346,4 +1352,35 @@ void setExpCollectionElement() { then(actual).isEqualTo("test"); } + + @RepeatedTest(TEST_COUNT) + void setListRecursiveImplementations() { + // given + FixtureMonkey sut = FixtureMonkey.builder() + .objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE) + .plugin(new InterfacePlugin() + .interfaceImplements( + InterfaceObject.class, + Arrays.asList( + InterfaceStringObject.class, + InterfaceIntegerObject.class, + InterfaceListObject.class + ) + ) + ) + .build(); + + List element = sut.giveMeOne(new TypeReference>() { + }).stream() + .map(InterfaceObject.class::cast) + .collect(Collectors.toList()); + InterfaceListObject expected = new InterfaceListObject(element); + + // when + InterfaceWrapperObject actual = sut.giveMeBuilder(InterfaceWrapperObject.class) + .set("value", expected) + .sample(); + + then(actual.getValue()).isEqualTo(expected); + } } diff --git a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/tree/ObjectNode.java b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/tree/ObjectNode.java index d5273b56f..e5187a75a 100644 --- a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/tree/ObjectNode.java +++ b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/tree/ObjectNode.java @@ -340,7 +340,7 @@ public void forceExpand() { if (this.getTreeProperty().isContainer()) { return this.expandContainerNode( typeDefinition, - traverseContext.withoutRecursiveTreeProperties() + traverseContext.withParentProperties() ); } @@ -349,7 +349,7 @@ public void forceExpand() { typeDefinition.getPropertyGenerator() .generateChildProperties(typeDefinition.getResolvedProperty()), this.nullInject, - traverseContext.withoutRecursiveTreeProperties() + traverseContext.withParentProperties() ).stream(); } ).collect(Collectors.toList()); @@ -363,7 +363,7 @@ public void forceExpand(TypeDefinition typeDefinition) { if (this.getTreeProperty().isContainer()) { newChildren = expandContainerNode( typeDefinition, - this.traverseContext.withoutRecursiveTreeProperties() + this.traverseContext.withParentProperties() ) .collect(Collectors.toList()); } else { @@ -372,7 +372,7 @@ public void forceExpand(TypeDefinition typeDefinition) { typeDefinition.getPropertyGenerator() .generateChildProperties(typeDefinition.getResolvedProperty()), this.nullInject, - this.traverseContext.withoutRecursiveTreeProperties() + this.traverseContext.withParentProperties() ); } diff --git a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/tree/TraverseContext.java b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/tree/TraverseContext.java index 62e7082aa..0e478e97d 100644 --- a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/tree/TraverseContext.java +++ b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/tree/TraverseContext.java @@ -121,15 +121,11 @@ public TreeProperty getLastTreeProperty() { return this.treeProperties.get(this.treeProperties.size() - 1); } - public TraverseContext withoutRecursiveTreeProperties() { - List newTreeProperties = new ArrayList<>(this.treeProperties); - for (int i = 1; i < this.treeProperties.size(); i++) { - Property rootProperty = getRootTreeProperty().getObjectProperty().getProperty(); - TreeProperty treeProperty = treeProperties.get(i); - if (isSameType(rootProperty, treeProperty.getObjectProperty().getProperty())) { - newTreeProperties = newTreeProperties.subList(i, newTreeProperties.size()); - break; - } + public TraverseContext withParentProperties() { + List newTreeProperties = new ArrayList<>(); + + if (!this.treeProperties.isEmpty()) { + newTreeProperties.add(this.treeProperties.get(this.treeProperties.size() - 1)); } return new TraverseContext(