-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #770 from seadowg/label-type
Fix crash for non-`String` entity labels
- Loading branch information
Showing
3 changed files
with
56 additions
and
78 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/main/java/org/javarosa/entities/internal/EntityConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.javarosa.entities.internal; | ||
|
||
public class EntityConstants { | ||
|
||
private EntityConstants() {} | ||
|
||
public static final String ELEMENT_ENTITY = "entity"; | ||
public static final String ELEMENT_LABEL = "label"; | ||
|
||
public static final String ATTRIBUTE_DATASET = "dataset"; | ||
public static final String ATTRIBUTE_ID = "id"; | ||
public static final String ATTRIBUTE_BASE_VERSION = "baseVersion"; | ||
public static final String ATTRIBUTE_CREATE = "create"; | ||
public static final String ATTRIBUTE_UPDATE = "update"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 25 additions & 70 deletions
95
src/test/java/org/javarosa/entities/EntityFormParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,45 @@ | ||
package org.javarosa.entities; | ||
|
||
import kotlin.Pair; | ||
import org.javarosa.core.model.FormDef; | ||
import org.javarosa.core.util.XFormsElement; | ||
import org.javarosa.core.model.data.IntegerData; | ||
import org.javarosa.core.model.instance.TreeElement; | ||
import org.javarosa.entities.internal.EntityFormParser; | ||
import org.javarosa.xform.parse.XFormParser; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStreamReader; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.javarosa.core.util.BindBuilderXFormsElement.bind; | ||
import static org.javarosa.core.util.XFormsElement.body; | ||
import static org.javarosa.core.util.XFormsElement.head; | ||
import static org.javarosa.core.util.XFormsElement.input; | ||
import static org.javarosa.core.util.XFormsElement.mainInstance; | ||
import static org.javarosa.core.util.XFormsElement.model; | ||
import static org.javarosa.core.util.XFormsElement.t; | ||
import static org.javarosa.core.util.XFormsElement.title; | ||
import static org.javarosa.entities.internal.EntityConstants.ATTRIBUTE_CREATE; | ||
import static org.javarosa.entities.internal.EntityConstants.ATTRIBUTE_UPDATE; | ||
import static org.javarosa.entities.internal.EntityConstants.ELEMENT_ENTITY; | ||
import static org.javarosa.entities.internal.EntityConstants.ELEMENT_LABEL; | ||
|
||
public class EntityFormParserTest { | ||
|
||
@Test | ||
public void parseAction_findsCreateWithTrueString() throws XFormParser.ParseException { | ||
XFormsElement form = XFormsElement.html( | ||
asList( | ||
new Pair<>("entities", "http://www.opendatakit.org/xforms/entities") | ||
), | ||
head( | ||
title("Create entity form"), | ||
model( | ||
mainInstance( | ||
t("data id=\"create-entity-form\"", | ||
t("name"), | ||
t("meta", | ||
t("entity dataset=\"people\" create=\"true\"") | ||
) | ||
) | ||
), | ||
bind("/data/name").type("string").withAttribute("entities", "saveto", "name") | ||
) | ||
), | ||
body( | ||
input("/data/name") | ||
) | ||
); | ||
|
||
XFormParser parser = new XFormParser(new InputStreamReader(new ByteArrayInputStream(form.asXml().getBytes()))); | ||
FormDef formDef = parser.parse(null); | ||
public void parseAction_findsCreateWithTrueString() { | ||
TreeElement entityElement = new TreeElement(ELEMENT_ENTITY); | ||
entityElement.setAttribute(null, ATTRIBUTE_CREATE, "true"); | ||
|
||
EntityAction action = EntityFormParser.parseAction(EntityFormParser.getEntityElement(formDef.getMainInstance())); | ||
EntityAction action = EntityFormParser.parseAction(entityElement); | ||
assertThat(action, equalTo(EntityAction.CREATE)); | ||
} | ||
|
||
@Test | ||
public void parseAction_findsUpdateWithTrueString() throws XFormParser.ParseException { | ||
XFormsElement form = XFormsElement.html( | ||
asList( | ||
new Pair<>("entities", "http://www.opendatakit.org/xforms/entities") | ||
), | ||
head( | ||
title("Create entity form"), | ||
model( | ||
mainInstance( | ||
t("data id=\"create-entity-form\"", | ||
t("name"), | ||
t("meta", | ||
t("entity dataset=\"people\" update=\"true\"") | ||
) | ||
) | ||
), | ||
bind("/data/name").type("string").withAttribute("entities", "saveto", "name") | ||
) | ||
), | ||
body( | ||
input("/data/name") | ||
) | ||
); | ||
|
||
XFormParser parser = new XFormParser(new InputStreamReader(new ByteArrayInputStream(form.asXml().getBytes()))); | ||
FormDef formDef = parser.parse(null); | ||
public void parseAction_findsUpdateWithTrueString() { | ||
TreeElement entityElement = new TreeElement(ELEMENT_ENTITY); | ||
entityElement.setAttribute(null, ATTRIBUTE_UPDATE, "true"); | ||
|
||
EntityAction dataset = EntityFormParser.parseAction(EntityFormParser.getEntityElement(formDef.getMainInstance())); | ||
EntityAction dataset = EntityFormParser.parseAction(entityElement); | ||
assertThat(dataset, equalTo(EntityAction.UPDATE)); | ||
} | ||
|
||
@Test | ||
public void parseLabel_whenLabelIsAnInt_convertsToString() { | ||
TreeElement labelElement = new TreeElement(ELEMENT_LABEL); | ||
labelElement.setAnswer(new IntegerData(0)); | ||
TreeElement entityElement = new TreeElement(ELEMENT_ENTITY); | ||
entityElement.addChild(labelElement); | ||
|
||
String label = EntityFormParser.parseLabel(entityElement); | ||
assertThat(label, equalTo("0")); | ||
} | ||
} |