Esempio n. 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"))));
 }
Esempio n. 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());
   }
 }
Esempio n. 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>")));
 }
Esempio n. 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;
  }
 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;
 }
Esempio n. 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!"
 }
Esempio n. 9
0
 @Test
 public void testMissingValueWithDefaultNonEmptyString() {
   test(
       Mustache.compiler().defaultValue("foo"),
       "foobar",
       "{{missing}}{{notmissing}}",
       context("notmissing", "bar"));
 }
Esempio n. 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;
     }
   };
 }
Esempio n. 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"));
 }
Esempio n. 13
0
 @Test
 public void testNullSectionWithNullValue() {
   test(
       Mustache.compiler().nullValue(""),
       "",
       "{{#foo}}{{bar}}{{/foo}}",
       new Object() {
         Object foo = null;
       });
 }
Esempio n. 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"));
   }
 }
Esempio n. 15
0
 @Test(expected = MustacheException.class)
 public void testMissingComponentInCompoundVariable() {
   test(
       Mustache.compiler(),
       "unused",
       "{{foo.bar.baz}}",
       new Object() {
         Object foo = new Object(); // no bar
       });
 }
Esempio n. 16
0
 @Test
 public void testSectionWithNonFalseyZero() {
   test(
       Mustache.compiler(),
       "test",
       "{{#foo}}test{{/foo}}",
       new Object() {
         Long foo = 0L;
       });
 }
Esempio n. 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);
 }
Esempio n. 18
0
 @Test
 public void testMissingSectionWithDefaultValue() {
   test(
       Mustache.compiler().defaultValue(""),
       "",
       "{{#foo}}{{bar}}{{/foo}}",
       new Object() {
         // no foo
       });
 }
Esempio n. 19
0
 @Test(expected = MustacheException.class)
 public void testMissingSectionWithNullValue() {
   test(
       Mustache.compiler().nullValue(""),
       "",
       "{{#foo}}{{bar}}{{/foo}}",
       new Object() {
         // no foo
       });
 }
Esempio n. 20
0
 @Test
 public void testSectionWithNonFalseyEmptyString() {
   test(
       Mustache.compiler(),
       "test",
       "{{#foo}}test{{/foo}}",
       new Object() {
         String foo = "";
       });
 }
Esempio n. 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;
 }
Esempio n. 23
0
 @Test
 public void testNullValueGetsNullDefault() {
   test(
       Mustache.compiler().nullValue("foo"),
       "foobar",
       "{{nullvar}}{{nonnullvar}}",
       new Object() {
         String nonnullvar = "bar";
         String nullvar = null;
       });
 }
Esempio n. 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'
       });
 }
Esempio n. 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;
             };
       });
 }
Esempio n. 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]")));
 }
Esempio n. 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;
             };
       });
 }
Esempio n. 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);
 }
Esempio n. 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);
 }