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.beanutils.expression;
18  
19  import junit.framework.TestCase;
20  import junit.framework.TestSuite;
21  
22  /**
23   * Junit Test for BasicResolver.
24   *
25   * @version $Id$
26   */
27  public class DefaultResolverTestCase extends TestCase {
28  
29      private final DefaultResolver resolver = new DefaultResolver();
30  
31      // Simple Properties Test Data
32      private final String[] validProperties = new String[] {null, "", "a", "bc", "def", "g.h", "ij.k", "lm.no", "pqr.stu"};
33      private final String[] validNames      = new String[] {null, "", "a", "bc", "def", "g",   "ij",   "lm",    "pqr"};
34  
35      // Indexed Properties Test Data
36      private final String[] validIndexProperties = new String[] {"a[1]", "b[12]", "cd[3]", "ef[45]", "ghi[6]", "jkl[789]", };
37      private final String[] validIndexNames      = new String[] {"a",    "b",     "cd",    "ef",     "ghi",    "jkl"};
38      private final int[]    validIndexValues     = new int[]    {1,      12,      3,       45,       6,        789};
39  
40      // Mapped Properties Test Data
41      private final String[] validMapProperties = new String[] {"a(b)", "c(de)", "fg(h)", "ij(kl)", "mno(pqr.s)", "tuv(wx).yz[1]"};
42      private final String[] validMapNames      = new String[] {"a",    "c",     "fg",    "ij",     "mno",        "tuv"};
43      private final String[] validMapKeys       = new String[] {"b",    "de",    "h",     "kl",     "pqr.s",      "wx"};
44  
45      private final String[] nextExpressions   = new String[] {"a", "bc", "d.e", "fg.h", "ij.kl", "m(12)", "no(3.4)", "pq(r).s", "t[12]", "uv[34].wx"};
46      private final String[] nextProperties    = new String[] {"a", "bc", "d",   "fg",   "ij",    "m(12)", "no(3.4)", "pq(r)",   "t[12]", "uv[34]"};
47      private final String[] removeProperties  = new String[] {null, null, "e",  "h",    "kl",    null,    null,      "s",       null,    "wx"};
48  
49      /**
50       * Construct a DefaultResolver Test Case.
51       * @param name The name of the test
52       */
53      public DefaultResolverTestCase(final String name) {
54          super(name);
55      }
56  
57      // ------------------------------------------------------------------------
58  
59      /**
60       * Create Test Suite
61       * @return test suite
62       */
63      public static TestSuite suite() {
64          return new TestSuite(DefaultResolverTestCase.class);
65      }
66  
67      /**
68       * Set Up
69       */
70      @Override
71      protected void setUp() {
72      }
73  
74      /**
75       * Tear Down
76       */
77      @Override
78      protected void tearDown() {
79      }
80  
81      // ------------------------------------------------------------------------
82  
83      /**
84       * Test getIndex() method.
85       */
86      public void testGetIndex() {
87          String label = null;
88  
89          // Simple Properties (expect -1)
90          for (int i = 0; i < validProperties.length; i++) {
91              try {
92                  label = "Simple " + label(validProperties[i], i);
93                  assertEquals(label, -1, resolver.getIndex(validProperties[i]));
94              } catch (final Throwable t) {
95                  fail(label + " threw " + t);
96              }
97          }
98  
99          // Indexed Properties (expect correct index value)
100         for (int i = 0; i < validIndexProperties.length; i++) {
101             try {
102                 label = "Indexed " + label(validIndexProperties[i], i);
103                 assertEquals(label, validIndexValues[i], resolver.getIndex(validIndexProperties[i]));
104             } catch (final Throwable t) {
105                 fail(label + " threw " + t);
106             }
107         }
108 
109         // Mapped Properties (expect -1)
110         for (int i = 0; i < validMapProperties.length; i++) {
111             try {
112                 label = "Mapped " + label(validMapProperties[i], i);
113                 assertEquals(label, -1, resolver.getIndex(validMapProperties[i]));
114             } catch (final Throwable t) {
115                 fail(label + " threw " + t);
116             }
117         }
118 
119         // Missing Index Value
120         label = "Missing Index";
121         try {
122             final int index  = resolver.getIndex("foo[]");
123             fail(label + " expected IllegalArgumentException: " + index);
124         } catch (final IllegalArgumentException e) {
125             assertEquals(label + " Error Message", "No Index Value", e.getMessage());
126         } catch (final Throwable t) {
127             fail(label + " expected IllegalArgumentException: " + t);
128         }
129 
130         // Malformed
131         label = "Malformed";
132         try {
133             final int index  = resolver.getIndex("foo[12");
134             fail(label + " expected IllegalArgumentException: " + index);
135         } catch (final IllegalArgumentException e) {
136             assertEquals(label + " Error Message", "Missing End Delimiter", e.getMessage());
137         } catch (final Throwable t) {
138             fail(label + " expected IllegalArgumentException: " + t);
139         }
140 
141         // Non-numeric
142         label = "Malformed";
143         try {
144             final int index  = resolver.getIndex("foo[BAR]");
145             fail(label + " expected IllegalArgumentException: " + index);
146         } catch (final IllegalArgumentException e) {
147             assertEquals(label + " Error Message", "Invalid index value 'BAR'", e.getMessage());
148         } catch (final Throwable t) {
149             fail(label + " expected IllegalArgumentException: " + t);
150         }
151     }
152 
153     /**
154      * Test getMapKey() method.
155      */
156     public void testGetMapKey() {
157         String label = null;
158 
159         // Simple Properties (expect null)
160         for (int i = 0; i < validProperties.length; i++) {
161             try {
162                 label = "Simple " + label(validProperties[i], i);
163                 assertEquals(label, null, resolver.getKey(validProperties[i]));
164             } catch (final Throwable t) {
165                 fail(label + " threw " + t);
166             }
167         }
168 
169         // Indexed Properties (expect null)
170         for (int i = 0; i < validIndexProperties.length; i++) {
171             try {
172                 label = "Indexed " + label(validIndexProperties[i], i);
173                 assertEquals(label, null, resolver.getKey(validIndexProperties[i]));
174             } catch (final Throwable t) {
175                 fail(label + " threw " + t);
176             }
177         }
178 
179         // Mapped Properties (expect correct map key)
180         for (int i = 0; i < validMapProperties.length; i++) {
181             try {
182                 label = "Mapped " + label(validMapProperties[i], i);
183                 assertEquals(label, validMapKeys[i], resolver.getKey(validMapProperties[i]));
184             } catch (final Throwable t) {
185                 fail(label + " threw " + t);
186             }
187         }
188 
189         // Malformed
190         label = "Malformed";
191         try {
192             final String key  = resolver.getKey("foo(bar");
193             fail(label + " expected IllegalArgumentException: " + key);
194         } catch (final IllegalArgumentException e) {
195             assertEquals(label + " Error Message", "Missing End Delimiter", e.getMessage());
196         } catch (final Throwable t) {
197             fail(label + " expected IllegalArgumentException: " + t);
198         }
199  }
200 
201     /**
202      * Test isIndexed() method.
203      */
204     public void testIsIndexed() {
205         String label = null;
206 
207         // Simple Properties (expect -1)
208         for (int i = 0; i < validProperties.length; i++) {
209             try {
210                 label = "Simple " + label(validProperties[i], i);
211                 assertFalse(label, resolver.isIndexed(validProperties[i]));
212             } catch (final Throwable t) {
213                 fail(label + " threw " + t);
214             }
215         }
216 
217         // Indexed Properties (expect correct index value)
218         for (int i = 0; i < validIndexProperties.length; i++) {
219             try {
220                 label = "Indexed " + label(validIndexProperties[i], i);
221                 assertTrue(label, resolver.isIndexed(validIndexProperties[i]));
222             } catch (final Throwable t) {
223                 fail(label + " threw " + t);
224             }
225         }
226 
227         // Mapped Properties (expect -1)
228         for (int i = 0; i < validMapProperties.length; i++) {
229             try {
230                 label = "Mapped " + label(validMapProperties[i], i);
231                 assertFalse(label, resolver.isIndexed(validMapProperties[i]));
232             } catch (final Throwable t) {
233                 fail(label + " threw " + t);
234             }
235         }
236     }
237 
238     /**
239      * Test isMapped() method.
240      */
241     public void testIsMapped() {
242         String label = null;
243 
244         // Simple Properties (expect null)
245         for (int i = 0; i < validProperties.length; i++) {
246             try {
247                 label = "Simple " + label(validProperties[i], i);
248                 assertFalse(label, resolver.isMapped(validProperties[i]));
249             } catch (final Throwable t) {
250                 fail(label + " threw " + t);
251             }
252         }
253 
254         // Indexed Properties (expect null)
255         for (int i = 0; i < validIndexProperties.length; i++) {
256             try {
257                 label = "Indexed " + label(validIndexProperties[i], i);
258                 assertFalse(label, resolver.isMapped(validIndexProperties[i]));
259             } catch (final Throwable t) {
260                 fail(label + " threw " + t);
261             }
262         }
263 
264         // Mapped Properties (expect correct map key)
265         for (int i = 0; i < validMapProperties.length; i++) {
266             try {
267                 label = "Mapped " + label(validMapProperties[i], i);
268                 assertTrue(label, resolver.isMapped(validMapProperties[i]));
269             } catch (final Throwable t) {
270                 fail(label + " threw " + t);
271             }
272         }
273     }
274 
275     /**
276      * Test getName() method.
277      */
278     public void testGetName() {
279         String label = null;
280 
281         // Simple Properties
282         for (int i = 0; i < validProperties.length; i++) {
283             try {
284                 label = "Simple " + label(validProperties[i], i);
285                 assertEquals(label, validNames[i], resolver.getProperty(validProperties[i]));
286             } catch (final Throwable t) {
287                 fail(label + " threw " + t);
288             }
289         }
290 
291         // Indexed Properties
292         for (int i = 0; i < validIndexProperties.length; i++) {
293             try {
294                 label = "Indexed " + label(validIndexProperties[i], i);
295                 assertEquals(label, validIndexNames[i], resolver.getProperty(validIndexProperties[i]));
296             } catch (final Throwable t) {
297                 fail(label + " threw " + t);
298             }
299         }
300 
301         // Mapped Properties
302         for (int i = 0; i < validMapProperties.length; i++) {
303             try {
304                 label = "Mapped " + label(validMapProperties[i], i);
305                 assertEquals(label, validMapNames[i], resolver.getProperty(validMapProperties[i]));
306             } catch (final Throwable t) {
307                 fail(label + " threw " + t);
308             }
309         }
310     }
311 
312     /**
313      * Test next() method.
314      */
315     public void testNext() {
316         String label = null;
317         for (int i = 0; i < nextExpressions.length; i++) {
318             try {
319                 label = label(nextExpressions[i], i);
320                 assertEquals(label, nextProperties[i], resolver.next(nextExpressions[i]));
321             } catch (final Throwable t) {
322                 fail(label + " threw " + t);
323             }
324         }
325     }
326 
327     /**
328      * Test remove() method.
329      */
330     public void testRemove() {
331         String label = null;
332         for (int i = 0; i < nextExpressions.length; i++) {
333             try {
334                 label = label(nextExpressions[i], i);
335                 assertEquals(label, removeProperties[i], resolver.remove(nextExpressions[i]));
336             } catch (final Throwable t) {
337                 fail(label + " threw " + t);
338             }
339         }
340     }
341 
342     private String label(final String expression, final int i) {
343         return "Expression[" + i + "]=\"" + expression + "\"";
344     }
345 }