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