Skip to content

Commit

Permalink
Tests for checked templates
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Mar 10, 2020
1 parent 02febd8 commit c7d1056
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,86 @@
package io.quarkus.qute.resteasy.deployment;

import java.util.Map;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

import org.jboss.resteasy.annotations.jaxrs.QueryParam;

import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateInstance;
import io.quarkus.qute.api.CheckedTemplateInstance;
import io.quarkus.resteasy.qute.runtime.ResteasyTemplate;

@Path("hello")
public class HelloResource {

public static class typeError2 extends CheckedTemplateInstance {
public typeError2(String name) {
}
}

public static class typedTemplate extends CheckedTemplateInstance {
// // GENERATED
// private final Map<String, Object> bindings = new HashMap<>();

public typedTemplate(String name, Map<String, Object> other) {
// // GENERATED
// bindings.put("name", name);
}

// // GENERATED
// @Override
// protected String getTemplateId() {
// return "HelloResource/typedTemplate";
// }
//
// // GENERATED
// @Override
// protected Map<String, Object> getBindings() {
// return bindings;
// }
}

@Inject
Template hello;

@GET
public TemplateInstance get(@QueryParam("name") String name) {
public TemplateInstance get(@QueryParam String name) {
if (name == null) {
name = "world";
}
return hello.data("name", name);
}

@Path("no-injection")
@GET
public TemplateInstance hello(@QueryParam String name) {
if (name == null) {
name = "world";
}
return ResteasyTemplate.data("name", name);
}

@Path("type-error")
@GET
public TemplateInstance typeError() {
return ResteasyTemplate.data("name", "world");
}

@Path("type-error2")
@GET
public typeError2 typeError2() {
return new typeError2("world");
}

@Path("typed-template")
@GET
public typedTemplate typedTemplate(@QueryParam String name) {
if (name == null) {
name = "world";
}
return new typedTemplate(name, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ public class TemplateResponseFilterTest {
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(HelloResource.class)
.addAsResource("templates/HelloResource/hello.txt")
.addAsResource("templates/HelloResource/typedTemplate.txt")
.addAsResource(new StringAsset("Hello {name}!"), "templates/hello.txt"));

@Test
public void testFilter() {
when().get("/hello").then().body(Matchers.is("Hello world!"));
when().get("/hello?name=Joe").then().body(Matchers.is("Hello Joe!"));
when().get("/hello/no-injection").then().body(Matchers.is("Salut world!"));
when().get("/hello/no-injection?name=Joe").then().body(Matchers.is("Salut Joe!"));
when().get("/hello/typed-template").then().body(Matchers.is("Salut world!"));
when().get("/hello/typed-template?name=Joe").then().body(Matchers.is("Salut Joe!"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.qute.resteasy.deployment;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.wildfly.common.Assert;

import io.quarkus.test.QuarkusUnitTest;

public class TypeErrorTest {

@RegisterExtension
static final QuarkusUnitTest configError = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(HelloResource.class)
.addAsResource("templates/HelloResource/hello.txt")
.addAsResource("templates/HelloResource/typeError.txt")
.addAsResource(new StringAsset("Hello {name}!"), "templates/hello.txt"))
.assertException(t -> {
Assert.assertTrue(t.getMessage().contains("Incorrect expression: name.foo()"));
});

@Test
public void emptyTest() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.qute.resteasy.deployment;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.wildfly.common.Assert;

import io.quarkus.test.QuarkusUnitTest;

public class TypeErrorTest2 {

@RegisterExtension
static final QuarkusUnitTest configError = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(HelloResource.class)
.addAsResource("templates/HelloResource/hello.txt")
.addAsResource("templates/HelloResource/typeError2.txt")
.addAsResource(new StringAsset("Hello {name}!"), "templates/hello.txt"))
.assertException(t -> {
t.printStackTrace();
Assert.assertTrue(t.getMessage().contains("Incorrect expression: name.foo()"));
});

@Test
public void emptyTest() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Salut {name}!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{@java.lang.String name}Salut {name.foo()}!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Salut {name.foo()}!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Salut {name}!

0 comments on commit c7d1056

Please sign in to comment.