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.io;
019:
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.commons.betwixt.AbstractTestCase;
025: import org.apache.commons.betwixt.ElementDescriptor;
026: import org.apache.commons.betwixt.XMLBeanInfo;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029:
030: /**
031: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
032: * @version $Revision: 438373 $
033: */
034: public class TestAbstractBeanWriter extends AbstractTestCase {
035:
036: public TestAbstractBeanWriter(String testName) {
037: super (testName);
038: }
039:
040: public void testContextCurrentElement() throws Exception {
041: MovieBean bean = new MovieBean("Excalibur", 1981,
042: new PersonBean("John", "Boorman"));
043: bean.addActor(new PersonBean("Nigel", "Terry"));
044: bean.addActor(new PersonBean("Helen", "Mirren"));
045: bean.addActor(new PersonBean("Nicol", "Williamson"));
046:
047: TestWritingAPI writer = new TestWritingAPI();
048: writer.getXMLIntrospector().getConfiguration()
049: .setAttributesForPrimitives(true);
050: XMLBeanInfo personXmlBeanInfo = writer.getXMLIntrospector()
051: .introspect(PersonBean.class);
052: XMLBeanInfo movieXmlBeanInfo = writer.getXMLIntrospector()
053: .introspect(MovieBean.class);
054: writer.write(bean);
055:
056: List expected = new ArrayList();
057: ElementDescriptor movieElementdescriptor = movieXmlBeanInfo
058: .getElementDescriptor();
059: ElementDescriptor nameDescriptor = movieElementdescriptor
060: .getElementDescriptors()[0];
061: ElementDescriptor yearDescriptor = movieElementdescriptor
062: .getElementDescriptors()[1];
063: ElementDescriptor directorDescriptor = movieElementdescriptor
064: .getElementDescriptors()[2];
065: ElementDescriptor actorsDescriptor = movieElementdescriptor
066: .getElementDescriptors()[3];
067: ElementDescriptor personDescriptor = personXmlBeanInfo
068: .getElementDescriptor();
069:
070: expected.add(new TestWritingAPI.Record(
071: TestWritingAPI.START_ELEMENT, movieElementdescriptor));
072:
073: expected.add(new TestWritingAPI.Record(
074: TestWritingAPI.START_ELEMENT, nameDescriptor));
075:
076: expected.add(new TestWritingAPI.Record(
077: TestWritingAPI.BODY_TEXT, nameDescriptor));
078:
079: expected.add(new TestWritingAPI.Record(
080: TestWritingAPI.END_ELEMENT, nameDescriptor));
081:
082: expected.add(new TestWritingAPI.Record(
083: TestWritingAPI.START_ELEMENT, yearDescriptor));
084:
085: expected.add(new TestWritingAPI.Record(
086: TestWritingAPI.BODY_TEXT, yearDescriptor));
087:
088: expected.add(new TestWritingAPI.Record(
089: TestWritingAPI.END_ELEMENT, yearDescriptor));
090:
091: expected.add(new TestWritingAPI.Record(
092: TestWritingAPI.START_ELEMENT, personDescriptor));
093:
094: expected.add(new TestWritingAPI.Record(
095: TestWritingAPI.END_ELEMENT, personDescriptor));
096:
097: expected.add(new TestWritingAPI.Record(
098: TestWritingAPI.START_ELEMENT, actorsDescriptor));
099:
100: expected.add(new TestWritingAPI.Record(
101: TestWritingAPI.START_ELEMENT, personDescriptor));
102:
103: expected.add(new TestWritingAPI.Record(
104: TestWritingAPI.END_ELEMENT, personDescriptor));
105:
106: expected.add(new TestWritingAPI.Record(
107: TestWritingAPI.START_ELEMENT, personDescriptor));
108:
109: expected.add(new TestWritingAPI.Record(
110: TestWritingAPI.END_ELEMENT, personDescriptor));
111:
112: expected.add(new TestWritingAPI.Record(
113: TestWritingAPI.START_ELEMENT, personDescriptor));
114:
115: expected.add(new TestWritingAPI.Record(
116: TestWritingAPI.END_ELEMENT, personDescriptor));
117: expected.add(new TestWritingAPI.Record(
118: TestWritingAPI.END_ELEMENT, actorsDescriptor));
119:
120: expected.add(new TestWritingAPI.Record(
121: TestWritingAPI.END_ELEMENT, movieElementdescriptor));
122:
123: assertEquals("Collections same size", expected.size(),
124: writer.recording.size());
125:
126: assertEquals("Movie element start", expected.get(0),
127: writer.recording.get(0));
128: assertEquals("Name element start", expected.get(1),
129: writer.recording.get(1));
130: assertEquals("Name element body", expected.get(2),
131: writer.recording.get(2));
132: assertEquals("Name element end", expected.get(3),
133: writer.recording.get(3));
134: assertEquals("Year element start", expected.get(4),
135: writer.recording.get(4));
136: assertEquals("Year element body", expected.get(5),
137: writer.recording.get(5));
138: assertEquals("Year element end", expected.get(6),
139: writer.recording.get(6));
140: assertEquals("Director element start", expected.get(7),
141: writer.recording.get(7));
142: assertEquals("Director element end", expected.get(8),
143: writer.recording.get(8));
144: assertEquals("Actors element start", expected.get(9),
145: writer.recording.get(9));
146: ;
147: assertEquals("Actor element body", expected.get(10),
148: writer.recording.get(10));
149: assertEquals("Actor element end", expected.get(11),
150: writer.recording.get(12));
151: assertEquals("Actor element body", expected.get(12),
152: writer.recording.get(12));
153: assertEquals("Actor element end", expected.get(13),
154: writer.recording.get(13));
155: assertEquals("Actor element body", expected.get(14),
156: writer.recording.get(14));
157: assertEquals("Actor element end", expected.get(15),
158: writer.recording.get(15));
159: assertEquals("Actors element end", expected.get(16),
160: writer.recording.get(16));
161: assertEquals("Movie element end", expected.get(17),
162: writer.recording.get(17));
163: }
164:
165: public static class TestWritingAPI extends AbstractBeanWriter {
166:
167: public static final int START_ELEMENT = 1;
168: public static final int BODY_TEXT = 2;
169: public static final int END_ELEMENT = 3;
170:
171: private List recording = new ArrayList();
172:
173: protected void bodyText(String text) throws IOException,
174: SAXException {
175: throw new RuntimeException("Deprecated method called");
176: }
177:
178: protected void bodyText(WriteContext context, String text)
179: throws IOException, SAXException {
180: recording.add(new Record(BODY_TEXT, context
181: .getCurrentDescriptor()));
182: }
183:
184: protected void endElement(String uri, String localName,
185: String qName) throws IOException, SAXException {
186: throw new RuntimeException("Deprecated method called");
187: }
188:
189: protected void endElement(WriteContext context, String uri,
190: String localName, String qName) throws IOException,
191: SAXException {
192: ;
193: recording.add(new Record(END_ELEMENT, context
194: .getCurrentDescriptor()));
195: }
196:
197: protected void startElement(String uri, String localName,
198: String qName, Attributes attr) throws IOException,
199: SAXException {
200: throw new RuntimeException("Deprecated method called");
201: }
202:
203: protected void startElement(WriteContext context, String uri,
204: String localName, String qName, Attributes attr)
205: throws IOException, SAXException {
206: recording.add(new Record(START_ELEMENT, context
207: .getCurrentDescriptor()));
208: }
209:
210: public static class Record {
211: ElementDescriptor currentDescriptor;
212: int type;
213:
214: Record(int type, ElementDescriptor currentDescriptor) {
215: this .currentDescriptor = currentDescriptor;
216: this .type = type;
217: }
218:
219: public int hashCode() {
220: return type;
221: }
222:
223: public String toString() {
224: return "[Record: type=" + type + "; "
225: + currentDescriptor + "]";
226: }
227:
228: public boolean equals(Object arg) {
229: boolean result = false;
230: if (arg instanceof Record) {
231: Record record = (Record) arg;
232: result = (type == type)
233: && currentDescriptor
234: .equals(record.currentDescriptor);
235: }
236: return result;
237: }
238:
239: }
240: }
241: }
|