예제 #1
0
 @Test
 public void testNestedThis() {
   assertEquals(
       "barbazbif",
       Mustache.compiler()
           .compile("{{#things}}{{this}}{{/things}}")
           .execute(context("things", Arrays.asList("bar", "baz", "bif"))));
   assertEquals(
       "barbazbif",
       Mustache.compiler()
           .compile("{{#things}}{{.}}{{/things}}")
           .execute(context("things", Arrays.asList("bar", "baz", "bif"))));
 }
예제 #2
0
 @Test
 public void testInvalidTripleMustache() {
   try {
     Mustache.compiler().compile("{{{foo}}");
     fail("Expected MustacheParseException");
   } catch (MustacheParseException e) {
     assertEquals("Invalid triple-mustache tag: {{{foo}} @ line 1", e.getMessage());
   }
   try {
     Mustache.compiler().compile("{{{foo}}]");
     fail("Expected MustacheParseException");
   } catch (MustacheParseException e) {
     assertEquals("Invalid triple-mustache tag: {{{foo}}] @ line 1", e.getMessage());
   }
 }
예제 #3
0
 @Test
 public void testUnescapeHTML() {
   assertEquals(
       "<b>", Mustache.compiler().escapeHTML(true).compile("{{&a}}").execute(context("a", "<b>")));
   assertEquals(
       "<b>",
       Mustache.compiler().escapeHTML(true).compile("{{{a}}}").execute(context("a", "<b>")));
   // make sure these also work when escape HTML is off
   assertEquals(
       "<b>",
       Mustache.compiler().escapeHTML(false).compile("{{&a}}").execute(context("a", "<b>")));
   assertEquals(
       "<b>",
       Mustache.compiler().escapeHTML(false).compile("{{{a}}}").execute(context("a", "<b>")));
 }
