Skip to content

Commit

Permalink
Merge pull request #802 from grzesiek2010/fixed_partial_elements
Browse files Browse the repository at this point in the history
Fixed detecting partial elements that should be populated
  • Loading branch information
seadowg authored Oct 17, 2024
2 parents 3efbb35 + 4102e51 commit 6ce1352
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 34 deletions.
12 changes: 7 additions & 5 deletions src/main/java/org/javarosa/core/model/instance/DataInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ public T resolveReference(TreeReference ref) {
AbstractTreeElement<T> node = getBase();
T result = null;
for (int i = 0; i < ref.size(); i++) {
if (node instanceof TreeElement && ((TreeElement) node).isPartial()) {
throw new PartialElementEncounteredException();
}

String name = ref.getName(i);
int mult = ref.getMultiplicity(i);

Expand All @@ -110,7 +106,13 @@ public T resolveReference(TreeReference ref) {
}
}

return (node == getBase() ? null : result); // never return a reference to '/'
if (node == getBase() || result == null) {
return null; // never return a reference to '/'
} else if (((TreeElement) result).isPartial()) {
throw new PartialElementEncounteredException();
} else {
return result;
}
}

public List<AbstractTreeElement<T>> explodeReference(TreeReference ref) {
Expand Down
87 changes: 58 additions & 29 deletions src/test/java/org/javarosa/plugins/InstancePluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,39 @@ public void instanceProvider_supportsPartialElements() throws IOException, XForm
assertThat(fakeInstance.getRoot().getNumChildren(), equalTo(2));

TreeElement firstItem = (TreeElement) fakeInstance.getRoot().getChild("item", 0);
assertThat(firstItem.isPartial(), equalTo(true));
assertThat(firstItem.isPartial(), equalTo(false));
assertThat(firstItem.getNumChildren(), equalTo(2));
assertThat(firstItem.getChildAt(0).getName(), equalTo("value"));
assertThat(firstItem.getChildAt(0).getValue(), equalTo(null));
assertThat(firstItem.getChildAt(0).getValue(), equalTo(new StringData("0")));
assertThat(firstItem.getChildAt(1).getName(), equalTo("label"));
assertThat(firstItem.getChildAt(1).getValue(), equalTo(null));
assertThat(firstItem.getChildAt(1).getValue(), equalTo(new StringData("Item 0")));

TreeElement secondItem = (TreeElement) fakeInstance.getRoot().getChild("item", 1);
assertThat(secondItem.isPartial(), equalTo(true));
assertThat(secondItem.getNumChildren(), equalTo(0));

List<SelectChoice> selectChoices = scenario.choicesOf("/data/question");
assertThat(selectChoices.size(), equalTo(2));

assertThat(selectChoices.get(0).getLabelInnerText(), equalTo("Item 0"));
assertThat(selectChoices.get(0).getValue(), equalTo("0"));
assertThat(selectChoices.get(1).getLabelInnerText(), equalTo("Item 1"));
assertThat(selectChoices.get(1).getValue(), equalTo("1"));

firstItem = (TreeElement) fakeInstance.getRoot().getChild("item", 0);
assertThat(firstItem.isPartial(), equalTo(false));
assertThat(firstItem.getNumChildren(), equalTo(2));
assertThat(firstItem.getChildAt(0).getName(), equalTo("value"));
assertThat(firstItem.getChildAt(0).getValue(), equalTo(new StringData("0")));
assertThat(firstItem.getChildAt(1).getName(), equalTo("label"));
assertThat(firstItem.getChildAt(1).getValue(), equalTo(new StringData("Item 0")));

secondItem = (TreeElement) fakeInstance.getRoot().getChild("item", 1);
assertThat(secondItem.isPartial(), equalTo(false));
assertThat(secondItem.getNumChildren(), equalTo(2));
assertThat(secondItem.getChildAt(0).getName(), equalTo("value"));
assertThat(secondItem.getChildAt(0).getValue(), equalTo(new StringData("1")));
assertThat(secondItem.getChildAt(1).getName(), equalTo("label"));
assertThat(secondItem.getChildAt(1).getValue(), equalTo(new StringData("Item 1")));
}

@Test
Expand Down Expand Up @@ -137,24 +152,39 @@ public void fileInstanceParser_supportsPartialElements() throws IOException, XFo
assertThat(fakeInstance.getRoot().getNumChildren(), equalTo(2));

TreeElement firstItem = (TreeElement) fakeInstance.getRoot().getChild("item", 0);
assertThat(firstItem.isPartial(), equalTo(true));
assertThat(firstItem.isPartial(), equalTo(false));
assertThat(firstItem.getNumChildren(), equalTo(2));
assertThat(firstItem.getChildAt(0).getName(), equalTo("value"));
assertThat(firstItem.getChildAt(0).getValue(), equalTo(null));
assertThat(firstItem.getChildAt(0).getValue(), equalTo(new StringData("0")));
assertThat(firstItem.getChildAt(1).getName(), equalTo("label"));
assertThat(firstItem.getChildAt(1).getValue(), equalTo(null));
assertThat(firstItem.getChildAt(1).getValue(), equalTo(new StringData("Item 0")));

