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