1   /*
2    * $Id: TemplateParserTestCase.java 8077 2007-08-27 20:15:25Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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          Map props = new HashMap();
27          props.put("fromAddress", "ross.mason@symphonysoft.com");
28          String string = "smtp://[fromAddress]";
29  
30          String result = tp.parse(props, string);
31          assertEquals("smtp://ross.mason@symphonysoft.com", result);
32          string = "smtp://[toAddress]";
33          result = tp.parse(props, string);
34          assertEquals("smtp://[toAddress]", result);
35      }
36  
37      public void testStringParserAntBraces()
38      {
39          TemplateParser tp = TemplateParser.createAntStyleParser();
40          Map props = new HashMap();
41          props.put("prop1", "value1");
42          props.put("prop2", "value2");
43  
44          String string = "Some String with ${prop1} and ${prop2} in it";
45          String result = tp.parse(props, string);
46          assertEquals("Some String with value1 and value2 in it", result);
47  
48          string = "${prop1}${prop1}${prop2}";
49          result = tp.parse(props, string);
50          assertEquals("value1value1value2", result);
51  
52          // MULE-978: a property with backslashes (on Windows)
53          String homeDir = System.getProperty("user.home");
54          props.put("homeDir", homeDir);
55          string = "${homeDir}/foo";
56          result = tp.parse(props, string);
57          assertEquals(homeDir + "/foo", result);
58  
59          // whitespace is really popular too
60          String whitespaceValue = "C:\\Documents and Settings\\";
61          props.put("whitespaceValue", whitespaceValue);
62          string = "start${whitespaceValue}end";
63          result = tp.parse(props, string);
64          assertEquals("start" + whitespaceValue + "end", result);
65      }
66  
67      public void testListParserAntBraces()
68      {
69          TemplateParser tp = TemplateParser.createAntStyleParser();
70          Map props = new HashMap();
71          props.put("prop1", "value1");
72          props.put("prop2", "value2");
73          List list = new ArrayList();
74          list.add("Some String with ${prop1} and ${prop2} in it");
75          list.add("Some String with ${prop1} in it");
76  
77          List result = tp.parse(props, list);
78          assertEquals("Some String with value1 and value2 in it", result.get(0));
79          assertEquals("Some String with value1 in it", result.get(1));
80  
81          result = tp.parse(props, (List)null);
82          assertNotNull(result);
83          assertEquals(0, result.size());
84      }
85  
86      public void testMapParserAntBraces()
87      {
88          TemplateParser tp = TemplateParser.createAntStyleParser();
89          Map props = new HashMap();
90          props.put("prop1", "value1");
91          props.put("prop2", "value2");
92          Map map = new HashMap();
93          map.put("value1", "Some String with ${prop1} and ${prop2} in it");
94          map.put("value2", "Some String with ${prop1} in it");
95  
96          Map result = tp.parse(props, map);
97          assertEquals("Some String with value1 and value2 in it", result.get("value1"));
98          assertEquals("Some String with value1 in it", result.get("value2"));
99  
100         result = tp.parse(props, (Map)null);
101         assertNotNull(result);
102         assertEquals(0, result.size());
103     }
104 
105     public void testStringParserAntBracesWithSimilarNames()
106     {
107         TemplateParser tp = TemplateParser.createAntStyleParser();
108         Map props = new HashMap();
109         props.put("prop1", "value1");
110         props.put("prop1-2", "value2");
111         String string = "Some String with ${prop1} and ${prop1-2} in it";
112 
113         String result = tp.parse(props, string);
114         assertEquals("Some String with value1 and value2 in it", result);
115         string = "A${prop1-2}B${prop1}C${prop2}";
116         result = tp.parse(props, string);
117         assertEquals("Avalue2Bvalue1C${prop2}", result);
118     }
119 
120 }