Skip to content

Commit

Permalink
test: Convert XML checks to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
rhwood committed Jun 28, 2020
1 parent a5a4f9d commit b9a35ff
Show file tree
Hide file tree
Showing 25 changed files with 429 additions and 351 deletions.
19 changes: 11 additions & 8 deletions java/test/apps/ValidateConfigFilesTest.java
@@ -1,26 +1,29 @@
package apps;

import java.io.File;
import java.util.stream.Stream;

import jmri.configurexml.SchemaTestBase;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test upper level loading of config files
*
* @author Bob Jacobsen Copyright 2012
* @since 2.5.5
*/
@RunWith(Parameterized.class)
public class ValidateConfigFilesTest extends SchemaTestBase {

@Parameters(name = "{0} (pass={1})")
public static Iterable<Object[]> data() {
public static Stream<Arguments> data() {
return getFiles(new File("xml/config"), true, true);
}

public ValidateConfigFilesTest(File file, boolean pass) {
super(file, pass);
@ParameterizedTest
@MethodSource("data")
public void validateConfigFiles(File file, boolean pass) {
super.validate(file, pass);
}
}
18 changes: 10 additions & 8 deletions java/test/jmri/SchemaTest.java
@@ -1,31 +1,33 @@
package jmri;

import java.io.File;
import java.util.stream.Stream;

import jmri.configurexml.SchemaTestBase;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Checks of JMRI ml/sample files; here because where else would you put it?
*
* @author Bob Jacobsen Copyright 2009, 2016
* @since 4.3.3
*/
@RunWith(Parameterized.class)
public class SchemaTest extends SchemaTestBase {

@Parameters(name = "{0} (pass={1})")
public static Iterable<Object[]> data() {
public static Stream<Arguments> data() {
// the following are just tested for schema pass/fail, not load/store
// could recurse, but xml/samples/javaone/Throttles.xml fails
// (was not tested prior to 4.7.1)
return getFiles(new File("xml/samples"), false, true);
}

public SchemaTest(File file, boolean pass) {
super(file, pass);
@ParameterizedTest
@MethodSource("data")
public void schemaTest(File file, boolean pass) {
super.validate(file, pass);
}

}
21 changes: 14 additions & 7 deletions java/test/jmri/configurexml/LoadAndStoreTest.java
Expand Up @@ -2,8 +2,11 @@


import java.io.File;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test that configuration files can be read and then stored again consistently.
Expand All @@ -19,15 +22,19 @@
* @author Bob Jacobsen Copyright 2009, 2014
* @since 2.5.5 (renamed & reworked in 3.9 series)
*/
@RunWith(Parameterized.class)
public class LoadAndStoreTest extends LoadAndStoreTestBase {

@Parameterized.Parameters(name = "{0} (pass={1})")
public static Iterable<Object[]> data() {
public static Stream<Arguments> data() {
return getFiles(new File("java/test/jmri/configurexml"), false, true);
}

public LoadAndStoreTest(File file, boolean pass) {
super(file, pass, SaveType.Config, false);
@ParameterizedTest
@MethodSource("data")
public void loadAndStoreTest(File file) throws Exception {
super.loadLoadStoreFileCheck(file);
}

public LoadAndStoreTest() {
super(SaveType.Config, false);
}
}
53 changes: 21 additions & 32 deletions java/test/jmri/configurexml/LoadAndStoreTestBase.java
Expand Up @@ -5,7 +5,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.stream.Stream;

import jmri.ConfigureManager;
import jmri.InstanceManager;
Expand All @@ -14,13 +14,10 @@
import jmri.util.JUnitAppender;
import jmri.util.JUnitUtil;

import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.provider.Arguments;

/**
* Base for testing load-and-store of configuration files.
Expand All @@ -31,27 +28,23 @@
* "loadref" directory, or against the original file itself. A minimal test
* class is:
<pre>
@RunWith(Parameterized.class)
public class LoadAndStoreTest extends LoadAndStoreTestBase {
@Parameterized.Parameters(name = "{0} (pass={1})")
public static Iterable<Object[]> data() {
public static Stream&amp;Arguments&amp; data() {
return getFiles(new File("java/test/jmri/configurexml"), false, true);
}
public LoadAndStoreTest(File file, boolean pass) { super(file, pass); }
@ParameterizedTest
@MethodSource("data")
public void loadAndStoreTest(File file, boolean pass) { super.validate(file, pass); }
}
</pre>
*
* @author Bob Jacobsen Copyright 2009, 2014
* @since 2.5.5 (renamed & reworked in 3.9 series)
*/
@RunWith(Parameterized.class)
public class LoadAndStoreTestBase {

// allows code reuse when building the parameter collection in getFiles()
protected final File file;

public enum SaveType {
All, Config, Prefs, User, UserPrefs
}
Expand All @@ -63,16 +56,12 @@ public enum SaveType {
* Get all XML files in a directory and validate the ability to load and
* store them.
*
* @param file the file to be tested
* @param pass if true, successful validation will pass; if false,
* successful validation will fail
* @param saveType the type (i.e. level) of ConfigureXml information being saved
* @param isGUI true for files containing GUI elements, i.e. panels. These
* can only be loaded once (others can be loaded twice, and that's
* tested when this is false), and can't be loaded when running headless.
*/
public LoadAndStoreTestBase(File file, boolean pass, SaveType saveType, boolean isGUI) {
this.file = file;
public LoadAndStoreTestBase(SaveType saveType, boolean isGUI) {
this.saveType = saveType;
this.guiOnly = isGUI;
}
Expand All @@ -86,11 +75,11 @@ public LoadAndStoreTestBase(File file, boolean pass, SaveType saveType, boolean
* @param recurse if true, will recurse into subdirectories
* @param pass if true, successful validation will pass; if false,
* successful validation will fail
* @return a collection of Object arrays, where each array contains the
* @return a stream of {@link Arguments}, where each Argument contains the
* {@link java.io.File} to validate and a boolean matching the pass
* parameter
*/
public static Collection<Object[]> getFiles(File directory, boolean recurse, boolean pass) {
public static Stream<Arguments> getFiles(File directory, boolean recurse, boolean pass) {
// since this method gets the files to test, but does not trigger any
// tests itself, we can use SchemaTestBase.getFiles() by adding "load"
// to the directory to test
Expand All @@ -111,7 +100,7 @@ public static Collection<Object[]> getFiles(File directory, boolean recurse, boo
* validate and a boolean matching the pass parameter
* @throws IllegalArgumentException if directory is a file
*/
public static Collection<Object[]> getDirectories(File directory, boolean recurse, boolean pass) throws IllegalArgumentException {
public static Stream<Arguments> getDirectories(File directory, boolean recurse, boolean pass) throws IllegalArgumentException {
// since this method gets the files to test, but does not trigger any
// tests itself, we can use SchemaTestBase.getDirectories() by adding "load"
// to the directory to test
Expand Down Expand Up @@ -300,31 +289,31 @@ public static File storeFile(File inFile, SaveType inSaveType) throws Exception
}

@Test
public void loadLoadStoreFileCheck() throws Exception {
public void loadLoadStoreFileCheck(File file) throws Exception {
if (guiOnly) {
Assume.assumeFalse(GraphicsEnvironment.isHeadless());
}

log.debug("Start check file {}", this.file.getCanonicalPath());
log.debug("Start check file {}", file.getCanonicalPath());

loadFile(this.file);
loadFile(file);
// Panel sub-classes (with GUI) will fail if you try to load them twice.
// (So don't!)
if (!guiOnly) {
loadFile(this.file);
loadFile(file);
}

// find comparison files
File compFile = new File(this.file.getCanonicalFile().getParentFile().
getParent() + "/loadref/" + this.file.getName());
File compFile = new File(file.getCanonicalFile().getParentFile().
getParent() + "/loadref/" + file.getName());
if (!compFile.exists()) {
compFile = this.file;
compFile = file;
}
log.debug(" Chose comparison file {}", compFile.getCanonicalPath());

postLoadProcessing();

File outFile = storeFile(this.file, this.saveType);
File outFile = storeFile(file, this.saveType);
checkFile(compFile, outFile);

JUnitAppender.suppressErrorMessage("systemName is already registered: ");
Expand All @@ -337,7 +326,7 @@ public void loadLoadStoreFileCheck() throws Exception {
*/
protected void postLoadProcessing(){}

@Before
@BeforeEach
public void setUp() {
JUnitUtil.setUp();
JUnitUtil.resetProfileManager();
Expand All @@ -352,7 +341,7 @@ public void setUp() {
System.setProperty("jmri.test.no-dialogs", "true");
}

@After
@AfterEach
public void tearDown() {
JUnitUtil.closeAllPanels();
JUnitUtil.clearShutDownManager();
Expand Down
18 changes: 10 additions & 8 deletions java/test/jmri/configurexml/SchemaTest.java
@@ -1,25 +1,27 @@
package jmri.configurexml;

import java.io.File;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Checks of JMRI XML Schema
*
* @author Bob Jacobsen Copyright 2009
* @since 2.5.5
*/
@RunWith(Parameterized.class)
public class SchemaTest extends SchemaTestBase {

@Parameters(name = "{0} (pass={1})")
public static Iterable<Object[]> data() {
public static Stream<Arguments> data() {
return setTestFilesBelowThisPath("java/test/jmri/configurexml");
}

public SchemaTest(File file, boolean pass) {
super(file, pass);
@ParameterizedTest
@MethodSource("data")
public void schemaTest(File file, boolean pass) {
super.validate(file, pass);
}
}

0 comments on commit b9a35ff

Please sign in to comment.