KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > schema > TestSchemaValidity


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.betwixt.schema;
19
20 import java.io.StringReader JavaDoc;
21 import java.io.StringWriter JavaDoc;
22
23 import org.apache.commons.betwixt.AbstractTestCase;
24 import org.apache.commons.betwixt.examples.rss.Channel;
25 import org.apache.commons.betwixt.examples.rss.Image;
26 import org.apache.commons.betwixt.examples.rss.Item;
27 import org.apache.commons.betwixt.examples.rss.TextInput;
28 import org.apache.commons.betwixt.io.BeanWriter;
29 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
30 import org.xml.sax.InputSource JavaDoc;
31
32 /**
33  * Tests for the validity of the schema produced.
34  * @author <a HREF='http://jakarta.apache.org/'>Jakarta Commons Team</a>
35  * @version $Revision: 1.2.2.1 $
36  */

37 public class TestSchemaValidity extends AbstractTestCase {
38
39     public TestSchemaValidity(String JavaDoc name) {
40         super(name);
41     }
42     
43     private String JavaDoc generateSchema(Class JavaDoc clazz) throws Exception JavaDoc {
44         SchemaTranscriber transcriber = new SchemaTranscriber();
45         transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
46         Schema schema = transcriber.generate(clazz);
47         
48         StringWriter JavaDoc out = new StringWriter JavaDoc();
49         out.write("<?xml version='1.0'?>");
50         BeanWriter writer = new BeanWriter(out);
51         writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration());
52         writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration());
53         writer.write(schema);
54         
55         String JavaDoc xsd = out.getBuffer().toString();
56         return xsd;
57     }
58     
59     public void testSimplestBeanWithAttributes() throws Exception JavaDoc {
60        String JavaDoc xsd = generateSchema(SimplestBean.class);
61             
62        StringWriter JavaDoc out = new StringWriter JavaDoc();
63        out.write("<?xml version='1.0'?>");
64        BeanWriter writer = new BeanWriter(out);
65        writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
66        writer.getXMLIntrospector().getConfiguration().getPrefixMapper().setPrefix(SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI, "xsi");
67        writer.getBindingConfiguration().setMapIDs(false);
68        SimplestBean bean = new SimplestBean("Simon");
69        writer.write(bean);
70        
71        String JavaDoc xml = out.getBuffer().toString();
72        
73        xmlAssertIsValid(new InputSource JavaDoc(new StringReader JavaDoc(xml)), new InputSource JavaDoc(new StringReader JavaDoc(xsd)));
74     }
75     
76     
77     public void testSimplestBeanWithElements() throws Exception JavaDoc {
78        String JavaDoc xsd = generateSchema(SimplestElementBean.class);
79             
80        StringWriter JavaDoc out = new StringWriter JavaDoc();
81        out.write("<?xml version='1.0'?>");
82        BeanWriter writer = new BeanWriter(out);
83        writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
84        writer.getXMLIntrospector().getConfiguration().getPrefixMapper().setPrefix(SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI, "xsi");
85        writer.getBindingConfiguration().setMapIDs(false);
86        SimplestElementBean bean = new SimplestElementBean("Simon");
87        writer.write(bean);
88        
89        String JavaDoc xml = out.getBuffer().toString();
90        
91        xmlAssertIsValid(new InputSource JavaDoc(new StringReader JavaDoc(xml)), new InputSource JavaDoc(new StringReader JavaDoc(xsd)));
92     }
93     
94     
95     public void testSimpleBean() throws Exception JavaDoc {
96        String JavaDoc xsd = generateSchema(SimpleBean.class);
97             
98        StringWriter JavaDoc out = new StringWriter JavaDoc();
99        out.write("<?xml version='1.0'?>");
100        BeanWriter writer = new BeanWriter(out);
101        writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
102        writer.getXMLIntrospector().getConfiguration().getPrefixMapper().setPrefix(SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI, "xsi");
103        writer.getBindingConfiguration().setMapIDs(false);
104        SimpleBean bean = new SimpleBean("One", "Two", "A", "One, Two, Three, Four");
105        writer.write(bean);
106        
107        String JavaDoc xml = out.getBuffer().toString();
108        
109        xmlAssertIsValid(new InputSource JavaDoc(new StringReader JavaDoc(xml)), new InputSource JavaDoc(new StringReader JavaDoc(xsd)));
110     }
111    
112     private String JavaDoc generateOrderLineSchema() throws Exception JavaDoc {
113         SchemaTranscriber transcriber = new SchemaTranscriber();
114         transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
115         transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
116         Schema schema = transcriber.generate(OrderLineBean.class);
117         
118         StringWriter JavaDoc out = new StringWriter JavaDoc();
119         out.write("<?xml version='1.0'?>");
120         BeanWriter writer = new BeanWriter(out);
121         writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration());
122         writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration());
123         writer.write(schema);
124         
125         String JavaDoc xsd = out.getBuffer().toString();
126         return xsd;
127     }
128    
129     public void testOrderLine() throws Exception JavaDoc {
130
131        String JavaDoc xsd = generateOrderLineSchema();
132        StringWriter JavaDoc out = new StringWriter JavaDoc();
133        out.write("<?xml version='1.0'?>");
134        BeanWriter writer = new BeanWriter(out);
135        writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
136        writer.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
137        writer.getXMLIntrospector().getConfiguration().getPrefixMapper().setPrefix(SchemaTranscriber.W3C_SCHEMA_INSTANCE_URI, "xsi");
138        writer.getBindingConfiguration().setMapIDs(false);
139        OrderLineBean bean = new OrderLineBean(3, new ProductBean("00112234", "A11", "Fat Fish", "A Fat Fish"));
140        writer.write(bean);
141        
142        String JavaDoc xml = out.getBuffer().toString();
143        
144        xmlAssertIsValid(new InputSource JavaDoc(new StringReader JavaDoc(xml)), new InputSource JavaDoc(new StringReader JavaDoc(xsd)));
145     }
146     
147     private String JavaDoc generateOrderSchema() throws Exception JavaDoc {
148         SchemaTranscriber transcriber = new SchemaTranscriber();
149         transcriber.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
150         transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
151         transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
152         transcriber.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
153         Schema schema = transcriber.generate(OrderBean.class);
154         
155         StringWriter JavaDoc out = new StringWriter JavaDoc();
156         out.write("<?xml version='1.0'?>");
157         BeanWriter writer = new BeanWriter(out);
158         writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration());
159         writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration());
160         writer.write(schema);
161         
162         String JavaDoc xsd = out.getBuffer().toString();
163         return xsd;
164     }
165     
166     public void testOrder() throws Exception JavaDoc {
167         String JavaDoc xsd = generateOrderSchema();
168         StringWriter JavaDoc out = new StringWriter JavaDoc();
169         out.write("<?xml version='1.0'?>");
170         BeanWriter writer = new BeanWriter(out);
171         writer.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
172         writer.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
173         writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
174         writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
175         writer.getBindingConfiguration().setMapIDs(false);
176         
177         OrderBean bean = new OrderBean("XA-2231",
178             new CustomerBean("PB34", "Mr Abbot", "1, Skipton Road","Shipley", "Merry England", "BD4 8KL"));
179         bean.addLine(
180               new OrderLineBean(4, new ProductBean("00112234", "A11", "Taylor's Landlord", "Taylor's Landlord")));
181         bean.addLine(
182               new OrderLineBean(5, new ProductBean("00112235", "A13", "Black Sheep Special", "Black Sheep Special")));
183         writer.write(bean);
184        
185         String JavaDoc xml = out.getBuffer().toString();
186         
187         xmlAssertIsValid(new InputSource JavaDoc(new StringReader JavaDoc(xml)), new InputSource JavaDoc(new StringReader JavaDoc(xsd)));
188         
189     }
190     
191     
192     private String JavaDoc generateRSSSchema() throws Exception JavaDoc {
193         SchemaTranscriber transcriber = new SchemaTranscriber();
194         Schema schema = transcriber.generate(Channel.class);
195         
196         StringWriter JavaDoc out = new StringWriter JavaDoc();
197         out.write("<?xml version='1.0'?>");
198         BeanWriter writer = new BeanWriter(out);
199         writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration());
200         writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration());
201         writer.write(schema);
202         
203         String JavaDoc xsd = out.getBuffer().toString();
204         return xsd;
205     }
206     
207     public void testRSS() throws Exception JavaDoc {
208         String JavaDoc xsd = generateRSSSchema();
209         StringWriter JavaDoc out = new StringWriter JavaDoc();
210         out.write("<?xml version='1.0'?>");
211         BeanWriter writer = new BeanWriter(out);
212         writer.getBindingConfiguration().setMapIDs(false);
213         
214         Channel channel = new Channel();
215         channel.setTitle("Betwixt News");
216         channel.setLink("http://jakarta.apache.org/commons/betwixt");
217         channel.setDescription("Example feed themed on Betwixt news.");
218         channel.setRating("(PICS-1.1 'http://www.rsac.org/ratingsv01.html'" +
219             " 2 gen true comment 'RSACi North America Server'" +
220             " for 'http://www.rsac.org' on '1996.04.16T08:15-0500'" +
221             " r (n 0 s 0 v 0 l 0))");
222         channel.setLanguage("en-UK");
223         
224         Image image = new Image();
225         image.setTitle("Apache Feather");
226         image.setURL("http://www.apache.org/images/asf_logo_wide.gif");
227         image.setLink("http://www.apache.org");
228         image.setWidth(100);
229         image.setHeight(30);
230         image.setDescription("Example image");
231         channel.setImage(image);
232
233         Item itemOne = new Item();
234         itemOne.setTitle("Betwixt now generates w3c schema!");
235         itemOne.setLink("http://jakarta.apache.org/commons/betwixt");
236         itemOne.setDescription("Example description");
237         channel.addItem(itemOne);
238
239         Item itemTwo = new Item();
240         itemTwo.setTitle("Another News Item");
241         itemTwo.setLink("http://jakarta.apache.org/commons/betwixt");
242         itemTwo.setDescription("Blah Blah Blah");
243         channel.addItem(itemTwo);
244
245         TextInput textInput = new TextInput();
246         textInput.setTitle("Send");
247         textInput.setDescription("Comments about Betwixt news");
248         textInput.setName("Response text");
249         textInput.setLink("http://jakarta.apache.org/commons/betwixt");
250         channel.setTextInput(textInput);
251
252         writer.write(channel);
253
254         String JavaDoc xml = out.getBuffer().toString();
255        
256         xmlAssertIsValid(new InputSource JavaDoc(new StringReader JavaDoc(xml)), new InputSource JavaDoc(new StringReader JavaDoc(xsd)));
257         
258     }
259 }
260
Popular Tags