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