View Javadoc

1   /*
2    * $Id: TemplateParserTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util;
12  
13  import org.mule.tck.junit4.AbstractMuleTestCase;
14  
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  public class TemplateParserTestCase extends AbstractMuleTestCase
28  {
29  
30      @Test
31      public void testStringParserSquareBraces()
32      {
33          TemplateParser tp = TemplateParser.createSquareBracesStyleParser();
34          assertNotNull(tp.getStyle());
35          assertEquals("[", tp.getStyle().getPrefix());
36          assertEquals("]", tp.getStyle().getSuffix());
37  
38          Map props = new HashMap();
39          props.put("fromAddress", "ross.mason@symphonysoft.com");
40          String string = "smtp://[fromAddress]";
41  
42          String result = tp.parse(props, string);
43          assertEquals("smtp://ross.mason@symphonysoft.com", result);
44          string = "smtp://[toAddress]";
45          result = tp.parse(props, string);
46          assertEquals("smtp://[toAddress]", result);
47      }
48  
49      @Test
50      public void testParserValidationSquareBraces()
51      {
52          TemplateParser tp = TemplateParser.createSquareBracesStyleParser();
53          assertTrue(tp.isValid("[][]"));
54          assertTrue(tp.isValid("[[]]"));
55          assertFalse(tp.isValid("[[][]"));
56      }
57  
58      @Test
59      public void testParserValidationAntStyle()
60      {
61          TemplateParser tp = TemplateParser.createAntStyleParser();
62          assertTrue(tp.isValid("${}"));
63          assertTrue(tp.isValid("${}${}"));
64          assertFalse(tp.isValid("${}&{}"));
65          assertFalse(tp.isValid("{}${}"));
66          assertTrue(tp.isValid("${$}${}"));
67          assertFalse(tp.isValid("${${}}${}"));
68          assertFalse(tp.isValid("$ {}"));
69  
70      }
71  
72      @Test
73      public void testParserValidationMuleStyle()
74      {
75          TemplateParser tp = TemplateParser.createMuleStyleParser();
76          assertTrue(tp.isValid("#[]"));
77          assertTrue(tp.isValid("#[]   #[]"));
78          assertFalse(tp.isValid("#[]&[]"));
79          assertFalse(tp.isValid("[]$[]#"));
80          assertTrue(tp.isValid("#[#]#[]"));
81          assertFalse(tp.isValid("#[#[]]#[]"));
82          assertFalse(tp.isValid("# []"));
83  
84          assertTrue(tp.isValid("#[foo:blah[4] = 'foo']"));
85          assertTrue(tp.isValid("#[foo:blah[4] = '#foo']"));
86          assertFalse(tp.isValid("#[foo:blah4] = '#foo']"));
87          //Can't have embedded
88          assertFalse(tp.isValid("#[foo:blah = '#[foo]']"));
89  
90  
91      }
92  
93      @Test
94      public void testStringParserAntBraces()
95      {
96          TemplateParser tp = TemplateParser.createAntStyleParser();
97          assertNotNull(tp.getStyle());
98          assertEquals("${", tp.getStyle().getPrefix());
99          assertEquals("}", tp.getStyle().getSuffix());
100 
101         Map props = new HashMap();
102         props.put("prop1", "value1");
103         props.put("prop2", "value2");
104 
105         String string = "Some String with ${prop1} and ${prop2} in it";
106         String result = tp.parse(props, string);
107         assertEquals("Some String with value1 and value2 in it", result);
108 
109         string = "${prop1}${prop1}${prop2}";
110         result = tp.parse(props, string);
111         assertEquals("value1value1value2", result);
112 
113         // MULE-978: a property with backslashes (on Windows)
114         String homeDir = System.getProperty("user.home");
115         props.put("homeDir", homeDir);
116         string = "${homeDir}/foo";
117         result = tp.parse(props, string);
118         assertEquals(homeDir + "/foo", result);
119 
120         // whitespace is really popular too
121         String whitespaceValue = "C:\\Documents and Settings\\";
122         props.put("whitespaceValue", whitespaceValue);
123         string = "start${whitespaceValue}end";
124         result = tp.parse(props, string);
125         assertEquals("start" + whitespaceValue + "end", result);
126     }
127 
128     @Test
129     public void testListParserAntBraces()
130     {
131         TemplateParser tp = TemplateParser.createAntStyleParser();
132 
133         Map props = new HashMap();
134         props.put("prop1", "value1");
135         props.put("prop2", "value2");
136         List list = new ArrayList();
137         list.add("Some String with ${prop1} and ${prop2} in it");
138         list.add("Some String with ${prop1} in it");
139 
140         List result = tp.parse(props, list);
141         assertEquals("Some String with value1 and value2 in it", result.get(0));
142         assertEquals("Some String with value1 in it", result.get(1));
143 
144         result = tp.parse(props, (List)null);
145         assertNotNull(result);
146         assertEquals(0, result.size());
147     }
148 
149     @Test
150     public void testMapParserAntBraces()
151     {
152         TemplateParser tp = TemplateParser.createAntStyleParser();
153         Map props = new HashMap();
154         props.put("prop1", "value1");
155         props.put("prop2", "value2");
156         Map map = new HashMap();
157         map.put("value1", "Some String with ${prop1} and ${prop2} in it");
158         map.put("value2", "Some String with ${prop1} in it");
159 
160         Map result = tp.parse(props, map);
161         assertEquals("Some String with value1 and value2 in it", result.get("value1"));
162         assertEquals("Some String with value1 in it", result.get("value2"));
163 
164         result = tp.parse(props, (Map)null);
165         assertNotNull(result);
166         assertEquals(0, result.size());
167     }
168 
169     @Test
170     public void testStringParserAntBracesWithSimilarNames()
171     {
172         TemplateParser tp = TemplateParser.createAntStyleParser();
173         Map props = new HashMap();
174         props.put("prop1", "value1");
175         props.put("prop1-2", "value2");
176         String string = "Some String with ${prop1} and ${prop1-2} in it";
177 
178         String result = tp.parse(props, string);
179         assertEquals("Some String with value1 and value2 in it", result);
180         string = "A${prop1-2}B${prop1}C${prop2}";
181         result = tp.parse(props, string);
182         assertEquals("Avalue2Bvalue1C${prop2}", result);
183     }
184 
185 }