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.util.Date;
034:
035: import javax.swing.SpinnerModel;
036: import javax.swing.SpinnerNumberModel;
037:
038: import junit.framework.TestCase;
039:
040: import com.jgoodies.binding.adapter.SpinnerAdapterFactory;
041: import com.jgoodies.binding.value.ValueHolder;
042: import com.jgoodies.binding.value.ValueModel;
043:
044: /**
045: * A test case for class {@link SpinnerAdapterFactory}.
046: *
047: * @author Karsten Lentzsch
048: * @version $Revision: 1.3 $
049: */
050: public final class SpinnerAdapterFactoryTest extends TestCase {
051:
052: // #connect ***************************************************************
053:
054: public void testConnectRejectsNullParameters() {
055: SpinnerModel spinnerModel = new SpinnerNumberModel(1, 0, 10, 2);
056: ValueModel valueModel = new ValueHolder(1);
057: Object defaultValue = new Integer(1);
058: try {
059: SpinnerAdapterFactory.connect(null, valueModel,
060: defaultValue);
061: fail("#connect must reject null spinner models.");
062: } catch (NullPointerException e) {
063: // The expected behavior
064: }
065: try {
066: SpinnerAdapterFactory.connect(spinnerModel, null,
067: defaultValue);
068: fail("#connect must reject null value models.");
069: } catch (NullPointerException e) {
070: // The expected behavior
071: }
072: try {
073: SpinnerAdapterFactory.connect(spinnerModel, valueModel,
074: null);
075: fail("#connect must reject null default values.");
076: } catch (NullPointerException e) {
077: // The expected behavior
078: }
079: }
080:
081: // #createDateAdapter *****************************************************
082:
083: public void testCreateDateAdapterRejectsNullParameters() {
084: try {
085: SpinnerAdapterFactory.createDateAdapter(null, new Date());
086: fail("The factory must reject null value models.");
087: } catch (NullPointerException e) {
088: // The expected behavior
089: }
090: try {
091: SpinnerAdapterFactory.createDateAdapter(new ValueHolder(),
092: null);
093: fail("The factory must reject null default dates.");
094: } catch (NullPointerException e) {
095: // The expected behavior
096: }
097: }
098:
099: public void testCreateDateAdapterAcceptsInitialNullValueModelValues() {
100: Object initialValue = null;
101: ValueModel valueModel = new ValueHolder(initialValue);
102: Date defaultDate = new Date();
103: SpinnerModel spinnerModel = SpinnerAdapterFactory
104: .createDateAdapter(valueModel, defaultDate);
105: assertEquals("The spinner model's value is the default date.",
106: defaultDate, spinnerModel.getValue());
107: }
108:
109: public void testCreateDateAdapterAcceptsNullValueModelValues() {
110: Object initialValue = new Date(51267);
111: ValueModel valueModel = new ValueHolder(initialValue);
112: Date defaultDate = new Date();
113: SpinnerModel spinnerModel = SpinnerAdapterFactory
114: .createDateAdapter(valueModel, defaultDate);
115: assertEquals(
116: "The spinner model's value is the value model's value.",
117: valueModel.getValue(), spinnerModel.getValue());
118: valueModel.setValue(null);
119: assertEquals("The spinner model's value is the default date.",
120: defaultDate, spinnerModel.getValue());
121: }
122:
123: // #createNumberAdapter ***************************************************
124:
125: public void testCreateNumberAdapterRejectsNullValueModel() {
126: try {
127: SpinnerAdapterFactory
128: .createNumberAdapter(null, 0, 0, 10, 1);
129: fail("The factory must reject null value models.");
130: } catch (NullPointerException e) {
131: // The expected behavior
132: }
133: }
134:
135: public void testCreateNumberAdapterAcceptsInitialNullValueModelValues() {
136: Object initialValue = null;
137: ValueModel valueModel = new ValueHolder(initialValue);
138: int defaultValue = 5;
139: SpinnerModel spinnerModel = SpinnerAdapterFactory
140: .createNumberAdapter(valueModel, defaultValue, 0, 10, 2);
141: assertEquals("The spinner model's value is the default value.",
142: new Integer(defaultValue), spinnerModel.getValue());
143: }
144:
145: public void testCreateNumberAdapterAcceptsNullValueModelValues() {
146: Object initialValue = new Integer(1);
147: ValueModel valueModel = new ValueHolder(initialValue);
148: int defaultValue = 5;
149: SpinnerModel spinnerModel = SpinnerAdapterFactory
150: .createNumberAdapter(valueModel, defaultValue, 0, 10, 2);
151: assertEquals(
152: "The spinner model's value is the value model's value.",
153: valueModel.getValue(), spinnerModel.getValue());
154: valueModel.setValue(null);
155: assertEquals("The spinner model's value is the default value.",
156: new Integer(defaultValue), spinnerModel.getValue());
157: }
158:
159: }
|