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.prefs.BackingStoreException;
034: import java.util.prefs.Preferences;
035:
036: import junit.framework.TestCase;
037:
038: import com.jgoodies.binding.adapter.PreferencesAdapter;
039: import com.jgoodies.binding.value.AbstractValueModel;
040: import com.jgoodies.binding.value.ValueHolder;
041: import com.jgoodies.binding.value.ValueModel;
042:
043: /**
044: * A test case for class {@link PreferencesAdapter}.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.8 $
048: */
049: public final class PreferencesAdapterTest extends TestCase {
050:
051: private static final String NODE_NAME = "unit-tests";
052:
053: private Preferences prefs;
054:
055: @Override
056: protected void setUp() {
057: prefs = Preferences.userRoot().node(NODE_NAME);
058: }
059:
060: @Override
061: protected void tearDown() {
062: try {
063: prefs.removeNode();
064: } catch (BackingStoreException e) {
065: // TODO: handle exception
066: }
067: prefs = null;
068: }
069:
070: // Parameter Tests ******************************************************
071:
072: public void testConstructorRejectsNullValues() {
073: String key = "constructorNullTest";
074: prefs.remove(key);
075: try {
076: new PreferencesAdapter(null, key, "default");
077: fail("PreferencesAdapter(Preferences, String, Object) failed to reject null Preferences.");
078: } catch (NullPointerException ex) {
079: // The expected behavior
080: }
081: try {
082: new PreferencesAdapter(prefs, null, "default");
083: fail("PreferencesAdapter(Preferences, String, Object) failed to reject a null key.");
084: } catch (NullPointerException ex) {
085: // The expected behavior
086: }
087: try {
088: new PreferencesAdapter(prefs, key, null);
089: fail("PreferencesAdapter(Preferences, String, Object) failed to reject a null default value.");
090: } catch (NullPointerException ex) {
091: // The expected behavior
092: }
093: }
094:
095: public void testConstructorAcceptsKnownDefaultValueTypes() {
096: String key = "constructorIllegalTypeTest";
097: prefs.remove(key);
098: new PreferencesAdapter(prefs, key, "String");
099: new PreferencesAdapter(prefs, key, Boolean.TRUE);
100: new PreferencesAdapter(prefs, key, new Double(12.3));
101: new PreferencesAdapter(prefs, key, new Float(12.3F));
102: new PreferencesAdapter(prefs, key, new Integer(12));
103: new PreferencesAdapter(prefs, key, new Long(12L));
104: }
105:
106: public void testConstructorRejectsIllegalDefaultValueTypes() {
107: String key = "constructorIllegalTypeTest";
108: prefs.remove(key);
109: try {
110: new PreferencesAdapter(prefs, key, new ValueHolder(1));
111: fail("PreferencesAdapter(Preferences, String, Object) failed to reject an invalid default value type.");
112: } catch (IllegalArgumentException ex) {
113: // The expected behavior
114: }
115: }
116:
117: // Basic Adapter Features *************************************************
118:
119: public void testReadWrittenValue() {
120: testReadWrittenValue("Boolean", Boolean.TRUE, Boolean.FALSE);
121: testReadWrittenValue("Double", new Double(1), new Double(2));
122: testReadWrittenValue("Float", new Float(1), new Float(2));
123: testReadWrittenValue("Integer", new Integer(1), new Integer(2));
124: testReadWrittenValue("Long", new Long(1), new Long(2));
125: testReadWrittenValue("String", "default", "new");
126: }
127:
128: /**
129: * Checks that the PreferencesAdapter returns the default value
130: * if no value has been stored in the preferences under the given key.
131: * This test first removes the value for the given key - if any.
132: */
133: public void testReadDefaultValue() {
134: testReadDefaultValue("booleanDefault", Boolean.TRUE);
135: testReadDefaultValue("doubleDefault", new Double(1));
136: testReadDefaultValue("floatDefault", new Float(1));
137: testReadDefaultValue("integerDefault", new Integer(1));
138: testReadDefaultValue("longDefault", new Long(1));
139: testReadDefaultValue("stringDefault", "default");
140: }
141:
142: /**
143: * Checks that writing the default value actually writes a value.
144: * Just checks a String-typed value, because the mechanism is all
145: * the same for the different types.
146: */
147: public void testSettingDefaultValueWrites() {
148: String key = "stringTest";
149: Object defaultValue = "default";
150: Object prototypeValue = "prototype";
151: AbstractValueModel writeAdapter = new PreferencesAdapter(prefs,
152: key, defaultValue);
153: writeAdapter.setValue(defaultValue);
154: AbstractValueModel readAdapter = new PreferencesAdapter(prefs,
155: key, prototypeValue);
156: assertEquals("Failed to return the previously set value.",
157: defaultValue, readAdapter.getValue());
158: }
159:
160: public void testRejectsWritingNull() {
161: String key = "nullTest";
162: Object defaultValue = "default";
163: ValueModel adapter = new PreferencesAdapter(prefs, key,
164: defaultValue);
165: try {
166: adapter.setValue(null);
167: fail("Failed to reject writing null.");
168: } catch (NullPointerException e) {
169: // The expected behavior
170: }
171: }
172:
173: public void testRejectsWritingInconsistentType() {
174: String key = "inconsistentTypesWriteTest";
175: Object defaultValue = "default";
176: PreferencesAdapter adapter = new PreferencesAdapter(prefs, key,
177: defaultValue);
178: try {
179: adapter.setInt(3);
180: fail("Failed to reject writing a value type inconsistent with the default value type.");
181: } catch (ClassCastException e) {
182: // The expected behavior
183: }
184: try {
185: adapter.setValue(new Integer(3));
186: fail("Failed to reject writing a value type inconsistent with the default value type.");
187: } catch (ClassCastException e) {
188: // The expected behavior
189: }
190: }
191:
192: public void testRejectsReadingInconsistentType() {
193: String key = "inconsistentTypesReadTest";
194: Object defaultValue = "default";
195: PreferencesAdapter adapter = new PreferencesAdapter(prefs, key,
196: defaultValue);
197: try {
198: adapter.getInt();
199: fail("Failed to reject writing a value type inconsistent with the default value type.");
200: } catch (ClassCastException e) {
201: // The expected behavior
202: }
203: }
204:
205: // Test Implementations ***************************************************
206:
207: private void testReadWrittenValue(String key, Object defaultValue,
208: Object newValue) {
209: AbstractValueModel adapter = new PreferencesAdapter(prefs, key,
210: defaultValue);
211: adapter.setValue(newValue);
212: assertEquals("Failed to return the previously set value.",
213: newValue, adapter.getValue());
214: }
215:
216: private void testReadDefaultValue(String key, Object defaultValue) {
217: prefs.remove(key);
218: AbstractValueModel adapter = new PreferencesAdapter(prefs, key,
219: defaultValue);
220: assertEquals("Failed to return the default value.",
221: defaultValue, adapter.getValue());
222: }
223:
224: }
|