예제 #4
0
 private static Template compile(String name, InputSupplier<InputStreamReader> supplier) {
   try (InputStreamReader reader = supplier.getInput()) {
     return Mustache.compiler().escapeHTML(false).compile(reader);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
  /**
   * Searches classpath and then system for a file with the specified name.
   *
   * @param name name of template file to load
   * @return
   */
  protected Template loadTemplate(String name) throws FileNotFoundException {
    Template template = null;
    InputStream instream = this.getClass().getClassLoader().getResourceAsStream(name);

    if (instream == null) {
      instream = System.class.getResourceAsStream(name);
    }
    if (instream == null) {
      instream = new FileInputStream(name);
    }

    if (instream != null) {
      final Reader r = new InputStreamReader(instream);
      try {
        template = Mustache.compiler().compile(r);
      } finally {
        try {
          r.close();
        } catch (Exception e) {
          // skip
        }
      }
    }
    return template;
  }
예제 #6
0
 public static Template compile(InputStream stream) {
   try {
     return Mustache.compiler().escapeHTML(false).compile(new InputStreamReader(stream));
   } catch (Exception e) {
     LOGGER.debug("Error compiling mustache template: " + e);
   }
   return null;
 }
예제 #7
0
 @Test
 public void testMissingComponentInCompoundVariableWithDefault() {
   test(
       Mustache.compiler().defaultValue("?"),
       "?",
       "{{foo.bar.baz}}",
       new Object() {
         // no foo, no bar
       });
   test(
       Mustache.compiler().defaultValue("?"),
       "?",
       "{{foo.bar.baz}}",
       new Object() {
         Object foo = new Object(); // no bar
       });
 }
 @Test
 public void contextLoads() {
   String source = "Hello {{arg}}!";
   Template tmpl = Mustache.compiler().compile(source);
   Map<String, String> context = new HashMap<String, String>();
   context.put("arg", "world");
   assertEquals("Hello world!", tmpl.execute(context)); // returns "Hello world!"
 }
예제 #9
0
 @Test
 public void testMissingValueWithDefaultNonEmptyString() {
   test(
       Mustache.compiler().defaultValue("foo"),
       "foobar",
       "{{missing}}{{notmissing}}",
       context("notmissing", "bar"));
 }
예제 #10
0
 @Test
 public void testMissingValueWithDefaultSubstitution3() {
   test(
       Mustache.compiler().defaultValue("{{?{{name}}?}}"),
       "{{?missing?}}bar",
       "{{missing}}{{notmissing}}",
       context("notmissing", "bar"));
 }
 @Override
 public Template loadRequestTemplate(String requestTemplatePath) {
   return new Template(null, Mustache.compiler()) {
     @Override
     public String toString() {
       return requestTemplatePath;
     }
   };
 }
예제 #12
0
 @Test(expected = MustacheException.class)
 public void testStandardsModeWithNoParentContextSearching() {
   String tmpl = "{{#parent}}foo{{parentProperty}}bar{{/parent}}";
   String result =
       Mustache.compiler()
           .standardsMode(true)
           .compile(tmpl)
           .execute(context("parent", new Object(), "parentProperty", "bar"));
 }
예제 #13
0
 @Test
 public void testNullSectionWithNullValue() {
   test(
       Mustache.compiler().nullValue(""),
       "",
       "{{#foo}}{{bar}}{{/foo}}",
       new Object() {
         Object foo = null;
       });
 }
예제 #14
0
 @Test
 public void testLineReporting() {
   String tmpl = "first line\n{{nonexistent}}\nsecond line";
   try {
     Mustache.compiler().compile(tmpl).execute(new Object());
     fail("Referencing a nonexistent variable should throw MustacheException");
   } catch (MustacheException e) {
     assertTrue(e.getMessage().contains("line 2"));
   }
 }
예제 #15
0
 @Test(expected = MustacheException.class)
 public void testMissingComponentInCompoundVariable() {
   test(
       Mustache.compiler(),
       "unused",
       "{{foo.bar.baz}}",
       new Object() {
         Object foo = new Object(); // no bar
       });
 }
예제 #16
0
 @Test
 public void testSectionWithNonFalseyZero() {
   test(
       Mustache.compiler(),
       "test",
       "{{#foo}}test{{/foo}}",
       new Object() {
         Long foo = 0L;
       });
 }
예제 #17
0
 @Test
 public void testStandardsModeWithDotValue() {
   String tmpl = "{{#foo}}:{{.}}:{{/foo}}";
   String result =
       Mustache.compiler()
           .standardsMode(true)
           .compile(tmpl)
           .execute(Collections.singletonMap("foo", "bar"));
   assertEquals(":bar:", result);
 }
예제 #18
0
 @Test
 public void testMissingSectionWithDefaultValue() {
   test(
       Mustache.compiler().defaultValue(""),
       "",
       "{{#foo}}{{bar}}{{/foo}}",
       new Object() {
         // no foo
       });
 }
예제 #19
0
 @Test(expected = MustacheException.class)
 public void testMissingSectionWithNullValue() {
   test(
       Mustache.compiler().nullValue(""),
       "",
       "{{#foo}}{{bar}}{{/foo}}",
       new Object() {
         // no foo
       });
 }
예제 #20
0
 @Test
 public void testSectionWithNonFalseyEmptyString() {
   test(
       Mustache.compiler(),
       "test",
       "{{#foo}}test{{/foo}}",
       new Object() {
         String foo = "";
       });
 }
예제 #21
0
 @Test
 public void testNonStandardDefaultDelims() {
   test(
       Mustache.compiler().withDelims("<% %>"),
       "bar",
       "<%foo%>",
       new Object() {
         String foo = "bar";
       });
 }
 @Bean
 public MustacheViewResolver viewResolver() {
   MustacheViewResolver resolver = new MustacheViewResolver();
   resolver.setPrefix("classpath:/mustache-templates/");
   resolver.setSuffix(".html");
   resolver.setCompiler(
       Mustache.compiler()
           .withLoader(
               new MustacheResourceTemplateLoader("classpath:/mustache-templates/", ".html")));
   return resolver;
 }
예제 #23
0
 @Test
 public void testNullValueGetsNullDefault() {
   test(
       Mustache.compiler().nullValue("foo"),
       "foobar",
       "{{nullvar}}{{nonnullvar}}",
       new Object() {
         String nonnullvar = "bar";
         String nullvar = null;
       });
 }
예제 #24
0
 @Test(expected = MustacheException.class)
 public void testMissingValueWithNullDefault() {
   test(
       Mustache.compiler().nullValue(""),
       "bar",
       "{{missing}}{{notmissing}}",
       new Object() {
         String notmissing = "bar";
         // no field or method for 'missing'
       });
 }
예제 #25
0
 @Test
 public void testNullComponentInCompoundVariableWithDefault() {
   test(
       Mustache.compiler().nullValue("null"),
       "null",
       "{{foo.bar.baz}}",
       new Object() {
         Object foo = null;
       });
   test(
       Mustache.compiler().nullValue("null"),
       "null",
       "{{foo.bar.baz}}",
       new Object() {
         Object foo =
             new Object() {
               Object bar = null;
             };
       });
 }
예제 #26
0
 @Test
 public void testUserDefinedEscaping() {
   Mustache.Escaper escaper =
       Escapers.simple(
           new String[][] {
             {"[", ":BEGIN:"},
             {"]", ":END:"}
           });
   assertEquals(
       ":BEGIN:b:END:",
       Mustache.compiler().withEscaper(escaper).compile("{{a}}").execute(context("a", "[b]")));
 }
예제 #27
0
 @Test(expected = MustacheException.class)
 public void testNullComponentInCompoundVariable() {
   test(
       Mustache.compiler(),
       "unused",
       "{{foo.bar.baz}}",
       new Object() {
         Object foo =
             new Object() {
               Object bar = null;
             };
       });
 }
예제 #28
0
 @Test
 public void testCallSiteReuse() {
   Template tmpl = Mustache.compiler().compile("{{foo}}");
   Object ctx =
       new Object() {
         String getFoo() {
           return "bar";
         }
       };
   for (int ii = 0; ii < 50; ii++) {
     assertEquals("bar", tmpl.execute(ctx));
   }
 }
 @Override
 public void afterPropertiesSet() throws Exception {
   if (templateLoader == null) {
     templateLoader = new MustacheTemplateLoader();
     templateLoader.setPrefix(getPrefix());
     templateLoader.setSuffix(getSuffix());
     templateLoader.setResourceLoader(resourceLoader);
   }
   compiler =
       Mustache.compiler()
           .escapeHTML(escapeHTML)
           .standardsMode(standardsMode)
           .withLoader(templateLoader);
 }
예제 #30
0
 @Test
 public void testSectionWithFalseyEmptyString() {
   Object ctx =
       new Object() {
         String foo = "";
         String bar = "nonempty";
       };
   // test normal sections with falsey empty string
   Mustache.Compiler compiler = Mustache.compiler().emptyStringIsFalse(true);
   test(compiler, "", "{{#foo}}test{{/foo}}", ctx);
   test(compiler, "test", "{{#bar}}test{{/bar}}", ctx);
   // test inverted sections with falsey empty string
   test(compiler, "test", "{{^foo}}test{{/foo}}", ctx);
   test(compiler, "", "{{^bar}}test{{/bar}}", ctx);
 }