From 897251880f8d30a2b831563175bc55e2092a444d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 28 Dec 2020 05:22:47 +0300 Subject: [PATCH 1/4] #1517 new ctor for ResourceOf --- src/main/java/org/cactoos/io/ResourceOf.java | 27 +++++++++++++++++-- .../java/org/cactoos/io/ResourceOfTest.java | 14 ++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/io/ResourceOf.java b/src/main/java/org/cactoos/io/ResourceOf.java index afa09f2628..791d3cfa0c 100644 --- a/src/main/java/org/cactoos/io/ResourceOf.java +++ b/src/main/java/org/cactoos/io/ResourceOf.java @@ -68,6 +68,16 @@ public ResourceOf(final CharSequence res) { this(res, Thread.currentThread().getContextClassLoader()); } + /** + * New resource input with {@link ClassLoader} from the specified {@link Class}. + * @param res Resource name + * @param ldr Resource class loader + * @since 0.49 + */ + public ResourceOf(final CharSequence res, final Class cls) { + this(res, cls.getClassLoader()); + } + /** * New resource input with specified {@link ClassLoader}. * @param res Resource name @@ -77,6 +87,18 @@ public ResourceOf(final CharSequence res, final ClassLoader ldr) { this(new TextOf(res), ldr); } + /** + * New resource input with {@link ClassLoader} from the specified {@link Class}. + * @param res Resource name + * @param fbk Fallback + * @param ldr Resource class loader + * @since 0.49 + */ + public ResourceOf(final CharSequence res, + final Func fbk, final Class cls) { + this(res, fbk, cls.getClassLoader()); + } + /** * New resource input with specified {@link ClassLoader}. * @param res Resource name @@ -135,8 +157,9 @@ public ResourceOf(final Text res, final ClassLoader ldr) { input -> { throw new IOException( new FormattedText( - "Resource \"%s\" was not found", - input.asString() + "The resource \"%s\" was not found in %s", + input.asString(), + ldr ).asString() ); }, diff --git a/src/test/java/org/cactoos/io/ResourceOfTest.java b/src/test/java/org/cactoos/io/ResourceOfTest.java index fd2a36d0b9..f62544a9ef 100644 --- a/src/test/java/org/cactoos/io/ResourceOfTest.java +++ b/src/test/java/org/cactoos/io/ResourceOfTest.java @@ -75,6 +75,20 @@ public void readsTextResource() throws Exception { ).affirm(); } + @Test + public void readsTextResourceThroughClassloader() throws Exception { + new Assertion<>( + "Can't read a text resource from classloader", + new TextOf( + new ResourceOf( + "org/cactoos/large-text.txt", + ResourceOfTest.class + ) + ).asString(), + Matchers.endsWith(" laborum.\n") + ).affirm(); + } + @Test public void readAbsentResourceTest() throws Exception { new Assertion<>( From 99651b893c6710b71babe6bcbf2eefb455f695af Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 31 Dec 2020 17:48:56 +0300 Subject: [PATCH 2/4] #1517: fix --- src/main/java/org/cactoos/io/ResourceOf.java | 4 ++-- src/test/java/org/cactoos/io/ResourceOfTest.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/io/ResourceOf.java b/src/main/java/org/cactoos/io/ResourceOf.java index 791d3cfa0c..2894c84ed6 100644 --- a/src/main/java/org/cactoos/io/ResourceOf.java +++ b/src/main/java/org/cactoos/io/ResourceOf.java @@ -71,7 +71,7 @@ public ResourceOf(final CharSequence res) { /** * New resource input with {@link ClassLoader} from the specified {@link Class}. * @param res Resource name - * @param ldr Resource class loader + * @param cls Resource class loader * @since 0.49 */ public ResourceOf(final CharSequence res, final Class cls) { @@ -91,7 +91,7 @@ public ResourceOf(final CharSequence res, final ClassLoader ldr) { * New resource input with {@link ClassLoader} from the specified {@link Class}. * @param res Resource name * @param fbk Fallback - * @param ldr Resource class loader + * @param cls Resource class loader * @since 0.49 */ public ResourceOf(final CharSequence res, diff --git a/src/test/java/org/cactoos/io/ResourceOfTest.java b/src/test/java/org/cactoos/io/ResourceOfTest.java index f62544a9ef..84b7ec4313 100644 --- a/src/test/java/org/cactoos/io/ResourceOfTest.java +++ b/src/test/java/org/cactoos/io/ResourceOfTest.java @@ -63,6 +63,7 @@ public void readsBinaryResource() throws Exception { } @Test + @SuppressWarnings("PMD.AvoidDuplicateLiterals") public void readsTextResource() throws Exception { new Assertion<>( "Can't read a text resource from classpath", From bafb9690defca660e38559f8007ca432d2d63b47 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 31 Dec 2020 17:57:59 +0300 Subject: [PATCH 3/4] #1517: EndsWith --- src/test/java/org/cactoos/io/ResourceOfTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/cactoos/io/ResourceOfTest.java b/src/test/java/org/cactoos/io/ResourceOfTest.java index 84b7ec4313..f4cd404d1a 100644 --- a/src/test/java/org/cactoos/io/ResourceOfTest.java +++ b/src/test/java/org/cactoos/io/ResourceOfTest.java @@ -30,6 +30,7 @@ import org.hamcrest.Matchers; import org.junit.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.EndsWith; /** * Test case for {@link ResourceOf}. @@ -71,8 +72,8 @@ public void readsTextResource() throws Exception { new ResourceOf( "org/cactoos/large-text.txt" ) - ).asString(), - Matchers.endsWith("est laborum.\n") + ), + new EndsWith("est laborum.\n") ).affirm(); } From bc60ff882e90c8c4924ff81b6c4d618174be3529 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Sun, 10 Jan 2021 11:03:56 +0300 Subject: [PATCH 4/4] #1517: fixes --- .../java/org/cactoos/io/ResourceOfTest.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/cactoos/io/ResourceOfTest.java b/src/test/java/org/cactoos/io/ResourceOfTest.java index f4cd404d1a..7f5ef56be6 100644 --- a/src/test/java/org/cactoos/io/ResourceOfTest.java +++ b/src/test/java/org/cactoos/io/ResourceOfTest.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.util.Arrays; +import org.cactoos.Text; import org.cactoos.text.FormattedText; import org.cactoos.text.TextOf; import org.hamcrest.Matchers; @@ -64,15 +65,10 @@ public void readsBinaryResource() throws Exception { } @Test - @SuppressWarnings("PMD.AvoidDuplicateLiterals") public void readsTextResource() throws Exception { new Assertion<>( - "Can't read a text resource from classpath", - new TextOf( - new ResourceOf( - "org/cactoos/large-text.txt" - ) - ), + "Must read a text resource from classpath", + ResourceOfTest.large(), new EndsWith("est laborum.\n") ).affirm(); } @@ -80,14 +76,9 @@ public void readsTextResource() throws Exception { @Test public void readsTextResourceThroughClassloader() throws Exception { new Assertion<>( - "Can't read a text resource from classloader", - new TextOf( - new ResourceOf( - "org/cactoos/large-text.txt", - ResourceOfTest.class - ) - ).asString(), - Matchers.endsWith(" laborum.\n") + "Must read a text resource from classloader", + ResourceOfTest.large(), + new EndsWith(" laborum.\n") ).affirm(); } @@ -143,4 +134,17 @@ public void acceptsTextsAsResourceNameAndFallback() throws Exception { ).affirm(); } + /** + * Large text resource. + * @return The content of the large resource + */ + private static Text large() { + return new TextOf( + new ResourceOf( + "org/cactoos/large-text.txt", + ResourceOfTest.class + ) + ); + } + }