Search in sources :

Example 6 with InstanceInitializationFactory

use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.

the class GeoShapeAreaTest method testGeoShapeSupportForEnclosedArea.

public void testGeoShapeSupportForEnclosedArea() throws Exception {
    // Read the form definition
    String FORM_NAME = (new File(PathConst.getTestResourcePath(), "area.xml")).getAbsolutePath();
    InputStream is = null;
    FormDef formDef = null;
    is = new FileInputStream(new File(FORM_NAME));
    formDef = XFormUtils.getFormFromInputStream(is);
    // trigger all calculations
    formDef.initialize(true, new InstanceInitializationFactory());
    // get the calculated area
    IAnswerData areaResult = formDef.getMainInstance().getRoot().getChildAt(1).getValue();
    assertTrue((int) Math.rint((Double) areaResult.getValue()) == 151452);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormDef(org.javarosa.core.model.FormDef) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 7 with InstanceInitializationFactory

use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.

the class XFormParserTest method parseFormWithSetValueAction.

@Test
public void parseFormWithSetValueAction() throws IOException {
    // Given & When
    ParseResult parseResult = parse(r("form-with-setvalue-action.xml"));
    FormDef formDef = parseResult.formDef;
    // dispatch 'xforms-ready' action (Action.EVENT_XFORMS_READY)
    formDef.initialize(true, new InstanceInitializationFactory());
    // Then
    assertEquals(formDef.getTitle(), "SetValue action");
    assertNoParseErrors(parseResult);
    assertEquals(1, formDef.getEventListeners(Action.EVENT_XFORMS_READY).size());
    TreeElement textNode = formDef.getMainInstance().getRoot().getChildrenWithName("text").get(0);
    assertEquals("Test Value", textNode.getValue().getValue());
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) ParseResult(org.javarosa.xform.parse.FormParserHelper.ParseResult) FormDef(org.javarosa.core.model.FormDef) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Test(org.junit.Test)

Example 8 with InstanceInitializationFactory

use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.

the class Safe2014DagImplTest method deleteThirdRepeatGroup_evaluatesTriggerables_indirectlyDependentOnTheRepeatGroupsNumber.

/**
 * Indirectly means that the calculation - `concat(/rgwp/houseM/name)` - does not take the
 * `/rgwp/houseM` nodeset (the repeat group) as an argument
 * but since it takes one of its children (`name` children),
 * the calculation must re-evaluated once after a repeat group deletion because one of the children
 * has been deleted along with its parent (the repeat group instance).
 */
@Test
public void deleteThirdRepeatGroup_evaluatesTriggerables_indirectlyDependentOnTheRepeatGroupsNumber() throws Exception {
    // Given
    final FormDef formDef = parse(r("calculation-indirectly-dependent-on-the-repeat-groups-number.xml")).formDef;
    assertIDagImplUnderTest(formDef);
    // trigger all calculations
    formDef.initialize(false, new InstanceInitializationFactory());
    // it's important to set the test event notifier now to avoid storing events from the above initialization
    formDef.setEventNotifier(eventNotifier);
    final FormInstance mainInstance = formDef.getMainInstance();
    final TreeElement elementToBeDeleted = mainInstance.getRoot().getChildAt(2);
    final TreeReference elementToBeDeletedRef = elementToBeDeleted.getRef();
    // Index pointing to the second repeat group
    final FormIndex indexToBeDeleted = new FormIndex(0, 2, elementToBeDeletedRef);
    // When
    TreeElement summaryNode = mainInstance.getRoot().getChildrenWithName("summary").get(0);
    assertThat(summaryNode.getValue().getDisplayText(), equalTo("ABCDE"));
    // Safe2014DagImplTest.deleteRepeatGroup is called by the below method
    formDef.deleteRepeat(indexToBeDeleted);
    // Then
    final List<TreeElement> repeats = mainInstance.getRoot().getChildrenWithName("houseM");
    assertThat(repeats.size(), equalTo(4));
    assertThat(repeats.get(0).getChildAt(0).getValue().getDisplayText(), equalTo("A"));
    assertThat(repeats.get(1).getChildAt(0).getValue().getDisplayText(), equalTo("B"));
    assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("D"));
    assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("E"));
    assertThat(summaryNode.getValue().getDisplayText(), equalTo("ABDE"));
    // check that correct calculations were triggered
    final String[] expectedMessages = { "Processing 'Deleted: houseM [3]: 0 triggerables were fired.' for ", "Processing 'Recalculate' for summary [1] (ABDE)", "Processing 'Deleted: name [3_1]: 1 triggerables were fired.' for ", "Processing 'Deleted: houseM [4]: 0 triggerables were fired.' for " };
    assertThat(dagEvents.size(), equalTo(expectedMessages.length));
    int messageIndex = 0;
    for (String expectedMessage : expectedMessages) {
        assertThat(dagEvents.get(messageIndex++).getDisplayMessage(), equalTo(expectedMessage));
    }
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) TreeReference(org.javarosa.core.model.instance.TreeReference) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 9 with InstanceInitializationFactory

use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.

the class Safe2014DagImplTest method deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnTheParentPosition.

