View Javadoc

1   /*
2    * $Id: TemplateParserTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.AbstractMuleTestCase;
14  
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  public class TemplateParserTestCase extends AbstractMuleTestCase
21  {
22  
23      public void testStringParserSquareBraces()
24      {
25          TemplateParser tp = TemplateParser.createSquareBracesStyleParser();
26          assertNotNull(tp.getStyle());
27          assertEquals("[", tp.getStyle().getPrefix());
28          assertEquals("]", tp.getStyle().getSuffix());
29  
30          Map props = new HashMap();
31          props.put("fromAddress", "ross.mason@symphonysoft.com");
32          String string = "smtp://[fromAddress]";
33  
34          String result = tp.parse(props, string);
35          assertEquals("smtp://ross.mason@symphonysoft.com", result);
36          string = "smtp://[toAddress]";
37          result = tp.parse(props, string);
38          assertEquals("smtp://[toAddress]", result);
39      }
40  
41      public void testParserValidationSquareBraces()
42      {
43          TemplateParser tp = TemplateParser.createSquareBracesStyleParser();
44          assertTrue(tp.isValid("[][]"));
45          assertTrue(tp.isValid("[[]]"));
46          assertFalse(tp.isValid("[[][]"));
47      }
48  
49      public void testParserValidationAntStyle()
50      {
51          TemplateParser tp = TemplateParser.createAntStyleParser();
52          assertTrue(tp.isValid("${}"));
53          assertTrue(tp.isValid("${}${}"));
54          assertFalse(tp.isValid("${}&{}"));
55          assertFalse(tp.isValid("{}${}"));
56          assertTrue(tp.isValid("${$}${}"));
57          assertFalse(tp.isValid("${${}}${}"));
58          assertFalse(tp.isValid("$ {}"));
59  
60      }
61  
62      public void testParserValidationMuleStyle()
63      {
64          TemplateParser tp = TemplateParser.createMuleStyleParser();
65          assertTrue(tp.isValid("#[]"));
66          assertTrue(tp.isValid("#[]   #[]"));
67          assertFalse(tp.isValid("#[]&[]"));
68          assertFalse(tp.isValid("[]$[]#"));
69          assertTrue(tp.isValid("#[#]#[]"));
70          assertFalse(tp.isValid("#[#[]]#[]"));
71          assertFalse(tp.isValid("# []"));
72  
73          assertTrue(tp.isValid("#[foo:blah[4] = 'foo']"));
74          assertTrue(tp.isValid("#[foo:blah[4] = '#foo']"));
75          assertFalse(tp.isValid("#[foo:blah4] = '#foo']"));
76          //Can't have embedded
77          assertFalse(tp.isValid("#[foo:blah = '#[foo]']"));
78  
79  
80      }
81  
82      public void testStringParserAntBraces()
83      {
84          TemplateParser tp = TemplateParser.createAntStyleParser();
85          assertNotNull(tp.getStyle());
86          assertEquals("${", tp.getStyle().getPrefix());
87          assertEquals("}", tp.getStyle().getSuffix());
88  
89          Map props = new HashMap();
90          props.put("prop1", "value1");
91          props.put("prop2", "value2");
92  
93          String string = "Some String with ${prop1} and ${prop2} in it";
94          String result = tp.parse(props, string);
95          assertEquals("Some String with value1 and value2 in it", result);
96  
97          string = "${prop1}${prop1}${prop2}";
98          result = tp.parse(props, string);
99          assertEquals("value1value1value2", result);
100 
101         // MULE-978: a property with backslashes (on Windows)
102         String homeDir = System.getProperty("user.home");
103         props.put("homeDir", homeDir);
104         string = "${homeDir}/foo";
105         result = tp.parse(props, string);
106         assertEquals(homeDir + "/foo", result);
107 
108         // whitespace is really popular too
109         String whitespaceValue = "C:\\Documents and Settings\\";
110         props.put("whitespaceValue", whitespaceValue);
111         string = "start${whitespaceValue}end";
112         result = tp.parse(props, string);
113         assertEquals("start" + whitespaceValue + "end", result);
114     }
115 
116     public void testListParserAntBraces()
117     {
118         TemplateParser tp = TemplateParser.createAntStyleParser();
119 
120         Map props = new HashMap();
121         props.put("prop1", "value1");
122         props.put("prop2", "value2");
123         List list = new ArrayList();
124         list.add("Some String with ${prop1} and ${prop2} in it");
125         list.add("Some String with ${prop1} in it");
126 
127         List result = tp.parse(props, list);
128         assertEquals("Some String with value1 and value2 in it", result.get(0));
129         assertEquals("Some String with value1 in it", result.get(1));
130 
131         result = tp.parse(props, (List)null);
132         assertNotNull(result);
133         assertEquals(0, result.size());
134     }
135 
136     public void testMapParserAntBraces()
137     {
138         TemplateParser tp = TemplateParser.createAntStyleParser();
139         Map props = new HashMap();
140         props.put("prop1", "value1");
141         props.put("prop2", "value2");
142         Map map = new HashMap();
143         map.put("value1", "Some String with ${prop1} and ${prop2} in it");
144         map.put("value2", "Some String with ${prop1} in it");
145 
146         Map result = tp.parse(props, map);
147         assertEquals("Some String with value1 and value2 in it", result.get("value1"));
148         assertEquals("Some String with value1 in it", result.get("value2"));
149 
150         result = tp.parse(props, (Map)null);
151         assertNotNull(result);
152         assertEquals(0, result.size());
153     }
154 
155     public void testStringParserAntBracesWithSimilarNames()
156     {
157         TemplateParser tp = TemplateParser.createAntStyleParser();
158         Map props = new HashMap();
159         props.put("prop1", "value1");
160         props.put("prop1-2", "value2");
161         String string = "Some String with ${prop1} and ${prop1-2} in it";
162 
163         String result = tp.parse(props, string);
164         assertEquals("Some String with value1 and value2 in it", result);
165         string = "A${prop1-2}B${prop1}C${prop2}";
166         result = tp.parse(props, string);
167         assertEquals("Avalue2Bvalue1C${prop2}", result);
168     }
169 
170 }