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: }
|