001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */ 
017    package org.apache.commons.beanutils.expression;
018    
019    import junit.framework.TestCase;
020    import junit.framework.TestSuite;
021    
022    /**
023     * Junit Test for BasicResolver.
024     */
025    public class DefaultResolverTestCase extends TestCase {
026    
027        private DefaultResolver resolver = new DefaultResolver();
028    
029        // Simple Properties Test Data
030        private String[] validProperties = new String[] {null, "", "a", "bc", "def", "g.h", "ij.k", "lm.no", "pqr.stu"};
031        private String[] validNames      = new String[] {null, "", "a", "bc", "def", "g",   "ij",   "lm",    "pqr"};
032    
033        // Indexed Properties Test Data
034        private String[] validIndexProperties = new String[] {"a[1]", "b[12]", "cd[3]", "ef[45]", "ghi[6]", "jkl[789]", };
035        private String[] validIndexNames      = new String[] {"a",    "b",     "cd",    "ef",     "ghi",    "jkl"};
036        private int[]    validIndexValues     = new int[]    {1,      12,      3,       45,       6,        789};
037        
038        // Mapped Properties Test Data
039        private String[] validMapProperties = new String[] {"a(b)", "c(de)", "fg(h)", "ij(kl)", "mno(pqr.s)", "tuv(wx).yz[1]"};
040        private String[] validMapNames      = new String[] {"a",    "c",     "fg",    "ij",     "mno",        "tuv"};
041        private String[] validMapKeys       = new String[] {"b",    "de",    "h",     "kl",     "pqr.s",      "wx"};
042    
043        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"};
044        private String[] nextProperties    = new String[] {"a", "bc", "d",   "fg",   "ij",    "m(12)", "no(3.4)", "pq(r)",   "t[12]", "uv[34]"};
045        private String[] removeProperties  = new String[] {null, null, "e",  "h",    "kl",    null,    null,      "s",       null,    "wx"};
046    
047        /**
048         * Construct a DefaultResolver Test Case.
049         * @param name The name of the test
050         */
051        public DefaultResolverTestCase(String name) {
052            super(name);
053        }
054    
055        // ------------------------------------------------------------------------
056    
057        /**
058         * Create Test Suite
059         * @return test suite
060         */
061        public static TestSuite suite() {
062            return new TestSuite(DefaultResolverTestCase.class);        
063        }
064    
065        /**
066         * Set Up
067         */
068        protected void setUp() {
069        }
070    
071        /**
072         * Tear Down
073         */
074        protected void tearDown() {
075        }
076    
077        // ------------------------------------------------------------------------
078    
079        /**
080         * Test getIndex() method.
081         */
082        public void testGetIndex() {
083            String label = null;
084    
085            // Simple Properties (expect -1)
086            for (int i = 0; i < validProperties.length; i++) {
087                try {
088                    label = "Simple " + label(validProperties[i], i);
089                    assertEquals(label, -1, resolver.getIndex(validProperties[i]));
090                } catch (Throwable t) {
091                    fail(label + " threw " + t);
092                }
093            }
094    
095            // Indexed Properties (expect correct index value)
096            for (int i = 0; i < validIndexProperties.length; i++) {
097                try {
098                    label = "Indexed " + label(validIndexProperties[i], i);
099                    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    }