Skip to content

Commit

Permalink
FIX: Unimplemented profile checks
Browse files Browse the repository at this point in the history
- for unimplemented profile checks against a single file, return an empty message log;
- added protection against missing package at the start of the profile check;
- added some null checks to profile checking methods; and
- removed some unused imports and parameters.

TODO: Implement schematron checks for a single file.
  • Loading branch information
carlwilson committed Nov 14, 2024
1 parent d5937c4 commit 40ecf86
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.openpreservation.messages.MessageFactory;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.messages.Messages;
import org.openpreservation.odf.pkg.OdfPackages;
import org.openpreservation.odf.pkg.PackageParser;
import org.openpreservation.odf.pkg.PackageParser.ParseException;
import org.openpreservation.odf.validation.Profile;
import org.openpreservation.odf.validation.ProfileResult;
Expand All @@ -42,7 +40,6 @@ class CliValidator implements Callable<Integer> {
private File[] toValidateFiles;
private final Validator validator = new Validator();
private MessageLog appMessages = Messages.messageLogInstance();
private final PackageParser parser = OdfPackages.getPackageParser();

@Override
public Integer call() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ public MessageLog check(ValidationReport report) throws ParseException {

@Override
public MessageLog check(final OdfXmlDocument document) throws ParseException {
Objects.requireNonNull(document, "document must not be null");
return Messages.messageLogInstance();
}

@Override
public MessageLog check(final OdfPackage odfPackage) throws ParseException {
Objects.requireNonNull(odfPackage, "odfPackage must not be null");
return Messages.messageLogInstance();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.pkg.OdfPackages;
import org.openpreservation.odf.pkg.PackageParser.ParseException;
import org.openpreservation.odf.validation.ValidationReport;
import org.openpreservation.odf.xml.OdfXmlDocument;

final class DigitalSignaturesRule extends AbstractRule {

Expand All @@ -23,11 +21,6 @@ private DigitalSignaturesRule(final String id, final String name, final String d
super(id, name, description, severity, isPrerequisite);
}

@Override
public MessageLog check(final OdfXmlDocument document) {
throw new UnsupportedOperationException("Unimplemented method 'check'");
}

@Override
public MessageLog check(final OdfPackage odfPackage) throws ParseException {
Objects.requireNonNull(odfPackage, "odfPackage must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ private EmbeddedObjectsRule(final String id, final String name, final String des

@Override
public MessageLog check(final OdfXmlDocument document) throws ParseException {
throw new UnsupportedOperationException("Unimplemented method 'check'");
Objects.requireNonNull(document, "document must not be null");
return this.schematron.check(document);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

import javax.xml.parsers.ParserConfigurationException;

Expand Down Expand Up @@ -37,11 +38,13 @@ private MacroRule(final String id, final String name, final String description,

@Override
public MessageLog check(final OdfXmlDocument document) throws ParseException {
throw new UnsupportedOperationException("Unimplemented method 'check'");
Objects.requireNonNull(document, "document must not be null");
return this.schematron.check(document);
}

@Override
public MessageLog check(final OdfPackage odfPackage) throws ParseException {
Objects.requireNonNull(odfPackage, "odfPackage must not be null");
MessageLog messageLog;
try {
messageLog = checkOdfScriptXml(odfPackage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.pkg.OdfPackages;
import org.openpreservation.odf.pkg.PackageParser.ParseException;
import org.openpreservation.odf.xml.OdfXmlDocument;

final class PackageMimeTypeRule extends AbstractRule {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public ProfileResult check(final ValidationReport report) throws ParseException
messages.add(getRulesetMessages(report,
this.rules.stream().filter(rule -> !rule.isPrerequisite()).collect(Collectors.toList())));
}
return ProfileResultImpl.of(report.document == null ? "" : report.document.getPackage().getName(), this.name,
final String packageName = report.document == null || report.document.getPackage() == null ? ""
: report.document.getPackage().getName();
return ProfileResultImpl.of(packageName, this.name,
report, messages);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.pkg.OdfPackages;
import org.openpreservation.odf.pkg.PackageParser.ParseException;
import org.openpreservation.odf.xml.OdfXmlDocument;

import com.helger.schematron.pure.SchematronResourcePure;
import com.helger.schematron.svrl.AbstractSVRLMessage;
Expand All @@ -36,11 +35,6 @@ private SchematronRule(final String id, final String name, final String descript
this.schematron = SchematronResourcePure.fromURL(schematronLoc);
}

@Override
public MessageLog check(final OdfXmlDocument document) throws ParseException {
throw new UnsupportedOperationException("Unimplemented method 'check'");
}

@Override
public MessageLog check(final OdfPackage odfPackage) throws ParseException {
Objects.requireNonNull(odfPackage, "odfPackage must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void testGetInstance() {
public void testCheckNullXmlDoc() {
Rule rule = Rules.odf7();
OdfXmlDocument nullDoc = null;
assertThrows("UnsupportedOperationException expected",
UnsupportedOperationException.class,
assertThrows("NullPointerException expected",
NullPointerException.class,
() -> {
rule.check(nullDoc);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void testGetInstance() {
@Test
public void testCheckNullXmlDoc() {
OdfXmlDocument nullDoc = null;
assertThrows("UnsupportedOperationException expected",
UnsupportedOperationException.class,
assertThrows("NullPointerException expected",
NullPointerException.class,
() -> {
rule.check(nullDoc);
});
Expand All @@ -50,7 +50,7 @@ public void testCheckNullXmlDoc() {
public void testCheckNullPackage() {
OdfPackage nullPkg = null;
assertThrows("NullPointerException expected",
NullPointerException.class,
NullPointerException.class,
() -> {
rule.check(nullPkg);
});
Expand Down Expand Up @@ -94,6 +94,7 @@ public void testCheckValidDsigPackage() throws IOException, URISyntaxException,
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.DSIG_VALID.toURI()).getAbsolutePath()));
MessageLog results = rule.check(pkg);
assertTrue("File contains valid digital signatures.", results.hasErrors());
assertEquals(1, results.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("POL_9")).count() > 0).count());
assertEquals(1, results.getMessages().values().stream()
.filter(m -> m.stream().filter(e -> e.getId().equals("POL_9")).count() > 0).count());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

public class EmbeddedObjectsRuleTest {
private final Rule rule = Rules.odf6();

@Test
public void testGetInstance() {
final SchematronResourcePure schematron = EmbeddedObjectsRule.getInstance(Severity.INFO).schematron.schematron;
Expand All @@ -41,8 +42,8 @@ public void testGetInstance() {
@Test
public void testCheckNullXmlDoc() {
OdfXmlDocument nullDoc = null;
assertThrows("UnsupportedOperationException expected",
UnsupportedOperationException.class,
assertThrows("NullPointerException expected",
NullPointerException.class,
() -> {
rule.check(nullDoc);
});
Expand All @@ -52,7 +53,7 @@ public void testCheckNullXmlDoc() {
public void testCheckNullPackage() {
OdfPackage nullPkg = null;
assertThrows("NullPointerException expected",
NullPointerException.class,
NullPointerException.class,
() -> {
rule.check(nullPkg);
});
Expand Down Expand Up @@ -91,7 +92,8 @@ public void testCheckValidPackage() throws IOException, URISyntaxException, Pars
@Test
public void testCheckEmbeddedPackage() throws IOException, URISyntaxException, ParseException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.OLE_EMBEDDED_PACKAGE.toURI()).getAbsolutePath()));
OdfPackage pkg = parser
.parsePackage(Paths.get(new File(TestFiles.OLE_EMBEDDED_PACKAGE.toURI()).getAbsolutePath()));
MessageLog results = rule.check(pkg);
assertFalse("Valid Package should not return errors.", results.hasErrors());
assertTrue("Valid Package should have info messages.", results.hasInfos());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void testGetInstance() {
public void testCheckNullXmlDoc() {
Rule rule = Rules.odf5();
OdfXmlDocument nullDoc = null;
assertThrows("UnsupportedOperationException expected",
UnsupportedOperationException.class,
assertThrows("NullPointerException expected",
NullPointerException.class,
() -> {
rule.check(nullDoc);
});
Expand All @@ -48,7 +48,7 @@ public void testCheckNullPackage() {
Rule rule = Rules.odf5();
OdfPackage nullPkg = null;
assertThrows("NullPointerException expected",
NullPointerException.class,
NullPointerException.class,
() -> {
rule.check(nullPkg);
});
Expand Down Expand Up @@ -80,12 +80,14 @@ public void testSchematronExternalDataPass() throws Exception {
public void testPackageExternalDataFail() throws Exception {
final Rule odf5 = Rules.odf5();
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.SCHEMATRON_CHECKER_ODS.toURI()).getAbsolutePath()));
OdfPackage pkg = parser
.parsePackage(Paths.get(new File(TestFiles.SCHEMATRON_CHECKER_ODS.toURI()).getAbsolutePath()));
MessageLog messages = odf5.check(pkg);
assertNotNull(messages);
assertEquals(0, messages.getErrors().size());
assertEquals(1, messages.getInfos().size());
assertEquals(1, messages.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("POL_5")).count() > 0).count());
assertEquals(1, messages.getMessages().values().stream()
.filter(m -> m.stream().filter(e -> e.getId().equals("POL_5")).count() > 0).count());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

public class MacrosTest {
private final Rule rule = Rules.odf8();

@Test
public void testGetInstance() {
final SchematronResourcePure schematron = MacroRule.getInstance(Severity.ERROR).schematron.schematron;
Expand All @@ -37,8 +38,8 @@ public void testGetInstance() {
@Test
public void testCheckNullXmlDoc() {
OdfXmlDocument nullDoc = null;
assertThrows("UnsupportedOperationException expected",
UnsupportedOperationException.class,
assertThrows("NullPointerException expected",
NullPointerException.class,
() -> {
rule.check(nullDoc);
});
Expand All @@ -48,7 +49,7 @@ public void testCheckNullXmlDoc() {
public void testCheckNullPackage() {
OdfPackage nullPkg = null;
assertThrows("NullPointerException expected",
NullPointerException.class,
NullPointerException.class,
() -> {
rule.check(nullPkg);
});
Expand Down Expand Up @@ -83,7 +84,8 @@ public void testPackageMacroFail() throws Exception {
MessageLog messages = rule.check(pkg);
assertNotNull(messages);
assertEquals(2, messages.getErrors().size());
assertEquals(2, messages.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("POL_8")).count() > 0).count());
assertEquals(2, messages.getMessages().values().stream()
.filter(m -> m.stream().filter(e -> e.getId().equals("POL_8")).count() > 0).count());
}

@Test
Expand All @@ -102,6 +104,7 @@ public void testPackageStarBasicFail() throws Exception {
MessageLog messages = rule.check(pkg);
assertNotNull(messages);
assertEquals(1, messages.getErrors().size());
assertEquals(1, messages.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("POL_8")).count() > 0).count());
assertEquals(1, messages.getMessages().values().stream()
.filter(m -> m.stream().filter(e -> e.getId().equals("POL_8")).count() > 0).count());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

public class PackageMimeTypeRuleTest {
private final Rule rule = Rules.odf3();

@Test
public void testEqualsContract() {
EqualsVerifier.forClass(PackageMimeTypeRule.class).verify();
Expand All @@ -38,16 +39,18 @@ public void testGetInstance() {
@Test
public void testCheckNullXmlDoc() throws ParseException {
OdfXmlDocument nullDoc = null;
MessageLog log = rule.check(nullDoc);
assertNotNull(log);
assertTrue(log.isEmpty());
assertThrows("NullPointerException expected",
NullPointerException.class,
() -> {
rule.check(nullDoc);
});
}

@Test
public void testCheckNullPackage() {
OdfPackage nullPkg = null;
assertThrows("NullPointerException expected",
NullPointerException.class,
NullPointerException.class,
() -> {
rule.check(nullPkg);
});
Expand All @@ -67,7 +70,8 @@ public void testCheckNotZipPackage() throws IOException, URISyntaxException, Par
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.EMPTY_FODS.toURI()).getAbsolutePath()));
MessageLog results = rule.check(pkg);
assertTrue("Document XML should return errors", results.hasErrors());
assertEquals(1, results.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("POL_3")).count() > 0).count());
assertEquals(1, results.getMessages().values().stream()
.filter(m -> m.stream().filter(e -> e.getId().equals("POL_3")).count() > 0).count());
}

@Test
Expand All @@ -76,7 +80,8 @@ public void testCheckNotWellFormedPackage() throws IOException, URISyntaxExcepti
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.BADLY_FORMED_PKG.toURI()).getAbsolutePath()));
MessageLog results = rule.check(pkg);
assertTrue("Badly formed package should return errors", results.hasErrors());
assertEquals(1, results.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("POL_3")).count() > 0).count());
assertEquals(1, results.getMessages().values().stream()
.filter(m -> m.stream().filter(e -> e.getId().equals("POL_3")).count() > 0).count());
}

@Test
Expand All @@ -93,16 +98,18 @@ public void testCheckNoMimePackage() throws IOException, URISyntaxException, Par
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.NO_MIME_ROOT_ODS.toURI()).getAbsolutePath()));
MessageLog results = rule.check(pkg);
assertTrue("No mimetype with root entry (invalid) return errors", results.hasErrors());
assertEquals(1, results.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("POL_3")).count() > 0).count());
assertEquals(1, results.getMessages().values().stream()
.filter(m -> m.stream().filter(e -> e.getId().equals("POL_3")).count() > 0).count());
}

@Test
public void testCheckNoMimeNoRootPackage() throws IOException, URISyntaxException, ParseException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.NO_MIME_NO_ROOT_ODS.toURI()).getAbsolutePath()));
OdfPackage pkg = parser
.parsePackage(Paths.get(new File(TestFiles.NO_MIME_NO_ROOT_ODS.toURI()).getAbsolutePath()));
MessageLog results = rule.check(pkg);
assertTrue("No mimetype with no root entry (valid) should return errors", results.hasErrors());
assertEquals(1, results.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("POL_3")).count() > 0).count());
assertEquals(1, results.getMessages().values().stream()
.filter(m -> m.stream().filter(e -> e.getId().equals("POL_3")).count() > 0).count());
}
}

0 comments on commit 40ecf86

Please sign in to comment.