Skip to content

Commit 9888fcc

Browse files
committed
fix: tweak code and tests to better support Java 21
- 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
1 parent 183a3a6 commit 9888fcc

File tree

3 files changed

+17
-81
lines changed

3 files changed

+17
-81
lines changed

src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,18 @@ private boolean checkContainerStructure(OCFCheckerState state)
345345
return true;
346346
} catch (Exception e)
347347
{
348-
switch (e.getMessage())
348+
if (e.getMessage() != null &&
349+
(
350+
// reported by Open JDK:
351+
e.getMessage().startsWith("invalid CEN header")
352+
// reported by Oracle JDK 1.8:
353+
|| e.getMessage().equals("MALFORMED")))
349354
{
350-
case "invalid CEN header (bad entry name)": // reported by OpenJDK
351-
case "MALFORMED": // reported by Oracle JDK 1.8
352355
report.message(MessageId.PKG_027, EPUBLocation.of(context), e.getLocalizedMessage());
353-
break;
354-
default:
356+
}
357+
else
358+
{
355359
report.message(MessageId.PKG_008, EPUBLocation.of(context), e.getLocalizedMessage());
356-
break;
357360
}
358361
return false;
359362
}
@@ -556,7 +559,8 @@ private void reportFeatures(OCFResource resource)
556559
{
557560
for (FeatureEnum feature : resource.getProperties().keySet())
558561
{
559-
// report.info(context.path, feature, resource.getProperties().get(feature));
562+
// report.info(context.path, feature,
563+
// resource.getProperties().get(feature));
560564
report.info(resource.getPath(), feature, resource.getProperties().get(feature));
561565
}
562566
}

src/test/java/com/adobe/epubcheck/tools/CommandLineTest.java

+6-30
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.junit.Test;
2323

2424
import com.adobe.epubcheck.api.EpubCheck;
25-
import com.adobe.epubcheck.tool.Checker;
25+
import com.adobe.epubcheck.tool.EpubChecker;
2626
import com.adobe.epubcheck.util.Messages;
2727

2828
public class CommandLineTest
@@ -35,7 +35,6 @@ public class CommandLineTest
3535
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
3636
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
3737

38-
private SecurityManager originalManager;
3938
private PrintStream originalOut;
4039
private PrintStream originalErr;
4140
private final Messages messages = Messages.getInstance(Locale.ENGLISH);
@@ -46,8 +45,6 @@ public void setUp()
4645
{
4746
defaultLocale = Locale.getDefault();
4847
Locale.setDefault(Locale.ENGLISH);
49-
this.originalManager = System.getSecurityManager();
50-
System.setSecurityManager(new NoExitSecurityManager());
5148
originalOut = System.out;
5249
originalErr = System.err;
5350
System.setOut(new PrintStream(outContent));
@@ -58,7 +55,6 @@ public void setUp()
5855
public void tearDown()
5956
{
6057
Locale.setDefault(defaultLocale);
61-
System.setSecurityManager(this.originalManager);
6258
System.setOut(originalOut);
6359
System.setErr(originalErr);
6460
}
@@ -292,14 +288,7 @@ public void missingTranslationShouldFallbackTest()
292288
Locale.setDefault(Locale.FRANCE);
293289

294290
URL inputUrl = CommandLineTest.class.getResource("valid.epub");
295-
296-
try
297-
{
298-
Checker.main(new String[] { inputUrl.getPath(), "--locale", "ar-eg" });
299-
} catch (NoExitSecurityManager.ExitException e)
300-
{
301-
assertEquals("Return code should be zero", 0, e.status);
302-
}
291+
runCommandLineTest(0, inputUrl.getPath(), "--locale", "ar-eg");
303292

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

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

348-
try
349-
{
350-
Checker.main(new String[] { inputUrl.getPath(), "--locale", "foobar" });
351-
} catch (NoExitSecurityManager.ExitException e)
352-
{
353-
assertEquals("Return code should be zero", 0, e.status);
354-
}
337+
runCommandLineTest(0, inputUrl.getPath(), "--locale", "foobar");
355338

356339
assertTrue("Invalid Locale should use JVM default.",
357340
outContent.toString().contains("faites en utilisant"));
@@ -756,18 +739,11 @@ public void xmpFileTest()
756739
}
757740
}
758741

759-
public static void runCommandLineTest(int expectedReturnCode, String... args)
742+
public void runCommandLineTest(int expectedReturnCode, String... args)
760743
{
761-
int result = Integer.MAX_VALUE;
762-
try
763-
{
764-
Checker.main(args);
765-
} catch (NoExitSecurityManager.ExitException e)
766-
{
767-
result = e.status;
768-
}
769744

770-
assertEquals("Return code", expectedReturnCode, result);
745+
int returnCode = new EpubChecker().run(args);
746+
assertEquals("Return code", expectedReturnCode, returnCode);
771747
}
772748

773749
}

src/test/java/com/adobe/epubcheck/tools/NoExitSecurityManager.java

-44
This file was deleted.

0 commit comments

Comments
 (0)