Source Code Cross Referenced for SimpleGetterSetterTest.java in  » Testing » Ejb3Unit » com » bm » utils » 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 » Testing » Ejb3Unit » com.bm.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.bm.utils;
002:
003:        import java.beans.PropertyDescriptor;
004:        import java.lang.reflect.InvocationTargetException;
005:        import java.util.ArrayList;
006:        import java.util.List;
007:
008:        import junit.framework.Assert;
009:
010:        import org.apache.commons.beanutils.PropertyUtilsBean;
011:
012:        import com.bm.datagen.Generator;
013:        import com.bm.datagen.annotations.GeneratorType;
014:        import com.bm.datagen.empty.EmptyCollection;
015:        import com.bm.datagen.random.primitive.PrimitiveRandomBooleanGenerator;
016:        import com.bm.datagen.random.primitive.PrimitiveRandomDateGenerator;
017:        import com.bm.datagen.random.primitive.PrimitiveRandomDoubleGenerator;
018:        import com.bm.datagen.random.primitive.PrimitiveRandomIntegerGenerator;
019:        import com.bm.datagen.random.primitive.PrimitiveRandomLongGenerator;
020:        import com.bm.datagen.random.primitive.PrimitiveRandomShortGenerator;
021:        import com.bm.datagen.random.primitive.PrimitiveRandomStringGenerator;
022:
023:        /**
024:         * This class exectues a simple test of getter setters. All seeters with
025:         * primitive or basic type will be executed.
026:         * 
027:         * @author Daniel Wiese
028:         * @since 17.04.2006
029:         */
030:        public class SimpleGetterSetterTest extends Assert {
031:
032:            private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
033:                    .getLogger(SimpleGetterSetterTest.class);
034:
035:            private static final List<Generator> DEFAULT_GENERATORS = new ArrayList<Generator>();
036:
037:            private final Class toTestClass;
038:
039:            private final PropertyUtilsBean propUtils = new PropertyUtilsBean();
040:
041:            private final PropertyDescriptor[] properties;
042:
043:            private final Object toTestObj;
044:
045:            static {
046:                DEFAULT_GENERATORS.add(new PrimitiveRandomDateGenerator());
047:                DEFAULT_GENERATORS.add(new PrimitiveRandomBooleanGenerator());
048:                DEFAULT_GENERATORS.add(new PrimitiveRandomIntegerGenerator());
049:                DEFAULT_GENERATORS.add(new PrimitiveRandomLongGenerator());
050:                DEFAULT_GENERATORS.add(new PrimitiveRandomShortGenerator());
051:                DEFAULT_GENERATORS.add(new PrimitiveRandomStringGenerator());
052:                DEFAULT_GENERATORS.add(new PrimitiveRandomDoubleGenerator());
053:                DEFAULT_GENERATORS.add(new EmptyCollection());
054:            }
055:
056:            /**
057:             * Constructor.
058:             * 
059:             * @param toTest -
060:             *            the bean to test.
061:             */
062:            public SimpleGetterSetterTest(Object toTest) {
063:                this .toTestClass = toTest.getClass();
064:                properties = this .propUtils
065:                        .getPropertyDescriptors(this .toTestClass);
066:                this .toTestObj = toTest;
067:
068:            }
069:
070:            /**
071:             * Test all primitive getters/setters of the bean instance.
072:             * 
073:             * @author Daniel Wiese
074:             * @since 17.04.2006
075:             */
076:            public void testGetterSetter() {
077:                for (PropertyDescriptor current : properties) {
078:                    if (current.getWriteMethod() != null
079:                            && current.getReadMethod() != null) {
080:                        Class[] params = current.getWriteMethod()
081:                                .getParameterTypes();
082:                        if (params.length == 1 && params[0] != null) {
083:                            Class toCreate = Ejb3Utils
084:                                    .getNonPrimitiveType(params[0]);
085:                            Object[] value = new Object[1];
086:                            value[0] = this .getValue(toCreate);
087:                            if (value[0] != null) {
088:                                try {
089:                                    log.debug("Testing property (get-/set "
090:                                            + current.getName() + "()) ...");
091:                                    // call the setter method
092:                                    current.getWriteMethod().invoke(
093:                                            this .toTestObj, value);
094:
095:                                    // call the getter method
096:                                    Object result = current.getReadMethod()
097:                                            .invoke(this .toTestObj,
098:                                                    (Object[]) null);
099:                                    assertNotNull(
100:                                            "The result of getter-property ("
101:                                                    + current.getName()
102:                                                    + ") is null, but set.. was called (Expected: "
103:                                                    + value[0] + ")", result);
104:                                    assertEquals(
105:                                            "The result of getter-property ("
106:                                                    + current.getName()
107:                                                    + ") is (" + result
108:                                                    + ") but not (" + value[0]
109:                                                    + ")", value[0], result);
110:                                } catch (IllegalArgumentException e) {
111:                                    log.error(
112:                                            "WrongArgument-Can't invoke the setter of property ("
113:                                                    + current.getName() + ")",
114:                                            e);
115:                                    fail("WrongArgument-Can't invoke the setter of property ("
116:                                            + current.getName() + ")");
117:                                    throw new IllegalArgumentException(e);
118:                                } catch (IllegalAccessException e) {
119:                                    log.error(
120:                                            "WrongArgument-Can't invoke the setter of property ("
121:                                                    + current.getName() + ")",
122:                                            e);
123:                                    fail("WrongArgument-Can't invoke the setter of property ("
124:                                            + current.getName() + ")");
125:                                    throw new IllegalArgumentException(e);
126:                                } catch (InvocationTargetException e) {
127:                                    log.error(
128:                                            "Can't invoke the setter of property ("
129:                                                    + current.getName() + ")",
130:                                            e);
131:                                    fail("Can't invoke the setter of property ("
132:                                            + current.getName() + ")");
133:                                    throw new IllegalArgumentException(e);
134:                                }
135:
136:                            }
137:                        }
138:                    }
139:
140:                }
141:            }
142:
143:            private Object getValue(Class type) {
144:                Object back = null;
145:                for (Generator current : DEFAULT_GENERATORS) {
146:                    final GeneratorType gType = Ejb3Utils
147:                            .getGeneratorTypeAnnotation(current);
148:                    if (type.equals(Ejb3Utils.getNonPrimitiveType(gType
149:                            .className()))) {
150:                        back = current.getValue();
151:                        break;
152:                    }
153:                }
154:
155:                return back;
156:            }
157:
158:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.