Skip to content

Commit

Permalink
#146: bug in LengthOfInput
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 15, 2017
1 parent 2f25023 commit 2ff33c0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/io/LengthOfInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Long asValue() throws IOException {
if (len > 0) {
length += (long) len;
}
if (len != buf.length) {
if (len < 0) {
break;
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/cactoos/io/LengthOfInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
*/
package org.cactoos.io;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.cactoos.ScalarHasValue;
import org.cactoos.text.StringAsText;
import org.cactoos.text.TextAsBytes;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
Expand Down Expand Up @@ -66,4 +68,19 @@ public void calculatesZeroLength() {
);
}

@Test
public void readsRealUrl() throws IOException {
MatcherAssert.assertThat(
"Can't calculate length of a real page at the URL",
new LengthOfInput(
new UrlAsInput(
// @checkstyle LineLength (1 line)
"https://raw.githubusercontent.com/yegor256/cactoos/0.5/pom.xml"
)
).asValue(),
// @checkstyle MagicNumber (1 line)
Matchers.equalTo(5960L)
);
}

}
24 changes: 24 additions & 0 deletions src/test/java/org/cactoos/io/StickyInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
*/
package org.cactoos.io;

import org.cactoos.TextHasString;
import org.cactoos.func.FuncAsMatcher;
import org.cactoos.func.RepeatedFunc;
import org.cactoos.text.BytesAsText;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
Expand All @@ -34,6 +37,7 @@
* @version $Id$
* @since 0.6
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class StickyInputTest {

Expand All @@ -58,4 +62,24 @@ public void readsFileContent() {
);
}

@Test
public void readsRealUrl() {
MatcherAssert.assertThat(
"Can't fetch text page from the URL",
new BytesAsText(
new InputAsBytes(
new StickyInput(
new UrlAsInput(
// @checkstyle LineLength (1 line)
"https://raw.githubusercontent.com/yegor256/cactoos/0.5/pom.xml"
)
)
)
),
new TextHasString(
Matchers.endsWith("</project>\n")
)
);
}

}

0 comments on commit 2ff33c0

Please sign in to comment.