Skip to content

Commit

Permalink
test: make the configuration shareable between steps
Browse files Browse the repository at this point in the history
This commit extracts all configuration step definitions to a new
`TestConfiguration` class that can be shared (by injection) with
all the step definitions.
  • Loading branch information
rdeltour committed Nov 14, 2022
1 parent 47815e1 commit 5c35fa0
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 142 deletions.
4 changes: 2 additions & 2 deletions src/test/java/org/w3c/epubcheck/test/AssertionSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class AssertionSteps
private TestReport report;
private MessageInfo lastAssertedMessage;

public AssertionSteps(TestReport report)
public AssertionSteps(TestConfiguration configuration)
{
this.report = report;
this.report = configuration.getReport();
}

@Then("no( other) warning(s)/error(s) or error(s)/warning(s) are/is reported")
Expand Down
156 changes: 23 additions & 133 deletions src/test/java/org/w3c/epubcheck/test/ExecutionSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import java.util.Locale;

import org.w3c.epubcheck.core.Checker;
import org.w3c.epubcheck.test.TestConfiguration.CheckerMode;
import org.w3c.epubcheck.url.URLUtils;

import com.adobe.epubcheck.api.EPUBProfile;
import com.adobe.epubcheck.api.EpubCheck;
import com.adobe.epubcheck.api.Report;
import com.adobe.epubcheck.messages.Severity;
import com.adobe.epubcheck.nav.NavChecker;
import com.adobe.epubcheck.opf.OPFChecker;
import com.adobe.epubcheck.opf.OPFChecker30;
Expand All @@ -26,119 +24,17 @@
import com.adobe.epubcheck.util.Archive;
import com.adobe.epubcheck.util.EPUBVersion;
import com.adobe.epubcheck.util.FileResourceProvider;
import com.adobe.epubcheck.util.ReportingLevel;

import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;

public class ExecutionSteps
{

public enum CheckerMode
{
EPUB,
MEDIA_OVERLAYS_DOC,
NAVIGATION_DOC,
PACKAGE_DOC,
SVG_CONTENT_DOC,
XHTML_CONTENT_DOC;

public static CheckerMode fromExtension(String path)
{
int index = path.lastIndexOf(".");
String ext = (index > 0) ? path.substring(index + 1) : "epub";
switch (ext)
{
case "opf":
return PACKAGE_DOC;
case "xhtml":
case "html":
case "htm":
return XHTML_CONTENT_DOC;
case "svg":
return SVG_CONTENT_DOC;
case "smil":
return MEDIA_OVERLAYS_DOC;
case "epub":
default:
return EPUB;
}
}
}

private final TestReport report;
private String basepath = "";
private CheckerMode mode = null;
private EPUBVersion version = EPUBVersion.VERSION_3;
private EPUBProfile profile = EPUBProfile.DEFAULT;
private Locale defaultLocale = Locale.ENGLISH;

public ExecutionSteps(TestReport report)
{
this.report = report;
}

@Before("@debug")
public void beforeDebug()
{
report.setVerbose(true);
}

@After("@debug")
public void afterDebug()
{
report.setVerbose(false);
}

@Given("(EPUB )test files located at {string}")
public void setBasePath(String path)
{
this.basepath = path;
}
private final TestConfiguration configuration;

@And("EPUBCheck with default settings")
public void configureDefaults()
public ExecutionSteps(TestConfiguration configuration)
{
// nothing to do
}

@And("EPUBCheck configured to check EPUB {version} rules")
public void configureEPUBVersion(EPUBVersion version)
{
this.version = version;
}

@And("EPUBCheck configured to check a(n) {checkerMode}")
public void configureCheckerMode(ExecutionSteps.CheckerMode mode)
{
this.mode = mode;
}

@And("EPUBCheck configured with the ('){profile}(') profile")
public void configureProfile(EPUBProfile profile)
{
this.profile = profile;
}

@And("(the) default locale (is )set to ('){locale}(')")
public void configureDefaultLevel(Locale locale)
{
this.defaultLocale = locale;
}

@And("(the) reporting level (is )set to {severity}")
public void configureReportingLevel(Severity severity)
{
report.setReportingLevel(ReportingLevel.getReportingLevel(severity));
}

@And("(the) reporting locale (is )set to ('){locale}(')")
public void configureReportingLocale(Locale locale)
{
report.setLocale(locale);
this.configuration = configuration;
}

@When("checking EPUB/file/document {string}")
Expand All @@ -147,51 +43,45 @@ public void check(String path)
Locale oldDefaultLocale = Locale.getDefault();
try
{
Locale.setDefault(defaultLocale);
File testFile = getEPUBFile(basepath + path);
if (mode == null)
Locale.setDefault(configuration.getDefaultLocale());
File testFile = getEPUBFile(configuration.getBasepath() + path);
if (configuration.getMode() == null)
{
mode = CheckerMode.fromExtension(path);
configuration.setMode(CheckerMode.fromExtension(path));
}
Checker checker = getChecker(mode, testFile, version, profile, report);
Checker checker = getChecker(testFile);
checker.check();
} finally
{
Locale.setDefault(oldDefaultLocale);
}
}

