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.BoundedRangeModel;
034:
035: import junit.framework.TestCase;
036:
037: import com.jgoodies.binding.adapter.BoundedRangeAdapter;
038: import com.jgoodies.binding.value.ValueHolder;
039: import com.jgoodies.binding.value.ValueModel;
040:
041: /**
042: * A test case for class {@link BoundedRangeAdapter}.
043: *
044: * @author Karsten Lentzsch
045: * @version $Revision: 1.6 $
046: */
047: public final class BoundedRangeAdapterTest extends TestCase {
048:
049: // Constructor Tests ******************************************************
050:
051: public void testConstructorRejectsNullSubject() {
052: try {
053: new BoundedRangeAdapter(null, 1, 0, 10);
054: fail("BoundedRangeAdapter constructor failed to reject a null subject.");
055: } catch (NullPointerException ex) {
056: // The expected behavior
057: }
058: }
059:
060: public void testConstructorAcceptsNullSubjectValue() {
061: new BoundedRangeAdapter(new ValueHolder(), 1, 0, 10);
062: }
063:
064: public void testConstructorRejectsIllegalArguments() {
065: ValueModel subject = new ValueHolder(1);
066: try {
067: new BoundedRangeAdapter(subject, 1, 0, -1);
068: fail("Constructor must reject if min > max.");
069: } catch (IllegalArgumentException ex) {
070: // The expected behavior
071: }
072: try {
073: new BoundedRangeAdapter(subject, 1, 2, 0);
074: fail("Constructor must reject if initial value < min.");
075: } catch (IllegalArgumentException ex) {
076: // The expected behavior
077: }
078: try {
079: new BoundedRangeAdapter(subject, 1, 0, 1);
080: fail("Constructor must reject if initial value + extent > max.");
081: } catch (IllegalArgumentException ex) {
082: // The expected behavior
083: }
084: try {
085: new BoundedRangeAdapter(subject, -1, 0, 10);
086: fail("Constructor must reject if extent < 0.");
087: } catch (IllegalArgumentException ex) {
088: // The expected behavior
089: }
090: }
091:
092: // Basic Adapter Features *************************************************
093:
094: public void testAcceptsNullSubjectValue() {
095: BoundedRangeModel adapter = new BoundedRangeAdapter(
096: new ValueHolder(), 1, 0, 10);
097: adapter.getValue();
098: }
099:
100: public void testReadsSubjectValue() {
101: ValueModel subject = new ValueHolder(1);
102: BoundedRangeModel adapter = new BoundedRangeAdapter(subject, 1,
103: 0, 10);
104: assertEquals("The adapter returns the initial subject value.",
105: subject.getValue(), new Integer(adapter.getValue()));
106: subject.setValue(new Integer(2));
107: assertEquals("The adapter reflects the subject value change.",
108: subject.getValue(), new Integer(adapter.getValue()));
109: }
110:
111: public void testWritesSubjectValue() {
112: ValueModel subject = new ValueHolder(1);
113: BoundedRangeModel adapter = new BoundedRangeAdapter(subject, 1,
114: 0, 10);
115: adapter.setValue(2);
116: assertEquals("The subject returns the new adapter value.",
117: subject.getValue(), new Integer(adapter.getValue()));
118: }
119:
120: }
|