From 1d88caf9ddf16d2602b1bd695124be594eb6f23e Mon Sep 17 00:00:00 2001 From: Isammoc OFF Date: Sat, 19 May 2018 18:36:57 +0200 Subject: [PATCH] Enforce immutability of IterableEnvelope through its iterator Ref: #862 --- .../cactoos/iterable/IterableEnvelope.java | 5 +- .../collection/CollectionEnvelopeTest.java | 3 -- .../iterable/IterableEnvelopeTest.java | 52 +++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java diff --git a/src/main/java/org/cactoos/iterable/IterableEnvelope.java b/src/main/java/org/cactoos/iterable/IterableEnvelope.java index d6e29538c5..1896b12d34 100644 --- a/src/main/java/org/cactoos/iterable/IterableEnvelope.java +++ b/src/main/java/org/cactoos/iterable/IterableEnvelope.java @@ -25,6 +25,7 @@ import java.util.Iterator; import org.cactoos.Scalar; +import org.cactoos.iterator.Immutable; import org.cactoos.scalar.UncheckedScalar; /** @@ -54,7 +55,9 @@ public IterableEnvelope(final Scalar> scalar) { @Override public final Iterator iterator() { - return this.iterable.value().iterator(); + return new Immutable<>( + this.iterable.value().iterator() + ); } } diff --git a/src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java b/src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java index 58d4f27e74..f2b80410e6 100644 --- a/src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java +++ b/src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java @@ -32,9 +32,6 @@ * Test case for {@link CollectionEnvelope}. * * @since 0.32 - * @todo #813:30min IterableEnvelope appears to be read only but it returns - * mutable Iterator through which user can remove elements. Implement immutable - * Iterator for IterableEnvelope `iterator()` method. * @checkstyle JavadocMethodCheck (500 lines) */ public final class CollectionEnvelopeTest { diff --git a/src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java b/src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java new file mode 100644 index 0000000000..88b5b65271 --- /dev/null +++ b/src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java @@ -0,0 +1,52 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.iterable; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import org.junit.Test; + +/** + * Test case for {@link IterableEnvelope}. + * + * @since 0.35 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class IterableEnvelopeTest { + + @Test(expected = UnsupportedOperationException.class) + public void returnsIteratorWithUnsupportedRemove() { + final IterableEnvelope list = new IterableEnvelope( + () -> { + final List inner = new LinkedList<>(); + inner.add("eleven"); + return inner; + } + ) { }; + final Iterator iterator = list.iterator(); + iterator.next(); + iterator.remove(); + } +}