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