Repository navigation
Fix comparison of Keys in Name Trees #742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| /* | ||
| * This file is part of veraPDF Parser, a module of the veraPDF project. | ||
| * Copyright (c) 2015-2026, veraPDF Consortium <[email protected]> | ||
| * All rights reserved. | ||
| * | ||
| * veraPDF Parser is free software: you can redistribute it and/or modify | ||
| * it under the terms of either: | ||
| * | ||
| * The GNU General public license GPLv3+. | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with veraPDF Parser as the LICENSE.GPL file in the root of the source | ||
| * tree. If not, see http://www.gnu.org/licenses/ or | ||
| * https://www.gnu.org/licenses/gpl-3.0.en.html. | ||
| * | ||
| * The Mozilla Public License MPLv2+. | ||
| * You should have received a copy of the Mozilla Public License along with | ||
| * veraPDF Parser as the LICENSE.MPL file in the root of the source tree. | ||
| * If a copy of the MPL was not distributed with this file, you can obtain one at | ||
| * http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package org.verapdf.pd; | ||
|
|
||
| import org.junit.Test; | ||
| import org.verapdf.as.ASAtom; | ||
| import org.verapdf.cos.COSArray; | ||
| import org.verapdf.cos.COSDictionary; | ||
| import org.verapdf.cos.COSInteger; | ||
| import org.verapdf.cos.COSObject; | ||
| import org.verapdf.cos.COSString; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
|
|
||
| public class PDNameTreeNodeTest { | ||
|
|
||
| @Test | ||
| public void returnsValueForEachKeyInNames() { | ||
| PDNameTreeNode node = PDNameTreeNode.create(treeNode( | ||
| array(literal("Alpha"), literal("Gamma")), | ||
| array(literal("Alpha"), integer(1), literal("Beta"), integer(2), literal("Gamma"), integer(3)), | ||
| null)); | ||
|
|
||
| assertValue(1, node.getObject(key("Alpha"))); | ||
| assertValue(2, node.getObject(key("Beta"))); | ||
| assertValue(3, node.getObject(key("Gamma"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void searchesKeysEqualToLimits() { | ||
| PDNameTreeNode node = PDNameTreeNode.create(treeNode( | ||
| array(literal("Alpha"), literal("Gamma")), | ||
| array(literal("Alpha"), integer(1), literal("Gamma"), integer(3)), | ||
| null)); | ||
|
|
||
| assertValue(1, node.getObject(key("Alpha"))); | ||
| assertValue(3, node.getObject(key("Gamma"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsNullForKeyMissingWithinLimits() { | ||
| PDNameTreeNode node = PDNameTreeNode.create(treeNode( | ||
| array(literal("Alpha"), literal("Gamma")), | ||
| array(literal("Alpha"), integer(1), literal("Gamma"), integer(3)), | ||
| null)); | ||
|
|
||
| assertNull(node.getObject(key("Bravo"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsNullForKeysOutsideLimits() { | ||
| PDNameTreeNode node = PDNameTreeNode.create(treeNode( | ||
| array(literal("Beta"), literal("Gamma")), | ||
| array(literal("Beta"), integer(2), literal("Gamma"), integer(3)), | ||
| null)); | ||
|
|
||
| assertNull(node.getObject(key("Alpha"))); | ||
| assertNull(node.getObject(key("Zulu"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void findsKeyAcrossLiteralAndHexEncoding() { | ||
| PDNameTreeNode literalNode = PDNameTreeNode.create(treeNode( | ||
| null, array(literal("Beta"), integer(2)), null)); | ||
| PDNameTreeNode hexNode = PDNameTreeNode.create(treeNode( | ||
| null, array(hex("Beta"), integer(2)), null)); | ||
|
|
||
| assertValue(2, literalNode.getObject(hexKey("Beta"))); | ||
| assertValue(2, hexNode.getObject(key("Beta"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void comparesKeysAsUnsignedBytes() { | ||
| // Byte 0xE9 is negative as a signed byte, so this checks that keys compare as unsigned bytes | ||
| byte[] highKey = {(byte) 0xE9}; | ||
| PDNameTreeNode node = PDNameTreeNode.create(treeNode( | ||
| array(literal("Alpha"), COSString.construct(highKey, false)), | ||
| array(literal("Alpha"), integer(1), COSString.construct(highKey, false), integer(2)), | ||
| null)); | ||
|
|
||
| assertValue(2, node.getObject(new COSString(highKey, false))); | ||
| } | ||
|
|
||
| @Test | ||
| public void searchesNamesWhenNodeHasNoLimits() { | ||
| PDNameTreeNode node = PDNameTreeNode.create(treeNode( | ||
| null, array(literal("Alpha"), integer(1), literal("Beta"), integer(2)), null)); | ||
|
|
||
| assertValue(1, node.getObject(key("Alpha"))); | ||
| assertValue(2, node.getObject(key("Beta"))); | ||
| assertNull(node.getObject(key("Zulu"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsNullWhenNodeHasNoNamesOrKids() { | ||
| PDNameTreeNode node = PDNameTreeNode.create(treeNode( | ||
| array(literal("Alpha"), literal("Gamma")), null, null)); | ||
|
|
||
| assertNull(node.getObject(key("Beta"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void findsStringKeysAfterNonStringKeyInNames() { | ||
| PDNameTreeNode node = PDNameTreeNode.create(treeNode( | ||
| null, array(integer(7), integer(10), literal("Beta"), integer(2)), null)); | ||
|
|
||
| assertValue(2, node.getObject(key("Beta"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void searchesKidsForKey() { | ||
| PDNameTreeNode root = PDNameTreeNode.create(twoLeafTree()); | ||
|
|
||
| assertValue(1, root.getObject(key("Alpha"))); | ||
| assertValue(2, root.getObject(key("Beta"))); | ||
| assertValue(3, root.getObject(key("Gamma"))); | ||
| assertValue(4, root.getObject(key("Delta"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsNullWhenKeyIsNotInAnyKid() { | ||
| PDNameTreeNode root = PDNameTreeNode.create(twoLeafTree()); | ||
|
|
||
| // Bravo lies inside the root limits but in the gap between the two kids | ||
| assertNull(root.getObject(key("Bravo"))); | ||
| assertNull(root.getObject(key("Zulu"))); | ||
| } | ||
|
|
||
| private static COSObject twoLeafTree() { | ||
| return treeNode( | ||
| array(literal("Alpha"), literal("Gamma")), | ||
| null, | ||
| array( | ||
| treeNode(array(literal("Alpha"), literal("Beta")), | ||
| array(literal("Alpha"), integer(1), literal("Beta"), integer(2)), null), | ||
| treeNode(array(literal("Delta"), literal("Gamma")), | ||
| array(literal("Gamma"), integer(3), literal("Delta"), integer(4)), null))); | ||
| } | ||
|
|
||
| private static COSObject treeNode(COSObject limits, COSObject names, COSObject kids) { | ||
| COSObject tree = COSDictionary.construct(); | ||
| if (limits != null) { | ||
| tree.setKey(ASAtom.LIMITS, limits); | ||
| } | ||
| if (names != null) { | ||
| tree.setKey(ASAtom.NAMES, names); | ||
| } | ||
| if (kids != null) { | ||
| tree.setKey(ASAtom.KIDS, kids); | ||
| } | ||
| return tree; | ||
| } | ||
|
|
||
| private static COSObject array(COSObject... items) { | ||
| COSObject result = COSArray.construct(); | ||
| for (COSObject item : items) { | ||
| result.add(item); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static COSObject literal(String value) { | ||
| return COSString.construct(bytes(value), false); | ||
| } | ||
|
|
||
| private static COSObject hex(String value) { | ||
| return COSString.construct(bytes(value), true); | ||
| } | ||
|
|
||
| private static COSObject integer(long value) { | ||
| return COSInteger.construct(value); | ||
| } | ||
|
|
||
| private static COSString key(String value) { | ||
| return new COSString(bytes(value), false); | ||
| } | ||
|
|
||
| private static COSString hexKey(String value) { | ||
| return new COSString(bytes(value), true); | ||
| } | ||
|
|
||
| private static byte[] bytes(String value) { | ||
| return value.getBytes(StandardCharsets.ISO_8859_1); | ||
| } | ||
|
|
||
| private static void assertValue(long expected, COSObject actual) { | ||
| assertNotNull(actual); | ||
| assertEquals(Long.valueOf(expected), actual.getInteger()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
git diff cdbd343b729ec7769db172f72a27fa21450b033b 78a2d83db1ee695344d10bc2641a52bbd0e65aad -- src/main/java/org/verapdf/pd/PDNameTreeNode.java rg -n 'getNames\(\)|getLimitsArray\(\)|parseLimitsArray\(\)|getObject\(|containsKey\(' src/main/java/org/verapdf/pd/PDNameTreeNode.java pom.xmlRepository: veraPDF/veraPDF-parser
Length of output: 5460
Script executed:
Repository: veraPDF/veraPDF-parser
Length of output: 27240
Preserve legacy signatures or document an intentional breaking API change.
getObject(String)andcontainsKey(String)now have different parameter descriptors.getLimitsArray()andparseLimitsArray()now have different array return descriptors. Existing source callers will not compile, and compiled callers can fail withNoSuchMethodError.getNames()is a narrower case. Its generic type changes, but both versions erase toMap, so the method is not directly binary-incompatible. It remains source-incompatible, and existing binaries that read keys asStringcan fail because the returned keys are nowCOSString.If compatibility is required, retain the
Stringlookup overloads and the original names/limits methods. Add differently namedCOSStringAPIs where return-type overloading is impossible. If this release intentionally permits breaking changes, document the migration and release policy. Documentation alone does not preserve existing binaries.