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.AbstractConverter;
037: import com.jgoodies.binding.value.ValueHolder;
038: import com.jgoodies.binding.value.ValueModel;
039:
040: /**
041: * A test case for class {@link AbstractConverter}.
042: *
043: * @author Karsten Lentzsch
044: * @version $Revision: 1.5 $
045: */
046: public final class AbstractConverterTest extends TestCase {
047:
048: private ValueHolder subject;
049: private ValueModel converter;
050:
051: /**
052: * @throws Exception in case of an unexpected problem
053: */
054: @Override
055: protected void setUp() throws Exception {
056: super .setUp();
057: subject = new ValueHolder();
058: converter = new TestConverter(subject);
059: }
060:
061: /**
062: * @throws Exception in case of an unexpected problem
063: */
064: @Override
065: protected void tearDown() throws Exception {
066: super .tearDown();
067: converter = null;
068: subject = null;
069: }
070:
071: // Tests ******************************************************************
072:
073: public void testGetValueConversion() {
074: Integer value = new Integer(1);
075: Object expectedConversion = "1";
076:
077: subject.setValue(value);
078: assertEquals("The int " + value + " is converted to "
079: + expectedConversion, expectedConversion, converter
080: .getValue());
081: }
082:
083: public void testSetValueConversion() {
084: Object convertedValue = "1";
085: Integer expectedValue = new Integer(1);
086:
087: converter.setValue(convertedValue);
088: assertEquals("The converter value " + convertedValue
089: + " is converted to " + expectedValue, expectedValue,
090: subject.getValue());
091: }
092:
093: public void testPropertyChangeEventConversion() {
094: Integer oldSubjectValue = null;
095: Integer newSubjectValue = new Integer(1);
096: Object expectedOldValue = null;
097: Object expectedNewValue = "1";
098:
099: subject.setValue(oldSubjectValue);
100: PropertyChangeReport subjectReport = new PropertyChangeReport();
101: PropertyChangeReport converterReport = new PropertyChangeReport();
102: subject.addValueChangeListener(subjectReport);
103: converter.addValueChangeListener(converterReport);
104:
105: subject.setValue(newSubjectValue);
106: assertEquals("The old subject event value is "
107: + oldSubjectValue, oldSubjectValue, subjectReport
108: .lastOldValue());
109: assertEquals("The old converter event value is "
110: + expectedOldValue, expectedOldValue, converterReport
111: .lastOldValue());
112:
113: assertEquals("The new subject event value is "
114: + newSubjectValue, newSubjectValue, subjectReport
115: .lastNewValue());
116: assertEquals("The new converter event value is "
117: + expectedNewValue, expectedNewValue, converterReport
118: .lastNewValue());
119: }
120:
121: // Test Converter *********************************************************
122:
123: /**
124: * An example converter that converts Integers to their String
125: * representations. Cannot convert <code>null</code>.
126: */
127: private static final class TestConverter extends AbstractConverter {
128:
129: private TestConverter(ValueModel valueModel) {
130: super (valueModel);
131: }
132:
133: /**
134: * Converts the given value to its string representation.
135: *
136: * @param subjectValue the subject's value
137: * @return the string representation of the given value
138: */
139: @Override
140: public Object convertFromSubject(Object subjectValue) {
141: return subjectValue.toString();
142: }
143:
144: public void setValue(Object newValue) {
145: subject.setValue(new Integer(Integer
146: .parseInt((String) newValue)));
147: }
148: }
149:
150: }
|