View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  
19  package org.apache.commons.exec.util;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import junit.framework.TestCase;
25  
26  public class StringUtilTest extends TestCase
27  {
28      /**
29       * Test no string substitution
30       */
31      public void testNoStringSubstitution() throws Exception
32      {
33          Map vars = new HashMap();
34          vars.put("foo", "FOO");
35          vars.put("bar", "BAR");
36  
37          assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a FOO & BAR test", vars, true).toString());
38      }
39  
40      /**
41       * Test a default string substitution, e.g. all placeholders
42       * are expanded.
43       */
44      public void testDefaultStringSubstitution() throws Exception 
45      {
46          Map vars = new HashMap();
47          vars.put("foo", "FOO");
48          vars.put("bar", "BAR");
49  
50          assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
51          assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString());
52      }
53  
54      /**
55       * Test an incomplete string substitution where not all placeholders
56       * are expanded.
57       */
58      public void testIncompleteSubstitution() throws Exception {
59  
60          Map vars = new HashMap();
61          vars.put("foo", "FOO");
62  
63          assertEquals("This is a FOO & ${bar} test",  StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
64  
65          try
66          {
67              StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString();
68              fail();
69          }
70          catch(RuntimeException e)
71          {
72              // nothing to do
73          }
74      }
75  
76      /**
77       * Test a erroneous template.
78       */
79      public void testErroneousTemplate() throws Exception
80      {
81          Map vars = new HashMap();
82          vars.put("foo", "FOO");
83  
84          assertEquals("This is a FOO & ${}} test",  StringUtils.stringSubstitution("This is a ${foo} & ${}} test", vars, true).toString());
85      }
86  }