-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/test/java/org/javarosa/xform/parse/XPathProcessorTest.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,102 @@ | ||
package org.javarosa.xform.parse; | ||
|
||
import org.javarosa.core.util.XFormsElement; | ||
import org.javarosa.xpath.expr.XPathExpression; | ||
import org.javarosa.xpath.expr.XPathPathExpr; | ||
import org.javarosa.xpath.expr.XPathQName; | ||
import org.javarosa.xpath.expr.XPathStep; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
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; | ||
|
||
public class XPathProcessorTest { | ||
|
||
@Test | ||
public void processesXPathExpressions() throws Exception { | ||
XFormsElement form = XFormsElement.html( | ||
head( | ||
model( | ||
mainInstance( | ||
t("data id=\"form\"", | ||
t("question") | ||
) | ||
), | ||
bind("/data/question").type("string") | ||
) | ||
), | ||
body( | ||
input("/data/question") | ||
) | ||
); | ||
|
||
XFormParser parser = new XFormParser(new InputStreamReader(new ByteArrayInputStream(form.asXml().getBytes()))); | ||
RecordingXPathProcessor processor = new RecordingXPathProcessor(); | ||
parser.addProcessor(processor); | ||
parser.parse(null); | ||
|
||
assertThat(processor.processedExpressions, contains( | ||
new XPathPathExpr(XPathPathExpr.INIT_CONTEXT_ROOT, new XPathStep[]{ | ||
new XPathStep(XPathStep.AXIS_CHILD, new XPathQName("data")), | ||
new XPathStep(XPathStep.AXIS_CHILD, new XPathQName("question")) | ||
}), | ||
new XPathPathExpr(XPathPathExpr.INIT_CONTEXT_ROOT, new XPathStep[]{ | ||
new XPathStep(XPathStep.AXIS_CHILD, new XPathQName("data")), | ||
new XPathStep(XPathStep.AXIS_CHILD, new XPathQName("question")) | ||
}) | ||
)); | ||
} | ||
|
||
@Test | ||
public void processorsAreNotRetainedBetweenParses() throws Exception { | ||
XFormsElement form = XFormsElement.html( | ||
head( | ||
model( | ||
mainInstance( | ||
t("data id=\"form\"", | ||
t("question") | ||
) | ||
), | ||
bind("/data/question").type("string") | ||
) | ||
), | ||
body( | ||
input("/data/question") | ||
) | ||
); | ||
|
||
XFormParser parser = new XFormParser(new InputStreamReader(new ByteArrayInputStream(form.asXml().getBytes()))); | ||
RecordingXPathProcessor processor = new RecordingXPathProcessor(); | ||
parser.addProcessor(processor); | ||
parser.parse(null); | ||
assertThat(processor.processedExpressions.size(), equalTo(2)); | ||
|
||
XFormParser secondParser = new XFormParser(new InputStreamReader(new ByteArrayInputStream(form.asXml().getBytes()))); | ||
secondParser.parse(null); | ||
assertThat(processor.processedExpressions.size(), equalTo(2)); | ||
} | ||
|
||
private static class RecordingXPathProcessor implements XFormParser.XPathProcessor { | ||
|
||
public final List<XPathExpression> processedExpressions = new ArrayList<>(); | ||
|
||
@Override | ||
public void processXPath(@NotNull XPathExpression xPathExpression) { | ||
processedExpressions.add(xPathExpression); | ||
} | ||
} | ||
} |