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

Improve test suite focusing on the DAG's behavior #515

Merged
merged 15 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
207 changes: 136 additions & 71 deletions src/test/java/org/javarosa/core/model/Safe2014DagImplTest.java

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/test/java/org/javarosa/core/test/AnswerDataMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ protected boolean matchesSafely(IntegerData item) {
};
}

public static Matcher<IntegerData> intAnswer(Integer expectedAnswer) {
return new TypeSafeMatcher<IntegerData>() {
@Override
public void describeTo(Description description) {
description.appendText("answer with value " + expectedAnswer);
}

@Override
protected void describeMismatchSafely(IntegerData item, Description mismatchDescription) {
mismatchDescription.appendText("was answer " + item.getDisplayText() + "(").appendValue(item.getValue()).appendText(")");
}

@Override
protected boolean matchesSafely(IntegerData item) {
return item.getValue().equals(expectedAnswer);
}
};
}

public static <T extends IAnswerData> Matcher<T> answer(T expectedAnswer) {
return new TypeSafeMatcher<T>() {
@Override
Expand Down
50 changes: 48 additions & 2 deletions src/test/java/org/javarosa/core/test/Scenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

package org.javarosa.core.test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.createTempDirectory;
import static java.nio.file.Files.createTempFile;
import static java.nio.file.Files.delete;
import static java.nio.file.Files.newInputStream;
import static java.nio.file.Files.newOutputStream;
import static java.nio.file.Files.write;
import static java.nio.file.StandardOpenOption.CREATE;
import static org.javarosa.core.model.instance.TreeReference.CONTEXT_ABSOLUTE;
import static org.javarosa.core.model.instance.TreeReference.INDEX_TEMPLATE;
import static org.javarosa.core.model.instance.TreeReference.REF_ABSOLUTE;
Expand All @@ -42,9 +46,9 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.javarosa.core.model.CoreModelModule;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
Expand All @@ -61,7 +65,9 @@
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.services.PrototypeManager;
import org.javarosa.core.util.JavaRosaCoreModule;
import org.javarosa.core.util.XFormsElement;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.debug.Event;
import org.javarosa.form.api.FormEntryController;
import org.javarosa.form.api.FormEntryModel;
import org.javarosa.form.api.FormEntryPrompt;
Expand Down Expand Up @@ -112,6 +118,12 @@ private Scenario(FormDef formDef, FormEntryController formEntryController) {
this.formEntryController = formEntryController;
}

public static Scenario init(String formName, XFormsElement form) throws IOException {
Path formFile = createTempDirectory("javarosa").resolve(formName + ".xml");
write(formFile, form.asXml().getBytes(UTF_8), CREATE);
return Scenario.init(formFile);
}

/**
* Creates and prepares the test scenario loading and parsing the given form
*/
Expand Down Expand Up @@ -188,7 +200,36 @@ public Scenario jumpToFirst(String name) {
* Answers the current question.
*/
public AnswerResult answer(String value) {
return AnswerResult.from(formEntryController.answerQuestion(formEntryController.getModel().getFormIndex(), new StringData(value), true));
FormIndex formIndex = formEntryController.getModel().getFormIndex();
StringData data = new StringData(value);
IFormElement child = formDef.getChild(formIndex);
String reference = "";
try {
reference = Optional.ofNullable(child.getBind()).map(idr -> (TreeReference) idr.getReference()).map(Object::toString).map(s -> "ref:" + s).orElse("");
} catch (RuntimeException e) {
// Do nothing. Probably "method not implemented" in FormDef.getBind()
}
log.info("Answer {} at {}", data, reference);
int jrCode = formEntryController.answerQuestion(formIndex, data, true);
return AnswerResult.from(jrCode);
}

/**
* Answers the current question.
*/
public AnswerResult answer(int value) {
FormIndex formIndex = formEntryController.getModel().getFormIndex();
IntegerData data = new IntegerData(value);
IFormElement child = formDef.getChild(formIndex);
String reference = "";
try {
reference = Optional.ofNullable(child.getBind()).map(idr -> (TreeReference) idr.getReference()).map(Object::toString).map(s -> "ref:" + s).orElse("");
} catch (RuntimeException e) {
// Do nothing. Probably "method not implemented" in FormDef.getBind()
}
log.info("Answer {} at {}", data, reference);
int jrCode = formEntryController.answerQuestion(formIndex, data, true);
return AnswerResult.from(jrCode);
}

/**
Expand Down Expand Up @@ -533,6 +574,11 @@ public static AnswerResult from(int jrCode) {
}
}

public Scenario onDagEvent(Consumer<Event> callback) {
formDef.setEventNotifier(callback::accept);
return this;
}

public Scenario serializeAndDeserializeForm() throws IOException, DeserializationException {
// Initialize serialization
PrototypeManager.registerPrototypes(JavaRosaCoreModule.classNames);
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/org/javarosa/core/util/BindBuilderXFormsElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2019 Nafundi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.javarosa.core.util;

import java.util.HashMap;
import java.util.Map;

public class BindBuilderXFormsElement implements XFormsElement {
private final Map<String, String> attributes = new HashMap<>();

private BindBuilderXFormsElement(String nodeset) {
attributes.put("nodeset", nodeset);
}

public static BindBuilderXFormsElement bind(String nodeset) {
return new BindBuilderXFormsElement(nodeset);
}

public BindBuilderXFormsElement type(String type) {
attributes.put("type", type);
return this;
}

public BindBuilderXFormsElement constraint(String expression) {
attributes.put("constraint", expression);
return this;
}

public BindBuilderXFormsElement required() {
String expression = "true()";
return required(expression);
}

public BindBuilderXFormsElement required(String expression) {
attributes.put("required", expression);
return this;
}


public BindBuilderXFormsElement relevant(String expression) {
attributes.put("relevant", expression);
return this;
}

public BindBuilderXFormsElement calculate(String expression) {
attributes.put("calculate", expression);
return this;
}

public BindBuilderXFormsElement preload(String expression) {
attributes.put("jr:preload", expression);
return this;
}

public BindBuilderXFormsElement readonly() {
attributes.put("readonly", "true()");
return this;
}

@Override
public String asXml() {
return new EmptyXFormsElement("bind", attributes).asXml();
}
}
37 changes: 37 additions & 0 deletions src/test/java/org/javarosa/core/util/EmptyXFormsElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019 Nafundi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.javarosa.core.util;

import static org.javarosa.core.util.XFormsElement.buildAttributesString;

import java.util.Map;

public class EmptyXFormsElement implements XFormsElement {
private final String name;
private final Map<String, String> attributes;

EmptyXFormsElement(String name, Map<String, String> attributes) {
this.name = name;
this.attributes = attributes;
}

@Override
public String asXml() {
String attributesString = buildAttributesString(attributes);
return String.format("<%s%s/>", name, attributesString.isEmpty() ? "" : " " + attributesString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 Nafundi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.javarosa.core.util;

import static org.javarosa.core.util.XFormsElement.buildAttributesString;

import java.util.Map;

public class StringLiteralXFormsElement implements XFormsElement {
private final String name;
private final Map<String, String> attributes;
private final String innerHtml;

StringLiteralXFormsElement(String name, Map<String, String> attributes, String innerHtml) {
this.name = name;
this.attributes = attributes;
this.innerHtml = innerHtml;
}

@Override
public String asXml() {
String attributesString = buildAttributesString(attributes);
return String.format(
"<%s%s>%s</%s>",
name,
attributesString.isEmpty() ? "" : " " + attributesString,
innerHtml,
name
);
}

}
51 changes: 51 additions & 0 deletions src/test/java/org/javarosa/core/util/TagXFormsElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019 Nafundi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.javarosa.core.util;

import static org.javarosa.core.util.XFormsElement.buildAttributesString;

import java.util.List;
import java.util.Map;

public class TagXFormsElement implements XFormsElement {
private final String name;
private final Map<String, String> attributes;
private final List<XFormsElement> children;

TagXFormsElement(String name, Map<String, String> attributes, List<XFormsElement> children) {
assert !children.isEmpty();
this.name = name;
this.attributes = attributes;
this.children = children;
}

@Override
public String asXml() {
String attributesString = buildAttributesString(attributes);
StringBuilder childrenStringBuilder = new StringBuilder();
for (XFormsElement e : children)
childrenStringBuilder.append(e.asXml());
return String.format(
"%s<%s%s>%s</%s>",
name.equals("h:html") ? "<?xml version=\"1.0\"?>" : "",
name,
attributesString.isEmpty() ? "" : " " + attributesString,
childrenStringBuilder,
name
);
}
}
Loading