Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash if select element has empty child #793

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", "")))
);
}
}