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.beanutils2.expression;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import org.junit.jupiter.api.AfterEach;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Junit Test for BasicResolver.
30   */
31  public class DefaultResolverTest {
32  
33      private final DefaultResolver resolver = new DefaultResolver();
34      // Simple Properties Test Data
35      private final String[] validProperties = { null, "", "a", "bc", "def", "g.h", "ij.k", "lm.no", "pqr.stu" };
36  
37      private final String[] validNames = { null, "", "a", "bc", "def", "g", "ij", "lm", "pqr" };
38      // Indexed Properties Test Data
39      private final String[] validIndexProperties = { "a[1]", "b[12]", "cd[3]", "ef[45]", "ghi[6]", "jkl[789]", };
40      private final String[] validIndexNames = { "a", "b", "cd", "ef", "ghi", "jkl" };
41  
42      private final int[] validIndexValues = { 1, 12, 3, 45, 6, 789 };
43      // Mapped Properties Test Data
44      private final String[] validMapProperties = { "a(b)", "c(de)", "fg(h)", "ij(kl)", "mno(pqr.s)", "tuv(wx).yz[1]" };
45      private final String[] validMapNames = { "a", "c", "fg", "ij", "mno", "tuv" };
46  
47      private final String[] validMapKeys = { "b", "de", "h", "kl", "pqr.s", "wx" };
48      private final String[] nextExpressions = { "a", "bc", "d.e", "fg.h", "ij.kl", "m(12)", "no(3.4)", "pq(r).s", "t[12]", "uv[34].wx" };
49      private final String[] nextProperties = { "a", "bc", "d", "fg", "ij", "m(12)", "no(3.4)", "pq(r)", "t[12]", "uv[34]" };
50  
51      private final String[] removeProperties = { null, null, "e", "h", "kl", null, null, "s", null, "wx" };
52  
53      private String label(final String expression, final int i) {
54          return "Expression[" + i + "]=\"" + expression + "\"";
55      }
56  
57      /**
58       * Sets Up
59       */
60      @BeforeEach
61      protected void setUp() {
62      }
63  
64      /**
65       * Tear Down
66       */
67      @AfterEach
68      protected void tearDown() {
69      }
70  
71      /**
72       * Test getIndex() method.
73       */
74      @Test
75      public void testGetIndex() throws Exception {
76          String label = null;
77  
78          // Simple Properties (expect -1)
79          for (int i = 0; i < validProperties.length; i++) {
80              label = "Simple " + label(validProperties[i], i);
81              assertEquals(-1, resolver.getIndex(validProperties[i]), label);
82          }
83  
84          // Indexed Properties (expect correct index value)
85          for (int i = 0; i < validIndexProperties.length; i++) {
86              label = "Indexed " + label(validIndexProperties[i], i);
87              assertEquals(validIndexValues[i], resolver.getIndex(validIndexProperties[i]), label);
88          }
89  
90          // Mapped Properties (expect -1)
91          for (int i = 0; i < validMapProperties.length; i++) {
92              label = "Mapped " + label(validMapProperties[i], i);
93              assertEquals(-1, resolver.getIndex(validMapProperties[i]), label);
94          }
95  
96          // Missing Index Value
97          label = "Missing Index";
98          assertThrows(IllegalArgumentException.class, () -> resolver.getIndex("foo[]"));
99  
100         // Malformed
101         label = "Malformed";
102         assertThrows(IllegalArgumentException.class, () -> resolver.getIndex("foo[12"));
103 
104         // Non-numeric
105         label = "Malformed";
106         final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> resolver.getIndex("foo[BAR]"));
107         assertEquals("Invalid index value 'BAR'", e.getMessage(), label + " Error Message");
108     }
109 
110     /**
111      * Test getMapKey() method.
112      */
113     @Test
114     public void testGetMapKey() {
115         String label = null;
116 
117         // Simple Properties (expect null)
118         for (int i = 0; i < validProperties.length; i++) {
119             label = "Simple " + label(validProperties[i], i);
120             assertEquals(null, resolver.getKey(validProperties[i]), label);
121         }
122 
123         // Indexed Properties (expect null)
124         for (int i = 0; i < validIndexProperties.length; i++) {
125             label = "Indexed " + label(validIndexProperties[i], i);
126             assertEquals(null, resolver.getKey(validIndexProperties[i]), label);
127         }
128 
129         // Mapped Properties (expect correct map key)
130         for (int i = 0; i < validMapProperties.length; i++) {
131             label = "Mapped " + label(validMapProperties[i], i);
132             assertEquals(validMapKeys[i], resolver.getKey(validMapProperties[i]), label);
133         }
134 
135         // Malformed
136         label = "Malformed";
137         final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> resolver.getKey("foo(bar"));
138         assertEquals("Missing End Delimiter", e.getMessage(), label + " Error Message");
139     }
140 
141     /**
142      * Test getName() method.
143      */
144     @Test
145     public void testGetName() {
146         String label = null;
147 
148         // Simple Properties
149         for (int i = 0; i < validProperties.length; i++) {
150             label = "Simple " + label(validProperties[i], i);
151             assertEquals(validNames[i], resolver.getProperty(validProperties[i]), label);
152         }
153 
154         // Indexed Properties
155         for (int i = 0; i < validIndexProperties.length; i++) {
156             label = "Indexed " + label(validIndexProperties[i], i);
157             assertEquals(validIndexNames[i], resolver.getProperty(validIndexProperties[i]), label);
158         }
159 
160         // Mapped Properties
161         for (int i = 0; i < validMapProperties.length; i++) {
162             label = "Mapped " + label(validMapProperties[i], i);
163             assertEquals(validMapNames[i], resolver.getProperty(validMapProperties[i]), label);
164         }
165     }
166 
167     /**
168      * Test isIndexed() method.
169      */
170     @Test
171     public void testIsIndexed() {
172         String label = null;
173 
174         // Simple Properties (expect -1)
175         for (int i = 0; i < validProperties.length; i++) {
176             label = "Simple " + label(validProperties[i], i);
177             assertFalse(resolver.isIndexed(validProperties[i]), label);
178         }
179 
180         // Indexed Properties (expect correct index value)
181         for (int i = 0; i < validIndexProperties.length; i++) {
182             label = "Indexed " + label(validIndexProperties[i], i);
183             assertTrue(resolver.isIndexed(validIndexProperties[i]), label);
184         }
185 
186         // Mapped Properties (expect -1)
187         for (int i = 0; i < validMapProperties.length; i++) {
188             label = "Mapped " + label(validMapProperties[i], i);
189             assertFalse(resolver.isIndexed(validMapProperties[i]), label);
190         }
191     }
192 
193     /**
194      * Test isMapped() method.
195      */
196     @Test
197     public void testIsMapped() {
198         String label = null;
199 
200         // Simple Properties (expect null)
201         for (int i = 0; i < validProperties.length; i++) {
202             label = "Simple " + label(validProperties[i], i);
203             assertFalse(resolver.isMapped(validProperties[i]), label);
204         }
205 
206         // Indexed Properties (expect null)
207         for (int i = 0; i < validIndexProperties.length; i++) {
208             label = "Indexed " + label(validIndexProperties[i], i);
209             assertFalse(resolver.isMapped(validIndexProperties[i]), label);
210         }
211 
212         // Mapped Properties (expect correct map key)
213         for (int i = 0; i < validMapProperties.length; i++) {
214             label = "Mapped " + label(validMapProperties[i], i);
215             assertTrue(resolver.isMapped(validMapProperties[i]), label);
216         }
217     }
218 
219     /**
220      * Test next() method.
221      */
222     @Test
223     public void testNext() {
224         String label = null;
225         for (int i = 0; i < nextExpressions.length; i++) {
226             label = label(nextExpressions[i], i);
227             assertEquals(nextProperties[i], resolver.next(nextExpressions[i]), label);
228         }
229     }
230 
231     /**
232      * Test remove() method.
233      */
234     @Test
235     public void testRemove() {
236         String label = null;
237         for (int i = 0; i < nextExpressions.length; i++) {
238             label = label(nextExpressions[i], i);
239             assertEquals(removeProperties[i], resolver.remove(nextExpressions[i]), label);
240         }
241     }
242 }