View Javadoc
1   /*
2    *    Copyright 2009-2021 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.parsing;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.time.Duration;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.api.Disabled;
26  import org.junit.jupiter.api.Test;
27  
28  class GenericTokenParserTest {
29  
30    public static class VariableTokenHandler implements TokenHandler {
31      private Map<String, String> variables = new HashMap<>();
32  
33      VariableTokenHandler(Map<String, String> variables) {
34        this.variables = variables;
35      }
36  
37      @Override
38      public String handleToken(String content) {
39        return variables.get(content);
40      }
41    }
42  
43    @Test
44    void shouldDemonstrateGenericTokenReplacement() {
45      GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap<String, String>() {
46        {
47          put("first_name", "James");
48          put("initial", "T");
49          put("last_name", "Kirk");
50          put("var{with}brace", "Hiya");
51          put("", "");
52        }
53      }));
54  
55      assertEquals("James T Kirk reporting.", parser.parse("${first_name} ${initial} ${last_name} reporting."));
56      assertEquals("Hello captain James T Kirk", parser.parse("Hello captain ${first_name} ${initial} ${last_name}"));
57      assertEquals("James T Kirk", parser.parse("${first_name} ${initial} ${last_name}"));
58      assertEquals("JamesTKirk", parser.parse("${first_name}${initial}${last_name}"));
59      assertEquals("{}JamesTKirk", parser.parse("{}${first_name}${initial}${last_name}"));
60      assertEquals("}JamesTKirk", parser.parse("}${first_name}${initial}${last_name}"));
61  
62      assertEquals("}James{{T}}Kirk", parser.parse("}${first_name}{{${initial}}}${last_name}"));
63      assertEquals("}James}T{Kirk", parser.parse("}${first_name}}${initial}{${last_name}"));
64      assertEquals("}James}T{Kirk", parser.parse("}${first_name}}${initial}{${last_name}"));
65      assertEquals("}James}T{Kirk{{}}", parser.parse("}${first_name}}${initial}{${last_name}{{}}"));
66      assertEquals("}James}T{Kirk{{}}", parser.parse("}${first_name}}${initial}{${last_name}{{}}${}"));
67  
68      assertEquals("{$$something}JamesTKirk", parser.parse("{$$something}${first_name}${initial}${last_name}"));
69      assertEquals("${", parser.parse("${"));
70      assertEquals("${\\}", parser.parse("${\\}"));
71      assertEquals("Hiya", parser.parse("${var{with\\}brace}"));
72      assertEquals("", parser.parse("${}"));
73      assertEquals("}", parser.parse("}"));
74      assertEquals("Hello ${ this is a test.", parser.parse("Hello ${ this is a test."));
75      assertEquals("Hello } this is a test.", parser.parse("Hello } this is a test."));
76      assertEquals("Hello } ${ this is a test.", parser.parse("Hello } ${ this is a test."));
77    }
78  
79    @Test
80    void shallNotInterpolateSkippedVaiables() {
81      GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap<>()));
82  
83      assertEquals("${skipped} variable", parser.parse("\\${skipped} variable"));
84      assertEquals("This is a ${skipped} variable", parser.parse("This is a \\${skipped} variable"));
85      assertEquals("null ${skipped} variable", parser.parse("${skipped} \\${skipped} variable"));
86      assertEquals("The null is ${skipped} variable", parser.parse("The ${skipped} is \\${skipped} variable"));
87    }
88  
89    @Disabled("Because it randomly fails on Github CI. It could be useful during development.")
90    @Test
91    void shouldParseFastOnJdk7u6() {
92      Assertions.assertTimeout(Duration.ofMillis(1000), () -> {
93        // issue #760
94        GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap<String, String>() {
95          {
96            put("first_name", "James");
97            put("initial", "T");
98            put("last_name", "Kirk");
99            put("", "");
100         }
101       }));
102 
103       StringBuilder input = new StringBuilder();
104       for (int i = 0; i < 10000; i++) {
105         input.append("${first_name} ${initial} ${last_name} reporting. ");
106       }
107       StringBuilder expected = new StringBuilder();
108       for (int i = 0; i < 10000; i++) {
109         expected.append("James T Kirk reporting. ");
110       }
111       assertEquals(expected.toString(), parser.parse(input.toString()));
112     });
113   }
114 
115 }