Skip to content

Commit

Permalink
Merge pull request #781 from lognaturel/verifyItemsetBindings
Browse files Browse the repository at this point in the history
Document current verifyItemsetBindings behavior
  • Loading branch information
seadowg authored Dec 12, 2024
2 parents a5ea1e9 + 4ff0588 commit ab0e8f4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/javarosa/xform/parse/FormInstanceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,24 +336,24 @@ private void verifyItemsetBindings(FormInstance instance) {

//make sure the labelref is tested against the right instance
//check if it's not the main instance
DataInstance fi = null;
DataInstance secondaryInstance;
if (itemset.labelRef.getInstanceName() != null) {
fi = formDef.getNonMainInstance(itemset.labelRef.getInstanceName());
if (fi == null) {
secondaryInstance = formDef.getNonMainInstance(itemset.labelRef.getInstanceName());
if (secondaryInstance == null) {
throw new XFormParseException("Instance: " + itemset.labelRef.getInstanceName() + " Does not exists");
}
} else {
fi = instance;
secondaryInstance = instance;
}

// Don't try to validate references if the external instance could not be resolved. We allow parsing a form
// with placeholder external secondary instances for cases where a ReferenceManager can't be configured.
if (fi instanceof ExternalDataInstance && !((ExternalDataInstance) fi).isUsingPlaceholder()) {
if (fi.getTemplatePath(itemset.labelRef) == null) {
if (!(secondaryInstance instanceof ExternalDataInstance) || !((ExternalDataInstance) secondaryInstance).isUsingPlaceholder()) {
if (secondaryInstance.getTemplatePath(itemset.labelRef) == null) {
throw new XFormParseException("<label> node for itemset doesn't exist! [" + itemset.labelRef + "]");
}
//check value nodes exist
else if (itemset.valueRef != null && fi.getTemplatePath(itemset.valueRef) == null) {
else if (itemset.valueRef != null && secondaryInstance.getTemplatePath(itemset.valueRef) == null) {
throw new XFormParseException("<value> node for itemset doesn't exist! [" + itemset.valueRef + "]");
}
}
Expand Down
71 changes: 69 additions & 2 deletions src/test/java/org/javarosa/core/model/SelectChoiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package org.javarosa.core.model;

import kotlin.Pair;
import org.hamcrest.CoreMatchers;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.test.Scenario;
import org.javarosa.xform.parse.XFormParseException;
import org.javarosa.xform.parse.XFormParser;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

import kotlin.Pair;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
Expand All @@ -50,6 +51,7 @@
import static org.javarosa.test.XFormsElement.select1Dynamic;
import static org.javarosa.test.XFormsElement.t;
import static org.javarosa.test.XFormsElement.title;
import static org.junit.Assert.fail;

public class SelectChoiceTest {
@Test
Expand Down Expand Up @@ -113,7 +115,7 @@ public void getChild_returnsEmptyString_whenChoicesAreFromSecondaryInstance_andR
t("data id='select-empty'",
t("select"))),
instance("choices",
t("item", t("label", "Item"), t("property", ""))
t("item", t("label", "Item"), t("name", "item"), t("property", ""))
))),
body(
select1Dynamic("/data/select", "instance('choices')/root/item", "name", "label"))
Expand Down Expand Up @@ -274,4 +276,69 @@ public void getAdditionalChildren_returnsEmptyStringValue_forEmptyChildren() thr
is(contains(new Pair<>("value", "value"), new Pair<>("child", "")))
);
}

@Test
public void itemsetBindingVerification_doesNotVerifySecondItem() throws IOException, XFormParser.ParseException {
Scenario.init("Some form", html(
head(
title("Some form"),
model(
mainInstance(t("data id=\"some-form\"",
t("first")
)),

t("instance id=\"mixed-schema\"",
t("root",
t("item",
t("label", "A"),
t("name", "a")
),
t("item",
t("foo")))
),

bind("/data/first").type("string")
)
),
body(
// Define a select using value and label references that only exist for the first item
select1Dynamic("/data/first", "instance('mixed-schema')/root/item", "name", "label")
)));
}

@Test
public void itemsetBindingVerification_verifiesFirstItem() throws IOException {
try {
Scenario.init("Some form", html(
head(
title("Some form"),
model(
mainInstance(t("data id=\"some-form\"",
t("first")
)),

t("instance id=\"mixed-schema\"",
t("root",
t("item",
t("foo", "bar")),
t("item",
t("label", "A"),
t("value", "a")
))
),

bind("/data/first").type("string")
)
),
body(
// Define a select using value and label references that only exist for the second item
select1Dynamic("/data/first", "instance('mixed-schema')/root/item", "name", "label")
)));
fail("Expected XFormParseException because itemset references don't exist in external instance");
} catch (XFormParseException e) {
// pass
} catch (XFormParser.ParseException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import org.javarosa.core.model.data.helper.Selection;
import org.javarosa.core.model.instance.AbstractTreeElement;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.test.FormParseInit;
import org.javarosa.test.Scenario;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.xpath.expr.XPathPathExpr;
import org.javarosa.xpath.parser.XPathSyntaxException;
import org.junit.Test;
Expand All @@ -24,6 +24,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.javarosa.core.reference.ReferenceManagerTestUtils.setUpSimpleReferenceManager;
import static org.javarosa.test.BindBuilderXFormsElement.bind;
import static org.javarosa.test.ResourcePathHelper.r;
import static org.javarosa.test.XFormsElement.body;
import static org.javarosa.test.XFormsElement.head;
import static org.javarosa.test.XFormsElement.html;
Expand All @@ -32,7 +33,6 @@
import static org.javarosa.test.XFormsElement.select1Dynamic;
import static org.javarosa.test.XFormsElement.t;
import static org.javarosa.test.XFormsElement.title;
import static org.javarosa.test.ResourcePathHelper.r;
import static org.javarosa.xform.parse.FormParserHelper.deserializeAndCleanUpSerializedForm;
import static org.javarosa.xform.parse.FormParserHelper.getSerializedFormPath;
import static org.javarosa.xform.parse.FormParserHelper.parse;
Expand Down

0 comments on commit ab0e8f4

Please sign in to comment.