Skip to content

Commit

Permalink
Fix #117
Browse files Browse the repository at this point in the history
Signed-off-by: Clement Escoffier <[email protected]>
  • Loading branch information
cescoffier committed Apr 4, 2014
1 parent a391bb0 commit c4f49fe
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 14 deletions.
6 changes: 6 additions & 0 deletions thymeleaf-template-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
<artifactId>wisdom-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.wisdom-framework</groupId>
<artifactId>wisdom-test</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ow2.chameleon</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,44 @@
*/
package org.wisdom.template.thymeleaf.impl;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.wisdom.api.Controller;
import org.wisdom.api.bodies.RenderableString;
import org.wisdom.api.http.MimeTypes;
import org.wisdom.api.router.Router;
import org.wisdom.api.templates.Template;
import org.wisdom.template.thymeleaf.dialect.Routes;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.StringWriter;
import java.util.List;
import java.util.Map;

/**
* The main integration point of thymeleaf in wisdom.
* The main integration point of Thymeleaf in wisdom.
*/
public class WisdomTemplateEngine extends TemplateEngine {

/**
* Renders the given template.
* <p/>
* Variables from the session, flash and request parameters are added to the given parameters.
*
* @param template the template
* @param controller the template asking for the rendering
* @param router the router service
* @param variables the template parameters
* @return the rendered HTML page
*/
public RenderableString process(Template template, Controller controller, Router router, Map<String,
Object> variables) {
Context ctx = new Context();
// Add session
ctx.setVariables(org.wisdom.api.http.Context.CONTEXT.get().session().getData());
// Add flash
ctx.setVariables(org.wisdom.api.http.Context.CONTEXT.get().flash().getCurrentFlashCookieData());
ctx.setVariables(org.wisdom.api.http.Context.CONTEXT.get().flash().getOutgoingFlashCookieData());


// Add parameter from request, flattened
for (Map.Entry<String, List<String>> entry : org.wisdom.api.http.Context.CONTEXT.get()
Expand All @@ -59,7 +72,6 @@ public RenderableString process(Template template, Controller controller, Router
ctx.setVariables(variables);
ctx.setVariable(Routes.ROUTES_VAR, new Routes(router, controller));


StringWriter writer = new StringWriter();
this.process(template.fullName(), ctx, writer);
return new RenderableString(writer, MimeTypes.HTML);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,26 @@
*/
package org.wisdom.template.thymeleaf.impl;

import com.google.common.collect.ImmutableMap;
import nz.net.ultraq.thymeleaf.LayoutDialect;
import org.junit.Test;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.wisdom.api.Controller;
import org.wisdom.api.http.Result;
import org.wisdom.api.templates.Template;
import org.wisdom.template.thymeleaf.dialect.Routes;
import org.wisdom.template.thymeleaf.dialect.WisdomStandardDialect;
import org.wisdom.test.parents.Action;
import org.wisdom.test.parents.FakeContext;
import org.wisdom.test.parents.Invocation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.wisdom.api.http.Results.ok;
import static org.wisdom.test.parents.Action.action;


/**
Expand All @@ -54,8 +64,82 @@ public void testJavaScript() {
assertThat(processed).containsIgnoringCase("$(document).ready(function () {");
}

private TemplateEngine createWisdomEngine() {
TemplateEngine engine = new WisdomTemplateEngine();
@Test
public void testSessionScope() {
final WisdomTemplateEngine engine = createWisdomEngine();
final Template template = mock(Template.class);
when(template.fullName()).thenReturn("templates/var.thl.html");

final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);


Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
return ok(engine.process(template, controller, router, ImmutableMap.<String, Object>of("key", "test")));
}
}).with(new FakeContext().addToSession("key2", "session")).invoke();

String content = (String) result.getResult().getRenderable().content();
assertThat(content)
.contains("<span>KEY</span> = <span>test</span>")
.contains("<span>KEY2</span> = <span>session</span>");
}

@Test
public void testFlashScope() {
final WisdomTemplateEngine engine = createWisdomEngine();
final Template template = mock(Template.class);
when(template.fullName()).thenReturn("templates/var.thl.html");

final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);


Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
context().flash().put("key2", "ongoing");
return ok(engine.process(template, controller, router, ImmutableMap.<String, Object>of()));
}
}).with(new FakeContext().addToFlash("key", "incoming")).invoke();

String content = (String) result.getResult().getRenderable().content();
assertThat(content)
.contains("<span>KEY</span> = <span>incoming</span>")
.contains("<span>KEY2</span> = <span>ongoing</span>");
}

@Test
public void testParameter() {
final WisdomTemplateEngine engine = createWisdomEngine();
final Template template = mock(Template.class);
when(template.fullName()).thenReturn("templates/var.thl.html");

final FakeRouter router = new FakeRouter();
final Controller controller = new FakeController();
router.addController(controller);


Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
context().session().put("key2", "ongoing");
return ok(engine.process(template, controller, router, ImmutableMap.<String, Object>of()));
}
}).parameter("key", "param").invoke();

String content = (String) result.getResult().getRenderable().content();
assertThat(content)
.contains("<span>KEY</span> = <span>param</span>")
.contains("<span>KEY2</span> = <span>ongoing</span>");
}

private WisdomTemplateEngine createWisdomEngine() {
WisdomTemplateEngine engine = new WisdomTemplateEngine();
engine.setTemplateResolver(new ClassLoaderTemplateResolver());

// We clear the dialects as we are using our own standard dialect.
Expand All @@ -67,12 +151,4 @@ private TemplateEngine createWisdomEngine() {
return engine;
}

private TemplateEngine createRegularEngine() {
TemplateEngine engine = new TemplateEngine();
engine.setTemplateResolver(new ClassLoaderTemplateResolver());

engine.initialize();
return engine;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8"/>
<title>Used for testing</title>
</head>
<body>

<div>
<ul>
<li><span>KEY</span> = <span th:text="${key}">VALUE</span></li>
<li><span>KEY2</span> = <span th:text="${key2}">VALUE</span></li>
</ul>
</div>

</body>
</html>

0 comments on commit c4f49fe

Please sign in to comment.