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.text.DateFormat;
034: import java.text.DecimalFormat;
035: import java.text.DecimalFormatSymbols;
036: import java.util.Locale;
037:
038: import junit.framework.TestCase;
039:
040: import com.jgoodies.binding.value.AbstractConverter;
041: import com.jgoodies.binding.value.ConverterFactory;
042: import com.jgoodies.binding.value.ValueHolder;
043: import com.jgoodies.binding.value.ValueModel;
044:
045: /**
046: * A test case for converters created by the ConverterFactory.
047: *
048: * TODO: add tests for null values in subject.
049: *
050: * @author Andrej Golovnin
051: * @author Karsten Lentzsch
052: * @version $Revision: 1.8 $
053: */
054: public final class ConverterFactoryTest extends TestCase {
055:
056: // Constructor Tests ******************************************************
057:
058: public void testConstructorsRejectNullSubjects() {
059: try {
060: ConverterFactory.createBooleanNegator(null);
061: fail("BooleanNegator should reject null subject.");
062: } catch (NullPointerException e) {
063: // The expected behavior
064: }
065: try {
066: ConverterFactory.createBooleanToStringConverter(null,
067: "yes", "no");
068: fail("BooleanToStringConverter should reject null subject.");
069: } catch (NullPointerException e) {
070: // The expected behavior
071: }
072: try {
073: ConverterFactory.createBooleanToStringConverter(null,
074: "yes", "no", "none");
075: fail("BooleanToStringConverter should reject null subject.");
076: } catch (NullPointerException e) {
077: // The expected behavior
078: }
079: try {
080: ConverterFactory.createDoubleToIntegerConverter(null);
081: fail("DoubleToIntegerConverter should reject null subject.");
082: } catch (NullPointerException e) {
083: // The expected behavior
084: }
085: try {
086: ConverterFactory.createDoubleToIntegerConverter(null, 100);
087: fail("DoubleToIntegerConverter should reject null subject.");
088: } catch (NullPointerException e) {
089: // The expected behavior
090: }
091: try {
092: ConverterFactory.createFloatToIntegerConverter(null);
093: fail("FloatToIntegerConverter should reject null subject.");
094: } catch (NullPointerException e) {
095: // The expected behavior
096: }
097: try {
098: ConverterFactory.createFloatToIntegerConverter(null, 100);
099: fail("FloatToIntegerConverter should reject null subject.");
100: } catch (NullPointerException e) {
101: // The expected behavior
102: }
103: try {
104: ConverterFactory.createLongToIntegerConverter(null);
105: fail("LongToIntegerConverter should reject null subject.");
106: } catch (NullPointerException e) {
107: // The expected behavior
108: }
109: try {
110: ConverterFactory.createLongToIntegerConverter(null, 100);
111: fail("LongToIntegerConverter should reject null subject.");
112: } catch (NullPointerException e) {
113: // The expected behavior
114: }
115: try {
116: ConverterFactory.createStringConverter(null, DateFormat
117: .getDateInstance());
118: fail("StringConverter should reject null subject.");
119: } catch (NullPointerException e) {
120: // The expected behavior
121: }
122: }
123:
124: public void testBooleanToStringConverterRejectsNullTexts() {
125: try {
126: ConverterFactory.createBooleanToStringConverter(
127: new ValueHolder(), null, "no", "none");
128: fail("BooleanToStringConverter should reject null trueText.");
129: } catch (NullPointerException e) {
130: // The expected behavior
131: }
132: try {
133: ConverterFactory.createBooleanToStringConverter(
134: new ValueHolder(), "yes", null, "none");
135: fail("BooleanToStringConverter should reject null falseText.");
136: } catch (NullPointerException e) {
137: // The expected behavior
138: }
139: try {
140: ConverterFactory.createBooleanToStringConverter(
141: new ValueHolder(), "yes", "no", null);
142: fail("BooleanToStringConverter should reject null falseText.");
143: } catch (NullPointerException e) {
144: // The expected behavior
145: }
146: }
147:
148: public void testBooleanToStringConverterRejectsEqualTexts() {
149: try {
150: ConverterFactory.createBooleanToStringConverter(
151: new ValueHolder(), "yes", "yes", "none");
152: fail("BooleanToStringConverter should reject equal trueText and falseText.");
153: } catch (IllegalArgumentException e) {
154: // The expected behavior
155: }
156: try {
157: ConverterFactory.createBooleanToStringConverter(
158: new ValueHolder(), "none", "no", "none");
159: } catch (IllegalArgumentException e) {
160: fail("BooleanToStringConverter should accept equal trueText and nullText.");
161: }
162: try {
163: ConverterFactory.createBooleanToStringConverter(
164: new ValueHolder(), "yes", "none", "none");
165: } catch (IllegalArgumentException e) {
166: fail("BooleanToStringConverter should accept equal falseText and nullText.");
167: }
168: }
169:
170: // Conversion Tests *******************************************************
171:
172: public void testBooleanNegator() {
173: ValueModel subject = new ValueHolder(Boolean.TRUE);
174: AbstractConverter c = (AbstractConverter) ConverterFactory
175: .createBooleanNegator(subject);
176: assertInvertable(c, null);
177: assertInvertable(c, Boolean.TRUE);
178: assertInvertable(c, Boolean.FALSE);
179: assertProperConversions(subject, Boolean.TRUE, c, Boolean.FALSE);
180: assertProperConversions(subject, Boolean.FALSE, c, Boolean.TRUE);
181: assertProperConversions(subject, null, c, null);
182: }
183:
184: public void testBooleanToStringConverter() {
185: String trueText = "true";
186: String falseText = "false";
187: String nullText = "unknown";
188: ValueModel subject = new ValueHolder(Boolean.TRUE);
189: AbstractConverter c = (AbstractConverter) ConverterFactory
190: .createBooleanToStringConverter(subject, trueText,
191: falseText, nullText);
192: assertInvertable(c, null);
193: assertInvertable(c, Boolean.TRUE);
194: assertInvertable(c, Boolean.FALSE);
195: assertProperConversions(subject, Boolean.TRUE, c, trueText);
196: assertProperConversions(subject, Boolean.FALSE, c, falseText);
197: assertProperConversions(subject, null, c, nullText);
198: }
199:
200: public void testDoubleConverter() {
201: Double value = new Double(1.0);
202: Double convertedValue = new Double(100);
203: ValueModel subject = new ValueHolder(value);
204: AbstractConverter c = (AbstractConverter) ConverterFactory
205: .createDoubleConverter(subject, 100);
206: assertInvertable(c, value);
207: assertProperConversions(subject, value, c, convertedValue);
208: }
209:
210: public void testDoubleToIntegerConverter() {
211: Double value = new Double(100.0);
212: Integer convertedValue = new Integer(100);
213: ValueModel subject = new ValueHolder(value);
214: AbstractConverter c = (AbstractConverter) ConverterFactory
215: .createDoubleToIntegerConverter(subject, 1);
216: assertInvertable(c, value);
217: assertProperConversions(subject, value, c, convertedValue);
218: }
219:
220: public void testFloatConverter() {
221: Float value = new Float(1.0);
222: Float convertedValue = new Float(100);
223: ValueModel subject = new ValueHolder(value);
224: AbstractConverter c = (AbstractConverter) ConverterFactory
225: .createFloatConverter(subject, 100);
226: assertInvertable(c, value);
227: assertProperConversions(subject, value, c, convertedValue);
228: }
229:
230: public void testFloatToIntegerConverter() {
231: Float value = new Float(100.0f);
232: Integer convertedValue = new Integer(100);
233: ValueModel subject = new ValueHolder(value);
234: AbstractConverter c = (AbstractConverter) ConverterFactory
235: .createFloatToIntegerConverter(subject, 1);
236: assertInvertable(c, value);
237: assertProperConversions(subject, value, c, convertedValue);
238: }
239:
240: public void testIntegerConverter() {
241: Integer value = new Integer(1);
242: Integer convertedValue = new Integer(100);
243: ValueModel subject = new ValueHolder(value);
244: AbstractConverter c = (AbstractConverter) ConverterFactory
245: .createIntegerConverter(subject, 100);
246: assertInvertable(c, value);
247: assertProperConversions(subject, value, c, convertedValue);
248: }
249:
250: public void testLongConverter() {
251: Long value = new Long(1L);
252: Long convertedValue = new Long(100L);
253: ValueModel subject = new ValueHolder(value);
254: AbstractConverter c = (AbstractConverter) ConverterFactory
255: .createLongConverter(subject, 100);
256: assertInvertable(c, value);
257: assertProperConversions(subject, value, c, convertedValue);
258: }
259:
260: public void testLongToIntegerConverter() {
261: Long value = new Long(100);
262: Integer convertedValue = new Integer(100);
263: ValueModel subject = new ValueHolder(value);
264: AbstractConverter c = (AbstractConverter) ConverterFactory
265: .createLongToIntegerConverter(subject, 1);
266: assertInvertable(c, value);
267: assertProperConversions(subject, value, c, convertedValue);
268: }
269:
270: public void testStringConverter() {
271: Long value = new Long(100);
272: String convertedValue = "100";
273: DecimalFormat format = new DecimalFormat("#",
274: new DecimalFormatSymbols(Locale.US));
275: format.setParseIntegerOnly(true);
276: ValueModel subject = new ValueHolder(value);
277: AbstractConverter c = (AbstractConverter) ConverterFactory
278: .createStringConverter(subject, format);
279: assertInvertable(c, value);
280: assertProperConversions(subject, value, c, convertedValue);
281: }
282:
283: // Helper Code ************************************************************
284:
285: private void assertInvertable(AbstractConverter converter,
286: Object value) {
287: try {
288: converter.setValue(converter.convertFromSubject(value));
289: } catch (Exception e) {
290: fail("Unexpected exception is thrown: " + e);
291: }
292: }
293:
294: private void assertProperConversions(ValueModel subject,
295: Object value, ValueModel converter, Object convertedValue) {
296: subject.setValue(value);
297: assertEquals(
298: "The value returned by converter is not the expected one:",
299: convertedValue, converter.getValue());
300: converter.setValue(convertedValue);
301: assertEquals(
302: "The value returned by subject is not the expected one:",
303: value, subject.getValue());
304: }
305:
306: }
|