Skip to content

Commit

Permalink
fix: tweak code and tests to better support Java 21
Browse files Browse the repository at this point in the history
- adapt code in OFCChecker to a slight rewording of the OpenJDK message
  reported for invalid ZIP headers, so that EPUBCheck can issue the right
  error code (PKG-017)
- update the command line tests to no longer rely on the deprecated
  SecurityManager API to catch system exit codes. The tests now get
  the integer return code directly from the EpubChecker API
  • Loading branch information
rdeltour committed Dec 8, 2023
1 parent 183a3a6 commit 9888fcc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 81 deletions.
18 changes: 11 additions & 7 deletions src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,18 @@ private boolean checkContainerStructure(OCFCheckerState state)
return true;
} catch (Exception e)
{
switch (e.getMessage())
if (e.getMessage() != null &&
(
// reported by Open JDK:
e.getMessage().startsWith("invalid CEN header")
// reported by Oracle JDK 1.8:
|| e.getMessage().equals("MALFORMED")))
{
case "invalid CEN header (bad entry name)": // reported by OpenJDK
case "MALFORMED": // reported by Oracle JDK 1.8
report.message(MessageId.PKG_027, EPUBLocation.of(context), e.getLocalizedMessage());
break;
default:
}
else
{
report.message(MessageId.PKG_008, EPUBLocation.of(context), e.getLocalizedMessage());
break;
}
return false;
}
Expand Down Expand Up @@ -556,7 +559,8 @@ private void reportFeatures(OCFResource resource)
{
for (FeatureEnum feature : resource.getProperties().keySet())
{
// report.info(context.path, feature, resource.getProperties().get(feature));
// report.info(context.path, feature,
// resource.getProperties().get(feature));
report.info(resource.getPath(), feature, resource.getProperties().get(feature));
}
}
Expand Down
36 changes: 6 additions & 30 deletions src/test/java/com/adobe/epubcheck/tools/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.junit.Test;

import com.adobe.epubcheck.api.EpubCheck;
import com.adobe.epubcheck.tool.Checker;
import com.adobe.epubcheck.tool.EpubChecker;
import com.adobe.epubcheck.util.Messages;

public class CommandLineTest
Expand All @@ -35,7 +35,6 @@ public class CommandLineTest
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

private SecurityManager originalManager;
private PrintStream originalOut;
private PrintStream originalErr;
private final Messages messages = Messages.getInstance(Locale.ENGLISH);
Expand All @@ -46,8 +45,6 @@ public void setUp()
{
defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.ENGLISH);
this.originalManager = System.getSecurityManager();
System.setSecurityManager(new NoExitSecurityManager());
originalOut = System.out;
originalErr = System.err;
System.setOut(new PrintStream(outContent));
Expand All @@ -58,7 +55,6 @@ public void setUp()
public void tearDown()
{
Locale.setDefault(defaultLocale);
System.setSecurityManager(this.originalManager);
System.setOut(originalOut);
System.setErr(originalErr);
}
Expand Down Expand Up @@ -292,14 +288,7 @@ public void missingTranslationShouldFallbackTest()
Locale.setDefault(Locale.FRANCE);

URL inputUrl = CommandLineTest.class.getResource("valid.epub");

try
{
Checker.main(new String[] { inputUrl.getPath(), "--locale", "ar-eg" });
} catch (NoExitSecurityManager.ExitException e)
{
assertEquals("Return code should be zero", 0, e.status);
}
runCommandLineTest(0, inputUrl.getPath(), "--locale", "ar-eg");

assertTrue("Valid Locale without translation should fallback to JVM default.",
outContent.toString().contains("faites en utilisant"));
Expand Down Expand Up @@ -345,13 +334,7 @@ public void incorrectLocaleShouldFailTest()

URL inputUrl = CommandLineTest.class.getResource("valid.epub");

try
{
Checker.main(new String[] { inputUrl.getPath(), "--locale", "foobar" });
} catch (NoExitSecurityManager.ExitException e)
{
assertEquals("Return code should be zero", 0, e.status);
}
runCommandLineTest(0, inputUrl.getPath(), "--locale", "foobar");

assertTrue("Invalid Locale should use JVM default.",
outContent.toString().contains("faites en utilisant"));
Expand Down Expand Up @@ -756,18 +739,11 @@ public void xmpFileTest()
}
}

public static void runCommandLineTest(int expectedReturnCode, String... args)
public void runCommandLineTest(int expectedReturnCode, String... args)
{
int result = Integer.MAX_VALUE;
try
{
Checker.main(args);
} catch (NoExitSecurityManager.ExitException e)
{
result = e.status;
}

assertEquals("Return code", expectedReturnCode, result);
int returnCode = new EpubChecker().run(args);
assertEquals("Return code", expectedReturnCode, returnCode);
}

}
44 changes: 0 additions & 44 deletions src/test/java/com/adobe/epubcheck/tools/NoExitSecurityManager.java

This file was deleted.

0 comments on commit 9888fcc

Please sign in to comment.