
org.specrunner.junit.SpecRunnerParametrized Maven / Gradle / Ivy
/*
SpecRunner - Acceptance Test Driven Development Tool
Copyright (C) 2011-2018 Thiago Santos
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
*/
package org.specrunner.junit;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.specrunner.SRServices;
import org.specrunner.configuration.IConfiguration;
import org.specrunner.configuration.IConfigurationFactory;
import org.specrunner.util.UtilLog;
/**
* JUnit parametrized test helper. Example of use:
*
*
* public class TestParametrized extends SpecRunnerParametrized {
*
* public TestParametrized(Entry entry) {
* super(entry);
* }
*
* // JUnit annotation
* @SuppressWarnings("unchecked")
* @Parameters
* public static Collection<Object[]> getTestFiles() {
* File inDir = new File("src/test/resources/sources"); // input directory
* File outDir = new File("src/test/resources/out"); // output directory
* // files ending with 'e.html' OR starting with 'cust' OR containing 'st'
* return merge(endsWith(inDir, "e.html", outDir), startsWith(inDir, "cust", outDir), contains(inDir, "st", outDir));
* }
* }
*
*
* @author Thiago Santos
*
*/
@RunWith(Parameterized.class)
public abstract class SpecRunnerParametrized {
/**
* File entry.
*/
protected final Entry entry;
/**
* Creates a instance by entry.
*
* @param entry
* The entry.
*/
protected SpecRunnerParametrized(Entry entry) {
this.entry = entry;
}
/**
* A execution mapping.
*
* @author Thiago Santos
*
*/
protected static final class Entry {
/**
* Entry input.
*/
protected final Object input;
/**
* Entry output.
*/
protected final Object output;
/**
* Entry configuration.
*/
protected final IConfiguration cfg;
/**
* Creates an entry.
*
* @param input
* The input file.
* @param output
* The output file.
* @param cfg
* The configuration.
*/
protected Entry(Object input, Object output, IConfiguration cfg) {
this.input = input;
this.output = output;
this.cfg = cfg;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cfg == null) ? 0 : cfg.hashCode());
result = prime * result + ((input == null) ? 0 : input.hashCode());
result = prime * result + ((output == null) ? 0 : output.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Entry other = (Entry) obj;
return input.equals(other.input) && output.equals(other.output);
}
}
/**
* Runs a specification.
*
*/
@Test
public void run() {
SpecRunnerJUnit.defaultRun(String.valueOf(entry.input), String.valueOf(entry.output), entry.cfg);
}
/**
* Filter types.
*
* @author Thiago Santos
*
*/
enum Filter {
/**
* Starts with a string.
*/
STARTWITH,
/**
* Contains a string.
*/
CONTAINS,
/**
* Ends with a string.
*/
ENDSWITH;
}
/**
* Filter files starting with.
*
* @param inDir
* Base directory.
* @param prefix
* Prefix.
* @param outDir
* The output directory.
* @return A collection of entries.
*/
protected static Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy