Skip to content

Commit 4669e14

Browse files
committed
Make Attributes iterator throw NoSuchElementException
Once drained
1 parent 297ea4f commit 4669e14

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/main/java/org/jsoup/nodes/Attributes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Iterator;
1818
import java.util.List;
1919
import java.util.Map;
20+
import java.util.NoSuchElementException;
2021
import java.util.Set;
2122

2223
import static org.jsoup.internal.Normalizer.lowerCase;
@@ -367,6 +368,7 @@ public boolean hasNext() {
367368
@Override
368369
public Attribute next() {
369370
checkModified();
371+
if (i >= size) throw new NoSuchElementException();
370372
final Attribute attr = new Attribute(keys[i], (String) vals[i], Attributes.this);
371373
i++;
372374
return attr;

src/test/java/org/jsoup/nodes/AttributesTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Iterator;
88
import java.util.List;
99
import java.util.Map;
10+
import java.util.NoSuchElementException;
1011

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

@@ -143,6 +144,29 @@ public void testIteratorSkipsInternal() {
143144
assertEquals(2, seen);
144145
}
145146

147+
@Test void iteratorThrows() {
148+
Attributes attrs = new Attributes();
149+
attrs.put("One", "one").put("Two", "two");
150+
151+
Iterator<Attribute> it = attrs.iterator();
152+
int seen = 0;
153+
while (it.hasNext()) {
154+
it.next();
155+
seen++;
156+
}
157+
assertFalse(it.hasNext());
158+
assertEquals(2, seen);
159+
160+
boolean threw = false;
161+
try {
162+
Attribute next = it.next();
163+
assertNotNull(next); // not hit
164+
} catch (NoSuchElementException e) {
165+
threw = true;
166+
}
167+
assertTrue(threw);
168+
}
169+
146170
@Test
147171
public void testListSkipsInternal() {
148172
Attributes a = new Attributes();

0 commit comments

Comments
 (0)