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