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  package org.apache.commons.exec.util;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.fail;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   */
30  public class StringUtilTest {
31      /**
32       * Test a default string substitution, e.g. all placeholders are expanded.
33       */
34      @Test
35      public void testDefaultStringSubstitution() throws Exception {
36          final Map<String, String> vars = new HashMap<>();
37          vars.put("foo", "FOO");
38          vars.put("bar", "BAR");
39  
40          assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
41          assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString());
42      }
43  
44      /**
45       * Test a erroneous template.
46       */
47      @Test
48      public void testErroneousTemplate() throws Exception {
49          final Map<String, String> vars = new HashMap<>();
50          vars.put("foo", "FOO");
51  
52          assertEquals("This is a FOO & ${}} test", StringUtils.stringSubstitution("This is a ${foo} & ${}} test", vars, true).toString());
53      }
54  
55      /**
56       * Test an incomplete string substitution where not all placeholders are expanded.
57       */
58      @Test
59      public void testIncompleteSubstitution() throws Exception {
60  
61          final Map<String, String> vars = new HashMap<>();
62          vars.put("foo", "FOO");
63  
64          assertEquals("This is a FOO & ${bar} test", StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, true).toString());
65  
66          try {
67              StringUtils.stringSubstitution("This is a ${foo} & ${bar} test", vars, false).toString();
68              fail();
69          } catch (final RuntimeException e) {
70              // nothing to do
71          }
72      }
73  
74      /**
75       * Test no string substitution
76       */
77      @Test
78      public void testNoStringSubstitution() throws Exception {
79          final Map<String, String> vars = new HashMap<>();
80          vars.put("foo", "FOO");
81          vars.put("bar", "BAR");
82  
83          assertEquals("This is a FOO & BAR test", StringUtils.stringSubstitution("This is a FOO & BAR test", vars, true).toString());
84      }
85  }