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",
031: "bc", "def", "g.h", "ij.k", "lm.no", "pqr.stu" };
032: private String[] validNames = new String[] { null, "", "a", "bc",
033: "def", "g", "ij", "lm", "pqr" };
034:
035: // Indexed Properties Test Data
036: private String[] validIndexProperties = new String[] { "a[1]",
037: "b[12]", "cd[3]", "ef[45]", "ghi[6]", "jkl[789]", };
038: private String[] validIndexNames = new String[] { "a", "b", "cd",
039: "ef", "ghi", "jkl" };
040: private int[] validIndexValues = new int[] { 1, 12, 3, 45, 6, 789 };
041:
042: // Mapped Properties Test Data
043: private String[] validMapProperties = new String[] { "a(b)",
044: "c(de)", "fg(h)", "ij(kl)", "mno(pqr.s)", "tuv(wx).yz[1]" };
045: private String[] validMapNames = new String[] { "a", "c", "fg",
046: "ij", "mno", "tuv" };
047: private String[] validMapKeys = new String[] { "b", "de", "h",
048: "kl", "pqr.s", "wx" };
049:
050: private String[] nextExpressions = new String[] { "a", "bc", "d.e",
051: "fg.h", "ij.kl", "m(12)", "no(3.4)", "pq(r).s", "t[12]",
052: "uv[34].wx" };
053: private String[] nextProperties = new String[] { "a", "bc", "d",
054: "fg", "ij", "m(12)", "no(3.4)", "pq(r)", "t[12]", "uv[34]" };
055: private String[] removeProperties = new String[] { null, null, "e",
056: "h", "kl", null, null, "s", null, "wx" };
057:
058: /**
059: * Construct a DefaultResolver Test Case.
060: * @param name The name of the test
061: */
062: public DefaultResolverTestCase(String name) {
063: super (name);
064: }
065:
066: // ------------------------------------------------------------------------
067:
068: /**
069: * Create Test Suite
070: * @return test suite
071: */
072: public static TestSuite suite() {
073: return new TestSuite(DefaultResolverTestCase.class);
074: }
075:
076: /**
077: * Set Up
078: */
079: protected void setUp() {
080: }
081:
082: /**
083: * Tear Down
084: */
085: protected void tearDown() {
086: }
087:
088: // ------------------------------------------------------------------------
089:
090: /**
091: * Test getIndex() method.
092: */
093: public void testGetIndex() {
094: String label = null;
095:
096: // Simple Properties (expect -1)
097: for (int i = 0; i < validProperties.length; i++) {
098: try {
099: label = "Simple " + label(validProperties[i], i);
100: assertEquals(label, -1, resolver
101: .getIndex(validProperties[i]));
102: } catch (Throwable t) {
103: fail(label + " threw " + t);
104: }
105: }
106:
107: // Indexed Properties (expect correct index value)
108: for (int i = 0; i < validIndexProperties.length; i++) {
109: try {
110: label = "Indexed " + label(validIndexProperties[i], i);
111: assertEquals(label, validIndexValues[i], resolver
112: .getIndex(validIndexProperties[i]));
113: } catch (Throwable t) {
114: fail(label + " threw " + t);
115: }
116: }
117:
118: // Mapped Properties (expect -1)
119: for (int i = 0; i < validMapProperties.length; i++) {
120: try {
121: label = "Mapped " + label(validMapProperties[i], i);
122: assertEquals(label, -1, resolver
123: .getIndex(validMapProperties[i]));
124: } catch (Throwable t) {
125: fail(label + " threw " + t);
126: }
127: }
128:
129: // Missing Index Value
130: label = "Missing Index";
131: try {
132: int index = resolver.getIndex("foo[]");
133: fail(label + " expected IllegalArgumentException: " + index);
134: } catch (IllegalArgumentException e) {
135: assertEquals(label + " Error Message", "No Index Value", e
136: .getMessage());
137: } catch (Throwable t) {
138: fail(label + " expected IllegalArgumentException: " + t);
139: }
140:
141: // Malformed
142: label = "Malformed";
143: try {
144: int index = resolver.getIndex("foo[12");
145: fail(label + " expected IllegalArgumentException: " + index);
146: } catch (IllegalArgumentException e) {
147: assertEquals(label + " Error Message",
148: "Missing End Delimiter", e.getMessage());
149: } catch (Throwable t) {
150: fail(label + " expected IllegalArgumentException: " + t);
151: }
152:
153: // Non-numeric
154: label = "Malformed";
155: try {
156: int index = resolver.getIndex("foo[BAR]");
157: fail(label + " expected IllegalArgumentException: " + index);
158: } catch (IllegalArgumentException e) {
159: assertEquals(label + " Error Message",
160: "Invalid index value 'BAR'", e.getMessage());
161: } catch (Throwable t) {
162: fail(label + " expected IllegalArgumentException: " + t);
163: }
164: }
165:
166: /**
167: * Test getMapKey() method.
168: */
169: public void testGetMapKey() {
170: String label = null;
171:
172: // Simple Properties (expect null)
173: for (int i = 0; i < validProperties.length; i++) {
174: try {
175: label = "Simple " + label(validProperties[i], i);
176: assertEquals(label, null, resolver
177: .getKey(validProperties[i]));
178: } catch (Throwable t) {
179: fail(label + " threw " + t);
180: }
181: }
182:
183: // Indexed Properties (expect null)
184: for (int i = 0; i < validIndexProperties.length; i++) {
185: try {
186: label = "Indexed " + label(validIndexProperties[i], i);
187: assertEquals(label, null, resolver
188: .getKey(validIndexProperties[i]));
189: } catch (Throwable t) {
190: fail(label + " threw " + t);
191: }
192: }
193:
194: // Mapped Properties (expect correct map key)
195: for (int i = 0; i < validMapProperties.length; i++) {
196: try {
197: label = "Mapped " + label(validMapProperties[i], i);
198: assertEquals(label, validMapKeys[i], resolver
199: .getKey(validMapProperties[i]));
200: } catch (Throwable t) {
201: fail(label + " threw " + t);
202: }
203: }
204:
205: // Malformed
206: label = "Malformed";
207: try {
208: String key = resolver.getKey("foo(bar");
209: fail(label + " expected IllegalArgumentException: " + key);
210: } catch (IllegalArgumentException e) {
211: assertEquals(label + " Error Message",
212: "Missing End Delimiter", e.getMessage());
213: } catch (Throwable t) {
214: fail(label + " expected IllegalArgumentException: " + t);
215: }
216: }
217:
218: /**
219: * Test isIndexed() method.
220: */
221: public void testIsIndexed() {
222: String label = null;
223:
224: // Simple Properties (expect -1)
225: for (int i = 0; i < validProperties.length; i++) {
226: try {
227: label = "Simple " + label(validProperties[i], i);
228: assertFalse(label, resolver
229: .isIndexed(validProperties[i]));
230: } catch (Throwable t) {
231: fail(label + " threw " + t);
232: }
233: }
234:
235: // Indexed Properties (expect correct index value)
236: for (int i = 0; i < validIndexProperties.length; i++) {
237: try {
238: label = "Indexed " + label(validIndexProperties[i], i);
239: assertTrue(label, resolver
240: .isIndexed(validIndexProperties[i]));
241: } catch (Throwable t) {
242: fail(label + " threw " + t);
243: }
244: }
245:
246: // Mapped Properties (expect -1)
247: for (int i = 0; i < validMapProperties.length; i++) {
248: try {
249: label = "Mapped " + label(validMapProperties[i], i);
250: assertFalse(label, resolver
251: .isIndexed(validMapProperties[i]));
252: } catch (Throwable t) {
253: fail(label + " threw " + t);
254: }
255: }
256: }
257:
258: /**
259: * Test isMapped() method.
260: */
261: public void testIsMapped() {
262: String label = null;
263:
264: // Simple Properties (expect null)
265: for (int i = 0; i < validProperties.length; i++) {
266: try {
267: label = "Simple " + label(validProperties[i], i);
268: assertFalse(label, resolver
269: .isMapped(validProperties[i]));
270: } catch (Throwable t) {
271: fail(label + " threw " + t);
272: }
273: }
274:
275: // Indexed Properties (expect null)
276: for (int i = 0; i < validIndexProperties.length; i++) {
277: try {
278: label = "Indexed " + label(validIndexProperties[i], i);
279: assertFalse(label, resolver
280: .isMapped(validIndexProperties[i]));
281: } catch (Throwable t) {
282: fail(label + " threw " + t);
283: }
284: }
285:
286: // Mapped Properties (expect correct map key)
287: for (int i = 0; i < validMapProperties.length; i++) {
288: try {
289: label = "Mapped " + label(validMapProperties[i], i);
290: assertTrue(label, resolver
291: .isMapped(validMapProperties[i]));
292: } catch (Throwable t) {
293: fail(label + " threw " + t);
294: }
295: }
296: }
297:
298: /**
299: * Test getName() method.
300: */
301: public void testGetName() {
302: String label = null;
303:
304: // Simple Properties
305: for (int i = 0; i < validProperties.length; i++) {
306: try {
307: label = "Simple " + label(validProperties[i], i);
308: assertEquals(label, validNames[i], resolver
309: .getProperty(validProperties[i]));
310: } catch (Throwable t) {
311: fail(label + " threw " + t);
312: }
313: }
314:
315: // Indexed Properties
316: for (int i = 0; i < validIndexProperties.length; i++) {
317: try {
318: label = "Indexed " + label(validIndexProperties[i], i);
319: assertEquals(label, validIndexNames[i], resolver
320: .getProperty(validIndexProperties[i]));
321: } catch (Throwable t) {
322: fail(label + " threw " + t);
323: }
324: }
325:
326: // Mapped Properties
327: for (int i = 0; i < validMapProperties.length; i++) {
328: try {
329: label = "Mapped " + label(validMapProperties[i], i);
330: assertEquals(label, validMapNames[i], resolver
331: .getProperty(validMapProperties[i]));
332: } catch (Throwable t) {
333: fail(label + " threw " + t);
334: }
335: }
336: }
337:
338: /**
339: * Test next() method.
340: */
341: public void testNext() {
342: String label = null;
343: for (int i = 0; i < nextExpressions.length; i++) {
344: try {
345: label = label(nextExpressions[i], i);
346: assertEquals(label, nextProperties[i], resolver
347: .next(nextExpressions[i]));
348: } catch (Throwable t) {
349: fail(label + " threw " + t);
350: }
351: }
352: }
353:
354: /**
355: * Test remove() method.
356: */
357: public void testRemove() {
358: String label = null;
359: for (int i = 0; i < nextExpressions.length; i++) {
360: try {
361: label = label(nextExpressions[i], i);
362: assertEquals(label, removeProperties[i], resolver
363: .remove(nextExpressions[i]));
364: } catch (Throwable t) {
365: fail(label + " threw " + t);
366: }
367: }
368: }
369:
370: private String label(String expression, int i) {
371: return "Expression[" + i + "]=\"" + expression + "\"";
372: }
373: }
|