Skip to content

Commit

Permalink
Make Attributes iterator throw NoSuchElementException
Browse files Browse the repository at this point in the history
Once drained
  • Loading branch information
jhy committed Nov 23, 2023
1 parent 297ea4f commit 4669e14
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/jsoup/nodes/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

import static org.jsoup.internal.Normalizer.lowerCase;
Expand Down Expand Up @@ -367,6 +368,7 @@ public boolean hasNext() {
@Override
public Attribute next() {
checkModified();
if (i >= size) throw new NoSuchElementException();
final Attribute attr = new Attribute(keys[i], (String) vals[i], Attributes.this);
i++;
return attr;
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/jsoup/nodes/AttributesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -143,6 +144,29 @@ public void testIteratorSkipsInternal() {
assertEquals(2, seen);
}

@Test void iteratorThrows() {
Attributes attrs = new Attributes();
attrs.put("One", "one").put("Two", "two");

Iterator<Attribute> it = attrs.iterator();
int seen = 0;
while (it.hasNext()) {
it.next();
seen++;
}
assertFalse(it.hasNext());
assertEquals(2, seen);

boolean threw = false;
try {
Attribute next = it.next();
assertNotNull(next); // not hit
} catch (NoSuchElementException e) {
threw = true;
}
assertTrue(threw);
}

@Test
public void testListSkipsInternal() {
Attributes a = new Attributes();
Expand Down

0 comments on commit 4669e14

Please sign in to comment.