001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.binding.tests;
032:
033: import java.beans.IntrospectionException;
034: import java.beans.PropertyDescriptor;
035: import java.beans.PropertyVetoException;
036:
037: import junit.framework.TestCase;
038:
039: import com.jgoodies.binding.beans.BeanUtils;
040: import com.jgoodies.binding.tests.beans.BeanClasses;
041: import com.jgoodies.binding.tests.beans.TestBean;
042: import com.jgoodies.binding.tests.beans.VetoableChangeRejector;
043:
044: /**
045: * A test case for class {@link BeanUtils}.
046: *
047: * @author Karsten Lentzsch
048: * @version $Revision: 1.14 $
049: */
050: public final class BeanUtilsTest extends TestCase {
051:
052: /**
053: * Checks that #supportsBoundProperties detects observable classes.
054: */
055: public void testDetectObservableClasses() {
056: for (Class<?> beanClass : BeanClasses.getObservableClasses()) {
057: assertTrue(
058: "Could not detect that the class supports bound properties.",
059: BeanUtils.supportsBoundProperties(beanClass));
060: }
061: }
062:
063: /**
064: * Checks that #supportsBoundProperties rejects unobservable classes.
065: */
066: public void testRejectUnobservableClasses() {
067: for (Class<?> beanClass : BeanClasses.getUnobservableClasses()) {
068: assertFalse(
069: "Failed to reject a class that supports no bound properties.",
070: BeanUtils.supportsBoundProperties(beanClass));
071: }
072: }
073:
074: public void testWriteConstrainedProperty() {
075: TestBean bean = new TestBean();
076: try {
077: bean.setConstrainedProperty("value1");
078: } catch (PropertyVetoException e1) {
079: fail("Couldn't set the valid value1.");
080: }
081: assertEquals("Bean has the initial value1.", bean
082: .getConstrainedProperty(), "value1");
083: PropertyDescriptor descriptor = null;
084: try {
085: descriptor = BeanUtils.getPropertyDescriptor(bean
086: .getClass(), "constrainedProperty");
087: try {
088: BeanUtils.setValue(bean, descriptor, "value2");
089: } catch (PropertyVetoException e) {
090: fail("No PropertyVetoException shall be thrown if there's no VetoableChangeListener.");
091: }
092: assertEquals("Bean now has the value2.", bean
093: .getConstrainedProperty(), "value2");
094: bean
095: .addVetoableChangeListener(new VetoableChangeRejector());
096: try {
097: BeanUtils.setValue(bean, descriptor, "value3");
098: fail("Setting a value that will be vetoed must throw an exception.");
099: } catch (PropertyVetoException e) {
100: // The expected behavior.
101: }
102: assertEquals("Bean still has the value2.", bean
103: .getConstrainedProperty(), "value2");
104: } catch (IntrospectionException e) {
105: fail("Couldn't look up the descriptor for the constrained property.");
106: }
107: }
108:
109: }
|