Skip to content

Commit

Permalink
Merge pull request #744 from lognaturel/header-only
Browse files Browse the repository at this point in the history
Use placeholder root for empty external secondary instance
  • Loading branch information
seadowg committed Feb 1, 2024
2 parents 7eb5802 + bd0abfe commit f5012d9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.javarosa.core.model.instance;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.javarosa.core.model.instance.geojson.GeoJsonExternalInstance;
import org.javarosa.core.reference.InvalidReferenceException;
import org.javarosa.core.reference.ReferenceManager;
Expand All @@ -18,6 +14,11 @@
import org.slf4j.LoggerFactory;
import org.xmlpull.v1.XmlPullParserException;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

// This is still a work in progress.

public class ExternalDataInstance extends DataInstance {
Expand Down Expand Up @@ -62,6 +63,11 @@ public static ExternalDataInstance build(String instanceSrc, String instanceId)
TreeElement root;
try {
root = parseExternalInstance(instanceSrc, instanceId);

// Avoid parse error for missing name and label refs if a select is built on an empty placeholder file
if (!root.hasChildren()) {
root = PLACEHOLDER_ROOT;
}
} catch (FileNotFoundException | InvalidReferenceException e) {
logger.info("External instance not found, falling back to placeholder");
root = PLACEHOLDER_ROOT;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package org.javarosa.xform.parse;

import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.SelectChoice;
import org.javarosa.core.model.condition.EvaluationContext;
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.test.FormParseInit;
import org.javarosa.core.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;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -22,23 +40,6 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.SelectChoice;
import org.javarosa.core.model.condition.EvaluationContext;
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.test.FormParseInit;
import org.javarosa.core.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;

public class ExternalSecondaryInstanceParseTest {

//region Parsing of different file types into external secondary instances
Expand Down Expand Up @@ -132,6 +133,30 @@ public void xformParseException_whenItemsetConfiguresValueOrLabelNotInExternalIn
}
}

@Test
public void csvSecondaryInstanceWithHeaderOnly_parsesWithoutError() throws IOException, XFormParser.ParseException {
configureReferenceManagerCorrectly();

Scenario scenario = Scenario.init("Some form", html(
head(
title("Some form"),
model(
mainInstance(t("data id=\"some-form\"",
t("first")
)),

t("instance id=\"external-csv\" src=\"jr:https://file-csv/header_only.csv\""),

bind("/data/first").type("string")
)
),
body(
select1Dynamic("/data/first", "instance('external-csv')/root/item")
)));

assertThat(scenario.choicesOf("/data/first").size(), is(0));
}

@Test
public void formWithExternalSecondaryXMLInstance_ShouldSerializeAndDeserialize() throws IOException, DeserializationException, XFormParser.ParseException {
configureReferenceManagerCorrectly();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name,label,first_name,last_name

0 comments on commit f5012d9

Please sign in to comment.