@Test
public void deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnTheParentPosition() throws Exception {
    // Given
    final FormDef formDef = parse(r("repeat-group-with-children-calculations-dependent-on-the-parent.xml")).formDef;
    assertIDagImplUnderTest(formDef);
    // trigger all calculations
    formDef.initialize(false, new InstanceInitializationFactory());
    // it's important to set the test event notifier now to avoid storing events from the above initialization
    formDef.setEventNotifier(eventNotifier);
    final FormInstance mainInstance = formDef.getMainInstance();
    final TreeElement elementToBeDeleted = mainInstance.getRoot().getChildAt(2);
    final TreeReference elementToBeDeletedRef = elementToBeDeleted.getRef();
    // Index pointing to the second repeat group
    final FormIndex indexToBeDeleted = new FormIndex(0, 1, elementToBeDeletedRef);
    // When
    // Safe2014DagImplTest.deleteRepeatGroup is called by the below method
    formDef.deleteRepeat(indexToBeDeleted);
    // Then
    final List<TreeElement> repeats = mainInstance.getRoot().getChildrenWithName("houseM");
    // check the values based on the position of the parents
    assertThat(repeats.get(0).getChildAt(0).getValue().getDisplayText(), equalTo("1"));
    assertThat(repeats.get(0).getChildAt(2).getValue().getDisplayText(), equalTo("A1"));
    assertThat(repeats.get(1).getChildAt(0).getValue().getDisplayText(), equalTo("2"));
    assertThat(repeats.get(1).getChildAt(2).getValue().getDisplayText(), equalTo("C2"));
    assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("3"));
    assertThat(repeats.get(2).getChildAt(2).getValue().getDisplayText(), equalTo("D3"));
    assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("4"));
    assertThat(repeats.get(3).getChildAt(2).getValue().getDisplayText(), equalTo("E4"));
    // check that correct calculations were triggered
    final String[] expectedMessages = { "Processing 'Recalculate' for no [2_1] (2.0)", "Processing 'Recalculate' for name_and_no [2_1] (C2)", "Processing 'Deleted: houseM [2]: 2 triggerables were fired.' for ", "Processing 'Deleted: no [2_1]: 0 triggerables were fired.' for ", "Processing 'Deleted: name [2_1]: 0 triggerables were fired.' for ", "Processing 'Deleted: name_and_no [2_1]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [3_1] (3.0)", "Processing 'Recalculate' for name_and_no [3_1] (D3)", "Processing 'Deleted: houseM [3]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [4_1] (4.0)", "Processing 'Recalculate' for name_and_no [4_1] (E4)", "Processing 'Deleted: houseM [4]: 2 triggerables were fired.' for " };
    assertThat(dagEvents.size(), equalTo(expectedMessages.length));
    int messageIndex = 0;
    for (String expectedMessage : expectedMessages) {
        assertThat(dagEvents.get(messageIndex++).getDisplayMessage(), equalTo(expectedMessage));
    }
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) TreeReference(org.javarosa.core.model.instance.TreeReference) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 10 with InstanceInitializationFactory

use of org.javarosa.core.model.instance.InstanceInitializationFactory in project javarosa by opendatakit.

the class Safe2014DagImplTest method deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnFollowingRepeatGroupSiblings.

@Test
public void deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnFollowingRepeatGroupSiblings() throws Exception {
    // Given
    final FormDef formDef = parse(r("repeat-group-with-children-position-calculation.xml")).formDef;
    assertIDagImplUnderTest(formDef);
    // trigger all calculations
    formDef.initialize(false, new InstanceInitializationFactory());
    // it's important to set the test event notifier now to avoid storing events from the above initialization
    formDef.setEventNotifier(eventNotifier);
    final FormInstance mainInstance = formDef.getMainInstance();
    final TreeElement elementToBeDeleted = mainInstance.getRoot().getChildAt(2);
    final TreeReference elementToBeDeletedRef = elementToBeDeleted.getRef();
    // Index pointing to the second repeat group
    final FormIndex indexToBeDeleted = new FormIndex(0, 1, elementToBeDeletedRef);
    // When
    // Safe2014DagImplTest.deleteRepeatGroup is called by the below method
    formDef.deleteRepeat(indexToBeDeleted);
    // Then
    final List<TreeElement> repeats = mainInstance.getRoot().getChildrenWithName("houseM");
    // check the values based on the position of the parents
    assertThat(repeats.get(0).getChildAt(0).getValue().getDisplayText(), equalTo("1"));
    assertThat(repeats.get(1).getChildAt(0).getValue().getDisplayText(), equalTo("2"));
    assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("3"));
    assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("4"));
    // check that correct calculations were triggered
    final String[] expectedMessages = { "Processing 'Recalculate' for no [2_1] (2.0)", "Processing 'Deleted: houseM [2]: 1 triggerables were fired.' for ", "Processing 'Deleted: no [2_1]: 1 triggerables were fired.' for ", "Processing 'Recalculate' for no [3_1] (3.0)", "Processing 'Deleted: houseM [3]: 1 triggerables were fired.' for ", "Processing 'Recalculate' for no [4_1] (4.0)", "Processing 'Deleted: houseM [4]: 1 triggerables were fired.' for " };
    assertThat(dagEvents.size(), equalTo(expectedMessages.length));
    int messageIndex = 0;
    for (String expectedMessage : expectedMessages) {
        assertThat(dagEvents.get(messageIndex++).getDisplayMessage(), equalTo(expectedMessage));
    }
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) TreeReference(org.javarosa.core.model.instance.TreeReference) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Aggregations

InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)10 TreeElement (org.javarosa.core.model.instance.TreeElement)7 Test (org.junit.Test)7 FormInstance (org.javarosa.core.model.instance.FormInstance)6 TreeReference (org.javarosa.core.model.instance.TreeReference)6 File (java.io.File)2 FormDef (org.javarosa.core.model.FormDef)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 FormParseInit (org.javarosa.core.test.FormParseInit)1 ParseResult (org.javarosa.xform.parse.FormParserHelper.ParseResult)1 XPathTypeMismatchException (org.javarosa.xpath.XPathTypeMismatchException)1 Before (org.junit.Before)1