juplo:variables parses its parameter as JSON and can merge variable-defs
[juplo-dialect] / src / test / java / de / juplo / thymeleaf / ImportVariablesAttrProcessorTest.java
1 package de.juplo.thymeleaf;
2
3
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.Scanner;
7 import java.util.regex.Matcher;
8 import static org.junit.Assert.assertEquals;
9 import static org.junit.Assert.assertNull;
10 import static org.junit.Assert.assertTrue;
11 import org.junit.Test;
12
13
14
15 /**
16  *
17  * @author Kai Moritz
18  */
19 public class ImportVariablesAttrProcessorTest
20 {
21   @Test
22   public void testPattern() throws IOException
23   {
24     Matcher matcher;
25
26     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("{}");
27     assertTrue(matcher.matches());
28     matcher = ImportVariablesAttrProcessor.PATTERN.matcher(" { } ");
29     assertTrue(matcher.matches());
30     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("\t{\t}\t");
31     assertTrue(matcher.matches());
32     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("\n{\n}\n");
33     assertTrue(matcher.matches());
34     matcher = ImportVariablesAttrProcessor.PATTERN.matcher(" \t\n{ \t\n} \t\n");
35     assertTrue(matcher.matches());
36
37     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("{\"foo\":\"bar\",\"foobar\":{\"foo\":\"bar\"}");
38     assertTrue(matcher.matches());
39     matcher = ImportVariablesAttrProcessor.PATTERN.matcher(" { \"foo\":\"bar\", \"foobar\": { \"foo\": \"bar\" } ");
40     assertTrue(matcher.matches());
41     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("\t{\t\"foo\":\t\"bar\"\t,\t\"foobar\":\t{\t\"foo\":\"bar\"\t}");
42     assertTrue(matcher.matches());
43     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("\n{\n\"foo\":\n\"bar\"\n,\n\"foobar\":\n{\n\"foo\":\"bar\"\n}");
44     assertTrue(matcher.matches());
45     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("\n\t {\n\t \"foo\":\n\t \"bar\"\n\t ,\n\t \"foobar\":\n\t {\n\t \"foo\":\"bar\"\n\t }");
46     assertTrue(matcher.matches());
47
48     String json;
49
50     json = read("/json/1.json");
51     matcher = ImportVariablesAttrProcessor.PATTERN.matcher(json);
52     assertTrue(matcher.matches());
53     json = read("/json/2.json");
54     matcher = ImportVariablesAttrProcessor.PATTERN.matcher(json);
55     assertTrue(matcher.matches());
56     json = read("/json/3.json");
57     matcher = ImportVariablesAttrProcessor.PATTERN.matcher(json);
58     assertTrue(matcher.matches());
59
60     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("foo:bar");
61     assertTrue(matcher.matches());
62     assertNull(matcher.group(1));
63     assertNull(matcher.group(2));
64     assertEquals("foo:bar", matcher.group(3));
65     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("replace:foo");
66     assertTrue(matcher.matches());
67     assertNull(matcher.group(1));
68     assertNull(matcher.group(2));
69     assertEquals("foo", matcher.group(3));
70     matcher = ImportVariablesAttrProcessor.PATTERN.matcher("merge:foo");
71     assertTrue(matcher.matches());
72     assertEquals("merge", matcher.group(1));
73     assertNull(matcher.group(2));
74     assertEquals("foo", matcher.group(3));
75   }
76
77
78   String read(String resource)
79   {
80     InputStream is = getClass().getResourceAsStream(resource);
81     return new Scanner(is).useDelimiter("\\Z").next();
82   }
83 }