From ac618b015d80394640a7518f3dcd44bbd0ec13fa Mon Sep 17 00:00:00 2001 From: Piotrek Zygielo Date: Sat, 19 Sep 2020 21:55:57 +0200 Subject: [PATCH] Prevent NPEx --- .../sun/messaging/jmq/util/lists/FifoSet.java | 2 +- .../jmq/util/lists/FifoSetIteratorTest.java | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 mq/main/comm-util/src/test/java/com/sun/messaging/jmq/util/lists/FifoSetIteratorTest.java diff --git a/mq/main/comm-util/src/main/java/com/sun/messaging/jmq/util/lists/FifoSet.java b/mq/main/comm-util/src/main/java/com/sun/messaging/jmq/util/lists/FifoSet.java index 05e77486a..acbcde574 100644 --- a/mq/main/comm-util/src/main/java/com/sun/messaging/jmq/util/lists/FifoSet.java +++ b/mq/main/comm-util/src/main/java/com/sun/messaging/jmq/util/lists/FifoSet.java @@ -135,7 +135,7 @@ public E next() { if (current == null && initialPass) { current = first_entry; initialPass = false; - } else { + } else if (current != null) { current = current.getNext(); } diff --git a/mq/main/comm-util/src/test/java/com/sun/messaging/jmq/util/lists/FifoSetIteratorTest.java b/mq/main/comm-util/src/test/java/com/sun/messaging/jmq/util/lists/FifoSetIteratorTest.java new file mode 100644 index 000000000..0bc1f4e14 --- /dev/null +++ b/mq/main/comm-util/src/test/java/com/sun/messaging/jmq/util/lists/FifoSetIteratorTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package com.sun.messaging.jmq.util.lists; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Set; +import java.util.TreeSet; + +import org.junit.jupiter.api.Test; + +class FifoSetIteratorTest { + + @Test + void consecutiveNextsOnEmptySetShouldThrowNoSuchElementException() { + consecutiveNextsOnEmptySetShouldThrowNoSuchElementException(new FifoSet()); + } + + @Test + void consecutiveNextsOnStandardSetThrowNoSuchElementException() { + consecutiveNextsOnEmptySetShouldThrowNoSuchElementException(new TreeSet()); + } + + private void consecutiveNextsOnEmptySetShouldThrowNoSuchElementException(Set s) { + Iterator iterator = s.iterator(); + assertThrows(NoSuchElementException.class, () -> iterator.next()); + assertThrows(NoSuchElementException.class, () -> iterator.next()); + } +}