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 junit.framework.TestCase;
034:
035: import com.jgoodies.binding.tests.event.PropertyChangeReport;
036: import com.jgoodies.binding.value.Trigger;
037:
038: /**
039: * A test case for class {@link Trigger}.
040: *
041: *
042: * @author Karsten Lentzsch
043: * @version $Revision: 1.10 $
044: */
045: public final class TriggerTest extends TestCase {
046:
047: /**
048: * Verifies that the trigger accepts Boolean values and null.
049: */
050: public void testAcceptsBoolean() {
051: Trigger trigger = new Trigger();
052: trigger.setValue(true);
053: trigger.setValue(false);
054: trigger.setValue(null);
055: trigger.setValue(Boolean.TRUE);
056: trigger.setValue(Boolean.FALSE);
057: }
058:
059: /**
060: * Checks that Trigger.setValue rejects non-Boolean values with an
061: * IllegalArgumentException.
062: */
063: public void testRejectsNonBooleans() {
064: Trigger trigger = new Trigger();
065: try {
066: trigger.setValue("Wow");
067: fail("The Trigger must reject non-Boolean values, here: String.");
068: } catch (IllegalArgumentException e) {
069: // The expected behavior
070: }
071: try {
072: trigger.setValue(1967);
073: fail("The Trigger must reject non-Boolean values, here: Integer.");
074: } catch (IllegalArgumentException e) {
075: // The expected behavior
076: }
077: }
078:
079: /**
080: * Checks that <code>#triggerCommit</code> fires a change event with new
081: * value = Boolean.TRUE.
082: */
083: public void testTriggerCommit() {
084: Trigger trigger = new Trigger();
085: PropertyChangeReport changeReport = new PropertyChangeReport();
086:
087: trigger.setValue(null);
088: trigger.addValueChangeListener(changeReport);
089: trigger.triggerCommit();
090: assertEquals("Commit from null fires a single event.",
091: changeReport.eventCount(), 1);
092: assertEquals("Event new value equals Boolean.TRUE.",
093: changeReport.lastNewValue(), Boolean.TRUE);
094: assertEquals("Trigger value equals Boolean.TRUE.", trigger
095: .getValue(), Boolean.TRUE);
096:
097: trigger.setValue(true);
098: assertEquals("Nothing changed.", changeReport.eventCount(), 1);
099: trigger.triggerCommit();
100: assertEquals("Commit from TRUE fires two events.", changeReport
101: .eventCount(), 3);
102: assertEquals("Event new value equals Boolean.TRUE.",
103: changeReport.lastNewValue(), Boolean.TRUE);
104: assertEquals("Trigger value equals Boolean.TRUE.", trigger
105: .getValue(), Boolean.TRUE);
106:
107: trigger.setValue(false);
108: assertEquals("Changed from true to false.", changeReport
109: .eventCount(), 4);
110: trigger.triggerCommit();
111: assertEquals("Commit from FALSE fires a single events.",
112: changeReport.eventCount(), 5);
113: assertEquals("Event new value equals Boolean.TRUE.",
114: changeReport.lastNewValue(), Boolean.TRUE);
115: assertEquals("Trigger value equals Boolean.TRUE.", trigger
116: .getValue(), Boolean.TRUE);
117: }
118:
119: /**
120: * Checks that <code>#triggerFlush</code> fires a change event with new
121: * value = Boolean.FALSE.
122: */
123: public void testTriggerFlush() {
124: Trigger trigger = new Trigger();
125: PropertyChangeReport changeReport = new PropertyChangeReport();
126:
127: trigger.setValue(null);
128: trigger.addValueChangeListener(changeReport);
129: trigger.triggerFlush();
130: assertEquals("Flush from null fires a single event.",
131: changeReport.eventCount(), 1);
132: assertEquals("Event new value equals Boolean.FALSE.",
133: changeReport.lastNewValue(), Boolean.FALSE);
134: assertEquals("Trigger value equals Boolean.FALSE.", trigger
135: .getValue(), Boolean.FALSE);
136:
137: trigger.setValue(true);
138: assertEquals("Changed from false to true.", changeReport
139: .eventCount(), 2);
140: trigger.triggerFlush();
141: assertEquals("Flush from TRUE fires a single event.",
142: changeReport.eventCount(), 3);
143: assertEquals("Event new value equals Boolean.FALSE.",
144: changeReport.lastNewValue(), Boolean.FALSE);
145: assertEquals("Trigger value equals Boolean.FALSE.", trigger
146: .getValue(), Boolean.FALSE);
147:
148: trigger.setValue(false);
149: assertEquals("Nothing changed.", changeReport.eventCount(), 3);
150: trigger.triggerFlush();
151: assertEquals("Flush from FALSE fires two events.", changeReport
152: .eventCount(), 5);
153: assertEquals("Event new value equals Boolean.FALSE.",
154: changeReport.lastNewValue(), Boolean.FALSE);
155: assertEquals("Trigger value equals Boolean.FALSE.", trigger
156: .getValue(), Boolean.FALSE);
157: }
158:
159: }
|