/*
 * Copyright (c) 2002-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *  o Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer. 
 *     
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution. 
 *     
 *  o Neither the name of JGoodies Karsten Lentzsch nor the names of 
 *    its contributors may be used to endorse or promote products derived 
 *    from this software without specific prior written permission. 
 *     
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

package com.jgoodies.binding.tests;

import junit.framework.TestCase;

import com.jgoodies.binding.beans.PropertyConnector;
import com.jgoodies.binding.tests.beans.TestBean;

/**
 * A test case for class {@link PropertyConnector}.
 * 
 * @author  Karsten Lentzsch
 * @version $Revision: 1.9 $
 */
public final class PropertyConnectorTest extends TestCase {
    
    private PropertyConnector createDefaultConnector(TestBean bean1, TestBean bean2) {
        return new PropertyConnector(
            bean1,
            "readWriteObjectProperty",
            bean2,
            "readWriteObjectProperty");
    }

    private PropertyConnector createDefaultConnector(TestBean bean1, Object initialValue1, TestBean bean2, Object initialValue2) {
        bean1.setReadWriteObjectProperty(initialValue1);
        bean2.setReadWriteObjectProperty(initialValue2);
        return createDefaultConnector(bean1, bean2);
    }


    // Constructor Tests ******************************************************

    public void testConstructorRejectsNullParameters() {
        TestBean bean1;
        TestBean bean2;
        bean1 = new TestBean();
        bean2 = new TestBean();
        try {
            new PropertyConnector(null, "readWriteObjectProperty", bean2, "readWriteObjectProperty");
            fail("Constructor failed to reject bean1==null.");
        } catch (NullPointerException ex) {
            // The expected behavior
        }
        bean1 = new TestBean();
        bean2 = new TestBean();
        try {
            new PropertyConnector(bean1, null, bean2, "readWriteObjectProperty");
            fail("Constructor failed to reject property1Name==null.");
        } catch (NullPointerException ex) {
            // The expected behavior
        }
        bean1 = new TestBean();
        bean2 = new TestBean();
        try {
            new PropertyConnector(bean1, "readWriteObjectProperty", null, "readWriteObjectProperty");
            fail("Constructor failed to reject bean2==null.");
        } catch (NullPointerException ex) {
            // The expected behavior
        }
        bean1 = new TestBean();
        bean2 = new TestBean();
        try {
            new PropertyConnector(bean1, "readWriteObjectProperty", bean2, null);
            fail("Constructor failed to reject property2Name==null.");
        } catch (NullPointerException ex) {
            // The expected behavior
        }
    }

