Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into 1367
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloamadeu committed May 5, 2020
2 parents ff77a3a + fd8d5bc commit 3f0a01d
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 53 deletions.
23 changes: 18 additions & 5 deletions src/main/java/org/cactoos/iterable/IterableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@
import org.cactoos.func.UncheckedFunc;
import org.cactoos.iterator.IteratorOf;
import org.cactoos.scalar.And;
import org.cactoos.scalar.FallbackFrom;
import org.cactoos.scalar.False;
import org.cactoos.scalar.Folded;
import org.cactoos.scalar.Or;
import org.cactoos.scalar.ScalarWithFallback;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.SumOfInt;
import org.cactoos.scalar.True;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
Expand Down Expand Up @@ -146,6 +150,7 @@ public Iterator<X> iterator() {

@Override
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings("EQ_UNUSUAL")
@SuppressWarnings (value = "unchecked")
public boolean equals(final Object other) {
return new Unchecked<>(
new Or(
Expand All @@ -154,12 +159,20 @@ public boolean equals(final Object other) {
() -> other != null,
() -> Iterable.class.isAssignableFrom(other.getClass()),
() -> {
final Iterable<?> compared = (Iterable<?>) other;
final Iterator<?> iterator = compared.iterator();
return new Unchecked<>(
final Iterable<X> compared = (Iterable<X>) other;
return new ScalarWithFallback<>(
new And(
(X input) -> input.equals(iterator.next()),
this
(X value) -> true,
new Matched<>(
this,
compared
)
),
new IterableOf<>(
new FallbackFrom<>(
IllegalStateException.class,
ex -> false
)
)
).value();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/iterable/Matched.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public Matched(
final Iterator<X> ftr = fst.iterator();
final Iterator<X> str = snd.iterator();
final List<X> rslt = new LinkedList<>();
while (ftr.hasNext()) {
if (!str.hasNext()) {
while (ftr.hasNext() || str.hasNext()) {
if (!str.hasNext() || !ftr.hasNext()) {
throw new IllegalStateException(
"Size mismatch of iterators"
);
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/cactoos/scalar/ScalarWithFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

import java.util.Comparator;
import java.util.Map;
import org.cactoos.Func;
import org.cactoos.Scalar;
import org.cactoos.func.FuncWithFallback;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterator.Filtered;
import org.cactoos.iterator.Sorted;
import org.cactoos.map.MapOf;
Expand All @@ -52,6 +54,41 @@ public final class ScalarWithFallback<T> implements Scalar<T> {
*/
private final Iterable<FallbackFrom<T>> fallbacks;

/**
* Ctor.
* @param origin Original scalar
* @param exception Supported exception type
* @param fallback Function that converts the given exception into fallback value
*/
public ScalarWithFallback(
final Scalar<T> origin,
final Class<? extends Throwable> exception,
final Func<Throwable, T> fallback
) {
this(origin, new IterableOf<Class<? extends Throwable>>(exception), fallback);
}

/**
* Ctor.
* @param origin Original scalar
* @param exceptions Supported exceptions types
* @param fallback Function that converts the given exception into fallback value
*/
public ScalarWithFallback(
final Scalar<T> origin,
final Iterable<Class<? extends Throwable>> exceptions,
final Func<Throwable, T> fallback
) {
this(
origin,
new IterableOf<FallbackFrom<T>>(
new FallbackFrom<>(
exceptions, fallback
)
)
);
}

/**
* Ctor.
* @param origin Original scalar
Expand Down
4 changes: 0 additions & 4 deletions src/test/java/org/cactoos/io/TeeInputFromBytesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
* Test case for {@link TeeInput}. Cases for ctors which use
* {@link org.cactoos.Bytes} as an input.
* @since 1.0
* @todo #1341:30min Correct any TeeInput tests that are not testing if the
* path actually contains the content, just that TeeInput, as an Input,has
* the content. Solution: test the content of the file, not the content of
* teeinput (e.g. #1331).
* @checkstyle JavadocMethodCheck (100 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (100 lines)
*/
Expand Down
79 changes: 37 additions & 42 deletions src/test/java/org/cactoos/io/TeeInputFromCharArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.cactoos.scalar.LengthOf;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -52,13 +53,12 @@ public void copiesFromCharArrayWithCharsetToFile() throws IOException {
final String input =
"Hello, товарищ file #1 äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
new LengthOf(
new TeeInput(input.toCharArray(), output, StandardCharsets.UTF_8)
).intValue();
new Assertion<>(
"char array must be copied to the file with charset UTF_8",
new TeeInput(
input.toCharArray(),
output,
StandardCharsets.UTF_8
),
new InputOf(output),
new InputHasContent(input)
).affirm();
}
Expand All @@ -69,13 +69,12 @@ public void copiesFromCharArrayWithCharsetByNameToFile()
final String input =
"Hello, товарищ file #2 äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
new LengthOf(
new TeeInput(input.toCharArray(), output, StandardCharsets.UTF_8.name())
).intValue();
new Assertion<>(
"char array must be copied to the file with UTF_8 charset's name",
new TeeInput(
input.toCharArray(),
output,
StandardCharsets.UTF_8.name()
),
new InputOf(output),
new InputHasContent(input)
).affirm();
}
Expand All @@ -85,12 +84,12 @@ public void copiesFromCharArrayToOutput() throws IOException {
final String input =
"Hello, товарищ output #1 äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
new LengthOf(
new TeeInput(input.toCharArray(), new OutputTo(output))
).intValue();
new Assertion<>(
"char array must be copied to the output",
new TeeInput(
input.toCharArray(),
new OutputTo(output)
),
new InputOf(output),
new InputHasContent(input)
).affirm();
}
Expand All @@ -100,13 +99,12 @@ public void copiesFromCharArrayWithCharsetToOutput() throws IOException {
final String input =
"Hello, товарищ output #2 äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
new LengthOf(
new TeeInput(input.toCharArray(), new OutputTo(output), StandardCharsets.UTF_8)
).intValue();
new Assertion<>(
"char array must be copied to the output with UTF_8 charset",
new TeeInput(
input.toCharArray(),
new OutputTo(output),
StandardCharsets.UTF_8
),
new InputOf(output),
new InputHasContent(input)
).affirm();
}
Expand All @@ -117,13 +115,12 @@ public void copiesFromCharArrayWithCharsetByNameToOutput()
final String input =
"Hello, товарищ output #3 äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
new LengthOf(
new TeeInput(input.toCharArray(), new OutputTo(output), StandardCharsets.UTF_8.name())
).intValue();
new Assertion<>(
"char array must be copied to the output with UTF_8 charset's name",
new TeeInput(
input.toCharArray(),
new OutputTo(output),
StandardCharsets.UTF_8.name()
),
new InputOf(output),
new InputHasContent(input)
).affirm();
}
Expand All @@ -133,12 +130,12 @@ public void copiesFromCharArrayToPath() throws IOException {
final String input =
"Hello, товарищ path #1 äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
new LengthOf(
new TeeInput(input.toCharArray(), output.toPath())
).intValue();
new Assertion<>(
"char array must be copied to the path",
new TeeInput(
input.toCharArray(),
output.toPath()
),
new InputOf(output),
new InputHasContent(input)
).affirm();
}
Expand All @@ -148,13 +145,12 @@ public void copiesFromCharArrayWithCharsetToPath() throws IOException {
final String input =
"Hello, товарищ path #2 äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
new LengthOf(
new TeeInput(input.toCharArray(), output.toPath(), StandardCharsets.UTF_8)
).intValue();
new Assertion<>(
"char array must be copied to the path with UTF_8 charset",
new TeeInput(
input.toCharArray(),
output.toPath(),
StandardCharsets.UTF_8
),
new InputOf(output),
new InputHasContent(input)
).affirm();
}
Expand All @@ -165,13 +161,12 @@ public void copiesFromCharArrayWithCharsetByNameToPath()
final String input =
"Hello, товарищ path #3 äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
new LengthOf(
new TeeInput(input.toCharArray(), output.toPath(), StandardCharsets.UTF_8.name())
).intValue();
new Assertion<>(
"char array must be copied to the path with UTF_8 charset's name",
new TeeInput(
input.toCharArray(),
output.toPath(),
StandardCharsets.UTF_8.name()
),
new InputOf(output),
new InputHasContent(input)
).affirm();
}
Expand All @@ -181,12 +176,12 @@ public void copiesFromCharArrayToFile() throws IOException {
final File output = this.folder.newFile();
final String input =
"Hello, товарищ file äÄ üÜ öÖ and ß";
new LengthOf(
new TeeInput(input.toCharArray(), output)
).intValue();
new Assertion<>(
"char array must be copied to the file",
new TeeInput(
input.toCharArray(),
output
),
new InputOf(output),
new InputHasContent(input)
).affirm();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
* Test case for {@link TeeInput}. Cases for ctors which use char sequence as
* an input.
* @since 1.0
* @todo #1360:30min Correct any TeeInput tests that are not testing if the
* path actually contains the content, just that TeeInput, as an Input,has
* the content. Solution: test the content of the file, not the content of
* teeinput (e.g. #1331).
* @checkstyle JavadocMethodCheck (215 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (215 lines)
*/
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/cactoos/iterable/IterableOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.hamcrest.core.IsNot;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsTrue;

/**
* Test case for {@link IterableOf}.
Expand Down Expand Up @@ -105,6 +106,24 @@ public void containAllPagedContentInOrder() throws Exception {
);
}

@Test
public void isNotEqualsToIterableWithMoreElements() {
new Assertion<>(
"Must compare iterables and second one is bigger",
new IterableOf<>("a", "b").equals(new IterableOf<>("a")),
new IsNot<>(new IsTrue())
).affirm();
}

@Test
public void isNotEqualsToIterableWithLessElements() {
new Assertion<>(
"Must compare iterables and first one is bigger",
new IterableOf<>("a").equals(new IterableOf<>("a", "b")),
new IsNot<>(new IsTrue())
).affirm();
}

@Test
@SuppressWarnings("unchecked")
public void reportTotalPagedLength() throws Exception {
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/cactoos/iterable/MatchedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link Matched}.
Expand All @@ -51,6 +53,32 @@ public void iterator() {
);
}

@Test
public void noCorrelationWithBiggerSecondIterable() throws IllegalStateException {
new Assertion<>(
"All elements have correlation function as `endsWith`",
() -> new Matched<>(
(fst, snd) -> fst.endsWith("elem") && snd.endsWith("elem"),
new IterableOf<>("1st elem", "2nd elem"),
new IterableOf<>("`A` elem", "`B` elem", "'C' elem")
).iterator(),
new Throws<>(IllegalStateException.class)
).affirm();
}

@Test
public void noCorrelationWithSmallerSecondIterable() throws IllegalStateException {
new Assertion<>(
"All elements have correlation function as `endsWith`",
() -> new Matched<>(
(fst, snd) -> fst.endsWith("elem") && snd.endsWith("elem"),
new IterableOf<>("1st elem", "2nd elem", "3rd elem"),
new IterableOf<>("`A` elem", "`B` elem")
).iterator(),
new Throws<>(IllegalStateException.class)
).affirm();
}

@Test
public void endsWith() {
MatcherAssert.assertThat(
Expand Down
Loading

0 comments on commit 3f0a01d

Please sign in to comment.