Search in sources :

Example 16 with TreeElement

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

the class TreeElementTests method testPopulate_withNodesAttributes.

@Test
public void testPopulate_withNodesAttributes() {
    // Given
    FormParseInit formParseInit = new FormParseInit();
    formParseInit.setFormToParse(Paths.get(PathConst.getTestResourcePath().getAbsolutePath(), "populate-nodes-attributes.xml").toString());
    FormEntryController formEntryController = formParseInit.getFormEntryController();
    byte[] formInstanceAsBytes = null;
    try {
        formInstanceAsBytes = Files.readAllBytes(Paths.get(PathConst.getTestResourcePath().getAbsolutePath(), "populate-nodes-attributes-instance.xml"));
    } catch (IOException e) {
        fail("There was a problem with reading the test data.\n" + e.getMessage());
    }
    TreeElement savedRoot = XFormParser.restoreDataModel(formInstanceAsBytes, null).getRoot();
    FormDef formDef = formEntryController.getModel().getForm();
    TreeElement dataRootNode = formDef.getInstance().getRoot().deepCopy(true);
    // When
    dataRootNode.populate(savedRoot, formDef);
    // Then
    assertEquals(2, dataRootNode.getNumChildren());
    TreeElement freeText1Question = dataRootNode.getChildAt(0);
    TreeElement regularGroup = dataRootNode.getChildAt(1);
    assertEquals(1, regularGroup.getNumChildren());
    TreeElement freeText2Question = regularGroup.getChildAt(0);
    assertEquals("free_text_1", freeText1Question.getName());
    assertEquals(1, freeText1Question.getAttributeCount());
    TreeElement customAttr1 = freeText1Question.getAttribute(null, "custom_attr_1");
    assertNotNull(customAttr1);
    assertEquals("custom_attr_1", customAttr1.getName());
    assertEquals("", customAttr1.getNamespace());
    assertEquals("xyz1", customAttr1.getAttributeValue());
    assertEquals("regular_group", regularGroup.getName());
    assertEquals(1, regularGroup.getAttributeCount());
    customAttr1 = regularGroup.getAttribute(null, "custom_attr_1");
    assertNotNull(customAttr1);
    assertEquals("custom_attr_1", customAttr1.getName());
    assertEquals("custom_name_space", customAttr1.getNamespace());
    assertEquals("xyz2", customAttr1.getAttributeValue());
    assertEquals("free_text_2", freeText2Question.getName());
    assertEquals(1, freeText1Question.getAttributeCount());
    TreeElement customAttr2 = freeText2Question.getAttribute(null, "custom_attr_2");
    assertNotNull(customAttr2);
    assertEquals("custom_attr_2", customAttr2.getName());
    assertEquals("", customAttr2.getNamespace());
    assertEquals("xyz3", customAttr2.getAttributeValue());
}
Also used : FormEntryController(org.javarosa.form.api.FormEntryController) FormDef(org.javarosa.core.model.FormDef) IOException(java.io.IOException) FormParseInit(org.javarosa.core.test.FormParseInit) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 17 with TreeElement

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

the class QuestionDataGroupTests method testAddLeafChild.

public void testAddLeafChild() {
    // boolean threw = false;
    boolean added = false;
    try {
        group.addChild(stringElement);
        group.getChildAt(0);
        assertTrue("Added element was not in Question Data Group's children!", group.getChildAt(0).equals(stringElement));
    } catch (RuntimeException e) {
        if (!added) {
            fail("Group did not report success adding a valid child");
        }
    }
    try {
        TreeElement leafGroup = new TreeElement("leaf group");
        group.addChild(leafGroup);
        assertTrue("Added element was not in Question Data Group's children", group.getChildAt(1).equals(leafGroup));
    } catch (RuntimeException e) {
        if (!added) {
            fail("Group did not report success adding a valid child");
        }
    // threw = true;
    }
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 18 with TreeElement

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

the class QuestionDataGroupTests method testSetName.

public void testSetName() {
    String name = "TestGroup";
    group = new TreeElement(name);
    String newName = "TestGroupNew";
    group.setName(newName);
    assertEquals("Question Data Group did not properly get its name", group.getName(), newName);
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 19 with TreeElement

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

the class QuestionDataGroupTests method setUp.

public void setUp() throws Exception {
    super.setUp();
    stringData = new StringData("Answer Value");
    integerData = new IntegerData(4);
    stringReference = new IDataReference() {

        String reference = "stringValue";

        public Object getReference() {
            return reference;
        }

        public void setReference(Object reference) {
            this.reference = (String) reference;
        }

        /*
            public boolean referenceMatches(IDataReference reference) {
                return this.reference.equals(reference.getReference());
            }


            public IDataReference clone()  {
                IDataReference newReference = null;
                try {
                    newReference = (IDataReference)this.getClass().newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                newReference.setReference(reference);
                return newReference;
            }
            */
        public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
        }

        public void writeExternal(DataOutputStream out) throws IOException {
        }
    };
    integerReference = new IDataReference() {

        Integer intReference = new Integer(15);

        public Object getReference() {
            return intReference;
        }

        public void setReference(Object reference) {
            this.intReference = (Integer) reference;
        }

        /*
            public boolean referenceMatches(IDataReference reference) {
                return this.intReference.equals(reference.getReference());
            }


            public IDataReference clone()  {
                IDataReference newReference = null;
                try {
                    newReference = (IDataReference)this.getClass().newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                newReference.setReference(intReference);
                return newReference;
            }
            */
        public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
        }

        public void writeExternal(DataOutputStream out) throws IOException {
        }
    };
    intElement = new TreeElement("intElement");
    intElement.setValue(integerData);
    stringElement = new TreeElement(stringElementName);
    stringElement.setValue(stringData);
    group = new TreeElement(groupName);
}
Also used : PrototypeFactory(org.javarosa.core.util.externalizable.PrototypeFactory) IDataReference(org.javarosa.core.model.IDataReference) DataOutputStream(java.io.DataOutputStream) IntegerData(org.javarosa.core.model.data.IntegerData) IOException(java.io.IOException) StringData(org.javarosa.core.model.data.StringData) DataInputStream(java.io.DataInputStream) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 20 with TreeElement

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

