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    *      https://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.text.matcher;
19  
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import org.apache.commons.text.StringSubstitutor;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Test class for {@link StringSubstitutor}.
30   */
31  class StringSubstitutorGetSetTest {
32  
33      /**
34       * Tests get set.
35       */
36      @Test
37      void testGetSetPrefix() {
38          final StringSubstitutor sub = new StringSubstitutor();
39          assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
40          sub.setVariablePrefix('<');
41          assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.CharMatcher);
42  
43          sub.setVariablePrefix("<<");
44          assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
45          assertThrows(IllegalArgumentException.class, () -> sub.setVariablePrefix((String) null));
46          assertTrue(sub.getVariablePrefixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
47  
48          final StringMatcher matcher = StringMatcherFactory.INSTANCE.commaMatcher();
49          sub.setVariablePrefixMatcher(matcher);
50          assertSame(matcher, sub.getVariablePrefixMatcher());
51          assertThrows(IllegalArgumentException.class, () -> sub.setVariablePrefixMatcher((StringMatcher) null));
52          assertSame(matcher, sub.getVariablePrefixMatcher());
53      }
54  
55      /**
56       * Tests get set.
57       */
58      @Test
59      void testGetSetSuffix() {
60          final StringSubstitutor sub = new StringSubstitutor();
61          assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.CharMatcher);
62          sub.setVariableSuffix('<');
63          assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.CharMatcher);
64  
65          sub.setVariableSuffix("<<");
66          assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
67          assertThrows(IllegalArgumentException.class, () -> sub.setVariableSuffix((String) null));
68          assertTrue(sub.getVariableSuffixMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
69  
70          final StringMatcher matcher = StringMatcherFactory.INSTANCE.commaMatcher();
71          sub.setVariableSuffixMatcher(matcher);
72          assertSame(matcher, sub.getVariableSuffixMatcher());
73          assertThrows(IllegalArgumentException.class, () -> sub.setVariableSuffixMatcher((StringMatcher) null));
74          assertSame(matcher, sub.getVariableSuffixMatcher());
75      }
76  
77      /**
78       * Tests get set.
79       */
80      @Test
81      void testGetSetValueDelimiter() {
82          final StringSubstitutor sub = new StringSubstitutor();
83          assertTrue(sub.getValueDelimiterMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
84          sub.setValueDelimiter(':');
85          assertTrue(sub.getValueDelimiterMatcher() instanceof AbstractStringMatcher.CharMatcher);
86  
87          sub.setValueDelimiter("||");
88          assertTrue(sub.getValueDelimiterMatcher() instanceof AbstractStringMatcher.CharArrayMatcher);
89          sub.setValueDelimiter((String) null);
90          assertNull(sub.getValueDelimiterMatcher());
91  
92          final StringMatcher matcher = StringMatcherFactory.INSTANCE.commaMatcher();
93          sub.setValueDelimiterMatcher(matcher);
94          assertSame(matcher, sub.getValueDelimiterMatcher());
95          sub.setValueDelimiterMatcher((StringMatcher) null);
96          assertNull(sub.getValueDelimiterMatcher());
97      }
98  
99  }