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  package org.apache.commons.configuration2.tree;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotEquals;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Test class for {@code DefaultExpressionEngineSymbols}.
27   */
28  public class TestDefaultExpressionEngineSymbols {
29      /**
30       * Helper method for creating a builder object which is initialized with the default symbols.
31       *
32       * @return the initialized builder
33       */
34      private static DefaultExpressionEngineSymbols.Builder builder() {
35          return new DefaultExpressionEngineSymbols.Builder(DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS);
36      }
37  
38      /**
39       * Helper method for checking whether two objects are equal.
40       *
41       * @param o1 object 1
42       * @param o2 object 2
43       */
44      private static void expEqual(final Object o1, final Object o2) {
45          assertEquals(o1, o2);
46          assertEquals(o2, o1);
47          assertEquals(o1.hashCode(), o2.hashCode());
48      }
49  
50      /**
51       * Helper method for testing that two objects are not equal.
52       *
53       * @param o1 object 1
54       * @param o2 object 2
55       */
56      private static void expNE(final Object o1, final Object o2) {
57          assertNotEquals(o1, o2);
58          if (o2 != null) {
59              assertNotEquals(o2, o1);
60          }
61      }
62  
63      /**
64       * Tests the instance with default symbols.
65       */
66      @Test
67      void testDefaultSymbols() {
68          assertEquals(".", DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS.getPropertyDelimiter());
69          assertEquals("..", DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS.getEscapedDelimiter());
70          assertEquals("(", DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS.getIndexStart());
71          assertEquals(")", DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS.getIndexEnd());
72          assertEquals("[@", DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS.getAttributeStart());
73          assertEquals("]", DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS.getAttributeEnd());
74      }
75  
76      /**
77       * Tests equals() if the expected result is false.
78       */
79      @Test
80      void testEqualsFalse() {
81          final DefaultExpressionEngineSymbols s1 = DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS;
82          DefaultExpressionEngineSymbols s2 = builder().setPropertyDelimiter("/").create();
83          expNE(s1, s2);
84          s2 = builder().setEscapedDelimiter("\\.").create();
85          expNE(s1, s2);
86          s2 = builder().setIndexStart("[").create();
87          expNE(s1, s2);
88          s2 = builder().setIndexEnd("]").create();
89          expNE(s1, s2);
90          s2 = builder().setAttributeStart("#").create();
91          expNE(s1, s2);
92          s2 = builder().setAttributeEnd("~").create();
93          expNE(s1, s2);
94      }
95  
96      /**
97       * Tests equals for null input.
98       */
99      @Test
100     void testEqualsNull() {
101         expNE(builder().create(), null);
102     }
103 
104     /**
105      * Tests equals with an object of another class.
106      */
107     @Test
108     void testEqualsOtherClass() {
109         expNE(builder().create(), this);
110     }
111 
112     /**
113      * Tests equals() if the expected result is true.
114      */
115     @Test
116     void testEqualsTrue() {
117         expEqual(DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS, DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS);
118         final DefaultExpressionEngineSymbols s2 = new DefaultExpressionEngineSymbols.Builder(DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS).create();
119         expEqual(DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS, s2);
120     }
121 
122     /**
123      * Tests the string representation.
124      */
125     @Test
126     void testToString() {
127         final DefaultExpressionEngineSymbols symbols = builder().create();
128         final String s = symbols.toString();
129         assertTrue(s.contains("propertyDelimiter=" + symbols.getPropertyDelimiter()));
130         assertTrue(s.contains("escapedDelimiter=" + symbols.getEscapedDelimiter()));
131         assertTrue(s.contains("indexStart=" + symbols.getIndexStart()));
132         assertTrue(s.contains("indexEnd=" + symbols.getIndexEnd()));
133         assertTrue(s.contains("attributeStart=" + symbols.getAttributeStart()));
134         assertTrue(s.contains("attributeEnd=" + symbols.getAttributeEnd()));
135     }
136 }