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 javax.swing.*;
034:
035: import junit.framework.TestCase;
036:
037: import com.jgoodies.binding.adapter.Bindings;
038: import com.jgoodies.binding.value.ValueHolder;
039: import com.jgoodies.binding.value.ValueModel;
040:
041: /**
042: * A test case for class {@link Bindings}.
043: *
044: * @author Jeanette Winzenburg
045: * @author Karsten Lentzsch
046: * @version $Revision: 1.7 $
047: */
048: public final class BindingsTest extends TestCase {
049:
050: private static final String TEXT_WITH_NEWLINE = "First line\nsecond line";
051:
052: private static final String TEXT_WITHOUT_NEWLINE = TEXT_WITH_NEWLINE
053: .replace('\n', ' ');
054:
055: /**
056: * Checks that the enablement of a JCheckBox is in synch
057: * with the enablement of its model after Binding#bind.
058: */
059: public void testCheckBoxEnablementInSynchWithModel() {
060: JCheckBox box = new JCheckBox();
061: box.setEnabled(false);
062: Bindings.bind(box, new ValueHolder());
063: assertButtonEnablementInSynchWithModel(box);
064: }
065:
066: /**
067: * Checks that the enablement of a JCheckBoxMenuItem is in synch
068: * with the enablement of its model after Binding#bind.
069: */
070: public void testCheckBoxItemEnablementInSynchWithModel() {
071: JCheckBoxMenuItem item = new JCheckBoxMenuItem();
072: item.setEnabled(false);
073: Bindings.bind(item, new ValueHolder());
074: assertButtonEnablementInSynchWithModel(item);
075: }
076:
077: /**
078: * Checks that the enablement of a JRadioButton is in synch
079: * with the enablement of its model after Binding#bind.
080: */
081: public void testRadioButtonEnablementInSynchWithModel() {
082: JRadioButton radio = new JRadioButton();
083: radio.setEnabled(false);
084: Bindings.bind(radio, new ValueHolder(), null);
085: assertButtonEnablementInSynchWithModel(radio);
086: }
087:
088: /**
089: * Checks that the enablement of a JRadioButtonMenuItem is in synch
090: * with the enablement of its model after Binding#bind.
091: */
092: public void testRadioButtonMenuItemEnablementInSynchWithModel() {
093: JRadioButtonMenuItem item = new JRadioButtonMenuItem();
094: item.setEnabled(false);
095: Bindings.bind(item, new ValueHolder(), null);
096: assertButtonEnablementInSynchWithModel(item);
097: }
098:
099: public void testBoundTextAreaRetainsNewlines() {
100: ValueModel subject = new ValueHolder(TEXT_WITH_NEWLINE);
101: JTextArea textArea = new JTextArea();
102: Bindings.bind(textArea, subject);
103: assertEquals("The text area's text contains newlines.",
104: TEXT_WITH_NEWLINE, textArea.getText());
105: }
106:
107: public void testBoundTextFieldFiltersNewlines() {
108: ValueModel subject = new ValueHolder(TEXT_WITH_NEWLINE);
109: JTextField textField = new JTextField();
110: Bindings.bind(textField, subject);
111: assertEquals("The text field's text contains no newlines.",
112: TEXT_WITHOUT_NEWLINE, textField.getText());
113: }
114:
115: // Helper Code ************************************************************
116:
117: /**
118: * Checks that the enablement of the given button is in synch
119: * with the enablement of its model, an instance of ToggleButtonModel.
120: *
121: * @param button the button to check
122: */
123: private void assertButtonEnablementInSynchWithModel(
124: AbstractButton button) {
125: assertEquals(
126: "Enabled state of component and model must be in synch.",
127: button.isEnabled(), button.getModel().isEnabled());
128: }
129:
130: }
|