    public void testConstructorRejectsToConnectTheSameProperty() {
        TestBean bean = new TestBean();
        try {
            new PropertyConnector(bean, "readWriteObjectProperty", bean, "readWriteObjectProperty");
            fail("Constructor failed to reject bean1==bean2 && property1Name.equals(property2Name).");
        } catch (IllegalArgumentException ex) {
            // The expected behavior
        }
    }

    
    public void testConstructorRejectsReadOnlyBeans() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        try {
            new PropertyConnector(bean1, "readOnlyObjectProperty", 
                                  bean2, "readOnlyObjectProperty");
            fail("Constructor failed to reject read-only bean properties.");
        } catch (IllegalArgumentException ex) {
            // The expected behavior
        }
    }

    public void testConstructorLeavesValuesUnchanged() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        Object initialValue1 = "initialValue1";
        Object initialValue2 = "initialValue2";
        createDefaultConnector(bean1, initialValue1, bean2, initialValue2);
        assertEquals("The constructor must not change the property1.",
                initialValue1,
                bean1.getReadWriteObjectProperty());
        assertEquals("The constructor must not change the property2.",
                initialValue2,
                bean2.getReadWriteObjectProperty());
    }

    
    // Synchronization ********************************************************

    public void testUpdateProperty1() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        Object initialValue2 = "newValue2";
        PropertyConnector connector =
            createDefaultConnector(bean1, "value1", bean2, initialValue2);
        connector.updateProperty1();
        assertEquals(
            "#updateProperty1 failed to update property1.",
            bean1.getReadWriteObjectProperty(),
            bean2.getReadWriteObjectProperty());
    }

    public void testUpdateProperty2() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        Object initialValue1 = "newValue1";
        PropertyConnector connector =
            createDefaultConnector(bean1, initialValue1, bean2, "value2");
        connector.updateProperty2();
        assertEquals(
            "#updateProperty2 failed to update property2.",
            bean1.getReadWriteObjectProperty(),
            bean2.getReadWriteObjectProperty());
    }

    public void testProperty1ChangeUpdatesProperty2() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        createDefaultConnector(bean1, "value1", bean2, "value2");
        bean1.setReadWriteObjectProperty("newValue1");
        assertEquals(
            "Failed to update property2 after a named change.",
            bean1.getReadWriteObjectProperty(),
            bean2.getReadWriteObjectProperty());
    }

    public void testProperty2ChangeUpdatesProperty1() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        createDefaultConnector(bean1, "value1", bean2, "value2");
        bean2.setReadWriteObjectProperty("newValue1");
        assertEquals(
            "Failed to update property1 after a named change.",
            bean1.getReadWriteObjectProperty(),
            bean2.getReadWriteObjectProperty());
    }
    
    public void testProperty1AnonymousChangeUpdatesProperty2() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        createDefaultConnector(bean1, "value1", bean2, "value2");
        bean1.setReadWriteObjectProperties("newValue1", true, 42);
        assertEquals(
            "Failed to update property2 after an unnamed change.",
            bean1.getReadWriteObjectProperty(),
            bean2.getReadWriteObjectProperty());
    }

    public void testProperty2AnonymousChangeUpdatesProperty1() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        createDefaultConnector(bean1, "value1", bean2, "value2");
        bean2.setReadWriteObjectProperties("newValue1", true, 42);
        assertEquals(
            "Failed to update property1 after an unnamed change.",
            bean1.getReadWriteObjectProperty(),
            bean2.getReadWriteObjectProperty());
    }
    
    
    // Events that lack the new value *****************************************
    
    public void testProperty1ChangeWithNullEventNewValueUpdatesProperty2() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        PropertyConnector.connect(
                bean1,
                "nullNewValueProperty",
                bean2,
                "readWriteObjectProperty");
        bean1.setNullNewValueProperty("newValue1");
        assertEquals(
            "Failed to update property2 after a change with event that has a null new value.",
            bean1.getNullNewValueProperty(),
            bean2.getReadWriteObjectProperty());
    }

    public void testProperty2ChangeWithNullEventNewValueUpdatesProperty1() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        PropertyConnector.connect(
                bean1,
                "readWriteObjectProperty",
                bean2,
                "nullNewValueProperty");
        bean2.setNullNewValueProperty("newValue1");
        assertEquals(
            "Failed to update property1 after a change with event that has a null new value.",
            bean1.getReadWriteObjectProperty(),
            bean2.getNullNewValueProperty());
    }

    
    
    
    // One-Way Synchronization ************************************************

    public void testConnectReadWriteProperty1WithReadOnlyProperty2() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        bean1.setReadWriteObjectProperty("initalValue1");
        new PropertyConnector(
                bean1,
                "readWriteObjectProperty",
                bean2,
                "readOnlyObjectProperty");
        testConnectReadWriteWithReadOnlyProperty(bean1, bean2);
    }
    
    public void testConnectReadOnlyProperty1WithReadWriteProperty2() {
        TestBean bean1 = new TestBean();
        TestBean bean2 = new TestBean();
        bean1.setReadWriteObjectProperty("initalValue1");
        new PropertyConnector(
                bean1,
                "readOnlyObjectProperty",
                bean2,
                "readWriteObjectProperty");
        testConnectReadWriteWithReadOnlyProperty(bean2, bean1);
    }
    
    private void testConnectReadWriteWithReadOnlyProperty(
            TestBean beanWithReadWriteProperty, 
            TestBean beanWithReadOnlyProperty) {
        Object initialValue2 = "initialValue2";
        beanWithReadOnlyProperty.readOnlyObjectProperty = initialValue2;
        
        // Ignore updates of property1.
        beanWithReadWriteProperty.setReadWriteObjectProperty("newValue1");
        assertEquals(
            "The connector must not update property2.",
            initialValue2,
            beanWithReadOnlyProperty.getReadOnlyObjectProperty());
        
        // Update property1 if the read-only property2 changes.
        Object newValue2 = "newValue2";
        beanWithReadOnlyProperty.fireChangeOnReadOnlyObjectProperty(newValue2);
        assertEquals(
                "Bean2 has a new value for the read-only property2.",
                newValue2,
                beanWithReadOnlyProperty.getReadOnlyObjectProperty());
        assertEquals(
            "The connector must update property1.",
            newValue2,
            beanWithReadWriteProperty.getReadWriteObjectProperty());

        // Ignore subsequent updates of property1.
        beanWithReadWriteProperty.setReadWriteObjectProperty("newValue1b");
        assertEquals(
            "The connector must not update property2.",
            newValue2,
            beanWithReadOnlyProperty.getReadOnlyObjectProperty());
    }
        
    
}
