Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jun 29, 2019
2 parents afa71cb + 94f70b4 commit 042cb34
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 136 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/io/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Input/Output.
*
* @since 0.1
* @todo #1033:30min Continue replacing usage of MatcherAssert.assertThat with
* @todo #1117:30min Continue replacing usage of MatcherAssert.assertThat with
* Assertion from cactoos-matchers. Once there is no more usage of
* MatcherAssert.assertThat, add the signature of MatcherAssert.assertThat to
* forbidden-apis.txt
Expand Down
129 changes: 65 additions & 64 deletions src/test/java/org/cactoos/io/BytesOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@
import org.cactoos.iterable.HeadOf;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.MatcherOf;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.EndsWith;
import org.llorllale.cactoos.matchers.IsTrue;
import org.llorllale.cactoos.matchers.StartsWith;
import org.llorllale.cactoos.matchers.TextHasString;
import org.llorllale.cactoos.matchers.TextIs;

/**
* Test case for {@link BytesOf}.
Expand All @@ -53,8 +57,8 @@ public final class BytesOfTest {
public void readsLargeInMemoryContent() throws Exception {
final int multiplier = 5_000;
final String body = "1234567890";
MatcherAssert.assertThat(
"Can't read large content from in-memory Input",
new Assertion<>(
"must read large content from in-memory Input",
new BytesOf(
new InputOf(
new Joined(
Expand All @@ -65,62 +69,62 @@ public void readsLargeInMemoryContent() throws Exception {
)
)
).asBytes().length,
Matchers.equalTo(body.length() * multiplier)
);
new IsEqual<>(body.length() * multiplier)
).affirm();
}

@Test
public void readsInputIntoBytes() throws Exception {
MatcherAssert.assertThat(
"Can't read bytes from Input",
new String(
new Assertion<>(
"must read bytes from Input",
new TextOf(
new BytesOf(
new InputOf("Hello, друг!")
).asBytes(),
StandardCharsets.UTF_8
)
),
Matchers.allOf(
Matchers.startsWith("Hello, "),
Matchers.endsWith("друг!")
new StartsWith("Hello, "),
new EndsWith("друг!")
)
);
).affirm();
}

@Test
public void readsFromReader() throws Exception {
final String source = "hello, друг!";
MatcherAssert.assertThat(
"Can't read string through a reader",
new Assertion<>(
"must read string through a reader",
new TextOf(
new BytesOf(
new StringReader(source),
StandardCharsets.UTF_8,
// @checkstyle MagicNumberCheck (1 line)
16 << 10
new Sticky(
new InputOf(
new BytesOf(
new StringReader(source),
StandardCharsets.UTF_8,
// @checkstyle MagicNumberCheck (1 line)
16 << 10
)
)
)
).asString(),
Matchers.equalTo(source)
);
),
new TextIs(source)
).affirm();
}

@Test
public void readsInputIntoBytesWithSmallBuffer() throws Exception {
MatcherAssert.assertThat(
"Can't read bytes from Input with a small reading buffer",
new String(
new Assertion<>(
"must read bytes from Input with a small reading buffer",
new TextOf(
new BytesOf(
new InputOf(
new TextOf("Hello, товарищ!")
),
new InputOf("Hello, товарищ!"),
2
).asBytes(),
StandardCharsets.UTF_8
)
),
Matchers.allOf(
Matchers.startsWith("Hello,"),
Matchers.endsWith("товарищ!")
new StartsWith("Hello,"),
new EndsWith("товарищ!")
)
);
).affirm();
}

@Test
Expand All @@ -129,49 +133,46 @@ public void closesInputStream() throws Exception {
final InputStream input = new ByteArrayInputStream(
"how are you?".getBytes()
);
MatcherAssert.assertThat(
"Can't close InputStream correctly",
new TextOf(
new InputOf(
new InputStream() {
@Override
public int read() throws IOException {
return input.read();
}
@Override
public void close() throws IOException {
input.close();
closed.set(true);
}
new TextOf(
new InputOf(
new InputStream() {
@Override
public int read() throws IOException {
return input.read();
}
@Override
public void close() throws IOException {
input.close();
closed.set(true);
}
),
StandardCharsets.UTF_8
).asString(),
new MatcherOf<>(
text -> {
return closed.get();
}
)
);
),
StandardCharsets.UTF_8
).asString();
new Assertion<>(
"must close InputStream correctly",
closed.get(),
new IsTrue()
).affirm();
}

@Test
public void asBytes() throws Exception {
final Text text = new TextOf("Hello!");
MatcherAssert.assertThat(
new Assertion<>(
"Can't convert text into bytes",
new BytesOf(
new InputOf(text)
).asBytes(),
Matchers.equalTo(
new IsEqual<>(
new BytesOf(text.asString()).asBytes()
)
);
).affirm();
}

@Test
public void printsStackTrace() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't print exception stacktrace",
new TextOf(
new BytesOf(
Expand All @@ -187,20 +188,20 @@ public void printsStackTrace() {
"\tat org.cactoos.io.BytesOfTest"
)
)
);
).affirm();
}

@Test
public void printsStackTraceFromArray() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't print exception stacktrace from array",
new TextOf(
new BytesOf(
new IOException("").getStackTrace()
)
),
new TextHasString("org.cactoos.io.BytesOfTest")
);
).affirm();
}

}
50 changes: 29 additions & 21 deletions src/test/java/org/cactoos/io/CheckedBytesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
package org.cactoos.io;

import java.io.EOFException;
import java.io.IOException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsNull;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link CheckedBytes}.
Expand All @@ -37,24 +37,32 @@
*/
public final class CheckedBytesTest {

@Test(expected = IllegalStateException.class)
public void runtimeExceptionIsNotWrapped() throws Exception {
new CheckedBytes<>(
() -> {
throw new IllegalStateException("runtime1");
},
IOException::new
).asBytes();
@Test
public void runtimeExceptionIsNotWrapped() {
new Assertion<>(
"must not wrap runtime exception",
new CheckedBytes<>(
() -> {
throw new IllegalStateException("runtime1");
},
IOException::new
)::asBytes,
new Throws<>(IllegalStateException.class)
).affirm();
}

@Test(expected = IOException.class)
public void checkedExceptionIsWrapped() throws Exception {
new CheckedBytes<>(
() -> {
throw new EOFException("runtime2");
},
IOException::new
).asBytes();
@Test
public void checkedExceptionIsWrapped() {
new Assertion<>(
"must wrap checked exception",
new CheckedBytes<>(
() -> {
throw new Exception("runtime2");
},
IOException::new
)::asBytes,
new Throws<>(IOException.class)
).affirm();
}

@Test
Expand All @@ -67,11 +75,11 @@ public void extraWrappingIgnored() {
IOException::new
).asBytes();
} catch (final IOException exp) {
MatcherAssert.assertThat(
"Extra wrapping of IOException has been added",
new Assertion<>(
"must not add extra wrapping of IOException",
exp.getCause(),
new IsNull<>()
);
).affirm();
}
}
}
50 changes: 29 additions & 21 deletions src/test/java/org/cactoos/io/CheckedInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
package org.cactoos.io;

