Source Code Cross Referenced for TestDefaultKeyValue.java in  » Library » Apache-common-Collections » org » apache » commons » collections » keyvalue » 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 » Library » Apache common Collections » org.apache.commons.collections.keyvalue 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2001-2004 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.apache.commons.collections.keyvalue;
017:
018:        import java.util.HashMap;
019:        import java.util.Map;
020:
021:        import junit.framework.Test;
022:        import junit.framework.TestCase;
023:        import junit.framework.TestSuite;
024:
025:        /**
026:         * Test the DefaultKeyValue class.
027:         * 
028:         * @since Commons Collections 3.0
029:         * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030:         * 
031:         * @author Neil O'Toole
032:         */
033:        public class TestDefaultKeyValue extends TestCase {
034:
035:            private final String key = "name";
036:            private final String value = "duke";
037:
038:            /**
039:             * JUnit constructor.
040:             * 
041:             * @param testName  the test name
042:             */
043:            public TestDefaultKeyValue(String testName) {
044:                super (testName);
045:
046:            }
047:
048:            public static void main(String[] args) {
049:                junit.textui.TestRunner.run(TestDefaultKeyValue.class);
050:            }
051:
052:            public static Test suite() {
053:                return new TestSuite(TestDefaultKeyValue.class);
054:            }
055:
056:            //-----------------------------------------------------------------------
057:            /**
058:             * Make an instance of DefaultKeyValue with the default (null) key and value.
059:             * Subclasses should override this method to return a DefaultKeyValue
060:             * of the type being tested.
061:             */
062:            protected DefaultKeyValue makeDefaultKeyValue() {
063:                return new DefaultKeyValue(null, null);
064:            }
065:
066:            /**
067:             * Make an instance of DefaultKeyValue with the specified key and value.
068:             * Subclasses should override this method to return a DefaultKeyValue
069:             * of the type being tested.
070:             */
071:            protected DefaultKeyValue makeDefaultKeyValue(Object key,
072:                    Object value) {
073:                return new DefaultKeyValue(key, value);
074:            }
075:
076:            //-----------------------------------------------------------------------
077:            public void testAccessorsAndMutators() {
078:                DefaultKeyValue kv = makeDefaultKeyValue();
079:
080:                kv.setKey(key);
081:                assertTrue(kv.getKey() == key);
082:
083:                kv.setValue(value);
084:                assertTrue(kv.getValue() == value);
085:
086:                // check that null doesn't do anything funny
087:                kv.setKey(null);
088:                assertTrue(kv.getKey() == null);
089:
090:                kv.setValue(null);
091:                assertTrue(kv.getValue() == null);
092:
093:            }
094:
095:            public void testSelfReferenceHandling() {
096:                // test that #setKey and #setValue do not permit
097:                //  the KVP to contain itself (and thus cause infinite recursion
098:                //  in #hashCode and #toString)
099:
100:                DefaultKeyValue kv = makeDefaultKeyValue();
101:
102:                try {
103:                    kv.setKey(kv);
104:                    fail("Should throw an IllegalArgumentException");
105:                } catch (IllegalArgumentException iae) {
106:                    // expected to happen...
107:
108:                    // check that the KVP's state has not changed
109:                    assertTrue(kv.getKey() == null && kv.getValue() == null);
110:                }
111:
112:                try {
113:                    kv.setValue(kv);
114:                    fail("Should throw an IllegalArgumentException");
115:                } catch (IllegalArgumentException iae) {
116:                    // expected to happen...
117:
118:                    // check that the KVP's state has not changed
119:                    assertTrue(kv.getKey() == null && kv.getValue() == null);
120:                }
121:            }
122:
123:            /**
124:             * Subclasses should override this method to test their own constructors.
125:             */
126:            public void testConstructors() {
127:                // 1. test default constructor
128:                DefaultKeyValue kv = new DefaultKeyValue();
129:                assertTrue(kv.getKey() == null && kv.getValue() == null);
130:
131:                // 2. test key-value constructor
132:                kv = new DefaultKeyValue(key, value);
133:                assertTrue(kv.getKey() == key && kv.getValue() == value);
134:
135:                // 3. test copy constructor
136:                DefaultKeyValue kv2 = new DefaultKeyValue(kv);
137:                assertTrue(kv2.getKey() == key && kv2.getValue() == value);
138:
139:                // test that the KVPs are independent
140:                kv.setKey(null);
141:                kv.setValue(null);
142:
143:                assertTrue(kv2.getKey() == key && kv2.getValue() == value);
144:
145:                // 4. test Map.Entry constructor
146:                Map map = new HashMap();
147:                map.put(key, value);
148:                Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
149:
150:                kv = new DefaultKeyValue(entry);
151:                assertTrue(kv.getKey() == key && kv.getValue() == value);
152:
153:                // test that the KVP is independent of the Map.Entry
154:                entry.setValue(null);
155:                assertTrue(kv.getValue() == value);
156:
157:            }
158:
159:            public void testEqualsAndHashCode() {
160:                // 1. test with object data
161:                DefaultKeyValue kv = makeDefaultKeyValue(key, value);
162:                DefaultKeyValue kv2 = makeDefaultKeyValue(key, value);
163:
164:                assertTrue(kv.equals(kv));
165:                assertTrue(kv.equals(kv2));
166:                assertTrue(kv.hashCode() == kv2.hashCode());
167:
168:                // 2. test with nulls
169:                kv = makeDefaultKeyValue(null, null);
170:                kv2 = makeDefaultKeyValue(null, null);
171:
172:                assertTrue(kv.equals(kv));
173:                assertTrue(kv.equals(kv2));
174:                assertTrue(kv.hashCode() == kv2.hashCode());
175:            }
176:
177:            public void testToString() {
178:                DefaultKeyValue kv = makeDefaultKeyValue(key, value);
179:                assertTrue(kv.toString().equals(
180:                        kv.getKey() + "=" + kv.getValue()));
181:
182:                // test with nulls
183:                kv = makeDefaultKeyValue(null, null);
184:                assertTrue(kv.toString().equals(
185:                        kv.getKey() + "=" + kv.getValue()));
186:            }
187:
188:            public void testToMapEntry() {
189:                DefaultKeyValue kv = makeDefaultKeyValue(key, value);
190:
191:                Map map = new HashMap();
192:                map.put(kv.getKey(), kv.getValue());
193:                Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
194:
195:                assertTrue(entry.equals(kv.toMapEntry()));
196:                assertTrue(entry.hashCode() == kv.hashCode());
197:            }
198:
199:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.