Source Code Cross Referenced for TestXmlToBean.java in  » Library » Apache-commons-betwixt-0.8-src » org » apache » commons » betwixt » dotbetwixt » 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.dotbetwixt 
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.dotbetwixt;
019:
020:        import java.io.StringReader;
021:        import java.io.StringWriter;
022:        import java.util.List;
023:
024:        import junit.framework.Test;
025:        import junit.framework.TestSuite;
026:
027:        import org.apache.commons.betwixt.ElementDescriptor;
028:        import org.apache.commons.betwixt.XMLBeanInfo;
029:        import org.apache.commons.betwixt.XMLIntrospector;
030:        import org.apache.commons.betwixt.io.BeanReader;
031:        import org.apache.commons.betwixt.io.BeanWriter;
032:        import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
033:        import org.apache.commons.betwixt.xmlunit.XmlTestCase;
034:
035:        /** 
036:         * Test customization of xml to bean mapping using .betwixt files.
037:         *
038:         * @author Robert Burrell Donkin
039:         */
040:        public class TestXmlToBean extends XmlTestCase {
041:
042:            //--------------------------------- Test Suite
043:
044:            public static Test suite() {
045:                return new TestSuite(TestXmlToBean.class);
046:            }
047:
048:            //--------------------------------- Constructor
049:
050:            public TestXmlToBean(String testName) {
051:                super (testName);
052:            }
053:
054:            //---------------------------------- Tests
055:
056:            public void testForceAccessibleSuper() throws Exception {
057:                XMLIntrospector xmlIntrospector = new XMLIntrospector();
058:                XMLBeanInfo xmlBeanInfo = xmlIntrospector
059:                        .introspect(MixedUpdatersBean.class);
060:                ElementDescriptor[] descriptors = xmlBeanInfo
061:                        .getElementDescriptor().getElementDescriptors();
062:                boolean propertyFound = false;
063:                for (int i = 0; i < descriptors.length; i++) {
064:                    ElementDescriptor descriptor = descriptors[i];
065:                    if ("private-super".equals(descriptor.getLocalName())) {
066:                        propertyFound = true;
067:                        assertNotNull("Updater found", descriptor.getUpdater());
068:                        assertNotNull("Expression found", descriptor
069:                                .getTextExpression());
070:                    }
071:                }
072:                assertTrue("Found inaccessible super methods", propertyFound);
073:            }
074:
075:            public void testCustomUpdaters() throws Exception {
076:                // might as well check writer whilst we're at it
077:                MixedUpdatersBean bean = new MixedUpdatersBean("Lov");
078:                bean.badNameSetter("Hate");
079:                bean.addItem("White");
080:                bean.badItemAdder("Black");
081:                bean.addItem("Life");
082:                bean.badItemAdder("Death");
083:                bean.privatePropertyWorkaroundSetter("Private");
084:                bean.getPrivateItems().add("private item 1");
085:                bean.privateField = 100;
086:
087:                StringWriter out = new StringWriter();
088:                out.write("<?xml version='1.0'?>");
089:                BeanWriter writer = new BeanWriter(out);
090:
091:                writer.getBindingConfiguration().setMapIDs(false);
092:                writer.write(bean);
093:
094:                String xml = "<?xml version='1.0'?><mixed><name>Lov</name><bad-name>Hate</bad-name>"
095:                        + "<items><item>White</item><item>Life</item></items>"
096:                        + "<bad-items><bad-item>Black</bad-item><bad-item>Death</bad-item></bad-items>"
097:                        + "<private-property>Private</private-property>"
098:                        + "<private-items><private-item>private item 1</private-item></private-items>"
099:                        + "<private-super>100</private-super>" + "</mixed>";
100:
101:                xmlAssertIsomorphicContent(parseString(xml), parseString(out
102:                        .toString()), true);
103:
104:                // now we'll test reading via round tripping
105:                BeanReader reader = new BeanReader();
106:                reader.getBindingConfiguration().setMapIDs(false);
107:                reader.registerBeanClass("mixed", MixedUpdatersBean.class);
108:                bean = (MixedUpdatersBean) reader.parse(new StringReader(xml));
109:
110:                assertEquals("Name incorrect", "Lov", bean.getName());
111:                assertEquals("BadName incorrect", "Hate", bean.getBadName());
112:                List items = bean.getItems();
113:                assertEquals("Wrong number of items", 2, items.size());
114:                assertEquals("Item one wrong", "White", items.get(0));
115:                assertEquals("Item two wrong", "Life", items.get(1));
116:                List badItems = bean.getBadItems();
117:                assertEquals("Wrong number of bad items", 2, badItems.size());
118:                // awaiting implementation
119:                //assertEquals("Bad item one wrong", "Black", badItems.get(0));
120:                //assertEquals("Bad item two wrong", "Death", badItems.get(1));       
121:                assertEquals("Private property incorrect", "Private", bean
122:                        .getPrivateProperty());
123:
124:                //this shows that a private adder can be utilized
125:                List privateItems = bean.getPrivateItems();
126:                assertEquals("Wrong number of private items", 1, privateItems
127:                        .size());
128:                //TODO can't assert contents - gets the right number of items, but each is null (badItems, too)
129:                assertEquals("Private property accessed on super", 100,
130:                        bean.privateField);
131:            }
132:
133:            /** Test output of bean with mixed content */
134:            public void testMixedContent() throws Exception {
135:
136:                StringReader xml = new StringReader(
137:                        "<?xml version='1.0' encoding='UTF-8'?><deep-thought alpha='Life' gamma='42'>"
138:                                + "The Universe And Everything</deep-thought>");
139:
140:                BeanReader reader = new BeanReader();
141:                reader.registerBeanClass(MixedContentOne.class);
142:                Object resultObject = reader.parse(xml);
143:                assertEquals("Object is MixedContentOne", true,
144:                        resultObject instanceof  MixedContentOne);
145:                MixedContentOne result = (MixedContentOne) resultObject;
146:                assertEquals("Property Alpha matches", "Life", result
147:                        .getAlpha());
148:                assertEquals("Property Beta matches",
149:                        "The Universe And Everything", result.getBeta());
150:                assertEquals("Property Gamma matches", 42, result.getGamma());
151:            }
152:
153:            /** Tests basic use of an implementation for an interface */
154:            public void _testBasicInterfaceImpl() throws Exception {
155:
156:                ExampleBean bean = new ExampleBean("Alice");
157:                bean.addExample(new ExampleImpl(1, "Mad Hatter"));
158:                bean.addExample(new ExampleImpl(2, "March Hare"));
159:                bean.addExample(new ExampleImpl(3, "Dormouse"));
160:
161:                String xml = "<?xml version='1.0' encoding='UTF-8'?>"
162:                        + "<example-bean><name>Alice</name>"
163:                        + "<example><id>1</id><name>Mad Hatter</name></example>"
164:                        + "<example><id>2</id><name>March Hare</name></example>"
165:                        + "<example><id>3</id><name>Dormouse</name></example>"
166:                        + "</example-bean>";
167:
168:                BeanReader reader = new BeanReader();
169:                reader.getXMLIntrospector().getConfiguration()
170:                        .setElementNameMapper(new HyphenatedNameMapper());
171:                reader.getXMLIntrospector().getConfiguration()
172:                        .setWrapCollectionsInElement(false);
173:                reader.registerBeanClass(ExampleBean.class);
174:
175:                StringReader in = new StringReader(xml);
176:                ExampleBean out = (ExampleBean) reader.parse(in);
177:                assertEquals("Interface read failed", bean, out);
178:
179:            }
180:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.