import java.io.EOFException;
import java.io.IOException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsNull;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link CheckedInput}.
Expand All @@ -37,24 +37,32 @@
*/
public final class CheckedInputTest {

@Test(expected = IllegalStateException.class)
public void runtimeExceptionIsNotWrapped() throws Exception {
new CheckedInput<>(
() -> {
throw new IllegalStateException("runtime1");
},
IOException::new
).stream();
@Test
public void runtimeExceptionIsNotWrapped() {
new Assertion<>(
"must not wrap runtime exception",
new CheckedInput<>(
() -> {
throw new IllegalStateException("runtime1");
},
IOException::new
)::stream,
new Throws<>(IllegalStateException.class)
).affirm();
}

@Test(expected = IOException.class)
public void checkedExceptionIsWrapped() throws Exception {
new CheckedInput<>(
() -> {
throw new EOFException("runtime2");
},
IOException::new
).stream();
@Test
public void checkedExceptionIsWrapped() {
new Assertion<>(
"must wrap checked exception",
new CheckedInput<>(
() -> {
throw new Exception("runtime2");
},
IOException::new
)::stream,
new Throws<>(IOException.class)
).affirm();
}

@Test
Expand All @@ -67,11 +75,11 @@ public void extraWrappingIgnored() {
IOException::new
).stream();
} catch (final IOException exp) {
MatcherAssert.assertThat(
"Extra wrapping of IOException has been added",
new Assertion<>(
"must not add extra wrapping of IOException",
exp.getCause(),
new IsNull<>()
);
).affirm();
}
}
}
Loading

2 comments on commit 042cb34

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 042cb34 Jun 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1033-b5541bca disappeared from src/main/java/org/cactoos/io/package-info.java, that's why I closed #1117. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 042cb34 Jun 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1117-b5541bca discovered in src/main/java/org/cactoos/io/package-info.java and submitted as #1152. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.