Skip to content

Commit

Permalink
refactor: allow TestReport to generate a formatted report
Browse files Browse the repository at this point in the history
The report used for tests (`TestReport`) can now generate a formatted report.

- a new `setReportingFormat(String)` method is used to set the generated report format (can be "JSON", "XML", "XMP", or defaulting to a silent report)
- the `initialize()` method (Report API) is used to instantiate a delegate formatting report
- the `generate()` method (Report API) is used to finalize the formatted report delegate
- a new  `getOutput()` method is used to retrieve a String containing the formatted report

This formatting functionality is exposed as a cucumber step implemented in `TestConfiguration` and called with (in this example, for a JSON report):

```
   Given the reporting format is set to JSON
```
  • Loading branch information
rdeltour committed Apr 21, 2023
1 parent 699c510 commit c075143
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/test/java/org/w3c/epubcheck/test/ExecutionSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,24 @@ public void check(String path)
Locale oldDefaultLocale = Locale.getDefault();
try
{
// Complete configuration and get the test file
Locale.setDefault(configuration.getDefaultLocale());
File testFile = getEPUBFile(configuration.getBasepath() + path);
if (configuration.getMode() == null)
{
configuration.setMode(CheckerMode.fromExtension(path));
}
File testFile = getEPUBFile(configuration.getBasepath() + path);

// Initialize the report
configuration.getReport().setEpubFileName(testFile.getAbsolutePath());
configuration.getReport().initialize();

// Create the checker and run checks
Checker checker = getChecker(testFile);
checker.check();

// Finalize the report
configuration.getReport().generate();
} finally
{
Locale.setDefault(oldDefaultLocale);
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/w3c/epubcheck/test/TestConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public void setProfile(EPUBProfile profile)
{
this.profile = profile;
}

@And("(the )reporting format (is )set to {}")
public void setReportingFormat(String format)
{
report.setReportingFormat(format);
}

@And("(the )reporting level (is )set to {severity}")
public void setReportingLevel(Severity severity)
Expand Down
68 changes: 65 additions & 3 deletions src/test/java/org/w3c/epubcheck/test/TestReport.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.w3c.epubcheck.test;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -8,10 +10,15 @@

import com.adobe.epubcheck.api.EPUBLocation;
import com.adobe.epubcheck.api.MasterReport;
import com.adobe.epubcheck.api.QuietReport;
import com.adobe.epubcheck.api.Report;
import com.adobe.epubcheck.messages.Message;
import com.adobe.epubcheck.messages.MessageId;
import com.adobe.epubcheck.messages.Severity;
import com.adobe.epubcheck.reporting.CheckingReport;
import com.adobe.epubcheck.util.FeatureEnum;
import com.adobe.epubcheck.util.XmlReportImpl;
import com.adobe.epubcheck.util.XmpReportImpl;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
Expand Down Expand Up @@ -59,8 +66,16 @@ public boolean apply(MessageInfo message)
}
});

/* Whether to output messages on System.out */
private boolean verbose = false;
/* Stores the messages to be queried by assertions */
private List<MessageInfo> messages = new LinkedList<MessageInfo>();
/* The output format (can be JSON/XML/XMP/text */
private String format = "default";
/* A delegate report, used mostly for output formatting */
private Report delegate = null;
/* a writer storing the report output */
private final StringWriter output = new StringWriter();

public TestReport()
{
Expand All @@ -80,6 +95,11 @@ public void message(Message message, EPUBLocation location, Object... args)
fixMessage(message.getMessage(args)));
if (verbose) System.out.println(messageInfo);
messages.add(messageInfo);
// delegate to the formatting report
if (delegate != null)
{
delegate.message(message, location, args);
}
}

@Override
Expand All @@ -89,17 +109,45 @@ public void info(String resource, FeatureEnum feature, String value)
fixMessage("[" + feature + "] " + value));
if (verbose) System.out.println(messageInfo);
messages.add(messageInfo);
// delegate to the formatting report
if (delegate != null)
{
delegate.info(resource, feature, value);
}
}

@Override
public int generate()
{
return 0;
int result = 0;
if (delegate != null)
{
result = delegate.generate();
if (verbose) System.out.println(output);
}
return result;
}

@Override
public void initialize()
{
assert format != null;
switch (format)
{
case "JSON":
delegate = new CheckingReport(new PrintWriter(output), getEpubFileName());
break;
case "XML":
delegate = new XmlReportImpl(new PrintWriter(output), getEpubFileName(), "test");
break;
case "XMP":
delegate = new XmpReportImpl(new PrintWriter(output), getEpubFileName(), "test");
break;
default:
delegate = QuietReport.INSTANCE;
break;
}
delegate.initialize();
}

public Iterable<MessageInfo> getAll(Severity severity)
Expand Down Expand Up @@ -144,11 +192,17 @@ public synchronized List<MessageInfo> consumeAll(Severity severity)
messages = partition.get(false);
return partition.get(true);
}

public List<MessageInfo> getAllMessages() {

public List<MessageInfo> getAllMessages()
{
return ImmutableList.copyOf(messages);
}

public String getOutput()
{
return output.toString();
}

private String fixMessage(String message)
{
if (message == null)
Expand All @@ -158,4 +212,12 @@ private String fixMessage(String message)
return message.replaceAll("[\\s]+", " ");
}

public void setReportingFormat(String format)
{
if (format != null)
{
this.format = format;
}
}

}

0 comments on commit c075143

Please sign in to comment.