Source Code Cross Referenced for TestXMLIntrospector.java in  » Library » Apache-commons-betwixt-0.8-src » org » apache » commons » betwixt » introspection » 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 betwixt 0.8 src » org.apache.commons.betwixt.introspection 
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.betwixt.introspection;
019:
020:        import java.beans.BeanInfo;
021:        import java.beans.Introspector;
022:        import java.beans.PropertyDescriptor;
023:        import java.io.StringWriter;
024:
025:        import junit.framework.Test;
026:        import junit.framework.TestSuite;
027:        import junit.textui.TestRunner;
028:
029:        import org.apache.commons.betwixt.AbstractTestCase;
030:        import org.apache.commons.betwixt.AttributeDescriptor;
031:        import org.apache.commons.betwixt.ElementDescriptor;
032:        import org.apache.commons.betwixt.XMLBeanInfo;
033:        import org.apache.commons.betwixt.XMLIntrospector;
034:        import org.apache.commons.betwixt.io.BeanWriter;
035:        import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
036:        import org.apache.commons.betwixt.registry.NoCacheRegistry;
037:        import org.apache.commons.betwixt.strategy.ClassNormalizer;
038:        import org.apache.commons.betwixt.strategy.ListedClassNormalizer;
039:        import org.apache.commons.digester.rss.Channel;
040:
041:        /** Test harness for the XMLIntrospector
042:         *
043:         * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
044:         * @version $Revision: 438373 $
045:         */
046:        public class TestXMLIntrospector extends AbstractTestCase {
047:
048:            public static void main(String[] args) {
049:                TestRunner.run(suite());
050:            }
051:
052:            public static Test suite() {
053:                return new TestSuite(TestXMLIntrospector.class);
054:            }
055:
056:            public TestXMLIntrospector(String testName) {
057:                super (testName);
058:            }
059:
060:            public void testIntrospector() throws Exception {
061:                //SimpleLog log = new SimpleLog("testIntrospector:introspector");
062:                //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
063:                XMLIntrospector introspector = new XMLIntrospector();
064:                //introspector.setLog(log);
065:
066:                introspector.getConfiguration()
067:                        .setAttributesForPrimitives(true);
068:
069:                Object bean = createBean();
070:
071:                XMLBeanInfo info = introspector.introspect(bean);
072:
073:                assertTrue("Found XMLBeanInfo", info != null);
074:
075:                ElementDescriptor descriptor = info.getElementDescriptor();
076:
077:                assertTrue("Found root element descriptor", descriptor != null);
078:
079:                AttributeDescriptor[] attributes = descriptor
080:                        .getAttributeDescriptors();
081:
082:                assertTrue("Found attributes", attributes != null
083:                        && attributes.length > 0);
084:
085:                // test second introspection with caching on
086:                info = introspector.introspect(bean);
087:
088:                assertTrue("Found XMLBeanInfo", info != null);
089:
090:                descriptor = info.getElementDescriptor();
091:
092:                assertTrue("Found root element descriptor", descriptor != null);
093:
094:                attributes = descriptor.getAttributeDescriptors();
095:
096:                assertTrue("Found attributes", attributes != null
097:                        && attributes.length > 0);
098:
099:                // test introspection with caching off      
100:                //introspector.setCachingEnabled(false);  
101:                introspector.setRegistry(new NoCacheRegistry());
102:                info = introspector.introspect(bean);
103:
104:                assertTrue("Found XMLBeanInfo", info != null);
105:
106:                descriptor = info.getElementDescriptor();
107:
108:                assertTrue("Found root element descriptor", descriptor != null);
109:
110:                attributes = descriptor.getAttributeDescriptors();
111:
112:                assertTrue("Found attributes", attributes != null
113:                        && attributes.length > 0);
114:
115:                // test introspection after flushing cache
116:                //        introspector.setCachingEnabled(true);
117:                introspector.setRegistry(new DefaultXMLBeanInfoRegistry());
118:                //introspector.flushCache();
119:                info = introspector.introspect(bean);
120:
121:                assertTrue("Found XMLBeanInfo", info != null);
122:
123:                descriptor = info.getElementDescriptor();
124:
125:                assertTrue("Found root element descriptor", descriptor != null);
126:
127:                attributes = descriptor.getAttributeDescriptors();
128:
129:                assertTrue("Found attributes", attributes != null
130:                        && attributes.length > 0);
131:
132:            }
133:
134:            public void testBeanWithBeanInfo() throws Exception {
135:
136:                // let's check that bean info's ok
137:                BeanInfo bwbiBeanInfo = Introspector
138:                        .getBeanInfo(BeanWithBeanInfoBean.class);
139:
140:                PropertyDescriptor[] propertyDescriptors = bwbiBeanInfo
141:                        .getPropertyDescriptors();
142:
143:                assertEquals("Wrong number of properties", 2,
144:                        propertyDescriptors.length);
145:
146:                // order of properties isn't guarenteed 
147:                if ("alpha".equals(propertyDescriptors[0].getName())) {
148:
149:                    assertEquals("Second property name", "gamma",
150:                            propertyDescriptors[1].getName());
151:
152:                } else {
153:
154:                    assertEquals("First property name", "gamma",
155:                            propertyDescriptors[0].getName());
156:                    assertEquals("Second property name", "alpha",
157:                            propertyDescriptors[1].getName());
158:                }
159:
160:                // finished with the descriptors
161:                propertyDescriptors = null;
162:
163:                //        SimpleLog log = new SimpleLog("[testBeanWithBeanInfo:XMLIntrospector]");
164:                //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
165:
166:                XMLIntrospector introspector = new XMLIntrospector();
167:                introspector.getConfiguration().setAttributesForPrimitives(
168:                        false);
169:                //        introspector.setLog(log);
170:
171:                XMLBeanInfo xmlBeanInfo = introspector
172:                        .introspect(BeanWithBeanInfoBean.class);
173:
174:                ElementDescriptor[] elementDescriptors = xmlBeanInfo
175:                        .getElementDescriptor().getElementDescriptors();
176:
177:                //        log = new SimpleLog("[testBeanWithBeanInfo]");
178:                //        log.setLevel(SimpleLog.LOG_LEVEL_DEBUG);
179:
180:                //        log.debug("XMLBeanInfo:");
181:                //        log.debug(xmlBeanInfo);
182:                //        log.debug("Elements:");
183:                //        log.debug(elementDescriptors[0].getPropertyName());
184:                //        log.debug(elementDescriptors[1].getPropertyName());
185:
186:                assertEquals("Wrong number of elements", 2,
187:                        elementDescriptors.length);
188:
189:                // order of properties isn't guarenteed 
190:                boolean alphaFirst = true;
191:                if ("alpha".equals(elementDescriptors[0].getPropertyName())) {
192:
193:                    assertEquals("Second element name", "gamma",
194:                            elementDescriptors[1].getPropertyName());
195:
196:                } else {
197:                    alphaFirst = false;
198:                    assertEquals("First element name", "gamma",
199:                            elementDescriptors[0].getPropertyName());
200:                    assertEquals("Second element name", "alpha",
201:                            elementDescriptors[1].getPropertyName());
202:                }
203:
204:                // might as well give test output
205:                StringWriter out = new StringWriter();
206:                BeanWriter writer = new BeanWriter(out);
207:                writer.getBindingConfiguration().setMapIDs(false);
208:                BeanWithBeanInfoBean bean = new BeanWithBeanInfoBean(
209:                        "alpha value", "beta value", "gamma value");
210:                writer.write(bean);
211:
212:                if (alphaFirst) {
213:
214:                    xmlAssertIsomorphicContent(
215:                            parseFile("src/test/org/apache/commons/betwixt/introspection/test-bwbi-output-a.xml"),
216:                            parseString(out.toString()));
217:
218:                } else {
219:                    xmlAssertIsomorphicContent(
220:                            parseFile("src/test/org/apache/commons/betwixt/introspection/test-bwbi-output-g.xml"),
221:                            parseString(out.toString()));
222:                }
223:            }
224:
225:            public void testDefaultClassNormalizer() throws Exception {
226:                XMLIntrospector introspector = new XMLIntrospector();
227:
228:                FaceImpl face = new FaceImpl();
229:                XMLBeanInfo info = introspector.introspect(face);
230:                ElementDescriptor elementDescriptor = info
231:                        .getElementDescriptor();
232:
233:                AttributeDescriptor[] attributeDescriptor = elementDescriptor
234:                        .getAttributeDescriptors();
235:                ElementDescriptor[] children = elementDescriptor
236:                        .getElementDescriptors();
237:
238:                assertEquals("Expected no attributes", 0,
239:                        attributeDescriptor.length);
240:                assertEquals("Expected two elements", 2, children.length);
241:            }
242:
243:            public void testClassNormalizer() throws Exception {
244:                XMLIntrospector introspector = new XMLIntrospector();
245:                introspector.getConfiguration().setClassNormalizer(
246:                        new ClassNormalizer() {
247:
248:                            public Class normalize(Class clazz) {
249:                                if (IFace.class.isAssignableFrom(clazz)) {
250:                                    return IFace.class;
251:                                }
252:                                return super .normalize(clazz);
253:                            }
254:                        });
255:
256:                FaceImpl face = new FaceImpl();
257:                XMLBeanInfo info = introspector.introspect(face);
258:                ElementDescriptor elementDescriptor = info
259:                        .getElementDescriptor();
260:                assertEquals("Expected only itself", 1, elementDescriptor
261:                        .getElementDescriptors().length);
262:
263:                AttributeDescriptor[] attributeDescriptor = elementDescriptor
264:                        .getAttributeDescriptors();
265:                ElementDescriptor[] children = elementDescriptor
266:                        .getElementDescriptors();
267:
268:                assertEquals("Expected no attributes", 0,
269:                        attributeDescriptor.length);
270:                assertEquals("Expected one elements", 1, children.length);
271:                assertEquals("Expected element", "name", children[0]
272:                        .getLocalName());
273:            }
274:
275:            public void testListedClassNormalizer() throws Exception {
276:                ListedClassNormalizer classNormalizer = new ListedClassNormalizer();
277:                classNormalizer.addSubstitution(IFace.class);
278:                XMLIntrospector introspector = new XMLIntrospector();
279:                introspector.getConfiguration().setClassNormalizer(
280:                        classNormalizer);
281:
282:                FaceImpl face = new FaceImpl();
283:
284:                XMLBeanInfo info = introspector.introspect(face);
285:                ElementDescriptor elementDescriptor = info
286:                        .getElementDescriptor();
287:                AttributeDescriptor[] attributeDescriptor = elementDescriptor
288:                        .getAttributeDescriptors();
289:                ElementDescriptor[] children = elementDescriptor
290:                        .getElementDescriptors();
291:
292:                assertEquals("Expected no attributes", 0,
293:                        attributeDescriptor.length);
294:                assertEquals("Expected one elements", 1, children.length);
295:                assertEquals("Expected element", "name", children[0]
296:                        .getLocalName());
297:            }
298:
299:            public void testListedClassNormalizerWrite() throws Exception {
300:                ListedClassNormalizer classNormalizer = new ListedClassNormalizer();
301:                classNormalizer.addSubstitution(IFace.class);
302:
303:                StringWriter out = new StringWriter();
304:                out.write("<?xml version='1.0'?>");
305:                BeanWriter writer = new BeanWriter(out);
306:                writer.getBindingConfiguration().setMapIDs(false);
307:                writer.getXMLIntrospector().getConfiguration()
308:                        .setClassNormalizer(classNormalizer);
309:                FaceImpl bean = new FaceImpl();
310:                bean.setName("Old Tom Cobbly");
311:                writer.write(bean);
312:
313:                String xml = "<?xml version='1.0'?><IFace><name>Old Tom Cobbly</name></IFace>";
314:                xmlAssertIsomorphicContent(parseString(out.getBuffer()
315:                        .toString()), parseString(xml), true);
316:            }
317:
318:            public void testBetwixtFileType() throws Exception {
319:                XMLIntrospector introspector = new XMLIntrospector();
320:                XMLBeanInfo info = introspector.introspect(Channel.class);
321:
322:                ElementDescriptor elementDescriptor = info
323:                        .getElementDescriptor();
324:
325:                Class clazz = elementDescriptor.getSingularPropertyType();
326:                assertEquals("Element type correct", Channel.class, clazz);
327:
328:                assertEquals("Element name correct", "rss", elementDescriptor
329:                        .getLocalName());
330:            }
331:
332:            public void testIgnoreAllBeanInfo() throws Exception {
333:                XMLIntrospector introspector = new XMLIntrospector();
334:                introspector.getConfiguration().setIgnoreAllBeanInfo(false);
335:                introspector.setRegistry(new NoCacheRegistry());
336:                XMLBeanInfo info = introspector
337:                        .introspect(BeanWithBeanInfoBean.class);
338:                ElementDescriptor[] elementDescriptors = info
339:                        .getElementDescriptor().getElementDescriptors();
340:                // When BeanInfo is used the properties alpha and gamma will be found 
341:                if ("alpha".equals(elementDescriptors[0].getPropertyName())) {
342:                    assertEquals("Second element name", "gamma",
343:                            elementDescriptors[1].getPropertyName());
344:                } else {
345:                    assertEquals("First element name", "gamma",
346:                            elementDescriptors[0].getPropertyName());
347:                    assertEquals("Second element name", "alpha",
348:                            elementDescriptors[1].getPropertyName());
349:                }
350:
351:                introspector.getConfiguration().setIgnoreAllBeanInfo(true);
352:                info = introspector.introspect(BeanWithBeanInfoBean.class);
353:                elementDescriptors = info.getElementDescriptor()
354:                        .getElementDescriptors();
355:                // When BeanInfo is ignored the properties alpha and beta will be found
356:                if ("alpha".equals(elementDescriptors[0].getPropertyName())) {
357:                    assertEquals("Second element name", "beta",
358:                            elementDescriptors[1].getPropertyName());
359:                } else {
360:                    assertEquals("First element name", "beta",
361:                            elementDescriptors[0].getPropertyName());
362:                    assertEquals("Second element name", "alpha",
363:                            elementDescriptors[1].getPropertyName());
364:                }
365:            }
366:
367:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.