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

Expose Scenario #772

Merged
merged 12 commits into from
Jul 3, 2024
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if (!project.hasProperty("android")) {
// Be sure to update version in pom.xml to match
// snapshot release = x.x.x-SNAPSHOT
// production release = x.x.x
archiveVersion = '4.4.0'
archiveVersion = '5.5.0-SNAPSHOT'
archiveBaseName = "javarosa"

manifest {
Expand Down Expand Up @@ -121,10 +121,10 @@ if (!project.hasProperty("android")) {
android {
namespace 'com.example'

compileSdkVersion 30
compileSdkVersion 34
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- Be sure to update version in build.gradle to match -->
<!-- snapshot release = x.x.x-SNAPSHOT -->
<!-- production release = x.x.x -->
<version>4.4.0</version>
<version>5.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>javarosa</name>
<description>A Java library for rendering forms that are compliant with ODK XForms spec</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void setUp() {
// If your forms load external files, you'll need to prime the ResourceManager using the assetsDir
// returned by BenchmarkUtils.prepareAssets(). You need to make sure that all external files in the
// form are like jr://file/filename (all use the "file" hostname, all are located at the root).
setUpSimpleReferenceManager(assetsPath, "file");
setUpSimpleReferenceManager(assetsPath.toFile(), "file");
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/jmh/java/org/javarosa/benchmarks/BenchmarkUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.javarosa.benchmarks;

import static org.javarosa.test.utils.ResourcePathHelper.r;
import static org.javarosa.test.ResourcePathHelper.r;
import static org.javarosa.core.reference.ReferenceManagerTestUtils.setUpSimpleReferenceManager;

import java.io.IOException;
Expand Down Expand Up @@ -35,7 +35,7 @@ public static Path prepareAssets(String... filenames) {
.getResource(filename.startsWith("/") ? filename : "/" + filename)
.toURI().toString();
Files.copy(
realPath.contains("!") ? getPathInJar(realPath) : r(filename),
realPath.contains("!") ? getPathInJar(realPath) : r(filename).toPath(),
assetsDir.resolve(filename)
);
}
Expand Down Expand Up @@ -146,21 +146,21 @@ public static Path getMinifiedNigeriaWardsXMLWithInternal2ndryInstance(){

public static Path getNigeriaWardsXMLWithExternal2ndryInstance(){
Path assetsPath = prepareAssets("nigeria_wards_external_2ndry_instance.xml", "lgas.xml", "wards.xml");
setUpSimpleReferenceManager(assetsPath, "file");
setUpSimpleReferenceManager(assetsPath.toFile(), "file");
Path filePath = assetsPath.resolve("nigeria_wards_external_2ndry_instance.xml");
return filePath;
}

public static Path getWardsExternalInstance(){
Path assetsPath = prepareAssets( "wards.xml");
setUpSimpleReferenceManager(assetsPath, "file");
setUpSimpleReferenceManager(assetsPath.toFile(), "file");
Path filePath = assetsPath.resolve("wards.xml");
return filePath;
}

public static Path getLGAsExternalInstance(){
Path assetsPath = prepareAssets( "lgas.xml");
setUpSimpleReferenceManager(assetsPath, "file");
setUpSimpleReferenceManager(assetsPath.toFile(), "file");
Path filePath = assetsPath.resolve("lgas.xml");
return filePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
import static org.javarosa.benchmarks.ChildVaccinationBenchmark.Vaccines.DIPHTERIA_FIRST_AND_MEASLES;
import static org.javarosa.benchmarks.ChildVaccinationBenchmark.Vaccines.MEASLES;
import static org.javarosa.benchmarks.ChildVaccinationBenchmark.Vaccines.NONE;
import static org.javarosa.core.test.Scenario.getRef;
import static org.javarosa.core.test.Scenario.init;
import static org.javarosa.test.Scenario.getRef;
import static org.javarosa.test.Scenario.init;

import java.nio.file.Path;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.test.Scenario;
import org.javarosa.test.Scenario;
import org.javarosa.xform.parse.XFormParser;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
Expand Down Expand Up @@ -77,7 +77,7 @@ public void initialize() throws XFormParser.ParseException {
finalFlatRef = getRef("/data/household/finalflat");
endOfVisitRefs = Arrays.asList(nextChildRef, finalFlatRef, getRef("/data/household/child_repeat"));
today = LocalDate.now();
scenario = init(formFile);
scenario = init(formFile.toFile());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class ExternalDataInstanceState {
@Setup(Level.Trial)
public void initialize() {
Path assetsPath = prepareAssets( "wards.xml", "lgas.xml");
setUpSimpleReferenceManager(assetsPath, "file");
setUpSimpleReferenceManager(assetsPath.toFile(), "file");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class FormDefCacheState {
public void
initialize() throws IOException, XFormParser.ParseException {
resourcePath = BenchmarkUtils.getNigeriaWardsXMLWithExternal2ndryInstance();
formDef = FormParserHelper.parse(resourcePath);
formDef = FormParserHelper.parse(resourcePath.toFile());
cachePath = getCachePath().toString();
PrototypeManager.registerPrototypes(JavaRosaCoreModule.classNames);
PrototypeManager.registerPrototypes(CoreModelModule.classNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class FormDefCacheState {
public void
initialize() throws IOException, XFormParser.ParseException {
resourcePath = BenchmarkUtils.getNigeriaWardsXMLWithExternal2ndryInstance();
formDef = FormParserHelper.parse(resourcePath);
formDef = FormParserHelper.parse(resourcePath.toFile());
cachePath = getCachePath().toString();
PrototypeManager.registerPrototypes(JavaRosaCoreModule.classNames);
PrototypeManager.registerPrototypes(CoreModelModule.classNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class FormDefValidateState {
@Setup(Level.Trial)
public void initialize() throws IOException, XFormParser.ParseException {
Path resourcePath = BenchmarkUtils.getNigeriaWardsXMLWithExternal2ndryInstance();
formDef = FormParserHelper.parse(resourcePath);
formDef = FormParserHelper.parse(resourcePath.toFile());
FormEntryModel formEntryModel = new FormEntryModel(formDef);
FormEntryController formEntryController = new FormEntryController(formEntryModel);
formEntryController.stepToNextEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class FormControllerAnswerQuestionState {
@Setup(Level.Trial)
public void initialize() throws IOException, XFormParser.ParseException {
Path formFile = BenchmarkUtils.getNigeriaWardsXMLWithExternal2ndryInstance();
FormDef formDef = FormParserHelper.parse(formFile);
FormDef formDef = FormParserHelper.parse(formFile.toFile());
formEntryModel = new FormEntryModel(formDef);
formEntryController = new FormEntryController(formEntryModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void initialize() {
@Benchmark
public void
benchmarkParseExternalSecondaryInstance(FormParserHelperParseExternalInstanceBenchmarkState state, Blackhole bh) throws IOException, XFormParser.ParseException {
bh.consume(FormParserHelper.parse(state.xFormFilePath));
bh.consume(FormParserHelper.parse(state.xFormFilePath.toFile()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public void initialize() {
@Benchmark
public void
benchmarkParseInternalSecondaryInstanceForm(FormParserHelperParseInternalInstanceBenchmarkState state, Blackhole bh) throws IOException, XFormParser.ParseException {
bh.consume(FormParserHelper.parse(state.xFormFilePath));
bh.consume(FormParserHelper.parse(state.xFormFilePath.toFile()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void initialize() {
@Benchmark
public void
benchmarkParseInternalInstanceFormMinified(FormParserHelperParseInternalInstanceMinifiedBenchmarkState state, Blackhole bh) throws IOException, XFormParser.ParseException {
bh.consume(FormParserHelper.parse(state.xFormFilePath));
bh.consume(FormParserHelper.parse(state.xFormFilePath.toFile()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.nio.file.Path;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.instance.TreeElement;
import org.javarosa.core.test.FormParseInit;
import org.javarosa.test.FormParseInit;
import org.javarosa.form.api.FormEntryController;
import org.javarosa.xform.parse.XFormParser;
import org.openjdk.jmh.annotations.Benchmark;
Expand Down Expand Up @@ -38,7 +38,7 @@ public void initialize() throws IOException, XFormParser.ParseException {
Path assetsDir = prepareAssets("nigeria_wards_external_combined.xml", "wards.xml", "lgas.xml", "populate-nodes-attributes-instance.xml");
Path formFile = assetsDir.resolve("nigeria_wards_external_combined.xml");
Path submissionFile = assetsDir.resolve("populate-nodes-attributes-instance.xml");
FormParseInit formParseInit = new FormParseInit(formFile);
FormParseInit formParseInit = new FormParseInit(formFile.toFile());
FormEntryController formEntryController = formParseInit.getFormEntryController();
byte[] formInstanceAsBytes = Files.readAllBytes(submissionFile);
savedRoot = XFormParser.restoreDataModel(formInstanceAsBytes, null).getRoot();
Expand Down
6 changes: 3 additions & 3 deletions src/jmh/java/org/javarosa/benchmarks/WhoVaBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import static org.javarosa.benchmarks.BenchmarkUtils.dryRun;
import static org.javarosa.benchmarks.BenchmarkUtils.prepareAssets;
import static org.javarosa.core.test.Scenario.init;
import static org.javarosa.test.Scenario.init;

import java.time.LocalDate;
import java.util.stream.IntStream;
import org.javarosa.core.test.Scenario;
import org.javarosa.test.Scenario;
import org.javarosa.xform.parse.XFormParser;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
Expand All @@ -44,7 +44,7 @@ public static class WhoVaState {

@Setup(Level.Trial)
public void initialize() throws XFormParser.ParseException {
scenario = init(prepareAssets("whova_form.xml").resolve("whova_form.xml"));
scenario = init(prepareAssets("whova_form.xml").resolve("whova_form.xml").toFile());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import static java.util.stream.IntStream.range;
import static org.javarosa.benchmarks.BenchmarkUtils.dryRun;
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.html;
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.repeat;
import static org.javarosa.core.util.XFormsElement.t;
import static org.javarosa.core.util.XFormsElement.title;
import static org.javarosa.test.BindBuilderXFormsElement.bind;
import static org.javarosa.test.XFormsElement.body;
import static org.javarosa.test.XFormsElement.head;
import static org.javarosa.test.XFormsElement.html;
import static org.javarosa.test.XFormsElement.input;
import static org.javarosa.test.XFormsElement.mainInstance;
import static org.javarosa.test.XFormsElement.model;
import static org.javarosa.test.XFormsElement.repeat;
import static org.javarosa.test.XFormsElement.t;
import static org.javarosa.test.XFormsElement.title;

import java.io.IOException;
import org.javarosa.core.test.Scenario;
import org.javarosa.test.Scenario;
import org.javarosa.xform.parse.XFormParser;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static org.javarosa.benchmarks.core.model.CreateRepeatDagBenchmark.getSumExpressionOutsideScenario;

import java.io.IOException;
import org.javarosa.core.test.Scenario;
import org.javarosa.test.Scenario;
import org.javarosa.xform.parse.XFormParser;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.javarosa.core.util;
package org.javarosa.test;

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

package org.javarosa.core.util;
package org.javarosa.test;

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

import java.util.Map;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package org.javarosa.core.test;
package org.javarosa.test;

import static org.javarosa.form.api.FormEntryController.EVENT_END_OF_FORM;
import static org.javarosa.test.utils.ResourcePathHelper.r;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.GroupDef;
Expand All @@ -21,6 +13,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import static org.javarosa.form.api.FormEntryController.EVENT_END_OF_FORM;
import static org.javarosa.test.ResourcePathHelper.r;

/**
* This class sets up everything you need to perform tests on the models and form elements found in JR (such
* as QuestionDef, FormDef, Selections, etc). It exposes hooks to the FormEntryController,FormEntryModel and
Expand All @@ -42,8 +43,8 @@ public FormParseInit() throws XFormParser.ParseException {
this.init();
}

public FormParseInit(Path form) throws XFormParser.ParseException {
FORM_NAME = form.toString();
public FormParseInit(File form) throws XFormParser.ParseException {
FORM_NAME = form.getAbsolutePath();
this.init();
}

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/javarosa/test/ResourcePathHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.javarosa.test;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

public class ResourcePathHelper {
private static Set<File> resourcePathsCache = buildCache();

private static synchronized Set<File> buildCache() {
File root = new File(ResourcePathHelper.class.getResource("/logback-test.xml.example").getFile()).getParentFile();
Collection<File> files = FileUtils.listFiles(root, null, true);
return files.stream()
.collect(Collectors.toSet());
}

/**
* Provides the path to the first filename match in a depth-first traversal starting
* at the test resource root.
*
* @param filename the file name for which to create a path
* @return a Path for the resource file
*/
public static File r(String filename) {
if (resourcePathsCache == null)
throw new RuntimeException("Too fast! The resources cache hasn't been built yet! Don't use r() within static members!");
return resourcePathsCache.stream()
.filter(f -> f.getAbsolutePath().endsWith(File.separator + filename))
.findFirst()
.orElseThrow(() -> new RuntimeException("File " + filename + " not found among files in resources"));
}
}
Loading