-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 75c2e3e
Showing
8 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SeverellJtePlugin.iml | ||
/target | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## JTE Plugin for Severell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.serverell.plugins</groupId> | ||
<artifactId>jte</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<junit.version>5.6.2</junit.version> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.severell</groupId> | ||
<artifactId>core</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>3.4.6</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.severell.plugins.jte; | ||
|
||
import com.severell.core.container.Container; | ||
import com.severell.core.providers.ServiceProvider; | ||
|
||
public class JteProvider extends ServiceProvider { | ||
|
||
public JteProvider(Container c) { | ||
super(c); | ||
} | ||
|
||
@Override | ||
public void register() { | ||
this.c.bind("ViewJteDriver", (container) -> new ViewJteDriver()); | ||
} | ||
|
||
@Override | ||
public void boot() throws Exception { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.severell.plugins.jte; | ||
|
||
import com.severell.core.config.Config; | ||
import com.severell.core.exceptions.ViewException; | ||
import com.severell.core.view.BaseView; | ||
import gg.jte.ContentType; | ||
import gg.jte.TemplateEngine; | ||
import gg.jte.TemplateOutput; | ||
import gg.jte.output.PrintWriterOutput; | ||
import gg.jte.output.StringOutput; | ||
import gg.jte.resolve.DirectoryCodeResolver; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* This class uses the JTE templating engine to render templates | ||
*/ | ||
public class ViewJteDriver extends BaseView { | ||
|
||
private TemplateEngine templateEngine; | ||
private Path templatePath; | ||
|
||
public ViewJteDriver(Path templatePath) { | ||
this.templatePath = templatePath; | ||
setupTemplateEngine(); | ||
} | ||
|
||
public ViewJteDriver() { | ||
templatePath = Path.of("src", "main", "resources", "templates"); | ||
setupTemplateEngine(); | ||
} | ||
|
||
private void setupTemplateEngine() { | ||
if (Config.isLocal()) { | ||
DirectoryCodeResolver codeResolver = new DirectoryCodeResolver(templatePath); | ||
templateEngine = TemplateEngine.create(codeResolver, ContentType.Html); | ||
} else { | ||
templateEngine = TemplateEngine.createPrecompiled(ContentType.Html); | ||
} | ||
} | ||
|
||
@Override | ||
public void render(String template, Object object, Writer writer) throws ViewException { | ||
render(template, object, "templates/", writer); | ||
} | ||
|
||
@Override | ||
public void render(String template, Object object, String baseDir, Writer writer) throws ViewException { | ||
if(writer instanceof PrintWriter) { | ||
TemplateOutput output = new PrintWriterOutput((PrintWriter) writer); | ||
templateEngine.render(template, object, output); | ||
} else if(writer instanceof StringWriter) { | ||
StringOutput output = new StringOutput(); | ||
templateEngine.render(template, object, output); | ||
((StringWriter) writer).write(output.toString()); | ||
} else { | ||
throw new ViewException("Invalid writer. Needs to be instance of PrintWriter"); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/test/java/com/severell/plugins/jte/ViewJteDriverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.severell.plugins.jte; | ||
|
||
import com.severell.core.config.Config; | ||
import com.severell.core.exceptions.ViewException; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.*; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class ViewJteDriverTest { | ||
|
||
@BeforeAll | ||
public static void setup() throws Exception { | ||
Config.setDir("src/test/resources"); | ||
Config.loadConfig(); | ||
} | ||
|
||
@AfterAll | ||
public static void tearDown() { | ||
Config.unload(); | ||
} | ||
|
||
@Test | ||
public void testJteDriver() throws ViewException { | ||
ViewJteDriver driver = new ViewJteDriver(Path.of("src", "test", "resources", "templates")); | ||
StringWriter writer = new StringWriter(); | ||
HashMap<String, Object> map = new HashMap<>(); | ||
|
||
driver.render("test.jte",map , writer); | ||
|
||
assertEquals("<html><body>Hello</body></html>", writer.toString()); | ||
} | ||
|
||
@Test | ||
public void testJteDriverPrintWriter() throws ViewException { | ||
ViewJteDriver driver = new ViewJteDriver(Path.of("src", "test", "resources", "templates")); | ||
StringWriter st = new StringWriter(); | ||
PrintWriter writer = new PrintWriter(st); | ||
HashMap<String, Object> map = new HashMap<>(); | ||
|
||
driver.render("test.jte",map , writer); | ||
writer.flush(); | ||
assertEquals("<html><body>Hello</body></html>", st.toString()); | ||
} | ||
|
||
@Test | ||
public void testJteDriverBufferedWriter() throws ViewException { | ||
ViewJteDriver driver = new ViewJteDriver(Path.of("src", "test", "resources", "templates")); | ||
Writer writer = new BufferedWriter(new StringWriter()); | ||
HashMap<String, Object> map = new HashMap<>(); | ||
|
||
assertThrows(ViewException.class, () -> { | ||
driver.render("test.jte",map , writer); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testJteDriverThrowsIOError() throws IOException { | ||
ViewJteDriver driver = new ViewJteDriver(); | ||
StringWriter writer = mock(StringWriter.class); | ||
|
||
|
||
HashMap<String, Object> map = new HashMap<>(); | ||
|
||
assertThrows(UncheckedIOException.class, () -> { | ||
driver.render("test.jte",map , writer); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ENV=TEST | ||
TEST=hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<html><body>Hello</body></html> |