Skip to content

Commit

Permalink
Fix crash if select element has empty child (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg authored Aug 28, 2024
1 parent 8a22ede commit 30ef2a9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/javarosa/core/model/SelectChoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public List<Pair<String, String>> getAdditionalChildren() {
for (int i = 0; i < item.getNumChildren(); i++) {
TreeElement child = item.getChildAt(i);
if (!child.getRef().getNameLast().equals(labelRefName)) {
children.add(new Pair<>(child.getName(), child.getValue().getDisplayText()));
IAnswerData childValue = child.getValue();
children.add(new Pair<>(child.getName(), childValue != null ? childValue.getDisplayText() : ""));
}
}
return children;
Expand Down
55 changes: 44 additions & 11 deletions src/test/java/org/javarosa/core/model/SelectChoiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@

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.XFormParser;
import org.junit.Test;

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

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
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 @@ -38,16 +50,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 java.io.IOException;
import java.util.List;
import kotlin.Pair;
import org.hamcrest.CoreMatchers;
import org.javarosa.test.Scenario;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.xform.parse.XFormParser;
import org.junit.Test;

public class SelectChoiceTest {
@Test
Expand Down Expand Up @@ -115,7 +117,7 @@ public void getChild_returnsEmptyString_whenChoicesAreFromSecondaryInstance_andR
))),
body(
select1Dynamic("/data/select", "instance('choices')/root/item", "name", "label"))
));
));

assertThat(scenario.choicesOf("/data/select").get(0).getChild("property"), is(""));
}
Expand Down Expand Up @@ -241,4 +243,35 @@ public void getAdditionalChildren_returnsEmpty_whenCalledOnAChoiceFromInlineSele

assertThat(scenario.choicesOf("/data/select").get(0).getAdditionalChildren(), is(empty()));
}

@Test
public void getAdditionalChildren_returnsEmptyStringValue_forEmptyChildren() throws IOException, XFormParser.ParseException {
Scenario scenario = Scenario.init("Internal instance select", html(
head(
title("Internal instance select"),
model(
mainInstance(
t("data id='static-select'",
t("select")
)
),
instance("instance",
t("item",
t("label", "label"),
t("value", "value"),
t("child")
)
)
)
),
body(
select1Dynamic("/data/select", "instance('instance')/root/item")
))
);

assertThat(
scenario.choicesOf("/data/select").get(0).getAdditionalChildren(),
is(contains(new Pair<>("value", "value"), new Pair<>("child", "")))
);
}
}

0 comments on commit 30ef2a9

Please sign in to comment.