Skip to content

Commit

Permalink
yegor256#278 NoNulls for Text
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Jul 3, 2017
1 parent 60f73b6 commit b1b2996
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 73 deletions.
44 changes: 44 additions & 0 deletions src/main/java/org/cactoos/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.cactoos;

import java.io.IOException;
import org.cactoos.text.UncheckedText;

/**
* Text.
Expand All @@ -44,4 +45,47 @@ public interface Text extends Comparable<Text> {
*/
String asString() throws IOException;

/**
* Text check for no nulls.
*
* <p>There is no thread-safety guarantee.
*
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @version $Id$
* @since 0.11
*/
final class NoNulls implements Text {
/**
* The origin text.
*/
private final Text origin;
/**
* Ctor.
* @param text The text
*/
public NoNulls(final Text text) {
this.origin = text;
}
@Override
public String asString() throws IOException {
if (this.origin == null) {
throw new IOException("NULL instead of a valid text");
}
final String string = this.origin.asString();
if (string == null) {
throw new IOException("NULL instead of a valid result string");
}
return string;
}
@Override
public int compareTo(final Text text) {
if (text == null) {
throw new IllegalArgumentException(
"NULL parameter instead of a valid text"
);
}
return new UncheckedText(this).compareTo(text);
}
}

}
66 changes: 0 additions & 66 deletions src/main/java/org/cactoos/text/NotNullText.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,49 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.text;
package org.cactoos;

import java.io.IOException;
import org.cactoos.TextHasString;
import org.cactoos.text.StringAsText;
import org.hamcrest.MatcherAssert;
import org.junit.Test;

/**
* Test case for {@link NotNullText}.
* Test case for {@link Text}.
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @version $Id$
* @since 0.3
* @since 0.11
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class NotNullTextTest {
public final class TextTest {

@Test(expected = IOException.class)
public void failForNullText() throws IOException {
new NotNullText(null).asString();
new Text.NoNulls(null).asString();
}

@Test(expected = IOException.class)
public void failForReturnNullString() throws IOException {
new Text.NoNulls(
new Text() {
@Override
public String asString() throws IOException {
return null;
}
@Override
public int compareTo(final Text text) {
return 0;
}
}
).asString();
}

@Test
public void checkForNullText() throws Exception {
final String message = "Hello";
MatcherAssert.assertThat(
"Can't work with null text",
new NotNullText(
new Text.NoNulls(
new StringAsText(message)
),
new TextHasString(message)
Expand Down

0 comments on commit b1b2996

Please sign in to comment.