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.awt.Color;
034: import java.util.Date;
035:
036: import javax.swing.JFormattedTextField;
037: import javax.swing.JPasswordField;
038: import javax.swing.JTextArea;
039: import javax.swing.JTextField;
040:
041: import junit.framework.TestCase;
042:
043: import com.jgoodies.binding.adapter.BasicComponentFactory;
044: import com.jgoodies.binding.value.ValueHolder;
045: import com.jgoodies.binding.value.ValueModel;
046:
047: /**
048: * A test case for class {@link BasicComponentFactory}.
049: *
050: * @author Karsten Lentzsch
051: * @version $Revision: 1.10 $
052: */
053: public final class BasicComponentFactoryTest extends TestCase {
054:
055: // Color Chooser **********************************************************
056:
057: public void testRejectPlainColorChooserCreationWithInitialNullValue() {
058: ValueModel valueModel = new ValueHolder();
059: try {
060: BasicComponentFactory.createColorChooser(valueModel);
061: fail("The ValueModel's value must not be null at creation time.");
062: } catch (NullPointerException e) {
063: // The expected behavior
064: }
065: }
066:
067: public void testPlainColorChooserWithNullValue() {
068: ValueModel valueModel = new ValueHolder(Color.RED);
069: BasicComponentFactory.createColorChooser(valueModel);
070: valueModel.setValue(null);
071: }
072:
073: public void testRejectColorChooserCreationWithNullDefaultColor() {
074: ValueModel valueModel = new ValueHolder(Color.RED);
075: try {
076: BasicComponentFactory.createColorChooser(valueModel, null);
077: fail("The default color must not be null.");
078: } catch (NullPointerException e) {
079: // The expected behavior
080: }
081: }
082:
083: public void testAcceptColorChooserCreationWithInitialNullValue() {
084: ValueModel valueModel = new ValueHolder();
085: BasicComponentFactory.createColorChooser(valueModel, Color.RED);
086: }
087:
088: // Dates ******************************************************************
089:
090: public void testDateFieldMapsNullToEmpty() {
091: ValueModel dateModel = new ValueHolder();
092: JFormattedTextField dateField = BasicComponentFactory
093: .createDateField(dateModel);
094: assertEquals("Date field maps null to an empty string.", "",
095: dateField.getText());
096:
097: dateModel.setValue(new Date(32168));
098: setTextAndCommitEdit(dateField, "");
099: assertEquals("Date field maps the empty string to null.", null,
100: dateModel.getValue());
101: dateModel.setValue(new Date(424242));
102: setTextAndCommitEdit(dateField, " ");
103: assertEquals("Date field maps blank strings to null.", null,
104: dateModel.getValue());
105: }
106:
107: public void testDateFieldIsValidOnEmptyAndBlank() {
108: JFormattedTextField dateField = BasicComponentFactory
109: .createDateField(new ValueHolder());
110: assertTrue("The empty field is valid.", dateField.isEditValid());
111: dateField.setText(" ");
112: assertTrue("The blank field is valid.", dateField.isEditValid());
113: }
114:
115: // Integers ***************************************************************
116:
117: public void testDefaultIntegerFieldMapsNullToEmpty() {
118: ValueHolder integerModel = new ValueHolder();
119: JFormattedTextField integerField = BasicComponentFactory
120: .createIntegerField(integerModel);
121: assertEquals(
122: "Default integer field maps null to an empty string.",
123: "", integerField.getText());
124:
125: integerModel.setValue(42L);
126: setTextAndCommitEdit(integerField, "");
127: assertEquals(
128: "Default integer field maps the empty string to null.",
129: null, integerModel.getValue());
130: integerModel.setValue(32168);
131: setTextAndCommitEdit(integerField, " ");
132: assertEquals(
133: "Default integer field maps blank strings to null.",
134: null, integerModel.getValue());
135: }
136:
137: public void testCustomIntegerFieldMapsEmptyValueToEmpty() {
138: Integer emptyValue = new Integer(-1);
139: ValueHolder integerModel = new ValueHolder(emptyValue);
140: JFormattedTextField integerField = BasicComponentFactory
141: .createIntegerField(integerModel, emptyValue.intValue());
142: assertEquals(
143: "Custom integer field maps the empty value to an empty string.",
144: "", integerField.getText());
145:
146: integerModel.setValue(42L);
147: setTextAndCommitEdit(integerField, "");
148: assertEquals(
149: "Custom integer field maps the empty string to the empty value.",
150: emptyValue, integerModel.getValue());
151: integerModel.setValue(32168);
152: setTextAndCommitEdit(integerField, " ");
153: assertEquals(
154: "Custom integer field maps blank strings to the empty value.",
155: emptyValue, integerModel.getValue());
156: }
157:
158: public void testDefaultIntegerFieldIsValidOnEmptyAndBlank() {
159: JFormattedTextField integerField = BasicComponentFactory
160: .createIntegerField(new ValueHolder());
161: assertTrue("The empty field is valid.", integerField
162: .isEditValid());
163: integerField.setText(" ");
164: assertTrue("The blank field is valid.", integerField
165: .isEditValid());
166: }
167:
168: // Longs ******************************************************************
169:
170: public void testDefaultLongFieldMapsNullToEmpty() {
171: ValueHolder longModel = new ValueHolder();
172: JFormattedTextField longField = BasicComponentFactory
173: .createLongField(longModel);
174: assertEquals(
175: "Default long field maps null to an empty string.", "",
176: longField.getText());
177:
178: longModel.setValue(42L);
179: setTextAndCommitEdit(longField, "");
180: assertEquals(
181: "Default long field maps the empty string to null.",
182: null, longModel.getValue());
183: longModel.setValue(32168);
184: setTextAndCommitEdit(longField, " ");
185: assertEquals("Default long field maps blank strings to null.",
186: null, longModel.getValue());
187: }
188:
189: public void testCustomLongFieldMapsEmptyValueToEmpty() {
190: Long emptyValue = new Long(-1);
191: ValueHolder longModel = new ValueHolder(emptyValue);
192: JFormattedTextField longField = BasicComponentFactory
193: .createLongField(longModel, emptyValue.longValue());
194: assertEquals(
195: "Custom long field maps the empty value to an empty string.",
196: "", longField.getText());
197:
198: longModel.setValue(42L);
199: setTextAndCommitEdit(longField, "");
200: assertEquals(
201: "Custom long field maps the empty string to the empty value.",
202: emptyValue, longModel.getValue());
203: longModel.setValue(32168);
204: setTextAndCommitEdit(longField, " ");
205: assertEquals(
206: "Custom long field maps blank strings to the empty value.",
207: emptyValue, longModel.getValue());
208: }
209:
210: public void testDefaultLongFieldIsValidOnEmptyAndBlank() {
211: JFormattedTextField longField = BasicComponentFactory
212: .createLongField(new ValueHolder());
213: assertTrue("The empty field is valid.", longField.isEditValid());
214: longField.setText(" ");
215: assertTrue("The blank field is valid.", longField.isEditValid());
216: }
217:
218: // Filtering **************************************************************
219:
220: private static final String TEXT_WITH_NEWLINE = "First line\nsecond line";
221:
222: private static final String TEXT_WITHOUT_NEWLINE = TEXT_WITH_NEWLINE
223: .replace('\n', ' ');
224:
225: public void testPasswordFieldFiltersNewlines() {
226: ValueModel subject = new ValueHolder(TEXT_WITH_NEWLINE);
227: JPasswordField passwordField = BasicComponentFactory
228: .createPasswordField(subject);
229: assertEquals("The password field's text contains no newlines.",
230: TEXT_WITHOUT_NEWLINE, new String(passwordField
231: .getPassword()));
232: }
233:
234: public void testTextAreaRetainsNewlines() {
235: ValueModel subject = new ValueHolder(TEXT_WITH_NEWLINE);
236: JTextArea textArea = BasicComponentFactory
237: .createTextArea(subject);
238: assertEquals("The text area's text contains newlines.",
239: TEXT_WITH_NEWLINE, textArea.getText());
240: }
241:
242: public void testTextFieldFiltersNewlines() {
243: ValueModel subject = new ValueHolder(TEXT_WITH_NEWLINE);
244: JTextField textField = BasicComponentFactory
245: .createTextField(subject);
246: assertEquals("The text field's text contains no newlines.",
247: TEXT_WITHOUT_NEWLINE, textField.getText());
248: }
249:
250: // Helper Code ************************************************************
251:
252: private void setTextAndCommitEdit(
253: JFormattedTextField formattedTextField, String text) {
254: formattedTextField.setText(text);
255: try {
256: formattedTextField.commitEdit();
257: } catch (Exception e) {
258: fail("Failed to commit the text '" + text + "'.");
259: }
260: }
261:
262: }
|