From cd9852867b34c77e416932824634133ece2061ab Mon Sep 17 00:00:00 2001 From: David Wright <24604124+dimwight@users.noreply.github.com> Date: Sun, 19 Sep 2021 17:12:31 +0100 Subject: [PATCH] Fix for Collect #4059 --- src/main/java/org/javarosa/core/model/FormDef.java | 3 ++- .../javarosa/core/model/instance/TreeElement.java | 12 ++++++++++++ .../org/javarosa/core/model/TriggerableDagTest.java | 11 ++++++----- .../javarosa/form/api/FormNavigationTestCase.java | 8 +++++--- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/javarosa/core/model/FormDef.java b/src/main/java/org/javarosa/core/model/FormDef.java index 32aa3302e..b2e153542 100644 --- a/src/main/java/org/javarosa/core/model/FormDef.java +++ b/src/main/java/org/javarosa/core/model/FormDef.java @@ -506,7 +506,8 @@ public void createNewRepeat(FormIndex index) throws InvalidReferenceException { TreeReference repeatContextRef = getChildInstanceRef(index); TreeElement template = mainInstance.getTemplate(repeatContextRef); - mainInstance.copyNode(template, repeatContextRef); + //For #4059 fix + mainInstance.copyNode(template.deepCopyForRepeat(), repeatContextRef); TreeElement newNode = mainInstance.resolveReference(repeatContextRef); preloadInstance(newNode); diff --git a/src/main/java/org/javarosa/core/model/instance/TreeElement.java b/src/main/java/org/javarosa/core/model/instance/TreeElement.java index a54fca661..b707624e3 100644 --- a/src/main/java/org/javarosa/core/model/instance/TreeElement.java +++ b/src/main/java/org/javarosa/core/model/instance/TreeElement.java @@ -344,6 +344,18 @@ public TreeElement deepCopy(boolean includeTemplates) { return newNode; } + //For #4059 fix + public TreeElement deepCopyForRepeat() { + TreeElement newNode = shallowCopy(); + + newNode.children.clear(); + for (TreeElement child : children) { + child.setMult(TreeReference.DEFAULT_MULTIPLICITY); + newNode.addChild(child.deepCopyForRepeat()); + } + return newNode; + } + /* ==== MODEL PROPERTIES ==== */ // factoring inheritance rules diff --git a/src/test/java/org/javarosa/core/model/TriggerableDagTest.java b/src/test/java/org/javarosa/core/model/TriggerableDagTest.java index 297fe0b0f..7476980e3 100644 --- a/src/test/java/org/javarosa/core/model/TriggerableDagTest.java +++ b/src/test/java/org/javarosa/core/model/TriggerableDagTest.java @@ -1831,11 +1831,12 @@ public void addingNestedRepeatInstance_updatesExpressionTriggeredByGenericRef_fo scenario.createNewRepeat("/data/outer[1]/inner"); scenario.createNewRepeat("/data/outer[1]/inner"); - assertThat(scenario.answerOf("/data/outer[0]/inner[0]/count"), is(intAnswer(3))); - assertThat(scenario.answerOf("/data/outer[0]/inner[1]/count"), is(intAnswer(3))); - assertThat(scenario.answerOf("/data/outer[0]/inner[2]/count"), is(intAnswer(3))); - assertThat(scenario.answerOf("/data/outer[1]/inner[0]/count"), is(intAnswer(2))); - assertThat(scenario.answerOf("/data/outer[1]/inner[1]/count"), is(intAnswer(2))); + //For #4059 fix + assertThat(scenario.answerOf("/data/outer[0]/inner[0]/count"), is(intAnswer(4))); + assertThat(scenario.answerOf("/data/outer[0]/inner[1]/count"), is(intAnswer(4))); + assertThat(scenario.answerOf("/data/outer[0]/inner[2]/count"), is(intAnswer(4))); + assertThat(scenario.answerOf("/data/outer[1]/inner[0]/count"), is(intAnswer(3))); + assertThat(scenario.answerOf("/data/outer[1]/inner[1]/count"), is(intAnswer(3))); } @Test diff --git a/src/test/java/org/javarosa/form/api/FormNavigationTestCase.java b/src/test/java/org/javarosa/form/api/FormNavigationTestCase.java index 91b2ac6a0..a342c9f3f 100644 --- a/src/test/java/org/javarosa/form/api/FormNavigationTestCase.java +++ b/src/test/java/org/javarosa/form/api/FormNavigationTestCase.java @@ -14,8 +14,8 @@ @RunWith(Parameterized.class) public class FormNavigationTestCase { - private String formName; - private String[] expectedIndices; + private final String formName; + private final String[] expectedIndices; @Parameterized.Parameters(name = "{0}") public static Iterable data() { @@ -36,7 +36,9 @@ public static Iterable data() { ei("twoNestedRepeatGroups.xml", "-1, ", "0_0, ", "0_0, 0_0, ", "0_0, 0_0, 0, ", "0_0, 0_0, 1, ", "0_0, 0_1, ", - "0_0, 0_1, 0, ", "0_0, 0_1, 1, ", "0_0, 0_2, ", "0_1, ", "-1, "), + "0_0, 0_1, 0, ", "0_0, 0_1, 1, ", "0_0, 0_2, ", + //For #4059 fix + "0_0, 0_2, 0, ", "0_0, 0_2, 1, ", "0_0, 0_3, ", "0_1, ", "-1, "), ei("simpleFormWithThreeQuestions.xml", "-1, ", "0, ", "1, ", "2, ", "-1, ")