From a5e934b3c0e2829cad12a5d95944aa79c7cc3e5f Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Sun, 13 May 2018 16:46:29 +0300 Subject: [PATCH 1/3] #824: Use cactoos FormattedText instead of static String format. --- .../cactoos/collection/CollectionNoNulls.java | 18 ++-- .../org/cactoos/io/LoggingInputStream.java | 84 +++++++++++-------- .../org/cactoos/io/LoggingOutputStream.java | 65 ++++++++------ .../org/cactoos/iterator/IteratorNoNulls.java | 13 +-- .../java/org/cactoos/list/ListNoNulls.java | 53 +++++++----- src/main/java/org/cactoos/map/MapEntry.java | 10 ++- src/main/java/org/cactoos/map/MapNoNulls.java | 50 ++++++----- .../java/org/cactoos/scalar/AndInThreads.java | 6 +- .../org/cactoos/scalar/CheckedScalar.java | 12 ++- .../java/org/cactoos/func/ProcOfTest.java | 14 ++-- .../java/org/cactoos/io/TempFileTest.java | 11 ++- .../java/org/cactoos/map/StickyMapTest.java | 7 +- 12 files changed, 213 insertions(+), 130 deletions(-) diff --git a/src/main/java/org/cactoos/collection/CollectionNoNulls.java b/src/main/java/org/cactoos/collection/CollectionNoNulls.java index f68cea5509..24e313aec3 100644 --- a/src/main/java/org/cactoos/collection/CollectionNoNulls.java +++ b/src/main/java/org/cactoos/collection/CollectionNoNulls.java @@ -26,6 +26,8 @@ import java.util.Collection; import java.util.Iterator; import org.cactoos.iterator.IteratorNoNulls; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; /** * A decorator of {@link Collection} that tolerates no NULLs. @@ -82,9 +84,11 @@ public Object[] toArray() { for (int idx = 0; idx < array.length; ++idx) { if (array[idx] == null) { throw new IllegalStateException( - String.format( - "Item #%d of #toArray() is NULL", idx - ) + new UncheckedText( + new FormattedText( + "Item #%d of #toArray() is NULL", idx + ) + ).asString() ); } } @@ -98,9 +102,11 @@ public T[] toArray(final T[] array) { for (int idx = 0; idx < array.length; ++idx) { if (array[idx] == null) { throw new IllegalStateException( - String.format( - "Item #%d of #toArray(array) is NULL", idx - ) + new UncheckedText( + new FormattedText( + "Item #%d of #toArray(array) is NULL", idx + ) + ).asString() ); } } diff --git a/src/main/java/org/cactoos/io/LoggingInputStream.java b/src/main/java/org/cactoos/io/LoggingInputStream.java index 8ab71b65d4..cdedcc393f 100644 --- a/src/main/java/org/cactoos/io/LoggingInputStream.java +++ b/src/main/java/org/cactoos/io/LoggingInputStream.java @@ -32,6 +32,8 @@ import java.util.logging.Logger; import org.cactoos.scalar.StickyScalar; import org.cactoos.scalar.UncheckedScalar; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; /** * Logged input stream. @@ -139,19 +141,21 @@ public int read(final byte[] buf, final int offset, final int len) this.bytes.getAndAdd(byts); this.time.getAndAdd(millis); } - final String msg = String.format( - "Read %d byte(s) from %s in %dms.", - this.bytes.get(), - this.source, - this.time.get() + final UncheckedText msg = new UncheckedText( + new FormattedText( + "Read %d byte(s) from %s in %dms.", + this.bytes.get(), + this.source, + this.time.get() + ) ); if (byts > 0) { if (!this.level.value().equals(Level.INFO)) { - this.logger.log(this.level.value(), msg); + this.logger.log(this.level.value(), msg.asString()); } } else { if (this.level.value().equals(Level.INFO)) { - this.logger.info(msg); + this.logger.info(msg.asString()); } } return byts; @@ -162,11 +166,13 @@ public long skip(final long num) throws IOException { final long skipped = this.origin.skip(num); this.logger.log( this.level.value(), - String.format( - "Skipped %d byte(s) from %s.", - skipped, - this.source - ) + new UncheckedText( + new FormattedText( + "Skipped %d byte(s) from %s.", + skipped, + this.source + ) + ).asString() ); return skipped; } @@ -176,11 +182,13 @@ public int available() throws IOException { final int avail = this.origin.available(); this.logger.log( this.level.value(), - String.format( - "There is(are) %d byte(s) available from %s.", - avail, - this.source - ) + new UncheckedText( + new FormattedText( + "There is(are) %d byte(s) available from %s.", + avail, + this.source + ) + ).asString() ); return avail; } @@ -190,10 +198,12 @@ public void close() throws IOException { this.origin.close(); this.logger.log( this.level.value(), - String.format( - "Closed input stream from %s.", - this.source - ) + new UncheckedText( + new FormattedText( + "Closed input stream from %s.", + this.source + ) + ).asString() ); } @@ -202,11 +212,13 @@ public void mark(final int limit) { this.origin.mark(limit); this.logger.log( this.level.value(), - String.format( - "Marked position %d from %s.", - limit, - this.source - ) + new UncheckedText( + new FormattedText( + "Marked position %d from %s.", + limit, + this.source + ) + ).asString() ); } @@ -215,10 +227,12 @@ public void reset() throws IOException { this.origin.reset(); this.logger.log( this.level.value(), - String.format( - "Reset input stream from %s.", - this.source - ) + new UncheckedText( + new FormattedText( + "Reset input stream from %s.", + this.source + ) + ).asString() ); } @@ -233,10 +247,12 @@ public boolean markSupported() { } this.logger.log( this.level.value(), - String.format( - msg, - this.source - ) + new UncheckedText( + new FormattedText( + msg, + this.source + ) + ).asString() ); return supported; } diff --git a/src/main/java/org/cactoos/io/LoggingOutputStream.java b/src/main/java/org/cactoos/io/LoggingOutputStream.java index 05ce9e9b0d..f0f4762fc8 100644 --- a/src/main/java/org/cactoos/io/LoggingOutputStream.java +++ b/src/main/java/org/cactoos/io/LoggingOutputStream.java @@ -30,6 +30,8 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import java.util.logging.Logger; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; /** * Logged output stream. @@ -123,12 +125,14 @@ public void write(final byte[] buf, final int offset, if (!level.equals(Level.INFO)) { this.logger.log( level, - String.format( - "Written %d byte(s) to %s in %dms.", - this.bytes.get(), - this.destination, - this.time.get() - ) + new UncheckedText( + new FormattedText( + "Written %d byte(s) to %s in %dms.", + this.bytes.get(), + this.destination, + this.time.get() + ) + ).asString() ); } } @@ -140,20 +144,24 @@ public void close() throws IOException { if (level.equals(Level.INFO)) { this.logger.log( level, - String.format( - "Written %d byte(s) to %s in %dms.", - this.bytes.get(), - this.destination, - this.time.get() - ) + new UncheckedText( + new FormattedText( + "Written %d byte(s) to %s in %dms.", + this.bytes.get(), + this.destination, + this.time.get() + ) + ).asString() ); } this.logger.log( level, - String.format( - "Closed output stream from %s.", - this.destination - ) + new UncheckedText( + new FormattedText( + "Closed output stream from %s.", + this.destination + ) + ).asString() ); } @@ -164,21 +172,24 @@ public void flush() throws IOException { if (level.equals(Level.INFO)) { this.logger.log( level, - String.format( - "Written %d byte(s) to %s in %dms.", - this.bytes.get(), - this.destination, - this.time.get() - ) + new UncheckedText( + new FormattedText( + "Written %d byte(s) to %s in %dms.", + this.bytes.get(), + this.destination, + this.time.get() + ) + ).asString() ); } this.logger.log( level, - String.format( - "Flushed output stream from %s.", - this.destination - ) + new UncheckedText( + new FormattedText( + "Flushed output stream from %s.", + this.destination + ) + ).asString() ); } - } diff --git a/src/main/java/org/cactoos/iterator/IteratorNoNulls.java b/src/main/java/org/cactoos/iterator/IteratorNoNulls.java index d2c0356832..96940b6ed5 100644 --- a/src/main/java/org/cactoos/iterator/IteratorNoNulls.java +++ b/src/main/java/org/cactoos/iterator/IteratorNoNulls.java @@ -25,6 +25,8 @@ import java.util.Iterator; import java.util.concurrent.atomic.AtomicLong; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; /** * A decorator of an {@link Iterator} that returns no NULL. @@ -65,10 +67,12 @@ public X next() { final X next = this.iterator.next(); if (next == null) { throw new IllegalStateException( - String.format( - "Item #%d of %s is NULL", - this.pos.get(), this.iterator - ) + new UncheckedText( + new FormattedText( + "Item #%d of %s is NULL", + this.pos.get(), this.iterator + ) + ).asString() ); } this.pos.incrementAndGet(); @@ -79,5 +83,4 @@ public X next() { public void remove() { this.iterator.remove(); } - } diff --git a/src/main/java/org/cactoos/list/ListNoNulls.java b/src/main/java/org/cactoos/list/ListNoNulls.java index 6269d3816d..473b27b443 100644 --- a/src/main/java/org/cactoos/list/ListNoNulls.java +++ b/src/main/java/org/cactoos/list/ListNoNulls.java @@ -28,6 +28,8 @@ import java.util.List; import java.util.ListIterator; import org.cactoos.collection.CollectionNoNulls; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; /** * A decorator of {@link List} that tolerates no NULLs. @@ -130,10 +132,13 @@ public T get(final int index) { final T item = this.list.get(index); if (item == null) { throw new IllegalStateException( - String.format( - "Item #%d of %s is NULL", - index, this.list - ) + new UncheckedText( + new FormattedText( + "Item #%d of %s is NULL", + index, + this.list + ) + ).asString() ); } return item; @@ -143,19 +148,23 @@ public T get(final int index) { public T set(final int index, final T item) { if (item == null) { throw new IllegalArgumentException( - String.format( - "Item can't be NULL in #set(%d,T)", - index - ) + new UncheckedText( + new FormattedText( + "Item can't be NULL in #set(%d,T)", + index + ) + ).asString() ); } final T result = this.list.set(index, item); if (result == null) { throw new IllegalStateException( - String.format( - "Result of #set(%d,T) is NULL", - index - ) + new UncheckedText( + new FormattedText( + "Result of #set(%d,T) is NULL", + index + ) + ).asString() ); } return result; @@ -165,10 +174,12 @@ public T set(final int index, final T item) { public void add(final int index, final T item) { if (item == null) { throw new IllegalArgumentException( - String.format( - "Item can't be NULL in #add(%d,T)", - index - ) + new UncheckedText( + new FormattedText( + "Item can't be NULL in #add(%d,T)", + index + ) + ).asString() ); } this.list.add(index, item); @@ -179,10 +190,12 @@ public T remove(final int index) { final T result = this.list.remove(index); if (result == null) { throw new IllegalStateException( - String.format( - "Result of #remove(%d) is NULL", - index - ) + new UncheckedText( + new FormattedText( + "Result of #remove(%d) is NULL", + index + ) + ).asString() ); } return result; diff --git a/src/main/java/org/cactoos/map/MapEntry.java b/src/main/java/org/cactoos/map/MapEntry.java index 970fe1e667..f72bc59dfe 100644 --- a/src/main/java/org/cactoos/map/MapEntry.java +++ b/src/main/java/org/cactoos/map/MapEntry.java @@ -24,6 +24,8 @@ package org.cactoos.map; import java.util.Map; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; /** * MapEntry as {@link java.util.AbstractMap.Entry}. @@ -57,7 +59,13 @@ public MapEntry(final K src, final V trgt) { @Override public String toString() { - return String.format("%s=%s", this.key, this.value); + return new UncheckedText( + new FormattedText( + "%s=%s", + this.key, + this.value + ) + ).asString(); } @Override diff --git a/src/main/java/org/cactoos/map/MapNoNulls.java b/src/main/java/org/cactoos/map/MapNoNulls.java index 28f859eac3..0023bd4082 100644 --- a/src/main/java/org/cactoos/map/MapNoNulls.java +++ b/src/main/java/org/cactoos/map/MapNoNulls.java @@ -27,6 +27,8 @@ import java.util.Map; import java.util.Set; import org.cactoos.collection.CollectionNoNulls; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; /** * A decorator of {@link Map} that tolerates no NULLs. @@ -93,10 +95,12 @@ public final V get(final Object key) { final V value = this.map.get(key); if (value == null) { throw new IllegalStateException( - String.format( - "Value returned by #get(%s) is NULL", - key - ) + new UncheckedText( + new FormattedText( + "Value returned by #get(%s) is NULL", + key + ) + ).asString() ); } return value; @@ -106,25 +110,32 @@ public final V get(final Object key) { public final V put(final K key, final V value) { if (key == null) { throw new IllegalStateException( - String.format( - "Key at #put(K,%s) is NULL", value - ) + new UncheckedText( + new FormattedText( + "Key at #put(K,%s) is NULL", + value + ) + ).asString() ); } if (value == null) { throw new IllegalStateException( - String.format( - "Value at #put(%s,V) is NULL", key - ) + new UncheckedText( + new FormattedText( + "Value at #put(%s,V) is NULL", key + ) + ).asString() ); } final V result = this.map.put(key, value); if (result == null) { throw new IllegalStateException( - String.format( - "Value returned by #put(%s,%s) is NULL", - key, value - ) + new UncheckedText( + new FormattedText( + "Value returned by #put(%s,%s) is NULL", + key, value + ) + ).asString() ); } return result; @@ -140,10 +151,12 @@ public final V remove(final Object key) { final V result = this.map.remove(key); if (result == null) { throw new IllegalStateException( - String.format( - "Value returned by #remove(%s) is NULL", - key - ) + new UncheckedText( + new FormattedText( + "Value returned by #remove(%s) is NULL", + key + ) + ).asString() ); } return result; @@ -174,5 +187,4 @@ public final Collection values() { public final Set> entrySet() { return this.map.entrySet(); } - } diff --git a/src/main/java/org/cactoos/scalar/AndInThreads.java b/src/main/java/org/cactoos/scalar/AndInThreads.java index dcba7f01ea..b14d62e1c7 100644 --- a/src/main/java/org/cactoos/scalar/AndInThreads.java +++ b/src/main/java/org/cactoos/scalar/AndInThreads.java @@ -35,6 +35,7 @@ import org.cactoos.func.FuncOf; import org.cactoos.iterable.IterableOf; import org.cactoos.iterable.Mapped; +import org.cactoos.text.FormattedText; /** * Logical conjunction, in multiple threads. @@ -237,10 +238,10 @@ public Boolean value() throws Exception { try { if (!this.service.awaitTermination(1L, TimeUnit.MINUTES)) { throw new IllegalStateException( - String.format( + new FormattedText( "Can't terminate the service, result=%b", result - ) + ).asString() ); } } catch (final InterruptedException ex) { @@ -250,5 +251,4 @@ public Boolean value() throws Exception { } return result; } - } diff --git a/src/main/java/org/cactoos/scalar/CheckedScalar.java b/src/main/java/org/cactoos/scalar/CheckedScalar.java index 23e8ed94a2..d23c506589 100644 --- a/src/main/java/org/cactoos/scalar/CheckedScalar.java +++ b/src/main/java/org/cactoos/scalar/CheckedScalar.java @@ -26,6 +26,8 @@ import org.cactoos.Func; import org.cactoos.Scalar; import org.cactoos.func.UncheckedFunc; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; /** * Scalar that wraps an original checked exception thrown by the origin using @@ -108,7 +110,15 @@ private E wrappedException(final Exception exp) { exp.getClass(), wrapped.getClass() ).value(); final String message = wrapped.getMessage() - .replaceFirst(String.format("%s: ", exp.getClass().getName()), ""); + .replaceFirst( + new UncheckedText( + new FormattedText( + "%s: ", + exp.getClass().getName() + ) + ).asString(), + "" + ); if (level >= 0 && message.equals(exp.getMessage())) { wrapped = (E) exp; } diff --git a/src/test/java/org/cactoos/func/ProcOfTest.java b/src/test/java/org/cactoos/func/ProcOfTest.java index 3b9f5b7c56..e9c391aab1 100644 --- a/src/test/java/org/cactoos/func/ProcOfTest.java +++ b/src/test/java/org/cactoos/func/ProcOfTest.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; +import org.cactoos.text.FormattedText; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -51,10 +52,10 @@ public void run() { } ).exec("You can use any input in a Runnable."); MatcherAssert.assertThat( - String.format( + new FormattedText( "Wrong result when created with a Runnable. Expected %s", str - ), + ).asString(), list, Matchers.contains(str) ); @@ -74,10 +75,10 @@ public String call() throws Exception { } ).exec("You can use any input in a Callable"); MatcherAssert.assertThat( - String.format( + new FormattedText( "Wrong result when created with a Callable. Expected %s", str - ), + ).asString(), list, Matchers.contains(str) ); @@ -94,13 +95,12 @@ public void worksWithFunc() throws Exception { } ).exec(str); MatcherAssert.assertThat( - String.format( + new FormattedText( "Wrong result when created with a Func. Expected %s", str - ), + ).asString(), list, Matchers.contains(str) ); } - } diff --git a/src/test/java/org/cactoos/io/TempFileTest.java b/src/test/java/org/cactoos/io/TempFileTest.java index 37f1c3f484..d826054065 100644 --- a/src/test/java/org/cactoos/io/TempFileTest.java +++ b/src/test/java/org/cactoos/io/TempFileTest.java @@ -26,6 +26,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import org.cactoos.text.FormattedText; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -86,8 +87,10 @@ public void deleteFile() throws Exception { @Test public void createFileWithPrefix() throws Exception { - final String prefix = - String.format("randomPrefix%s", System.currentTimeMillis()); + final String prefix = new FormattedText( + "randomPrefix%s", + System.currentTimeMillis() + ).asString(); try (final TempFile file = new TempFile(prefix, "")) { MatcherAssert.assertThat( "File not created with the given prefix", @@ -99,9 +102,9 @@ public void createFileWithPrefix() throws Exception { @Test public void createFileWithSuffix() throws Exception { - final String suffix = String.format( + final String suffix = new FormattedText( "randomSuffix%s", System.currentTimeMillis() - ); + ).asString(); try (final TempFile file = new TempFile("", suffix)) { MatcherAssert.assertThat( "File not created with the given suffix", diff --git a/src/test/java/org/cactoos/map/StickyMapTest.java b/src/test/java/org/cactoos/map/StickyMapTest.java index 0e844ee970..d961ef516b 100644 --- a/src/test/java/org/cactoos/map/StickyMapTest.java +++ b/src/test/java/org/cactoos/map/StickyMapTest.java @@ -29,6 +29,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.cactoos.iterable.IterableOf; import org.cactoos.iterator.Repeated; +import org.cactoos.text.FormattedText; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -60,7 +61,8 @@ public void ignoresChangesInMap() throws Exception { final Map map = new StickyMap<>( new MapOf<>( () -> new Repeated<>( - size.incrementAndGet(), () -> new MapEntry<>( + size.incrementAndGet(), + () -> new MapEntry<>( new SecureRandom().nextInt(), 1 ) @@ -125,7 +127,7 @@ public void extendsExistingMapWithTwoFuncs() throws Exception { MatcherAssert.assertThat( "Can't transform and decorate a list of entries with two funcs", new StickyMap( - color -> String.format("[%s]", color), + color -> new FormattedText("[%s]", color).asString(), String::toUpperCase, new StickyMap( new MapEntry<>("black!", "Black!"), @@ -136,5 +138,4 @@ public void extendsExistingMapWithTwoFuncs() throws Exception { Matchers.hasValue(Matchers.equalTo("BLUE!")) ); } - } From d6614b60b422ee44a146e48b7134307fdabea0e0 Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Sun, 13 May 2018 17:04:14 +0300 Subject: [PATCH 2/3] #824: Use cactoos FormattedText instead of static String format - improve test coverage --- src/test/java/org/cactoos/map/MapEntryTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/org/cactoos/map/MapEntryTest.java b/src/test/java/org/cactoos/map/MapEntryTest.java index ec9faa3483..4a4667c443 100644 --- a/src/test/java/org/cactoos/map/MapEntryTest.java +++ b/src/test/java/org/cactoos/map/MapEntryTest.java @@ -25,6 +25,7 @@ import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.hamcrest.core.IsEqual; import org.junit.Test; /** @@ -83,4 +84,12 @@ public void compareHash() { ); } + @Test + public void toStringMethod() { + MatcherAssert.assertThat( + "ToString method returns unexpected value", + new MapEntry<>("somekey", "somevalue").toString(), + new IsEqual<>("somekey=somevalue") + ); + } } From 4cacb112b08fb1da069c2f4b41e172128c0847ac Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Sun, 20 May 2018 11:51:19 +0300 Subject: [PATCH 3/3] #824: Use cactoos FormattedText instead of static String format - improve test coverage --- .../collection/CollectionNoNullsTest.java | 68 ++++++++++++ .../cactoos/iterator/IteratorNoNullsTest.java | 70 ++++++++++++ .../org/cactoos/list/ListNoNullsTest.java | 104 ++++++++++++++++++ .../java/org/cactoos/map/MapNoNullsTest.java | 46 +++++++- 4 files changed, 283 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/cactoos/collection/CollectionNoNullsTest.java create mode 100644 src/test/java/org/cactoos/iterator/IteratorNoNullsTest.java create mode 100644 src/test/java/org/cactoos/list/ListNoNullsTest.java diff --git a/src/test/java/org/cactoos/collection/CollectionNoNullsTest.java b/src/test/java/org/cactoos/collection/CollectionNoNullsTest.java new file mode 100644 index 0000000000..37f8ba4604 --- /dev/null +++ b/src/test/java/org/cactoos/collection/CollectionNoNullsTest.java @@ -0,0 +1,68 @@ +/* + * 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.collection; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +/** + * Test cases for {@link CollectionNoNulls}. + * + *

