From 42ec23afbc4f65a1a521807e66c25b4ca8d57535 Mon Sep 17 00:00:00 2001 From: Maxim Date: Tue, 30 Apr 2024 12:19:11 +0300 Subject: [PATCH] Refactoring validateCorpus methods --- .../org/verapdf/pdfa/qa/ResultSetImpl.java | 58 +++++-------------- .../verapdf/integration/tests/CorpusTest.java | 13 +---- 2 files changed, 18 insertions(+), 53 deletions(-) diff --git a/veraPDF-integration/src/main/java/org/verapdf/pdfa/qa/ResultSetImpl.java b/veraPDF-integration/src/main/java/org/verapdf/pdfa/qa/ResultSetImpl.java index 04100c9..ec4b8ff 100644 --- a/veraPDF-integration/src/main/java/org/verapdf/pdfa/qa/ResultSetImpl.java +++ b/veraPDF-integration/src/main/java/org/verapdf/pdfa/qa/ResultSetImpl.java @@ -198,56 +198,27 @@ public String toString() { * @param validator * @return */ - public static ResultSet validateCorpus(final TestCorpus corpus, final PDFAValidator validator) { + public static ResultSet validateCorpus(final TestCorpus corpus, final PDFAValidator validator, final PDFAFlavour flavour) { Set results = new HashSet<>(); Set exceptions = new HashSet<>(); Components.Timer batchTimer = Components.Timer.start(); long maxMemUse = 0; for (String itemName : corpus.getItemNames()) { + System.out.println(itemName); CorpusItemId id = null; Components.Timer jobTimer = Components.Timer.start(); - try { - id = CorpusItemIdImpl.fromFileName(validator.getProfile().getPDFAFlavour().getPart(), itemName, ""); - } catch (IllegalArgumentException excep) { - LOG.log(Level.FINE, "Problem generating ID for corpus item:" + itemName, excep); - } - if (id != null) { - try (PDFAParser loader = Foundries.defaultInstance().createParser(corpus.getItemStream(itemName), - validator.getProfile().getPDFAFlavour())) { - ValidationResult result = validator.validate(loader); - long memUsed = (ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() / MEGABYTE); - maxMemUse = (memUsed > maxMemUse) ? memUsed : maxMemUse; - results.add(new Result(id, result, jobTimer.stop(), memUsed)); - } catch (Throwable e) { - LOG.log(Level.SEVERE, String.format("Caught throwable testing %s from corpus %s", itemName, - corpus.getDetails().getName())); - LOG.log(Level.SEVERE, e.getClass().getName()); - LOG.log(Level.SEVERE, e.getMessage()); - exceptions.add(new Incomplete(id, e)); - } - } - } - return new ResultSetImpl(corpus.getDetails(), corpus.getType().getId(), validator.getProfile(), results, exceptions, batchTimer.stop(), - maxMemUse); - } - - public static ResultSet validateCorpus(final TestCorpus corpus) { - Set results = new HashSet<>(); - Set exceptions = new HashSet<>(); - Components.Timer batchTimer = Components.Timer.start(); - long maxMemUse = 0; - for (String itemName : corpus.getItemNames()) { - CorpusItemId id = null; - Components.Timer jobTimer = Components.Timer.start(); - try (PDFAParser loader = Foundries.defaultInstance().createParser(corpus.getItemStream(itemName))) { - PDFAFlavour flavour = loader.getFlavour(); - try (PDFAValidator validator = Foundries.defaultInstance().createValidator(flavour, false)) { - id = CorpusItemIdImpl.fromFileName(validator.getProfile().getPDFAFlavour().getPart(), itemName, ""); - ValidationResult result = validator.validate(loader); - long memUsed = (ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() / MEGABYTE); - maxMemUse = (memUsed > maxMemUse) ? memUsed : maxMemUse; - results.add(new Result(id, result, jobTimer.stop(), memUsed)); + try (PDFAParser loader = Foundries.defaultInstance().createParser(corpus.getItemStream(itemName), flavour); + PDFAValidator newValidator = flavour != PDFAFlavour.NO_FLAVOUR ? null : Foundries.defaultInstance().createValidator(loader.getFlavour(), false)) { + PDFAValidator currentValidator = flavour != PDFAFlavour.NO_FLAVOUR ? validator : newValidator; + try { + id = CorpusItemIdImpl.fromFileName(currentValidator.getProfile().getPDFAFlavour().getPart(), itemName, ""); + } catch (IllegalArgumentException excep) { + LOG.log(Level.FINE, "Problem generating ID for corpus item:" + itemName, excep); } + ValidationResult result = currentValidator.validate(loader); + long memUsed = (ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() / MEGABYTE); + maxMemUse = (memUsed > maxMemUse) ? memUsed : maxMemUse; + results.add(new Result(id, result, jobTimer.stop(), memUsed)); } catch (Throwable e) { LOG.log(Level.SEVERE, String.format("Caught throwable testing %s from corpus %s", itemName, corpus.getDetails().getName())); @@ -256,7 +227,8 @@ public static ResultSet validateCorpus(final TestCorpus corpus) { exceptions.add(new Incomplete(id, e)); } } - return new ResultSetImpl(corpus.getDetails(), corpus.getType().getId(), Profiles.defaultProfile(), results, exceptions, batchTimer.stop(), + return new ResultSetImpl(corpus.getDetails(), corpus.getType().getId(), flavour != PDFAFlavour.NO_FLAVOUR ? + validator.getProfile() : Profiles.defaultProfile(), results, exceptions, batchTimer.stop(), maxMemUse); } diff --git a/veraPDF-integration/src/test/java/org/verapdf/integration/tests/CorpusTest.java b/veraPDF-integration/src/test/java/org/verapdf/integration/tests/CorpusTest.java index bd86f81..b60e5d8 100644 --- a/veraPDF-integration/src/test/java/org/verapdf/integration/tests/CorpusTest.java +++ b/veraPDF-integration/src/test/java/org/verapdf/integration/tests/CorpusTest.java @@ -25,14 +25,7 @@ import java.io.InputStream; import java.io.PrintWriter; import java.io.Writer; -import java.util.ArrayList; -import java.util.Collections; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -127,14 +120,14 @@ private static void testCorpora(final List resultSets) { for (TestCorpus corpus : CorpusManager.corporaForFlavour(flavour)) { if (flavour != PDFAFlavour.NO_FLAVOUR) { try (PDFAValidator validator = Foundries.defaultInstance().createValidator(flavour, false)) { - ResultSet results = ResultSetImpl.validateCorpus(corpus, validator); + ResultSet results = ResultSetImpl.validateCorpus(corpus, validator, flavour); resultSets.add(results); } catch (IOException excep) { // Just exception closing validator excep.printStackTrace(); } } else { - ResultSet results = ResultSetImpl.validateCorpus(corpus); + ResultSet results = ResultSetImpl.validateCorpus(corpus, null, flavour); resultSets.add(results); } }