Source Code Cross Referenced for TestSchema.java in  » Library » Apache-commons-betwixt-0.8-src » org » apache » commons » betwixt » schema » 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.schema 
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.schema;
019:
020:        import java.io.StringReader;
021:        import java.io.StringWriter;
022:        import java.io.Writer;
023:
024:        import junit.framework.Test;
025:        import junit.framework.TestSuite;
026:
027:        import org.apache.commons.betwixt.AbstractTestCase;
028:        import org.apache.commons.betwixt.XMLIntrospector;
029:        import org.apache.commons.betwixt.io.BeanReader;
030:        import org.apache.commons.betwixt.io.BeanWriter;
031:        import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
032:        import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
033:        import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
034:
035:        //import org.apache.commons.logging.impl.SimpleLog;
036:        //import org.apache.commons.betwixt.io.BeanRuleSet;
037:
038:        /**
039:         * This will test betwixt on handling a different kind of xml file, without
040:         * a "collection" tag.
041:         * 
042:         * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
043:         * @version $Id: TestSchema.java 438373 2006-08-30 05:17:21Z bayard $
044:         */
045:        public class TestSchema extends AbstractTestCase {
046:
047:            public static Test suite() {
048:                return new TestSuite(TestSchema.class);
049:            }
050:
051:            public TestSchema(String testName) {
052:                super (testName);
053:            }
054:
055:            /**
056:             * Test the roundtrip with an xml file that doesn't have
057:             * collection elements, writes it with collection elements
058:             * and then compares the 2 object, which should end up
059:             * equal..
060:             */
061:            public void testCombinedRoundTrip() throws Exception {
062:                //        SimpleLog log = new SimpleLog("[CombinedRoundTrip:BeanRuleSet]");
063:                //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
064:                //        BeanRuleSet.setLog(log);
065:
066:                //        log = new SimpleLog("[CombinedRoundTrip]");
067:                //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
068:
069:                BeanReader reader = createBeanReader();
070:
071:                PhysicalSchema schema = (PhysicalSchema) reader
072:                        .parse(getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
073:                StringWriter buffer = new StringWriter();
074:                write(schema, buffer, true);
075:
076:                //        log.debug(buffer.getBuffer().toString());
077:
078:                StringReader in = new StringReader(buffer.getBuffer()
079:                        .toString());
080:                reader = createBeanReader();
081:                XMLIntrospector intro = createXMLIntrospector();
082:                DefaultXMLBeanInfoRegistry registry = new DefaultXMLBeanInfoRegistry();
083:                intro.setRegistry(registry);
084:                // we have written the xml file back with element collections,
085:                // so we have to say to the reader we want to use that now
086:                // (the default when creating in this test is not to use them)
087:                intro.getConfiguration().setWrapCollectionsInElement(true);
088:                // first flush the cash, else setting other options, doesn't
089:                // end up in rereading / mapping the object model.
090:                registry.flush();
091:                // set the xmlIntrospector back to the reader
092:                reader.setXMLIntrospector(intro);
093:                reader.deregisterBeanClass(PhysicalSchema.class);
094:                reader.getRules().clear();
095:                reader.registerBeanClass(PhysicalSchema.class);
096:                PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
097:                buffer.close();
098:                write(schema, buffer, true);
099:                assertEquals(schema, schemaSecond);
100:            }
101:
102:            /**
103:             * Tests we can round trip from the XML -> bean -> XML -> bean.
104:             * It will test if both object are identical.
105:             * For this to actually work I implemented a details equals in my
106:             * Beans..
107:             */
108:            public void testRoundTripWithoutCollectionElement()
109:                    throws Exception {
110:                BeanReader reader = createBeanReader();
111:                PhysicalSchema schema = (PhysicalSchema) reader
112:                        .parse(getTestFileURL("src/test/org/apache/commons/betwixt/schema/schema.xml"));
113:                StringWriter buffer = new StringWriter();
114:                write(schema, buffer, false);
115:                StringReader in = new StringReader(buffer.getBuffer()
116:                        .toString());
117:                PhysicalSchema schemaSecond = (PhysicalSchema) reader.parse(in);
118:                assertEquals(schemaSecond, schema);
119:            }
120:
121:            /**
122:             * Creates a beanReader
123:             */
124:            protected BeanReader createBeanReader() throws Exception {
125:                BeanReader reader = new BeanReader();
126:                reader.setXMLIntrospector(createXMLIntrospector());
127:                // register the class which maps to the root element
128:                // of the xml file (this depends on the NameMapper used.
129:                reader.registerBeanClass(PhysicalSchema.class);
130:                return reader;
131:            }
132:
133:            /**
134:             * Set up the XMLIntroSpector
135:             */
136:            protected XMLIntrospector createXMLIntrospector() {
137:                XMLIntrospector introspector = new XMLIntrospector();
138:
139:                // set elements for attributes to true
140:                introspector.getConfiguration()
141:                        .setAttributesForPrimitives(true);
142:
143:                // Since we don't want to have collectionelements 
144:                // line <DBMSS>, we have to set this to false,
145:                // since the default is true.
146:                introspector.getConfiguration().setWrapCollectionsInElement(
147:                        false);
148:
149:                // We have to use the HyphenatedNameMapper
150:                // Since we want the names to resolve from eg PhysicalSchema
151:                // to PHYSICAL_SCHEMA.
152:                // we pass to the mapper we want uppercase and use _ for name
153:                // seperation.
154:                // This will set our ElementMapper.
155:                introspector.getConfiguration().setElementNameMapper(
156:                        new HyphenatedNameMapper(true, "_"));
157:                // since our attribute names will use a different 
158:                // naming convention in our xml file (just all lowercase)
159:                // we set another mapper for the attributes
160:                introspector.getConfiguration().setAttributeNameMapper(
161:                        new DecapitalizeNameMapper());
162:
163:                return introspector;
164:            }
165:
166:            /**
167:             * Opens a writer and writes an object model according to the
168:             * retrieved bean
169:             */
170:            private void write(Object bean, Writer out,
171:                    boolean wrapCollectionsInElement) throws Exception {
172:                BeanWriter writer = new BeanWriter(out);
173:                writer.setWriteEmptyElements(true);
174:                writer.setXMLIntrospector(createXMLIntrospector());
175:                // specifies weather to use collection elements or not.
176:                writer.getXMLIntrospector().getConfiguration()
177:                        .setWrapCollectionsInElement(wrapCollectionsInElement);
178:                // we don't want to write Id attributes to every element
179:                // we just want our opbject model written nothing more..
180:                writer.getBindingConfiguration().setMapIDs(false);
181:                // the source has 2 spaces indention and \n as line seperator.
182:                writer.setIndent("  ");
183:                writer.setEndOfLine("\n");
184:                writer.write(bean);
185:            }
186:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.