1 package de.juplo.thymeleaf;
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;
19 public class ImportVariablesAttributeProcessorTest
22 public void testPattern() throws IOException
26 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher("{}");
27 assertTrue(matcher.matches());
28 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher(" { } ");
29 assertTrue(matcher.matches());
30 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher("\t{\t}\t");
31 assertTrue(matcher.matches());
32 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher("\n{\n}\n");
33 assertTrue(matcher.matches());
34 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher(" \t\n{ \t\n} \t\n");
35 assertTrue(matcher.matches());
37 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher("{\"foo\":\"bar\",\"foobar\":{\"foo\":\"bar\"}");
38 assertTrue(matcher.matches());
39 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher(" { \"foo\":\"bar\", \"foobar\": { \"foo\": \"bar\" } ");
40 assertTrue(matcher.matches());
41 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher("\t{\t\"foo\":\t\"bar\"\t,\t\"foobar\":\t{\t\"foo\":\"bar\"\t}");
42 assertTrue(matcher.matches());
43 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher("\n{\n\"foo\":\n\"bar\"\n,\n\"foobar\":\n{\n\"foo\":\"bar\"\n}");
44 assertTrue(matcher.matches());
45 matcher = ImportVariablesAttributeProcessor.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());
50 json = read("/json/1.json");
51 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher(json);
52 assertTrue(matcher.matches());
53 json = read("/json/2.json");
54 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher(json);
55 assertTrue(matcher.matches());
56 json = read("/json/3.json");
57 matcher = ImportVariablesAttributeProcessor.PATTERN.matcher(json);
58 assertTrue(matcher.matches());
60 matcher = ImportVariablesAttributeProcessor.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 = ImportVariablesAttributeProcessor.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 = ImportVariablesAttributeProcessor.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));
78 String read(String resource)
80 InputStream is = getClass().getResourceAsStream(resource);
81 return new Scanner(is).useDelimiter("\\Z").next();