Repository: yegor256/cactoos
Pull request #28 (2017-05-25 21:17:24)

#27 TextAsBytes with test
As per https://github.com/yegor256/cactoos/issues/27

`TextAsBytes` introduced


Patch:
diff --git a/.travis.yml b/.travis.yml
index 1f8e9d15..cb342eb6 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -13,5 +13,3 @@ env:
     - JAVA_OPTS="-Xmx256m"
 jdk:
   - oraclejdk8
-after_success:
-    - "bash <(curl -s https://codecov.io/bash)"
diff --git a/README.md b/README.md
index 2ab52faa..5341f76f 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,6 @@
 
 [![Build Status](https://travis-ci.org/yegor256/cactoos.svg?branch=master)](https://travis-ci.org/yegor256/cactoos)
 [![PDD status](http://www.0pdd.com/svg?name=yegor256/cactoos)](http://www.0pdd.com/p?name=yegor256/cactoos)
-[![Test Coverage](https://img.shields.io/codecov/c/github/yegor256/cactoos.svg?style=flat-square)](https://codecov.io/github/yegor256/cactoos?branch=master)
 [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/takes/blob/master/LICENSE.txt)
 
 Cactoos is a collection of object-oriented Java primitives.
diff --git a/pom.xml b/pom.xml
index 4b6b638e..0b131887 100644
--- a/pom.xml
+++ b/pom.xml
@@ -119,29 +119,6 @@
                             </excludes>
                         </configuration>
                     </plugin>
-                    <plugin>
-                        <groupId>org.jacoco</groupId>
-                        <artifactId>jacoco-maven-plugin</artifactId>
-                        <version>0.7.8</version>
-                        <configuration>
-                            <output>file</output>
-                        </configuration>
-                        <executions>
-                            <execution>
-                                <id>jacoco-initialize</id>
-                                <goals>
-                                    <goal>prepare-agent</goal>
-                                </goals>
-                            </execution>
-                            <execution>
-                                <id>jacoco-site</id>
-                                <phase>install</phase>
-                                <goals>
-                                    <goal>report</goal>
-                                </goals>
-                            </execution>
-                        </executions>
-                    </plugin>
                 </plugins>
             </build>
         </profile>
diff --git a/src/main/java/org/cactoos/io/ResourceAsInput.java b/src/main/java/org/cactoos/io/ResourceAsInput.java
deleted file mode 100644
index 2bf620fc..00000000
--- a/src/main/java/org/cactoos/io/ResourceAsInput.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2017 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.IOException;
-import java.io.InputStream;
-import org.cactoos.Input;
-import org.cactoos.text.Sprintf;
-
-/**
- * Jar class resource.
- *
- * @author Kirill (g4s8.public@gmail.com)
- * @version $Id$
- * @see ClassLoader#getResource(String)
- * @since 0.1
- */
-public final class ResourceAsInput implements Input {
-
-    /**
-     * Resource name.
-     */
-    private final String res;
-
-    /**
-     * Resource class loader.
-     */
-    private final ClassLoader loader;
-
-    /**
-     * New resource input with current context {@link ClassLoader}.
-     *
-     * @param res Resource name
-     */
-    public ResourceAsInput(final String res) {
-        this(
-            res,
-            Thread.currentThread().getContextClassLoader()
-        );
-    }
-
-    /**
-     * New resource input with specified {@link ClassLoader}.
-     *
-     * @param res Resource name
-     * @param loader Resource class loader
-     */
-    public ResourceAsInput(final String res, final ClassLoader loader) {
-        this.res = res;
-        this.loader = loader;
-    }
-
-    @Override
-    public InputStream open() throws IOException {
-        final InputStream input = this.loader.getResourceAsStream(this.res);
-        if (input == null) {
-            throw new IOException(
-                new Sprintf(
-                    "Resource '%s' was not found",
-                    this.res
-                ).toString()
-            );
-        }
-        return input;
-    }
-}
diff --git a/src/main/java/org/cactoos/list/First.java b/src/main/java/org/cactoos/text/TextAsBytes.java
similarity index 60%
rename from src/main/java/org/cactoos/list/First.java
rename to src/main/java/org/cactoos/text/TextAsBytes.java
index 1788ace4..4cef689d 100644
--- a/src/main/java/org/cactoos/list/First.java
+++ b/src/main/java/org/cactoos/text/TextAsBytes.java
@@ -21,42 +21,54 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-package org.cactoos.list;
+package org.cactoos.text;
 
 import java.io.IOException;
-import java.util.Iterator;
-import org.cactoos.Scalar;
+import java.nio.charset.Charset;
+import org.cactoos.Bytes;
+import org.cactoos.Text;
 
 /**
- * First element in {@link Iterable}.
+ * Text as Bytes.
  *
- * @author Kirill (g4s8.public@gmail.com)
+ * <p>There is no thread-safety guarantee.
+ *
+ * @author Vseslav Sekorin (vssekorin@gmail.com)
  * @version $Id$
- * @param <T> Scalar type
  * @since 0.1
  */
-public final class First<T> implements Scalar<T> {
+public final class TextAsBytes implements Bytes {
 
     /**
-     * Source iterable.
+     * The source.
      */
-    private final Iterable<T> source;
+    private final Text source;
+
+    /**
+     * The charset.
+     */
+    private final Charset charset;
+
+    /**
+     * Ctor.
+     * @param text The source
+     */
+    public TextAsBytes(final Text text) {
+        this(text, Charset.defaultCharset());
+    }
 
     /**
      * Ctor.
-     *
-     * @param source Iterable
+     * @param text The source
+     * @param charset The charset
      */
-    public First(final Iterable<T> source) {
-        this.source = source;
+    public TextAsBytes(final Text text, final Charset charset) {
+        this.source = text;
+        this.charset = charset;
     }
 
     @Override
-    public T asValue() throws IOException {
-        final Iterator<T> iterator = this.source.iterator();
-        if (!iterator.hasNext()) {
-            throw new IOException("Iterable is empty");
-        }
-        return iterator.next();
+    public byte[] asBytes() throws IOException {
+        return this.source.asString().getBytes(this.charset);
     }
 }


Test patch:
diff --git a/src/test/java/org/cactoos/io/ResourceAsInputTest.java b/src/test/java/org/cactoos/io/ResourceAsInputTest.java
deleted file mode 100644
index b078db6a..00000000
--- a/src/test/java/org/cactoos/io/ResourceAsInputTest.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2017 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.util.Arrays;
-import org.hamcrest.MatcherAssert;
-import org.hamcrest.Matchers;
-import org.junit.Test;
-
-/**
- * Test case for {@link ResourceAsInput}.
- *
- * @author Kirill (g4s8.public@gmail.com)
- * @version $Id$
- * @since 0.1
- */
-public final class ResourceAsInputTest {
-
-    /**
-     * Test built-in resource.
-     *
-     * @throws Exception if failed
-     */
-    @Test
-    public void readResourceTest() throws Exception {
-        MatcherAssert.assertThat(
-            Arrays.copyOfRange(
-                new InputAsBytes(
-                    new ResourceAsInput(
-                        "org/cactoos/io/ResourceAsInputTest.class"
-                    )
-                ).asBytes(),
-                // @checkstyle MagicNumber (2 lines)
-                0,
-                4
-            ),
-            Matchers.equalTo(
-                new byte[]{
-                    // @checkstyle MagicNumber (1 line)
-                    (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE,
-                }
-            )
-        );
-    }
-}
diff --git a/src/test/java/org/cactoos/list/FirstTest.java b/src/test/java/org/cactoos/text/TextAsBytesTest.java
similarity index 64%
rename from src/test/java/org/cactoos/list/FirstTest.java
rename to src/test/java/org/cactoos/text/TextAsBytesTest.java
index 0364c22b..f17d0383 100644
--- a/src/test/java/org/cactoos/list/FirstTest.java
+++ b/src/test/java/org/cactoos/text/TextAsBytesTest.java
@@ -21,47 +21,41 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-package org.cactoos.list;
+package org.cactoos.text;
 
 import java.io.IOException;
-import java.util.Arrays;
-import java.util.Collections;
+import org.cactoos.Text;
+import org.cactoos.io.InputAsBytes;
+import org.cactoos.io.TextAsInput;
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
 import org.junit.Test;
 
 /**
- * Test case for {@link First}.
+ * Test case for {@link TextAsBytes}.
  *
- * @author Kirill (g4s8.public@gmail.com)
+ * @author Vseslav Sekorin (vssekorin@gmail.com)
  * @version $Id$
  * @since 0.1
  */
-public final class FirstTest {
+public final class TextAsBytesTest {
 
     /**
-     * Get first element in collection.
+     * TextAsBytes can convert with default encoding.
      *
-     * @throws Exception if failed.
+     * @throws IOException If some problem inside
      */
     @Test
-    public void firstElementTest() throws Exception {
+    public void asBytes() throws IOException {
+        final Text text = new StringAsText("Hello!");
         MatcherAssert.assertThat(
-            new First<>(
-                // @checkstyle MagicNumber (1 line)
-                Arrays.asList(1, 2, 3)
-            ).asValue(),
-            Matchers.equalTo(1)
+            new InputAsBytes(
+                new TextAsInput(text)
+            ).asBytes(),
+            Matchers.equalTo(
+                new TextAsBytes(text).asBytes()
+            )
         );
     }
 
-    /**
-     * Throws exception if collection is empty.
-     *
-     * @throws Exception expected exception
-     */
-    @Test(expected = IOException.class)
-    public void failForEmptyCollectionTest() throws Exception {
-        new First<>(Collections.emptyList()).asValue();
-    }
 }