TemplateTest.java revision 3170:dc017a37aac5
1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package tools.javac.combo;
25
26import org.testng.annotations.BeforeTest;
27import org.testng.annotations.Test;
28
29import java.util.HashMap;
30import java.util.Map;
31
32import static org.testng.Assert.assertEquals;
33
34/**
35 * TemplateTest
36 */
37@Test
38public class TemplateTest {
39    Map<String, Template> vars = new HashMap<>();
40
41    @BeforeTest
42    void before() { vars.clear(); }
43
44    private void assertTemplate(String expected, String template) {
45        String result = Template.Behavior.expandTemplate(template, vars);
46        assertEquals(result, expected, "for " + template);
47    }
48
49    private String dotIf(String s) {
50        return s == null || s.isEmpty() ? "" : "." + s;
51    }
52
53    public void testTemplateExpansion() {
54        vars.put("A", s -> "a" + dotIf(s));
55        vars.put("B", s -> "b" + dotIf(s));
56        vars.put("C", s -> "#{A}#{B}");
57        vars.put("D", s -> "#{A" + dotIf(s) + "}#{B" + dotIf(s) + "}");
58        vars.put("_D", s -> "d");
59
60        assertTemplate("", "");
61        assertTemplate("foo", "foo");
62        assertTemplate("a", "#{A}");
63        assertTemplate("a", "#{A.}");
64        assertTemplate("a.FOO", "#{A.FOO}");
65        assertTemplate("aa", "#{A}#{A}");
66        assertTemplate("ab", "#{C}");
67        assertTemplate("ab", "#{C.FOO}");
68        assertTemplate("ab", "#{C.}");
69        assertTemplate("a.FOOb.FOO", "#{D.FOO}");
70        assertTemplate("ab", "#{D}");
71        assertTemplate("d", "#{_D}");
72        assertTemplate("#{A", "#{A");
73    }
74
75    public void testIndexedTemplate() {
76        vars.put("A[0]", s -> "a" );
77        vars.put("A[1]", s -> "b" );
78        vars.put("A[2]", s -> "c" );
79        vars.put("X", s -> "0");
80        assertTemplate("a", "#{A[0]}");
81        assertTemplate("b", "#{A[1]}");
82        assertTemplate("c", "#{A[2]}");
83    }
84
85    public void testAngleBrackets() {
86        vars.put("X", s -> "xyz");
87        assertTemplate("List<String> ls = xyz;", "List<String> ls = #{X};");
88    }
89
90    @Test(expectedExceptions = IllegalStateException.class )
91    public void testUnknownKey() {
92        assertTemplate("#{Q}", "#{Q}");
93    }
94}
95