There is no thread-safety guarantee. + * + * @since 0.35 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +public final class CollectionNoNullsTest { + + /** + * A rule for handling an exception. + */ + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Test + public void throwsErrorIfNullInToArray() { + this.exception.expect(IllegalStateException.class); + this.exception.expectMessage( + "Item #1 of #toArray() is NULL" + ); + new CollectionNoNulls<>( + new CollectionOf<>(1, null, 3) + ).toArray(); + } + + @Test + public void throwsErrorIfNullInToArrayWithArg() { + this.exception.expect(IllegalStateException.class); + this.exception.expectMessage( + "Item #1 of #toArray(array) is NULL" + ); + new CollectionNoNulls<>( + new CollectionOf<>(1, null, 3) + ).toArray(new Object[3]); + } +} diff --git a/src/test/java/org/cactoos/iterator/IteratorNoNullsTest.java b/src/test/java/org/cactoos/iterator/IteratorNoNullsTest.java new file mode 100644 index 0000000000..65c47b0569 --- /dev/null +++ b/src/test/java/org/cactoos/iterator/IteratorNoNullsTest.java @@ -0,0 +1,70 @@ +/* + * 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.iterator; + +import java.util.Iterator; +import org.hamcrest.core.StringContains; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +/** + * Test cases for {@link IteratorNoNulls}. + * + *

There is no thread-safety guarantee. + * + * @since 0.35 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class IteratorNoNullsTest { + + /** + * A rule for handling an exception. + */ + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Test + public void nextThrowsErrorIfNull() { + this.exception.expect(IllegalStateException.class); + this.exception.expectMessage( + new StringContains( + "Item #0 of org.cactoos.iterator.IteratorNoNullsTest" + ) + ); + new IteratorNoNulls<>( + new Iterator() { + @Override + public boolean hasNext() { + return true; + } + + @Override + public Integer next() { + return null; + } + } + ).next(); + } +} diff --git a/src/test/java/org/cactoos/list/ListNoNullsTest.java b/src/test/java/org/cactoos/list/ListNoNullsTest.java new file mode 100644 index 0000000000..d551def2fc --- /dev/null +++ b/src/test/java/org/cactoos/list/ListNoNullsTest.java @@ -0,0 +1,104 @@ +/* + * 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.list; + +import java.util.ArrayList; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +/** + * Test cases for {@link ListNoNulls}. + * + *

There is no thread-safety guarantee. + * + * @since 0.35 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +public final class ListNoNullsTest { + + /** + * A rule for handling an exception. + */ + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Test + public void getThrowsErrorIfNull() { + this.exception.expect(IllegalStateException.class); + this.exception.expectMessage( + "Item #1 of [1, null, 3] is NULL" + ); + new ListNoNulls<>( + new ListOf<>(1, null, 3) + ).get(1); + } + + @Test + public void setThrowsErrorIfArgumentNull() { + this.exception.expect(IllegalArgumentException.class); + this.exception.expectMessage( + "Item can't be NULL in #set(2,T)" + ); + new ListNoNulls<>( + new ListOf<>(1, null, 3) + ).set(2, null); + } + + @Test + public void setThrowsErrorIfPreviousValueNull() { + this.exception.expect(IllegalStateException.class); + this.exception.expectMessage( + "Result of #set(0,T) is NULL" + ); + final ArrayList list = new ArrayList<>(1); + list.add(null); + new ListNoNulls<>(list).set(0, 2); + } + + @Test + public void addThrowsErrorIfArgumentNull() { + this.exception.expect(IllegalArgumentException.class); + this.exception.expectMessage( + "Item can't be NULL in #add(0,T)" + ); + new ListNoNulls<>( + new ArrayList<>(1) + ).add(0, null); + } + + @Test + public void removeThrowsErrorIfValueNull() { + this.exception.expect(IllegalStateException.class); + this.exception.expectMessage( + "Result of #remove(0) is NULL" + ); + final ArrayList list = new ArrayList<>(1); + list.add(null); + new ListNoNulls<>( + list + ).remove(0); + } +} diff --git a/src/test/java/org/cactoos/map/MapNoNullsTest.java b/src/test/java/org/cactoos/map/MapNoNullsTest.java index ac20defc67..25f687d290 100644 --- a/src/test/java/org/cactoos/map/MapNoNullsTest.java +++ b/src/test/java/org/cactoos/map/MapNoNullsTest.java @@ -28,7 +28,9 @@ import org.hamcrest.Matchers; import org.hamcrest.core.IsEqual; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * Test case for {@link MapNoNulls}. @@ -37,13 +39,21 @@ * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ -@SuppressWarnings({ - "PMD.TooManyMethods", - "PMD.NonStaticInitializer", - "serial" - }) +@SuppressWarnings( + { + "PMD.TooManyMethods", + "PMD.NonStaticInitializer", + "serial" + } +) public final class MapNoNullsTest { + /** + * A rule for handling an exception. + */ + @Rule + public final ExpectedException exception = ExpectedException.none(); + @Test public void getSize() { MatcherAssert.assertThat( @@ -356,4 +366,30 @@ public void entrySet() { ) ); } + + @Test + public void putThrowsErrorIfValueNull() { + this.exception.expect(IllegalStateException.class); + this.exception.expectMessage( + "Value at #put(1,V) is NULL" + ); + new MapNoNulls( + new HashMap<>() + ).put(1, null); + } + + @Test + public void putThrowsErrorIfPreviousValueNull() { + this.exception.expect(IllegalStateException.class); + this.exception.expectMessage( + "Value returned by #put(1,2) is NULL" + ); + new MapNoNulls<>( + new HashMap() { + { + put(1, null); + } + } + ).put(1, 2); + } }