Source Code Cross Referenced for PreferencesAdapterTest.java in  » Swing-Library » jgoodies-data-binding » com » jgoodies » binding » tests » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Swing Library » jgoodies data binding » com.jgoodies.binding.tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.