Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 10, 2021
2 parents eb202e1 + bc60ff8 commit 7138346
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
27 changes: 25 additions & 2 deletions src/main/java/org/cactoos/io/ResourceOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 cls 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
Expand All @@ -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 cls Resource class loader
* @since 0.49
*/
public ResourceOf(final CharSequence res,
final Func<CharSequence, Input> fbk, final Class<?> cls) {
this(res, fbk, cls.getClassLoader());
}

/**
* New resource input with specified {@link ClassLoader}.
* @param res Resource name
Expand Down Expand Up @@ -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()
);
},
Expand Down
34 changes: 27 additions & 7 deletions src/test/java/org/cactoos/io/ResourceOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

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;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.EndsWith;

/**
* Test case for {@link ResourceOf}.
Expand Down Expand Up @@ -65,13 +67,18 @@ public void readsBinaryResource() throws Exception {
@Test
public void readsTextResource() throws Exception {
new Assertion<>(
"Can't read a text resource from classpath",
new TextOf(
new ResourceOf(
"org/cactoos/large-text.txt"
)
).asString(),
Matchers.endsWith("est laborum.\n")
"Must read a text resource from classpath",
ResourceOfTest.large(),
new EndsWith("est laborum.\n")
).affirm();
}

@Test
public void readsTextResourceThroughClassloader() throws Exception {
new Assertion<>(
"Must read a text resource from classloader",
ResourceOfTest.large(),
new EndsWith(" laborum.\n")
).affirm();
}

Expand Down Expand Up @@ -127,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
)
);
}

}

0 comments on commit 7138346

Please sign in to comment.