Skip to content

Commit

Permalink
#865 Ensure empty PriorityQueue can be deserialized
Browse files Browse the repository at this point in the history
  • Loading branch information
theigl committed Nov 10, 2021
1 parent f68546e commit ffa3cf6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,8 @@ protected PriorityQueue createCopy (Kryo kryo, PriorityQueue original) {
}

private PriorityQueue createPriorityQueue (Class<? extends Collection> type, int size, Comparator comparator) {
if (type == PriorityQueue.class || type == null) return new PriorityQueue(size, comparator);
final int initialCapacity = Math.max(size, 1);
if (type == PriorityQueue.class || type == null) return new PriorityQueue(initialCapacity, comparator);
// Use reflection for subclasses.
try {
Constructor constructor = type.getConstructor(int.class, Comparator.class);
Expand All @@ -753,7 +754,7 @@ private PriorityQueue createPriorityQueue (Class<? extends Collection> type, int
} catch (SecurityException ignored) {
}
}
return (PriorityQueue)constructor.newInstance(comparator);
return (PriorityQueue)constructor.newInstance(initialCapacity, comparator);
} catch (Exception ex) {
throw new KryoException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,42 @@ public int compare (Integer o1, Integer o2) {
assertEquals(queue.peek(), copy.peek());
}

@Test
void testPriorityQueue () {
List<Integer> values = Arrays.asList(7, 0, 5, 123, 432);
PriorityQueue<Integer> queue = new PriorityQueue(3, new IntegerComparator());
queue.addAll(values);

kryo.register(PriorityQueue.class);
kryo.register(IntegerComparator.class);
roundTrip(12, queue);
}

@Test
void testPriorityQueueSubclass () {
List<Integer> values = Arrays.asList(7, 0, 5, 123, 432);
PriorityQueue<Integer> queue = new PriorityQueueSubclass(3, new IntegerComparator());
queue.addAll(values);

kryo.register(PriorityQueueSubclass.class);
kryo.register(IntegerComparator.class);
roundTrip(12, queue);
}

@Test
void testEmptyPriorityQueue () {
PriorityQueue<Integer> queue = new PriorityQueue();
kryo.register(PriorityQueue.class);
roundTrip(3, queue);
}

@Test
void testEmptyPriorityQueueSubclass () {
PriorityQueue<Integer> queue = new PriorityQueueSubclass();
kryo.register(PriorityQueueSubclass.class);
roundTrip(3, queue);
}

@Test
void testCalendar () {
kryo.setRegistrationRequired(false);
Expand Down Expand Up @@ -467,6 +503,17 @@ void testURLSerializer () throws Exception {
roundTrip(78, new URL("https://github.com:443/EsotericSoftware/kryo/pulls?utf8=%E2%9C%93&q=is%3Apr"));
}

protected void doAssertEquals(Object object1, Object object2) {
if (object1 instanceof PriorityQueue && object2 instanceof PriorityQueue) {
final PriorityQueue q1 = (PriorityQueue) object1;
final PriorityQueue q2 = (PriorityQueue) object2;
super.doAssertEquals(q1.peek(), q2.peek());
super.doAssertEquals(q1.toArray(), q2.toArray());
} else {
super.doAssertEquals(object1, object2);
}
}

public enum TestEnum {
a, b, c
}
Expand Down Expand Up @@ -500,4 +547,18 @@ public BigIntegerSubclass (String val) {
}
}

static class PriorityQueueSubclass extends PriorityQueue {
public PriorityQueueSubclass() {
}

public PriorityQueueSubclass(int initialCapacity, Comparator comparator) {
super(initialCapacity, comparator);
}
}

static class IntegerComparator implements Comparator<Integer> {
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
}
}

0 comments on commit ffa3cf6

Please sign in to comment.