Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#631: TeeInput test class is incomplete #698

Merged
merged 6 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions src/main/java/org/cactoos/matchers/TeeInputHasResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* 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.matchers;

import org.cactoos.Text;
import org.cactoos.io.TeeInput;
import org.cactoos.text.ComparableText;
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

/**
* Matcher for the {@link TeeInput}'s results.
*
* @author Roman Proshin ([email protected])
* @version $Id$
* @since 1.0
*/
public final class TeeInputHasResult extends TypeSafeMatcher<TeeInput> {

Choose a reason for hiding this comment

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

@proshin-roman We did not discuss about this new Matcher, did we? It's nice that you did it, but there are no unit tests for it, how do we know it works? Countless new tests are relying on it to make assertions, but it (the matcher itself) is not tested at all.

Write some tests for it if you want to use it in these tests, otherwise please use simple Matchers.
Not to mention, this new matcher, together with unit tests for it, should be in a separate PR on a separate ticket, it's basically a new functionality of the library.

/**
* Format for matcher description.
*/
private static final String DESCRIPTION =
"TeeInput with result '%s' and copied value '%s'";
/**
* Value that is expected to be returned from the given TeeInput.
*/
private final ComparableText expected;
/**
* Value that is expected to be copied into the output of TeeInput.
*/
private final ComparableText copied;
/**
* Actual value returned from the given TeeInput.
*/
private ComparableText actual;

/**
* Ctor.
* @param expected Value that is expected to be returned from the TeeInput
* @param copied Value that is expected to be copied into the output of
* the TeeInput
*/
public TeeInputHasResult(
final String expected,
final Text copied) {
this(
new TextOf(expected),
copied
);
}

/**
* Ctor.
* @param expected Value that is expected to be returned from the TeeInput
* @param copied Value that is expected to be copied into the output of
* the TeeInput
*/
public TeeInputHasResult(
final Text expected,
final Text copied) {
super();
this.expected = new ComparableText(expected);
this.copied = new ComparableText(copied);
this.actual = new ComparableText(new TextOf(""));
}

@Override
public boolean matchesSafely(final TeeInput item) {
this.actual = new ComparableText(new TextOf(item));
return
this.expected.compareTo(this.actual) == 0
&& this.expected.compareTo(this.copied) == 0;
}

@Override
public void describeTo(final Description description) {
description.appendText(
new UncheckedText(
new FormattedText(
TeeInputHasResult.DESCRIPTION,
new UncheckedText(this.expected).asString(),
new UncheckedText(this.expected).asString()
)
).asString()
);
}

@Override
public void describeMismatchSafely(
final TeeInput item,
final Description description) {
description.appendText(
new UncheckedText(
new FormattedText(
TeeInputHasResult.DESCRIPTION,
new UncheckedText(this.actual).asString(),
new UncheckedText(this.copied).asString()
)
).asString()
);
}
}
102 changes: 102 additions & 0 deletions src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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.io;

import java.io.File;
import java.nio.charset.StandardCharsets;
import org.cactoos.matchers.TeeInputHasResult;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link TeeInput}. Cases for ctors which use byte array as an
* input.
* @author Roman Proshin ([email protected])
* @version $Id$
* @since 1.0
* @checkstyle JavadocMethodCheck (100 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (100 lines)
*/
public final class TeeInputFromByteArrayTest {

Choose a reason for hiding this comment

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

@proshin-roman Just this test class and the next one are enough for this PR. No new, untested matchers, no 13 changed files etc.


/**
* Temporary files generator.
*/
@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void copiesFromByteArrayToPath() throws Exception {
final String input =
"Hello, товарищ path äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
MatcherAssert.assertThat(
new TeeInput(
input.getBytes(StandardCharsets.UTF_8),
output.toPath()
),
new TeeInputHasResult(
input,
new TextOf(output)
)
);
}

@Test
public void copiesFromByteArrayToFile() throws Exception {
final String input =
"Hello, товарищ file äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
MatcherAssert.assertThat(
new TeeInput(
input.getBytes(StandardCharsets.UTF_8),
output
),
new TeeInputHasResult(
input,
new TextOf(output)
)
);
}

@Test
public void copiesFromByteArrayToOutput() throws Exception {
final String input =
"Hello, товарищ output äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
MatcherAssert.assertThat(
new TeeInput(
input.getBytes(StandardCharsets.UTF_8),
new OutputTo(output)
),
new TeeInputHasResult(
input,
new TextOf(output)
)
);
}
}
102 changes: 102 additions & 0 deletions src/test/java/org/cactoos/io/TeeInputFromBytesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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.io;

import java.io.File;
import java.io.IOException;
import org.cactoos.matchers.TeeInputHasResult;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link TeeInput}. Cases for ctors which use
* {@link org.cactoos.Bytes} as an input.
* @author Roman Proshin ([email protected])
* @version $Id$
* @since 1.0
* @checkstyle JavadocMethodCheck (100 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (100 lines)
*/
public final class TeeInputFromBytesTest {

/**
* Temporary files generator.
*/
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void copiesFromBytesToPath() throws IOException {
final String input =
"Hello, товарищ path äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
MatcherAssert.assertThat(
new TeeInput(
new BytesOf(input),
output.toPath()
),
new TeeInputHasResult(
input,
new TextOf(output)
)
);
}

@Test
public void copiesFromBytesToFile() throws IOException {
final String input =
"Hello, товарищ file äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
MatcherAssert.assertThat(
new TeeInput(
new BytesOf(input),
output
),
new TeeInputHasResult(
input,
new TextOf(output)
)
);
}

@Test
public void copiesFromBytesToOutput() throws IOException {
final String input =
"Hello, товарищ output äÄ üÜ öÖ and ß";
final File output = this.folder.newFile();
MatcherAssert.assertThat(
new TeeInput(
new BytesOf(input),
new OutputTo(output)
),
new TeeInputHasResult(
input,
new TextOf(output)
)
);
}
}
Loading