Skip to content

Commit

Permalink
#23: Merge of Text & Classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroup committed Oct 30, 2018
1 parent 949d338 commit 5086ed7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 141 deletions.
76 changes: 0 additions & 76 deletions src/main/java/com/github/dgroup/velocity/template/Classpath.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
package com.github.dgroup.velocity.template;

import com.github.dgroup.velocity.Template;
import com.github.dgroup.velocity.path.RelativePath;
import java.util.Map;
import org.cactoos.Func;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.cactoos.map.StickyMap;
import org.cactoos.text.TextOf;

/**
* The storage for textual velocity templates.
Expand All @@ -41,7 +43,7 @@ public final class TemplatesOf extends TemplatesEnvelope<String> {
* Detect the velocity templates in the classpath.
*/
public TemplatesOf() {
this(Classpath::new);
this(fpath -> new Text(new RelativePath(new TextOf(fpath))));
}

/**
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/com/github/dgroup/velocity/template/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

package com.github.dgroup.velocity.template;

import com.github.dgroup.velocity.path.RelativePath;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.cactoos.Input;
import org.cactoos.Scalar;
import org.cactoos.io.Directory;
import org.cactoos.io.InputOf;
Expand Down Expand Up @@ -64,7 +66,7 @@ public Text(final String tname, final String dir) {
*/
@SafeVarargs
public Text(final String tname, final Scalar<Path>... roots) {
super(
this(
() -> tname,
() -> {
if (tname == null || tname.trim().isEmpty()) {
Expand Down Expand Up @@ -94,4 +96,40 @@ public Text(final String tname, final Scalar<Path>... roots) {
);
}

/**
* Find template from the classpath.
* @param rscr The relative path to resource from the classpath.
* For example, in case if you resource is placed to
* {@code src/main/resources/com/xxx/rs.txt} then relative path is
* {@code com/xxx/rs.txt}.
*/
public Text(final RelativePath rscr) {
this(
rscr,
() -> {
if (rscr == null || rscr.value() == null) {
throw new IllegalArgumentException(
"The resource path can't be a null"
);
}
return new InputOf(
Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream(rscr.value())
);
}
);
}

/**
* Ctor.
* @param tname The name of Velocity template.
* @param src The Velocity template as {@link org.cactoos.Input}.
* The input stream will be closed automatically by
* {@link TemplateEnvelope}.
*/
private Text(final Scalar<String> tname, final Scalar<Input> src) {
super(tname, src);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

package com.github.dgroup.velocity.arg.iterable;

import com.github.dgroup.velocity.template.Classpath;
import com.github.dgroup.velocity.path.RelativePath;
import com.github.dgroup.velocity.template.TemplateException;
import java.io.File;
import com.github.dgroup.velocity.template.Text;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand All @@ -45,7 +45,9 @@ public final class MappedTest {
@Test
public void mapped() throws TemplateException {
MatcherAssert.assertThat(
new Classpath("velocity{0}mapped.md", File.separator)
new Text(
new RelativePath("velocity/mapped.md")
)
.compose(
new Mapped<>(
"systems",
Expand Down

This file was deleted.

20 changes: 20 additions & 0 deletions src/test/java/com/github/dgroup/velocity/template/TextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.github.dgroup.velocity.arg.ArgOf;
import com.github.dgroup.velocity.path.PathOf;
import com.github.dgroup.velocity.path.RelativePath;
import org.cactoos.list.ListOf;
import org.cactoos.map.MapEntry;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -119,6 +120,25 @@ public void hierarchical() throws TemplateException {
);
}

@Test
public void classpath() throws TemplateException {
MatcherAssert.assertThat(
new Text(
new RelativePath("velocity{0}cls-loader.md")
).compose(
new ArgOf("way", "getResourceAsStream")
),
Matchers.equalTo(
"I'm going to use `getResourceAsStream` here."
)
);
}

@Test(expected = TemplateException.class)
public void resourceIsNull() throws TemplateException {
new Text(null).compose();
}

@Test(expected = TemplateException.class)
public void templateIsNull() throws TemplateException {
new Text(null, "").compose();
Expand Down

0 comments on commit 5086ed7

Please sign in to comment.