TreeElement secondItem = (TreeElement) fakeInstance.getRoot().getChild("item", 1);
assertThat(secondItem.isPartial(), equalTo(true));
assertThat(secondItem.getNumChildren(), equalTo(0));

List<SelectChoice> selectChoices = scenario.choicesOf("/data/question");
assertThat(selectChoices.size(), equalTo(2));

assertThat(selectChoices.get(0).getLabelInnerText(), equalTo("Item 0"));
assertThat(selectChoices.get(0).getValue(), equalTo("0"));
assertThat(selectChoices.get(1).getLabelInnerText(), equalTo("Item 1"));
assertThat(selectChoices.get(1).getValue(), equalTo("1"));

firstItem = (TreeElement) fakeInstance.getRoot().getChild("item", 0);
assertThat(firstItem.isPartial(), equalTo(false));
assertThat(firstItem.getNumChildren(), equalTo(2));
assertThat(firstItem.getChildAt(0).getName(), equalTo("value"));
assertThat(firstItem.getChildAt(0).getValue(), equalTo(new StringData("0")));
assertThat(firstItem.getChildAt(1).getName(), equalTo("label"));
assertThat(firstItem.getChildAt(1).getValue(), equalTo(new StringData("Item 0")));

secondItem = (TreeElement) fakeInstance.getRoot().getChild("item", 1);
assertThat(secondItem.isPartial(), equalTo(false));
assertThat(secondItem.getNumChildren(), equalTo(2));
assertThat(secondItem.getChildAt(0).getName(), equalTo("value"));
assertThat(secondItem.getChildAt(0).getValue(), equalTo(new StringData("1")));
assertThat(secondItem.getChildAt(1).getName(), equalTo("label"));
assertThat(secondItem.getChildAt(1).getValue(), equalTo(new StringData("Item 1")));
}

@Test
Expand Down Expand Up @@ -189,29 +219,26 @@ public void supportsReplacingPartialElements() throws IOException, XFormParser.P
HashMap<String, DataInstance> instances = scenario.getFormDef().getFormInstances();
DataInstance fakeInstance = instances.get("fake-instance");

TreeElement item = new TreeElement("item", 0);
TreeElement secondItem = (TreeElement) fakeInstance.getRoot().getChild("item", 1);
assertThat(secondItem.isPartial(), equalTo(true));
assertThat(secondItem.getNumChildren(), equalTo(0));

TreeElement item = new TreeElement("item", 1);
TreeElement value = new TreeElement("value");
TreeElement label = new TreeElement("label");
value.setValue(new StringData("0"));
label.setValue(new StringData("Item 0"));
value.setValue(new StringData("1"));
label.setValue(new StringData("Item 1"));
item.addChild(value);
item.addChild(label);
fakeInstance.replacePartialElements(asList(item));

TreeElement firstItem = (TreeElement) fakeInstance.getRoot().getChild("item", 0);
assertThat(firstItem.isPartial(), equalTo(false));
assertThat(firstItem.getNumChildren(), equalTo(2));
assertThat(firstItem.getChildAt(0).getName(), equalTo("value"));
assertThat(firstItem.getChildAt(0).getValue(), equalTo(new StringData("0")));
assertThat(firstItem.getChildAt(1).getName(), equalTo("label"));
assertThat(firstItem.getChildAt(1).getValue(), equalTo(new StringData("Item 0")));
TreeElement secondItem = (TreeElement) fakeInstance.getRoot().getChild("item", 1);
assertThat(secondItem.isPartial(), equalTo(true));
secondItem = (TreeElement) fakeInstance.getRoot().getChild("item", 1);
assertThat(secondItem.isPartial(), equalTo(false));
assertThat(secondItem.getNumChildren(), equalTo(2));
assertThat(secondItem.getChildAt(0).getName(), equalTo("value"));
assertThat(secondItem.getChildAt(0).getValue(), equalTo(null));
assertThat(secondItem.getChildAt(0).getValue(), equalTo(new StringData("1")));
assertThat(secondItem.getChildAt(1).getName(), equalTo("label"));
assertThat(secondItem.getChildAt(1).getValue(), equalTo(null));
assertThat(secondItem.getChildAt(1).getValue(), equalTo(new StringData("Item 1")));
}

@Test
Expand Down Expand Up @@ -327,21 +354,23 @@ public boolean isSupported(@NotNull String instanceId, @NotNull String instanceS
}

private @NotNull TreeElement createRoot(boolean partial) {
boolean isPartial = partialParse && partial;
TreeElement root = new TreeElement("root", 0);

for (int i = 0; i < items.size(); i++) {
TreeElement value = new TreeElement("value");
TreeElement label = new TreeElement("label");
boolean isPartial = partialParse && partial && i != 0;

TreeElement item = new TreeElement("item", i, isPartial);

if (!isPartial) {
TreeElement value = new TreeElement("value");
TreeElement label = new TreeElement("label");

value.setValue(new StringData(items.get(i).getFirst()));
label.setValue(new StringData(items.get(i).getSecond()));
}

TreeElement item = new TreeElement("item", i, isPartial);
item.addChild(value);
item.addChild(label);
item.addChild(value);
item.addChild(label);
}

root.addChild(item);
}
Expand Down

0 comments on commit 6ce1352

Please sign in to comment.