Source Code Cross Referenced for BeanComparatorTestCase.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » 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 commons beanutils 1.8.0 BETA src » org.apache.commons.beanutils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.beanutils;
019:
020:        import java.lang.reflect.InvocationTargetException;
021:        import java.util.HashMap;
022:        import java.util.Map;
023:        import junit.framework.TestCase;
024:        import junit.framework.Test;
025:        import junit.framework.TestSuite;
026:
027:        /**
028:         * <p>
029:         *  Test Case for the BeanComparator class.
030:         *
031:         * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
032:         * @version $Revision: 557796 $
033:         */
034:
035:        public class BeanComparatorTestCase extends TestCase {
036:
037:            // ---------------------------------------------------- Instance Variables
038:
039:            /**
040:             * The test beans for each test.
041:             */
042:            protected TestBean bean = null;
043:            protected AlphaBean alphaBean1 = null;
044:            protected AlphaBean alphaBean2 = null;
045:
046:            // The test BeanComparator
047:            protected BeanComparator beanComparator = null;
048:
049:            // ---------------------------------------------------------- Constructors
050:
051:            /**
052:             * Construct a new instance of this test case.
053:             *
054:             * @param name Name of the test case
055:             */
056:            public BeanComparatorTestCase(String name) {
057:                super (name);
058:            }
059:
060:            // -------------------------------------------------- Overall Test Methods
061:
062:            /**
063:             * Set up instance variables required by this test case.
064:             */
065:            public void setUp() {
066:                bean = new TestBean();
067:                alphaBean1 = new AlphaBean("alphaBean1");
068:                alphaBean2 = new AlphaBean("alphaBean2");
069:
070:            }
071:
072:            /**
073:             * Return the tests included in this test suite.
074:             */
075:            public static Test suite() {
076:                return (new TestSuite(BeanComparatorTestCase.class));
077:            }
078:
079:            /**
080:             * Tear down instance variables required by this test case.
081:             */
082:            public void tearDown() {
083:                bean = null;
084:                alphaBean1 = null;
085:                alphaBean2 = null;
086:                beanComparator = null;
087:            }
088:
089:            // ------------------------------------------------ Individual Test Methods
090:
091:            /**
092:             *  tests comparing two beans via their name using the default Comparator
093:             */
094:            public void testSimpleCompare() {
095:                try {
096:                    beanComparator = new BeanComparator("name");
097:                    int result = beanComparator.compare(alphaBean1, alphaBean2);
098:                    assertTrue("Comparator did not sort properly.  Result:"
099:                            + result, result == -1);
100:
101:                } catch (Exception e) {
102:                    fail("Exception");
103:                }
104:            }
105:
106:            /**
107:             *  tests comparing two beans via their name using the default Comparator, but the inverse
108:             */
109:            public void testSimpleCompareInverse() {
110:                try {
111:                    beanComparator = new BeanComparator("name");
112:                    int result = beanComparator.compare(alphaBean2, alphaBean1);
113:                    assertTrue("Comparator did not sort properly.  Result:"
114:                            + result, result == 1);
115:
116:                } catch (Exception e) {
117:                    fail("Exception" + e);
118:                }
119:            }
120:
121:            /**
122:             *  tests comparing two beans via their name using the default Comparator where they have the same value.
123:             */
124:            public void testCompareIdentical() {
125:                try {
126:                    alphaBean1 = new AlphaBean("alphabean");
127:                    alphaBean2 = new AlphaBean("alphabean");
128:                    beanComparator = new BeanComparator("name");
129:                    int result = beanComparator.compare(alphaBean1, alphaBean2);
130:                    assertTrue("Comparator did not sort properly.  Result:"
131:                            + result, result == 0);
132:
133:                } catch (Exception e) {
134:                    fail("Exception");
135:                }
136:            }
137:
138:            /**
139:             *  tests comparing one bean against itself.
140:             */
141:            public void testCompareBeanAgainstSelf() {
142:                try {
143:                    beanComparator = new BeanComparator("name");
144:                    int result = beanComparator.compare(alphaBean1, alphaBean1);
145:                    assertTrue("Comparator did not sort properly.  Result:"
146:                            + result, result == 0);
147:
148:                } catch (Exception e) {
149:                    fail("Exception");
150:                }
151:            }
152:
153:            /**
154:             *  tests comparing two beans via their name using the default Comparator, but with one of the beans
155:             *  being null.
156:             */
157:            public void testCompareWithNulls() {
158:                try {
159:                    beanComparator = new BeanComparator("name");
160:                    beanComparator.compare(alphaBean2, null);
161:
162:                    // DEP not sure if this is the best way to test an exception?
163:                    fail("Should not be able to compare a null value.");
164:
165:                } catch (Exception e) {
166:
167:                }
168:            }
169:
170:            /**
171:             *  tests comparing two beans who don't have a property
172:             */
173:            public void testCompareOnMissingProperty() {
174:                try {
175:                    beanComparator = new BeanComparator("bogusName");
176:                    beanComparator.compare(alphaBean2, alphaBean1);
177:                    fail("should not be able to compare");
178:
179:                } catch (Exception e) {
180:                    assertTrue("Wrong exception was thrown: " + e, e.toString()
181:                            .indexOf("Unknown property") > -1);
182:                }
183:            }
184:
185:            /**
186:             *  tests comparing two beans on a boolean property, which is not possible.
187:             */
188:            public void testCompareOnBooleanProperty() {
189:                try {
190:                    TestBean testBeanA = new TestBean();
191:                    TestBean testBeanB = new TestBean();
192:
193:                    testBeanA.setBooleanProperty(true);
194:                    testBeanB.setBooleanProperty(false);
195:
196:                    beanComparator = new BeanComparator("booleanProperty");
197:                    beanComparator.compare(testBeanA, testBeanB);
198:
199:                    // **** java.lang.Boolean implements Comparable from JDK 1.5 onwards
200:                    //      so this test no longer fails
201:                    // fail("BeanComparator should throw an exception when comparing two booleans.");
202:
203:                } catch (ClassCastException cce) {
204:                    ; // Expected result
205:                } catch (Exception e) {
206:                    fail("Exception" + e);
207:                }
208:            }
209:
210:            /**
211:             *  tests comparing two beans on a boolean property, then changing the property and testing
212:             */
213:            public void testSetProperty() {
214:                try {
215:                    TestBean testBeanA = new TestBean();
216:                    TestBean testBeanB = new TestBean();
217:
218:                    testBeanA.setDoubleProperty(5.5);
219:                    testBeanB.setDoubleProperty(1.0);
220:
221:                    beanComparator = new BeanComparator("doubleProperty");
222:                    int result = beanComparator.compare(testBeanA, testBeanB);
223:
224:                    assertTrue("Comparator did not sort properly.  Result:"
225:                            + result, result == 1);
226:
227:                    testBeanA.setStringProperty("string 1");
228:                    testBeanB.setStringProperty("string 2");
229:
230:                    beanComparator.setProperty("stringProperty");
231:
232:                    result = beanComparator.compare(testBeanA, testBeanB);
233:
234:                    assertTrue("Comparator did not sort properly.  Result:"
235:                            + result, result == -1);
236:
237:                } catch (ClassCastException cce) {
238:                    fail("ClassCaseException " + cce.toString());
239:                } catch (Exception e) {
240:                    fail("Exception" + e);
241:                }
242:            }
243:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.