hr.fer.junit.grading.command_line.CommandLineRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-grading Show documentation
Show all versions of junit-grading Show documentation
Testing library to run with grading student examination.
The newest version!
package hr.fer.junit.grading.command_line;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.file.Path;
import java.util.Collection;
import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import com.beust.jcommander.JCommander;
import hr.fer.junit.grading.GradingMatrixModel;
import hr.fer.junit.grading.GradingReport;
import hr.fer.junit.grading.GradingTestRunner;
import hr.fer.junit.grading.csv.GradingMatrixCsvLoader;
/**
* Running junit grading tests from command line.
*
*/
public class CommandLineRunner {
/**
* Main entry point.
*
* Usage: <main class> [options]
* Options:
* -gm
* Grading matrix file (default is looking for 'gradingMatrix.csv' in CWD
* and in classpath)
* Default: <empty string>
* -h
* help
* -m
* Running modes: normal - prints summary, details - prints details,
* statistics - print statistics (testGroup, number of tests, test IDs)
* Default: NORMAL
* Possible Values: [NORMAL, DETAILS, STATISTICS]
* * -p
* package names to find tests
* -tg
* test group (default is all test groups)
* Default: all
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ProgramArguments programArguments = new ProgramArguments();
JCommander commander = JCommander.newBuilder()
.addObject(programArguments)
.build();
commander.parse(args);
if(programArguments.help) {
commander.usage();
return;
}
GradingMatrixModel gradingMatrix = loadGradingMatrix(programArguments);
Writer output = new PrintWriter(System.out);
GradingReport gradingReport = runGrading(gradingMatrix, programArguments.testGroup, programArguments.packages);
switch (programArguments.mode) {
case NORMAL:
gradingReport.printSummaryTo(output);
break;
case DETAILS:
gradingReport.printDetailedSummaryTo(output);
break;
case STATISTICS:
gradingReport.printStatisticsTo(output);
break;
default:
throw new IllegalArgumentException("Unexpected value: " + programArguments.mode);
}
}
private static GradingMatrixModel loadGradingMatrix(ProgramArguments programArguments) throws IOException {
GradingMatrixModel gradingMatrix;
if(programArguments.gradingFilePath.isEmpty()) {
gradingMatrix = GradingMatrixCsvLoader.load();
} else {
gradingMatrix = GradingMatrixCsvLoader.loadFromFile(Path.of(programArguments.gradingFilePath));
}
return gradingMatrix;
}
/**
* Run grading tests
*
* @param gradingMatrix grading matrix
* @param testGroup test group to run. Special values "" or "all" run all test groups
* @param packageNames package names to find tests
* @return grading report
* @throws IOException thrown when grading matrix can not be loaded
*/
public static GradingReport runGrading(GradingMatrixModel gradingMatrix, String testGroup, Collection packageNames) throws IOException {
return runGradingWithDiscoverySelectors(gradingMatrix, testGroup,
packageNames.stream().map(pn -> (DiscoverySelector) DiscoverySelectors.selectPackage(pn)).toList());
}
/**
* Run grading tests
*
* @param gradingMatrix grading matrix
* @param testGroup test group to run. Special values "" or "all" run all test groups
* @param selectors discovery selectors to find tests
* @return grading report
* @throws IOException thrown when grading matrix can not be loaded
*/
public static GradingReport runGradingWithDiscoverySelectors(GradingMatrixModel gradingMatrix, String testGroup, Collection selectors) throws IOException {
GradingTestRunner runner = new GradingTestRunner(gradingMatrix);
for(DiscoverySelector selector: selectors)
runner.addDiscoverySelector(selector);
runner.setTestGroupExpressionFilter(testGroup);
return runner.run();
}
}