private Checker getChecker(CheckerMode mode, File file, EPUBVersion version, EPUBProfile profile,
Report report)
private Checker getChecker(File file)
{
switch (mode)

ValidationContextBuilder contextBuilder = configuration.getContextBuilder()
.url(URLUtils.toURL(file)).resourceProvider(new FileResourceProvider(file));

switch (configuration.getMode())
{
case MEDIA_OVERLAYS_DOC:
return new OverlayChecker(new ValidationContextBuilder().url(URLUtils.toURL(file))
.mimetype("application/smil+xml").resourceProvider(new FileResourceProvider(file))
.report(report).version(EPUBVersion.VERSION_3).profile(profile).build());
return new OverlayChecker(
contextBuilder.mimetype("application/smil+xml").version(EPUBVersion.VERSION_3).build());
case NAVIGATION_DOC:
return new NavChecker(new ValidationContextBuilder().url(URLUtils.toURL(file))
.mimetype("application/xhtml+xml").resourceProvider(new FileResourceProvider(file))
.report(report).version(EPUBVersion.VERSION_3).profile(profile).build());
return new NavChecker(
contextBuilder.mimetype("application/xhtml+xml").version(EPUBVersion.VERSION_3).build());
case PACKAGE_DOC:
ValidationContext context = new ValidationContextBuilder().url(URLUtils.toURL(file))
.mimetype("application/oebps-package+xml")
.resourceProvider(new FileResourceProvider(file)).report(report).version(version)
.profile(profile).build();
ValidationContext context = contextBuilder.mimetype("application/oebps-package+xml").build();
return (context.version == EPUBVersion.VERSION_2) ? new OPFChecker(context)
: new OPFChecker30(context);
case SVG_CONTENT_DOC:
return new OPSChecker(new ValidationContextBuilder().url(URLUtils.toURL(file))
.mimetype("image/svg+xml").resourceProvider(new FileResourceProvider(file)).report(report)
.version(version).profile(profile).build());
return new OPSChecker(contextBuilder.mimetype("image/svg+xml").build());
case XHTML_CONTENT_DOC:
return new OPSChecker(new ValidationContextBuilder().url(URLUtils.toURL(file))
.mimetype("application/xhtml+xml").resourceProvider(new FileResourceProvider(file))
.report(report).version(version).profile(profile).build());
return new OPSChecker(contextBuilder.mimetype("application/xhtml+xml").build());
case EPUB:
default:
return new EpubCheck(file, report, profile);
return new EpubCheck(file, configuration.getReport(), configuration.getProfile());
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/w3c/epubcheck/test/ParameterTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Locale;

import org.hamcrest.Matcher;
import org.w3c.epubcheck.test.ExecutionSteps.CheckerMode;
import org.w3c.epubcheck.test.TestConfiguration.CheckerMode;

import com.adobe.epubcheck.api.EPUBProfile;
import com.adobe.epubcheck.messages.MessageId;
Expand Down Expand Up @@ -42,17 +42,17 @@ public CheckerMode checkerMode(String mode)
switch (mode.toLowerCase(Locale.ENGLISH))
{
case "full publication":
return ExecutionSteps.CheckerMode.EPUB;
return CheckerMode.EPUB;
case "media overlays document":
return ExecutionSteps.CheckerMode.MEDIA_OVERLAYS_DOC;
return CheckerMode.MEDIA_OVERLAYS_DOC;
case "navigation document":
return ExecutionSteps.CheckerMode.NAVIGATION_DOC;
return CheckerMode.NAVIGATION_DOC;
case "package document":
return ExecutionSteps.CheckerMode.PACKAGE_DOC;
return CheckerMode.PACKAGE_DOC;
case "svg content document":
return ExecutionSteps.CheckerMode.SVG_CONTENT_DOC;
return CheckerMode.SVG_CONTENT_DOC;
case "xhtml content document":
return ExecutionSteps.CheckerMode.XHTML_CONTENT_DOC;
return CheckerMode.XHTML_CONTENT_DOC;
default:
throw new IllegalArgumentException("Unknown file type: " + mode);
}
Expand Down
Loading

0 comments on commit 5c35fa0

Please sign in to comment.