Skip to content

Commit

Permalink
Merge pull request #102 from openpreserve/feat/odf-2/compiance
Browse files Browse the repository at this point in the history
FEAT: ODF_2 Compliance testing
  • Loading branch information
carlwilson authored Nov 26, 2023
2 parents e1fabb3 + 58eee72 commit e5d4200
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static final MessageLog of() {
static final MessageLog of(final List<Message> messages) {
return new MessageLogImpl(messages);
}

@Override
public int size() {
return this.messages.size();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.openpreservation.odf.validation;

import java.io.IOException;

import org.openpreservation.messages.MessageLog;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.xml.OdfXmlDocument;

public interface Rule {
public String getId();
public String getName();
public String getDescription();
public MessageLog check(final OdfXmlDocument document) throws IOException;
public MessageLog check(final OdfPackage odfPackage) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.openpreservation.odf.validation.rules;

import org.openpreservation.odf.validation.Rule;

abstract class AbstractRule implements Rule {
final String id;
final String name;
final String description;

AbstractRule(final String id, final String name, final String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}

@Override
public String getId() {
return this.id;
}

@Override
public String getName() {
return this.name;
}

@Override
public String getDescription() {
return this.description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.openpreservation.odf.validation.rules;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

public class Rules {
private Rules() {
throw new AssertionError("Utility class must not be instantiated");
}

public static final ValidPackageRule odf2() throws ParserConfigurationException, SAXException {
return ValidPackageRule.getInstance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.openpreservation.odf.validation.rules;

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

import javax.xml.parsers.ParserConfigurationException;

import org.openpreservation.messages.Message;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.messages.Messages;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.validation.ValidatingParser;
import org.openpreservation.odf.validation.ValidationReport;
import org.openpreservation.odf.validation.Validators;
import org.openpreservation.odf.xml.OdfXmlDocument;
import org.xml.sax.SAXException;

class ValidPackageRule extends AbstractRule {
private final ValidatingParser validatingParser = Validators.getValidatingParser();

private ValidPackageRule(String id, String name, String description) throws ParserConfigurationException, SAXException {
super(id, name, description);
}

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

@Override
public MessageLog check(OdfPackage odfPackage) throws IOException {
Objects.requireNonNull(odfPackage, "odfPackage must not be null");
MessageLog messageLog = Messages.messageLogInstance();
ValidationReport validationReport = validatingParser.validatePackage(odfPackage);
if (!validationReport.isValid()) {
messageLog.add(validationReport.getMessages());
messageLog.add(Messages.getMessageInstance(this.id, Message.Severity.ERROR, this.getName(), this.getDescription()));
}
return messageLog;
}

static final ValidPackageRule getInstance() throws ParserConfigurationException, SAXException {
return new ValidPackageRule("ODF_2", "Standard Compliance", "The file MUST comply with the standard \"OASIS Open Document Format for Office Applications (OpenDocument) v1.3\".");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,4 @@ public class MessageImplTest {
public void testEqualsContract() {
EqualsVerifier.forClass(MessageImpl.class).verify();
}

@Test
public void testGetId() {

}

@Test
public void testGetInstance() {

}

@Test
public void testGetMessage() {

}

@Test
public void testGetSeverity() {

}

@Test
public void testGetSubMessage() {

}

@Test
public void testGetTimestamp() {

}

@Test
public void testHasSubMessage() {

}

@Test
public void testIsError() {

}

@Test
public void testIsFatal() {

}

@Test
public void testIsInfo() {

}

@Test
public void testIsWarning() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public void testParseNullStream() throws ParserConfigurationException, SAXExcept
@Test
public void testParsePackagePath() throws ParserConfigurationException, SAXException, URISyntaxException, IOException {
PackageParser parser = OdfPackages.getPackageParser();
Path path = Paths.get(new File(TestFiles.EMPTY_ODS.toURI()).getAbsolutePath());
OdfPackage pkg = parser.parsePackage(path);
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.EMPTY_ODS.toURI()).getAbsolutePath()));
assertNotNull("Parsed package should not be null", pkg);
assertTrue("Package should have a mimetype entry", pkg.hasMimeEntry());
assertEquals("Mimetype should be Spreadsheet", "application/vnd.oasis.opendocument.spreadsheet", pkg.getMimeType());
Expand All @@ -78,8 +77,7 @@ public void testParsePackagePath() throws ParserConfigurationException, SAXExcep
@Test
public void testParsePackageFile() throws ParserConfigurationException, SAXException, URISyntaxException, IOException {
PackageParser parser = OdfPackages.getPackageParser();
File file = new File(TestFiles.EMPTY_ODS.toURI());
OdfPackage pkg = parser.parsePackage(file);
OdfPackage pkg = parser.parsePackage(new File(TestFiles.EMPTY_ODS.toURI()));
assertNotNull("Parsed package should not be null", pkg);
assertTrue("Package should have a mimetype entry", pkg.hasMimeEntry());
assertEquals("Mimetype should be Spreadsheet", "application/vnd.oasis.opendocument.spreadsheet", pkg.getMimeType());
Expand All @@ -88,8 +86,7 @@ public void testParsePackageFile() throws ParserConfigurationException, SAXExcep
@Test
public void testParsePackageStream() throws ParserConfigurationException, SAXException, URISyntaxException, IOException {
PackageParser parser = OdfPackages.getPackageParser();
InputStream is = TestFiles.EMPTY_ODS.openStream();
OdfPackage pkg = parser.parsePackage(is, TestFiles.EMPTY_ODS.toString());
OdfPackage pkg = parser.parsePackage(TestFiles.EMPTY_ODS.openStream(), TestFiles.EMPTY_ODS.toString());
assertNotNull("Parsed package should not be null", pkg);
assertTrue("Package should have a mimetype entry", pkg.hasMimeEntry());
assertEquals("Mimetype should be Spreadsheet", "application/vnd.oasis.opendocument.spreadsheet", pkg.getMimeType());
Expand All @@ -98,8 +95,7 @@ public void testParsePackageStream() throws ParserConfigurationException, SAXExc
@Test
public void testDsigParsing() throws ParserConfigurationException, SAXException, IOException {
PackageParser parser = OdfPackages.getPackageParser();
InputStream is = TestFiles.DSIG_INVALID.openStream();
OdfPackage pkg = parser.parsePackage(is, TestFiles.DSIG_INVALID.toString());
OdfPackage pkg = parser.parsePackage(TestFiles.DSIG_INVALID.openStream(), TestFiles.DSIG_INVALID.toString());
ParseResult result = pkg.getEntryXmlParseResult("META-INF/documentsignatures.xml");
assertNotNull("Dsig file META-INF/documentsignatures.xml result should not be null" , result);
assertTrue("Package should have a well formed dsig for META-INF/documentsignatures.xml" , result.isWellFormed());
Expand All @@ -108,8 +104,7 @@ public void testDsigParsing() throws ParserConfigurationException, SAXException,
@Test
public void testManifestNotWF() throws IOException {
PackageParser parser = OdfPackages.getPackageParser();
InputStream is = TestFiles.MANIFEST_NOT_WF.openStream();
OdfPackage pkg = parser.parsePackage(is, TestFiles.MANIFEST_NOT_WF.toString());
OdfPackage pkg = parser.parsePackage(TestFiles.MANIFEST_NOT_WF.openStream(), TestFiles.MANIFEST_NOT_WF.toString());
ParseResult result = pkg.getEntryXmlParseResult("META-INF/manifest.xml");
assertNotNull("Dsig file META-INF/documentsignatures.xml result should not be null" , result);
assertFalse("Package should NOT have a well formed META-INF/manifest.xml" , result.isWellFormed());
Expand Down

0 comments on commit e5d4200

Please sign in to comment.