1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.schema;
19
20 import java.io.StringReader;
21 import java.io.StringWriter;
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;
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 name) {
40 super(name);
41 }
42
43 private String generateSchema(Class clazz) throws Exception {
44 SchemaTranscriber transcriber = new SchemaTranscriber();
45 transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
46 Schema schema = transcriber.generate(clazz);
47
48 StringWriter out = new StringWriter();
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 xsd = out.getBuffer().toString();
56 return xsd;
57 }
58
59 public void testSimplestBeanWithAttributes() throws Exception {
60 String xsd = generateSchema(SimplestBean.class);
61
62 StringWriter out = new StringWriter();
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 xml = out.getBuffer().toString();
72
73 xmlAssertIsValid(new InputSource(new StringReader(xml)), new InputSource(new StringReader(xsd)));
74 }
75
76
77 public void testSimplestBeanWithElements() throws Exception {
78 String xsd = generateSchema(SimplestElementBean.class);
79
80 StringWriter out = new StringWriter();
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 xml = out.getBuffer().toString();
90
91 xmlAssertIsValid(new InputSource(new StringReader(xml)), new InputSource(new StringReader(xsd)));
92 }
93
94
95 public void testSimpleBean() throws Exception {
96 String xsd = generateSchema(SimpleBean.class);
97
98 StringWriter out = new StringWriter();
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 xml = out.getBuffer().toString();
108
109 xmlAssertIsValid(new InputSource(new StringReader(xml)), new InputSource(new StringReader(xsd)));
110 }
111
112 private String generateOrderLineSchema() throws Exception {
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 out = new StringWriter();
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 xsd = out.getBuffer().toString();
126 return xsd;
127 }
128
129 public void testOrderLine() throws Exception {
130
131 String xsd = generateOrderLineSchema();
132 StringWriter out = new StringWriter();
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 xml = out.getBuffer().toString();
143
144 xmlAssertIsValid(new InputSource(new StringReader(xml)), new InputSource(new StringReader(xsd)));
145 }
146
147 private String generateOrderSchema() throws Exception {
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 out = new StringWriter();
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 xsd = out.getBuffer().toString();
163 return xsd;
164 }
165
166 public void testOrder() throws Exception {
167 String xsd = generateOrderSchema();
168 StringWriter out = new StringWriter();
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 xml = out.getBuffer().toString();
186
187 xmlAssertIsValid(new InputSource(new StringReader(xml)), new InputSource(new StringReader(xsd)));
188
189 }
190
191
192 private String generateRSSSchema() throws Exception {
193 SchemaTranscriber transcriber = new SchemaTranscriber();
194 Schema schema = transcriber.generate(Channel.class);
195
196 StringWriter out = new StringWriter();
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 xsd = out.getBuffer().toString();
204 return xsd;
205 }
206
207 public void testRSS() throws Exception {
208 String xsd = generateRSSSchema();
209 StringWriter out = new StringWriter();
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 xml = out.getBuffer().toString();
255
256 xmlAssertIsValid(new InputSource(new StringReader(xml)), new InputSource(new StringReader(xsd)));
257
258 }
259 }