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:
018: package org.apache.commons.beanutils;
019:
020: import junit.framework.TestCase;
021:
022: /**
023: * Test cases for <code>BeanPropertyValueEqualsPredicateTest</code>.
024: *
025: * @author Norm Deane
026: */
027: public class BeanPropertyValueEqualsPredicateTestCase extends TestCase {
028:
029: private static final Integer expectedIntegerValue = new Integer(123);
030: private static final Float expectedFloatValue = new Float(123.123f);
031: private static final Double expectedDoubleValue = new Double(
032: 567879.12344d);
033: private static final Boolean expectedBooleanValue = Boolean.TRUE;
034: private static final Byte expectedByteValue = new Byte("12");
035:
036: /**
037: * Constructor for BeanPropertyValueEqualsPredicateTest.
038: *
039: * @param name Name of this test case.
040: */
041: public BeanPropertyValueEqualsPredicateTestCase(String name) {
042: super (name);
043: }
044:
045: /**
046: * Test evaluate with simple String property.
047: */
048: public void testEvaluateWithSimpleStringProperty() {
049: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
050: "stringProperty", "foo");
051: assertTrue(predicate.evaluate(new TestBean("foo")));
052: assertTrue(!predicate.evaluate(new TestBean("bar")));
053: }
054:
055: /**
056: * Test evaluate with simple String property and null values.
057: */
058: public void testEvaluateWithSimpleStringPropertyWithNullValues() {
059: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
060: "stringProperty", null);
061: assertTrue(predicate.evaluate(new TestBean((String) null)));
062: assertTrue(!predicate.evaluate(new TestBean("bar")));
063: }
064:
065: /**
066: * Test evaluate with nested property.
067: */
068: public void testEvaluateWithNestedProperty() {
069: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
070: "anotherNested.stringProperty", "match");
071: TestBean testBean = new TestBean();
072: TestBean nestedBean = new TestBean("match");
073: testBean.setAnotherNested(nestedBean);
074: assertTrue(predicate.evaluate(testBean));
075: testBean.setAnotherNested(new TestBean("no-match"));
076: assertTrue(!predicate.evaluate(testBean));
077: }
078:
079: /**
080: * Test evaluate with null in property path and ignore=false.
081: */
082: public void testEvaluateWithNullInPath() {
083: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
084: "anotherNested.stringProperty", "foo");
085: try {
086: // try to evaluate the predicate
087: predicate.evaluate(new TestBean());
088: fail("Should have throw IllegalArgumentException");
089: } catch (IllegalArgumentException e) {
090: /* ignore this is what should happen */
091: }
092: }
093:
094: /**
095: * Test evaluate with null in property path and ignore=true.
096: */
097: public void testEvaluateWithNullInPathAndIgnoreTrue() {
098: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
099: "anotherNested.stringProperty", "foo", true);
100: try {
101: assertTrue(!predicate.evaluate(new TestBean()));
102: } catch (IllegalArgumentException e) {
103: fail("Should not have throw IllegalArgumentException");
104: }
105: }
106:
107: /**
108: * Test evaluate with int property.
109: */
110: public void testEvaluateWithIntProperty() {
111: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
112: "intProperty", expectedIntegerValue);
113: assertTrue(predicate.evaluate(new TestBean(expectedIntegerValue
114: .intValue())));
115: assertTrue(!predicate.evaluate(new TestBean(
116: expectedIntegerValue.intValue() - 1)));
117: }
118:
119: /**
120: * Test evaluate with float property.
121: */
122: public void testEvaluateWithFloatProperty() {
123: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
124: "floatProperty", expectedFloatValue);
125: assertTrue(predicate.evaluate(new TestBean(expectedFloatValue
126: .floatValue())));
127: assertTrue(!predicate.evaluate(new TestBean(expectedFloatValue
128: .floatValue() - 1)));
129: }
130:
131: /**
132: * Test evaluate with double property.
133: */
134: public void testEvaluateWithDoubleProperty() {
135: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
136: "doubleProperty", expectedDoubleValue);
137: assertTrue(predicate.evaluate(new TestBean(expectedDoubleValue
138: .doubleValue())));
139: assertTrue(!predicate.evaluate(new TestBean(expectedDoubleValue
140: .doubleValue() - 1)));
141: }
142:
143: /**
144: * Test evaluate with boolean property.
145: */
146: public void testEvaluateWithBooleanProperty() {
147: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
148: "booleanProperty", expectedBooleanValue);
149: assertTrue(predicate.evaluate(new TestBean(expectedBooleanValue
150: .booleanValue())));
151: assertTrue(!predicate.evaluate(new TestBean(
152: !expectedBooleanValue.booleanValue())));
153: }
154:
155: /**
156: * Test evaluate with byte property.
157: */
158: public void testEvaluateWithByteProperty() {
159: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
160: "byteProperty", expectedByteValue);
161: TestBean testBean = new TestBean();
162: testBean.setByteProperty(expectedByteValue.byteValue());
163: assertTrue(predicate.evaluate(testBean));
164: testBean
165: .setByteProperty((byte) (expectedByteValue.byteValue() - 1));
166: assertTrue(!predicate.evaluate(testBean));
167: }
168:
169: /**
170: * Test evaluate with mapped property.
171: */
172: public void testEvaluateWithMappedProperty() {
173: // try a key that is in the map
174: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
175: "mappedProperty(test-key)", "match");
176: TestBean testBean = new TestBean();
177: testBean.setMappedProperty("test-key", "match");
178: assertTrue(predicate.evaluate(testBean));
179: testBean.setMappedProperty("test-key", "no-match");
180: assertTrue(!predicate.evaluate(testBean));
181:
182: // try a key that isn't in the map
183: predicate = new BeanPropertyValueEqualsPredicate(
184: "mappedProperty(invalid-key)", "match");
185: assertTrue(!predicate.evaluate(testBean));
186: }
187:
188: /**
189: * Test evaluate with indexed property.
190: */
191: public void testEvaluateWithIndexedProperty() {
192: // try a valid index
193: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
194: "intIndexed[0]", expectedIntegerValue);
195: TestBean testBean = new TestBean();
196: testBean.setIntIndexed(0, expectedIntegerValue.intValue());
197: assertTrue(predicate.evaluate(testBean));
198: testBean.setIntIndexed(0, expectedIntegerValue.intValue() - 1);
199: assertTrue(!predicate.evaluate(testBean));
200:
201: // try an invalid index
202: predicate = new BeanPropertyValueEqualsPredicate(
203: "intIndexed[999]", "exception-ahead");
204:
205: try {
206: assertTrue(!predicate.evaluate(testBean));
207: } catch (ArrayIndexOutOfBoundsException e) {
208: /* this is what should happen */
209: }
210: }
211:
212: /**
213: * Test evaluate with primitive property and null value.
214: */
215: public void testEvaluateWithPrimitiveAndNull() {
216: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
217: "intProperty", null);
218: assertTrue(!predicate.evaluate(new TestBean(0)));
219:
220: predicate = new BeanPropertyValueEqualsPredicate(
221: "booleanProperty", null);
222: assertTrue(!predicate.evaluate(new TestBean(true)));
223:
224: predicate = new BeanPropertyValueEqualsPredicate(
225: "floatProperty", null);
226: assertTrue(!predicate.evaluate(new TestBean(expectedFloatValue
227: .floatValue())));
228: }
229:
230: /**
231: * Test evaluate with nested mapped property.
232: */
233: public void testEvaluateWithNestedMappedProperty() {
234: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
235: "anotherNested.mappedProperty(test-key)", "match");
236: TestBean testBean = new TestBean();
237: TestBean nestedBean = new TestBean();
238: nestedBean.setMappedProperty("test-key", "match");
239: testBean.setAnotherNested(nestedBean);
240: assertTrue(predicate.evaluate(testBean));
241: nestedBean.setMappedProperty("test-key", "no-match");
242: assertTrue(!predicate.evaluate(testBean));
243: }
244:
245: /**
246: * Test evaluate with write only property.
247: */
248: public void testEvaluateWithWriteOnlyProperty() {
249: try {
250: new BeanPropertyValueEqualsPredicate("writeOnlyProperty",
251: null).evaluate(new TestBean());
252: } catch (IllegalArgumentException e) {
253: /* This is what should happen */
254: }
255: }
256:
257: /**
258: * Test evaluate with read only property.
259: */
260: public void testEvaluateWithReadOnlyProperty() {
261: TestBean testBean = new TestBean();
262: BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate(
263: "readOnlyProperty", testBean.getReadOnlyProperty());
264: assertTrue(predicate.evaluate(new TestBean()));
265: }
266:
267: /**
268: * Test evaluate with an invalid property name.
269: */
270: public void testEvaluateWithInvalidPropertyName() {
271: try {
272: new BeanPropertyValueEqualsPredicate("bogusProperty", null)
273: .evaluate(new TestBean());
274: } catch (IllegalArgumentException e) {
275: /* This is what should happen */
276: }
277: }
278: }
|