Source Code Cross Referenced for TestSchemaValidity.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:
023:        import org.apache.commons.betwixt.AbstractTestCase;
024:        import org.apache.commons.betwixt.examples.rss.Channel;
025:        import org.apache.commons.betwixt.examples.rss.Image;
026:        import org.apache.commons.betwixt.examples.rss.Item;
027:        import org.apache.commons.betwixt.examples.rss.TextInput;
028:        import org.apache.commons.betwixt.io.BeanWriter;
029:        import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
030:        import org.xml.sax.InputSource;
031:
032:        /**
033:         * Tests for the validity of the schema produced.
034:         * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
035:         * @version $Revision: 438373 $
036:         */
037:        public class TestSchemaValidity extends AbstractTestCase {
038:
039:            public TestSchemaValidity(String name) {
040:                super (name);
041:            }
042:
043:            private String generateSchema(Class clazz) throws Exception {
044:                SchemaTranscriber transcriber = new SchemaTranscriber();
045:                transcriber.getXMLIntrospector().getConfiguration()
046:                        .setAttributesForPrimitives(true);
047:                Schema schema = transcriber.generate(clazz);
048:
049:                StringWriter out = new StringWriter();
050:                out.write("<?xml version='1.0'?>");
051:                BeanWriter writer = new BeanWriter(out);
052:                writer.setBindingConfiguration(transcriber
053:                        .createSchemaBindingConfiguration());
054:                writer.getXMLIntrospector().setConfiguration(
055:                        transcriber.createSchemaIntrospectionConfiguration());
056:                writer.write(schema);
057:
058:                String xsd = out.getBuffer().toString();
059:                return xsd;
060:            }
061:
062:            public void testSimplestBeanWithAttributes() throws Exception {
063:                String xsd = generateSchema(SimplestBean.class);
064:
065:                StringWriter out = new StringWriter();
066:                out.write("<?xml version='1.0'?>");
067:                BeanWriter writer = new BeanWriter(out);
068:                writer.getXMLIntrospector().getConfiguration()
069:                        .setAttributesForPrimitives(true);
070:                writer.getXMLIntrospector().getConfiguration()
071:                        .getPrefixMapper().setPrefix(
072:                                SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI,
073:                                "xsi");
074:                writer.getBindingConfiguration().setMapIDs(false);
075:                SimplestBean bean = new SimplestBean("Simon");
076:                writer.write(bean);
077:
078:                String xml = out.getBuffer().toString();
079:
080:                xmlAssertIsValid(new InputSource(new StringReader(xml)),
081:                        new InputSource(new StringReader(xsd)));
082:            }
083:
084:            public void testSimplestBeanWithElements() throws Exception {
085:                String xsd = generateSchema(SimplestElementBean.class);
086:
087:                StringWriter out = new StringWriter();
088:                out.write("<?xml version='1.0'?>");
089:                BeanWriter writer = new BeanWriter(out);
090:                writer.getXMLIntrospector().getConfiguration()
091:                        .setAttributesForPrimitives(true);
092:                writer.getXMLIntrospector().getConfiguration()
093:                        .getPrefixMapper().setPrefix(
094:                                SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI,
095:                                "xsi");
096:                writer.getBindingConfiguration().setMapIDs(false);
097:                SimplestElementBean bean = new SimplestElementBean("Simon");
098:                writer.write(bean);
099:
100:                String xml = out.getBuffer().toString();
101:
102:                xmlAssertIsValid(new InputSource(new StringReader(xml)),
103:                        new InputSource(new StringReader(xsd)));
104:            }
105:
106:            public void testSimpleBean() throws Exception {
107:                String xsd = generateSchema(SimpleBean.class);
108:
109:                StringWriter out = new StringWriter();
110:                out.write("<?xml version='1.0'?>");
111:                BeanWriter writer = new BeanWriter(out);
112:                writer.getXMLIntrospector().getConfiguration()
113:                        .setAttributesForPrimitives(true);
114:                writer.getXMLIntrospector().getConfiguration()
115:                        .getPrefixMapper().setPrefix(
116:                                SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI,
117:                                "xsi");
118:                writer.getBindingConfiguration().setMapIDs(false);
119:                SimpleBean bean = new SimpleBean("One", "Two", "A",
120:                        "One, Two, Three, Four");
121:                writer.write(bean);
122:
123:                String xml = out.getBuffer().toString();
124:
125:                xmlAssertIsValid(new InputSource(new StringReader(xml)),
126:                        new InputSource(new StringReader(xsd)));
127:            }
128:
129:            private String generateOrderLineSchema() throws Exception {
130:                SchemaTranscriber transcriber = new SchemaTranscriber();
131:                transcriber.getXMLIntrospector().getConfiguration()
132:                        .setAttributesForPrimitives(true);
133:                transcriber.getXMLIntrospector().getConfiguration()
134:                        .setAttributeNameMapper(new HyphenatedNameMapper());
135:                Schema schema = transcriber.generate(OrderLineBean.class);
136:
137:                StringWriter out = new StringWriter();
138:                out.write("<?xml version='1.0'?>");
139:                BeanWriter writer = new BeanWriter(out);
140:                writer.setBindingConfiguration(transcriber
141:                        .createSchemaBindingConfiguration());
142:                writer.getXMLIntrospector().setConfiguration(
143:                        transcriber.createSchemaIntrospectionConfiguration());
144:                writer.write(schema);
145:
146:                String xsd = out.getBuffer().toString();
147:                return xsd;
148:            }
149:
150:            public void testOrderLine() throws Exception {
151:
152:                String xsd = generateOrderLineSchema();
153:                StringWriter out = new StringWriter();
154:                out.write("<?xml version='1.0'?>");
155:                BeanWriter writer = new BeanWriter(out);
156:                writer.getXMLIntrospector().getConfiguration()
157:                        .setAttributesForPrimitives(true);
158:                writer.getXMLIntrospector().getConfiguration()
159:                        .setAttributeNameMapper(new HyphenatedNameMapper());
160:                writer.getXMLIntrospector().getConfiguration()
161:                        .getPrefixMapper().setPrefix(
162:                                SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI,
163:                                "xsi");
164:                writer.getBindingConfiguration().setMapIDs(false);
165:                OrderLineBean bean = new OrderLineBean(3, new ProductBean(
166:                        "00112234", "A11", "Fat Fish", "A Fat Fish"));
167:                writer.write(bean);
168:
169:                String xml = out.getBuffer().toString();
170:
171:                xmlAssertIsValid(new InputSource(new StringReader(xml)),
172:                        new InputSource(new StringReader(xsd)));
173:            }
174:
175:            private String generateOrderSchema() throws Exception {
176:                SchemaTranscriber transcriber = new SchemaTranscriber();
177:                transcriber.getXMLIntrospector().getConfiguration()
178:                        .setElementNameMapper(new HyphenatedNameMapper());
179:                transcriber.getXMLIntrospector().getConfiguration()
180:                        .setAttributeNameMapper(new HyphenatedNameMapper());
181:                transcriber.getXMLIntrospector().getConfiguration()
182:                        .setAttributesForPrimitives(true);
183:                transcriber.getXMLIntrospector().getConfiguration()
184:                        .setWrapCollectionsInElement(false);
185:                Schema schema = transcriber.generate(OrderBean.class);
186:
187:                StringWriter out = new StringWriter();
188:                out.write("<?xml version='1.0'?>");
189:                BeanWriter writer = new BeanWriter(out);
190:                writer.setBindingConfiguration(transcriber
191:                        .createSchemaBindingConfiguration());
192:                writer.getXMLIntrospector().setConfiguration(
193:                        transcriber.createSchemaIntrospectionConfiguration());
194:                writer.write(schema);
195:
196:                String xsd = out.getBuffer().toString();
197:                return xsd;
198:            }
199:
200:            public void testOrder() throws Exception {
201:                String xsd = generateOrderSchema();
202:                StringWriter out = new StringWriter();
203:                out.write("<?xml version='1.0'?>");
204:                BeanWriter writer = new BeanWriter(out);
205:                writer.getXMLIntrospector().getConfiguration()
206:                        .setElementNameMapper(new HyphenatedNameMapper());
207:                writer.getXMLIntrospector().getConfiguration()
208:                        .setAttributeNameMapper(new HyphenatedNameMapper());
209:                writer.getXMLIntrospector().getConfiguration()
210:                        .setAttributesForPrimitives(true);
211:                writer.getXMLIntrospector().getConfiguration()
212:                        .setWrapCollectionsInElement(false);
213:                writer.getBindingConfiguration().setMapIDs(false);
214:
215:                OrderBean bean = new OrderBean("XA-2231", new CustomerBean(
216:                        "PB34", "Mr Abbot", "1, Skipton Road", "Shipley",
217:                        "Merry England", "BD4 8KL"));
218:                bean.addLine(new OrderLineBean(4, new ProductBean("00112234",
219:                        "A11", "Taylor's Landlord", "Taylor's Landlord")));
220:                bean.addLine(new OrderLineBean(5, new ProductBean("00112235",
221:                        "A13", "Black Sheep Special", "Black Sheep Special")));
222:                writer.write(bean);
223:
224:                String xml = out.getBuffer().toString();
225:
226:                xmlAssertIsValid(new InputSource(new StringReader(xml)),
227:                        new InputSource(new StringReader(xsd)));
228:
229:            }
230:
231:            private String generateRSSSchema() throws Exception {
232:                SchemaTranscriber transcriber = new SchemaTranscriber();
233:                Schema schema = transcriber.generate(Channel.class);
234:
235:                StringWriter out = new StringWriter();
236:                out.write("<?xml version='1.0'?>");
237:                BeanWriter writer = new BeanWriter(out);
238:                writer.setBindingConfiguration(transcriber
239:                        .createSchemaBindingConfiguration());
240:                writer.getXMLIntrospector().setConfiguration(
241:                        transcriber.createSchemaIntrospectionConfiguration());
242:                writer.write(schema);
243:
244:                String xsd = out.getBuffer().toString();
245:                return xsd;
246:            }
247:
248:            public void testRSS() throws Exception {
249:                String xsd = generateRSSSchema();
250:                StringWriter out = new StringWriter();
251:                out.write("<?xml version='1.0'?>");
252:                BeanWriter writer = new BeanWriter(out);
253:                writer.getBindingConfiguration().setMapIDs(false);
254:
255:                Channel channel = new Channel();
256:                channel.setTitle("Betwixt News");
257:                channel.setLink("http://jakarta.apache.org/commons/betwixt");
258:                channel.setDescription("Example feed themed on Betwixt news.");
259:                channel
260:                        .setRating("(PICS-1.1 'http://www.rsac.org/ratingsv01.html'"
261:                                + " 2 gen true comment 'RSACi North America Server'"
262:                                + " for 'http://www.rsac.org' on '1996.04.16T08:15-0500'"
263:                                + " r (n 0 s 0 v 0 l 0))");
264:                channel.setLanguage("en-UK");
265:
266:                Image image = new Image();
267:                image.setTitle("Apache Feather");
268:                image.setURL("http://www.apache.org/images/asf_logo_wide.gif");
269:                image.setLink("http://www.apache.org");
270:                image.setWidth(100);
271:                image.setHeight(30);
272:                image.setDescription("Example image");
273:                channel.setImage(image);
274:
275:                Item itemOne = new Item();
276:                itemOne.setTitle("Betwixt now generates w3c schema!");
277:                itemOne.setLink("http://jakarta.apache.org/commons/betwixt");
278:                itemOne.setDescription("Example description");
279:                channel.addItem(itemOne);
280:
281:                Item itemTwo = new Item();
282:                itemTwo.setTitle("Another News Item");
283:                itemTwo.setLink("http://jakarta.apache.org/commons/betwixt");
284:                itemTwo.setDescription("Blah Blah Blah");
285:                channel.addItem(itemTwo);
286:
287:                TextInput textInput = new TextInput();
288:                textInput.setTitle("Send");
289:                textInput.setDescription("Comments about Betwixt news");
290:                textInput.setName("Response text");
291:                textInput.setLink("http://jakarta.apache.org/commons/betwixt");
292:                channel.setTextInput(textInput);
293:
294:                writer.write(channel);
295:
296:                String xml = out.getBuffer().toString();
297:
298:                xmlAssertIsValid(new InputSource(new StringReader(xml)),
299:                        new InputSource(new StringReader(xsd)));
300:
301:            }
302:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.