the class Safe2014DagImplTest method deleteThirdRepeatGroup_evaluatesTriggerables_dependentOnTheRepeatGroupsNumber.

@Test
public void deleteThirdRepeatGroup_evaluatesTriggerables_dependentOnTheRepeatGroupsNumber() throws Exception {
    // Given
    final FormDef formDef = parse(r("calculation-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);
    // check the calculation result for 10 repeat groups
    assertThat(summaryNode.getValue().getDisplayText(), equalTo("55"));
    // 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"));
    assertThat(repeats.get(4).getChildAt(0).getValue().getDisplayText(), equalTo("5"));
    assertThat(repeats.get(5).getChildAt(0).getValue().getDisplayText(), equalTo("6"));
    assertThat(repeats.get(6).getChildAt(0).getValue().getDisplayText(), equalTo("7"));
    assertThat(repeats.get(7).getChildAt(0).getValue().getDisplayText(), equalTo("8"));
    assertThat(repeats.get(8).getChildAt(0).getValue().getDisplayText(), equalTo("9"));
    assertThat(summaryNode.getValue().getDisplayText(), equalTo("45"));
    // check that correct calculations were triggered
    final String[] expectedMessages = { "Processing 'Recalculate' for no [3_1] (3.0)", "Processing 'Recalculate' for summary [1] (51.0)", "Processing 'Deleted: houseM [3]: 2 triggerables were fired.' for ", "Processing 'Deleted: no [3_1]: 0 triggerables were fired.' for ", "Processing 'Recalculate' for no [4_1] (4.0)", "Processing 'Recalculate' for summary [1] (50.0)", "Processing 'Deleted: houseM [4]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [5_1] (5.0)", "Processing 'Recalculate' for summary [1] (49.0)", "Processing 'Deleted: houseM [5]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [6_1] (6.0)", "Processing 'Recalculate' for summary [1] (48.0)", "Processing 'Deleted: houseM [6]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [7_1] (7.0)", "Processing 'Recalculate' for summary [1] (47.0)", "Processing 'Deleted: houseM [7]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [8_1] (8.0)", "Processing 'Recalculate' for summary [1] (46.0)", "Processing 'Deleted: houseM [8]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [9_1] (9.0)", "Processing 'Recalculate' for summary [1] (45.0)", "Processing 'Deleted: houseM [9]: 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)

Aggregations

TreeElement (org.javarosa.core.model.instance.TreeElement)86 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)33 TreeReference (org.javarosa.core.model.instance.TreeReference)28 FormInstance (org.javarosa.core.model.instance.FormInstance)16 ArrayList (java.util.ArrayList)15 Constraint (org.javarosa.core.model.condition.Constraint)11 Test (org.junit.Test)10 Element (org.kxml2.kdom.Element)9 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)8 FormDef (org.javarosa.core.model.FormDef)7 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)7 IOException (java.io.IOException)6 IFormElement (org.javarosa.core.model.IFormElement)6 StringData (org.javarosa.core.model.data.StringData)6 HashMap (java.util.HashMap)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 File (java.io.File)3 GroupDef (org.javarosa.core.model.GroupDef)3 IDataReference (org.javarosa.core.model.IDataReference)3 QuestionDef (org.javarosa.core.model.QuestionDef)3