Source Code Cross Referenced for ClassFieldInspectorTest.java in  » Rule-Engine » drolls-Rule-Engine » org » drools » util » asm » 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 » Rule Engine » drolls Rule Engine » org.drools.util.asm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.drools.util.asm;
002:
003:        /*
004:         * Copyright 2005 JBoss Inc
005:         * 
006:         * Licensed under the Apache License, Version 2.0 (the "License");
007:         * you may not use this file except in compliance with the License.
008:         * You may obtain a copy of the License at
009:         * 
010:         *      http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS,
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         */
018:
019:        import java.io.IOException;
020:        import java.lang.reflect.Method;
021:        import java.util.Iterator;
022:        import java.util.Map;
023:
024:        import junit.framework.Assert;
025:        import junit.framework.TestCase;
026:
027:        public class ClassFieldInspectorTest extends TestCase {
028:
029:            public void testIt() throws Exception {
030:                final ClassFieldInspector ext = new ClassFieldInspector(
031:                        Person.class);
032:                assertEquals(7, ext.getPropertyGetters().size());
033:                assertEquals("getAge", ((Method) ext.getPropertyGetters()
034:                        .get(0)).getName());
035:                assertEquals("isHappy", ((Method) ext.getPropertyGetters().get(
036:                        1)).getName());
037:                assertEquals("getName", ((Method) ext.getPropertyGetters().get(
038:                        2)).getName());
039:
040:                final Map names = ext.getFieldNames();
041:                assertNotNull(names);
042:                assertEquals(7, names.size());
043:                assertEquals(0, ((Integer) names.get("age")).intValue());
044:                assertEquals(1, ((Integer) names.get("happy")).intValue());
045:                assertEquals(2, ((Integer) names.get("name")).intValue());
046:                assertNull(names.get("nAme"));
047:
048:            }
049:
050:            public void testInterface() throws Exception {
051:                final ClassFieldInspector ext = new ClassFieldInspector(
052:                        TestInterface.class);
053:                assertEquals(2, ext.getPropertyGetters().size());
054:                assertEquals("getSomething", ((Method) ext.getPropertyGetters()
055:                        .get(0)).getName());
056:                assertEquals("getAnother", ((Method) ext.getPropertyGetters()
057:                        .get(1)).getName());
058:
059:                final Map names = ext.getFieldNames();
060:                assertNotNull(names);
061:                assertEquals(2, names.size());
062:                assertEquals(0, ((Integer) names.get("something")).intValue());
063:                assertEquals(1, ((Integer) names.get("another")).intValue());
064:
065:            }
066:
067:            public void testAbstract() throws Exception {
068:                final ClassFieldInspector ext = new ClassFieldInspector(
069:                        TestAbstract.class);
070:                assertEquals(5, ext.getPropertyGetters().size());
071:                assertEquals("getSomething", ((Method) ext.getPropertyGetters()
072:                        .get(0)).getName());
073:                assertEquals("getAnother", ((Method) ext.getPropertyGetters()
074:                        .get(1)).getName());
075:
076:                final Map names = ext.getFieldNames();
077:                assertNotNull(names);
078:                assertEquals(5, names.size());
079:                assertEquals(0, ((Integer) names.get("something")).intValue());
080:                assertEquals(1, ((Integer) names.get("another")).intValue());
081:
082:            }
083:
084:            public void testInheritedFields() throws Exception {
085:                ClassFieldInspector ext = new ClassFieldInspector(
086:                        BeanInherit.class);
087:                assertEquals(5, ext.getPropertyGetters().size());
088:
089:                ext = new ClassFieldInspector(InterfaceChildImpl.class);
090:                assertEquals(8, ext.getPropertyGetters().size());
091:                // test inheritence from abstract class
092:                assertEquals(4, ((Integer) ext.getFieldNames().get("HTML"))
093:                        .intValue());
094:
095:                // check normal field on child class
096:                assertEquals(1, ((Integer) ext.getFieldNames().get("baz"))
097:                        .intValue());
098:
099:                // test inheritence from an interface
100:                assertEquals(3, ((Integer) ext.getFieldNames().get("URI"))
101:                        .intValue());
102:            }
103:
104:            public void testIntefaceInheritance() throws Exception {
105:                final ClassFieldInspector ext = new ClassFieldInspector(
106:                        InterfaceChild.class);
107:                final Map fields = ext.getFieldNames();
108:                assertTrue(fields.containsKey("foo"));
109:                assertTrue(fields.containsKey("bar"));
110:                assertTrue(fields.containsKey("baz"));
111:                assertTrue(fields.containsKey("URI"));
112:            }
113:
114:            public void testFieldIndexCalculation() {
115:                try {
116:                    final ClassFieldInspector ext = new ClassFieldInspector(
117:                            SubPerson.class);
118:                    final Map map = ext.getFieldNames();
119:                    final String[] fields = new String[map.size()];
120:                    for (final Iterator i = map.entrySet().iterator(); i
121:                            .hasNext();) {
122:                        final Map.Entry entry = (Map.Entry) i.next();
123:                        final String fieldName = (String) entry.getKey();
124:                        final int fieldIndex = ((Integer) entry.getValue())
125:                                .intValue();
126:                        if (fields[fieldIndex] == null) {
127:                            fields[fieldIndex] = fieldName;
128:                        } else {
129:                            Assert
130:                                    .fail("Duplicate index found for 2 fields: index["
131:                                            + fieldIndex
132:                                            + "] = ["
133:                                            + fields[fieldIndex]
134:                                            + "] and ["
135:                                            + fieldName + "]");
136:                        }
137:                    }
138:                } catch (final IOException e) {
139:                    e.printStackTrace();
140:                    Assert.fail("Unexpected exception thrown");
141:                }
142:            }
143:
144:            public void testGetReturnTypes() throws Exception {
145:                final ClassFieldInspector ext = new ClassFieldInspector(
146:                        Person.class);
147:                final Map types = ext.getFieldTypes();
148:                assertNotNull(types);
149:                assertEquals(boolean.class, types.get("happy"));
150:                assertEquals(int.class, types.get("age"));
151:                assertEquals(String.class, types.get("name"));
152:            }
153:
154:            public void testGetMethodForField() throws Exception {
155:                final ClassFieldInspector ext = new ClassFieldInspector(
156:                        Person.class);
157:                final Map methods = ext.getGetterMethods();
158:                assertNotNull(methods);
159:                assertEquals("isHappy", ((Method) methods.get("happy"))
160:                        .getName());
161:                assertEquals("getName", ((Method) methods.get("name"))
162:                        .getName());
163:                // test case sensitive
164:                assertNull(methods.get("nAme"));
165:                assertEquals("getAge", ((Method) methods.get("age")).getName());
166:
167:            }
168:
169:            public void testNonGetter() throws Exception {
170:                final ClassFieldInspector ext = new ClassFieldInspector(
171:                        NonGetter.class);
172:                final Map methods = ext.getGetterMethods();
173:                assertEquals("getFoo", ((Method) methods.get("foo")).getName());
174:                assertEquals(5, methods.size());
175:                assertTrue(ext.getFieldNames().containsKey("foo"));
176:                assertTrue(ext.getFieldNames().containsKey("baz"));
177:                assertEquals(String.class, ext.getFieldTypes().get("foo"));
178:
179:            }
180:
181:            public void testWierdCapsForField() throws Exception {
182:                final ClassFieldInspector ext = new ClassFieldInspector(
183:                        Person.class);
184:                final Map methods = ext.getGetterMethods();
185:                assertEquals("getURI", ((Method) methods.get("URI")).getName());
186:                assertEquals(7, methods.size());
187:            }
188:
189:            static class NonGetter {
190:
191:                public int foo() {
192:                    return 42;
193:                }
194:
195:                public String getFoo() {
196:                    return "foo";
197:                }
198:
199:                public String baz() {
200:                    return "";
201:                }
202:
203:                public void bas() {
204:
205:                }
206:            }
207:
208:            static class Person {
209:                public static String aStaticString;
210:                private boolean happy;
211:                private String name;
212:                private int age;
213:                private String URI;
214:
215:                static {
216:                    aStaticString = "A static String";
217:                }
218:
219:                public int getAge() {
220:                    return this .age;
221:                }
222:
223:                public void setAge(final int age) {
224:                    this .age = age;
225:                }
226:
227:                public boolean isHappy() {
228:                    return this .happy;
229:                }
230:
231:                public void setHappy(final boolean happy) {
232:                    this .happy = happy;
233:                }
234:
235:                public String getName() {
236:                    return this .name;
237:                }
238:
239:                public void setName(final String name) {
240:                    this .name = name;
241:                }
242:
243:                //ignore this as it returns void type
244:                public void getNotAGetter() {
245:                    return;
246:                }
247:
248:                //ignore this as private
249:                private boolean isBogus() {
250:                    return false;
251:                }
252:
253:                //this will not show up as it is a getter that takes an argument
254:                public String getAlsoBad(final String s) {
255:                    return "ignored";
256:                }
257:
258:                //this should show up, as its a getter, but all CAPS
259:                public String getURI() {
260:                    return this .URI;
261:                }
262:
263:                public void setURI(final String URI) {
264:                    this .URI = URI;
265:                }
266:            }
267:
268:            static class SubPerson {
269:                private int childField;
270:
271:                /**
272:                 * @return the childField
273:                 */
274:                public int getChildField() {
275:                    return this .childField;
276:                }
277:
278:                /**
279:                 * @param childField the childField to set
280:                 */
281:                public void setChildField(final int childField) {
282:                    this.childField = childField;
283:                }
284:
285:            }
286:
287:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.