1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17   
18  package org.apache.commons.betwixt.io;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.betwixt.AbstractTestCase;
25  import org.apache.commons.betwixt.ElementDescriptor;
26  import org.apache.commons.betwixt.XMLBeanInfo;
27  import org.xml.sax.Attributes;
28  import org.xml.sax.SAXException;
29  
30  /**
31   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
32   * @version $Revision: 561314 $
33   */
34  public class TestAbstractBeanWriter extends AbstractTestCase {
35  
36      public TestAbstractBeanWriter(String testName) {
37          super(testName);
38      }
39  
40      public void testContextCurrentElement() throws Exception {
41          MovieBean bean = 
42              new MovieBean("Excalibur", 1981, new PersonBean("John", "Boorman"));
43          bean.addActor(new PersonBean("Nigel", "Terry"));
44          bean.addActor(new PersonBean("Helen", "Mirren"));
45          bean.addActor(new PersonBean("Nicol", "Williamson"));
46          
47          TestWritingAPI writer = new TestWritingAPI();
48          writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
49          XMLBeanInfo personXmlBeanInfo 
50              = writer.getXMLIntrospector().introspect(PersonBean.class);
51          XMLBeanInfo movieXmlBeanInfo 
52              = writer.getXMLIntrospector().introspect(MovieBean.class);
53          writer.write(bean);
54          
55          List expected = new ArrayList();
56          ElementDescriptor movieElementdescriptor 
57              = movieXmlBeanInfo.getElementDescriptor();
58          ElementDescriptor nameDescriptor 
59              = movieElementdescriptor.getElementDescriptors()[0];
60          ElementDescriptor yearDescriptor 
61              = movieElementdescriptor.getElementDescriptors()[1];
62          ElementDescriptor directorDescriptor 
63              = movieElementdescriptor.getElementDescriptors()[2];
64          ElementDescriptor actorsDescriptor 
65              = movieElementdescriptor.getElementDescriptors()[3];
66          ElementDescriptor personDescriptor
67              = personXmlBeanInfo.getElementDescriptor();            
68          
69          expected.add(
70              new TestWritingAPI.Record(
71                  TestWritingAPI.START_ELEMENT, 
72                  movieElementdescriptor));
73                  
74          expected.add(
75              new TestWritingAPI.Record(
76                  TestWritingAPI.START_ELEMENT, 
77                  nameDescriptor));
78  
79          expected.add(
80              new TestWritingAPI.Record(
81                  TestWritingAPI.BODY_TEXT, 
82                  nameDescriptor));
83                 
84          expected.add(
85              new TestWritingAPI.Record(
86                  TestWritingAPI.END_ELEMENT, 
87                  nameDescriptor));
88          
89          expected.add(
90              new TestWritingAPI.Record(
91                  TestWritingAPI.START_ELEMENT, 
92                  yearDescriptor));
93  
94          expected.add(
95              new TestWritingAPI.Record(
96                  TestWritingAPI.BODY_TEXT, 
97                  yearDescriptor));
98                 
99          expected.add(
100             new TestWritingAPI.Record(
101                 TestWritingAPI.END_ELEMENT, 
102                 yearDescriptor));
103         
104         expected.add(
105             new TestWritingAPI.Record(
106                 TestWritingAPI.START_ELEMENT, 
107                 personDescriptor));
108                
109         expected.add(
110             new TestWritingAPI.Record(
111                 TestWritingAPI.END_ELEMENT, 
112                 personDescriptor));
113               
114         expected.add(
115             new TestWritingAPI.Record(
116                 TestWritingAPI.START_ELEMENT, 
117                 actorsDescriptor));    
118                 
119         expected.add(
120             new TestWritingAPI.Record(
121                 TestWritingAPI.START_ELEMENT, 
122                 personDescriptor));
123                
124         expected.add(
125             new TestWritingAPI.Record(
126                 TestWritingAPI.END_ELEMENT, 
127                 personDescriptor));  
128         
129         expected.add(
130             new TestWritingAPI.Record(
131                 TestWritingAPI.START_ELEMENT, 
132                 personDescriptor));
133                
134         expected.add(
135             new TestWritingAPI.Record(
136                 TestWritingAPI.END_ELEMENT, 
137                 personDescriptor));  
138                 
139         expected.add(
140             new TestWritingAPI.Record(
141                 TestWritingAPI.START_ELEMENT, 
142                 personDescriptor));
143                
144 
145         expected.add(
146             new TestWritingAPI.Record(
147                 TestWritingAPI.END_ELEMENT, 
148                 personDescriptor));  
149         expected.add(
150             new TestWritingAPI.Record(
151                 TestWritingAPI.END_ELEMENT, 
152                 actorsDescriptor));     
153                 
154         expected.add(
155             new TestWritingAPI.Record(
156                 TestWritingAPI.END_ELEMENT, 
157                 movieElementdescriptor));       
158         
159         assertEquals("Collections same size", expected.size(), writer.recording.size());
160          
161         assertEquals("Movie element start", expected.get(0), writer.recording.get(0));
162         assertEquals("Name element start", expected.get(1), writer.recording.get(1));
163         assertEquals("Name element body", expected.get(2), writer.recording.get(2));
164         assertEquals("Name element end", expected.get(3), writer.recording.get(3));
165         assertEquals("Year element start", expected.get(4), writer.recording.get(4));
166         assertEquals("Year element body", expected.get(5), writer.recording.get(5));
167         assertEquals("Year element end", expected.get(6), writer.recording.get(6));
168         assertEquals("Director element start", expected.get(7), writer.recording.get(7));
169         assertEquals("Director element end", expected.get(8), writer.recording.get(8));
170         assertEquals("Actors element start", expected.get(9), writer.recording.get(9));;
171         assertEquals("Actor element body", expected.get(10), writer.recording.get(10));
172         assertEquals("Actor element end", expected.get(11), writer.recording.get(12));
173         assertEquals("Actor element body", expected.get(12), writer.recording.get(12));
174         assertEquals("Actor element end", expected.get(13), writer.recording.get(13));
175         assertEquals("Actor element body", expected.get(14), writer.recording.get(14));
176         assertEquals("Actor element end", expected.get(15), writer.recording.get(15));
177         assertEquals("Actors element end", expected.get(16), writer.recording.get(16));
178         assertEquals("Movie element end", expected.get(17), writer.recording.get(17));
179     }
180     
181     
182     public static class TestWritingAPI extends AbstractBeanWriter {
183         
184         public static final int START_ELEMENT = 1;
185         public static final int BODY_TEXT = 2;
186         public static final int END_ELEMENT = 3;
187         
188         private List recording = new ArrayList();
189         
190         protected void bodyText(String text) throws IOException, SAXException {
191             throw new RuntimeException("Deprecated method called");
192         }
193 
194 
195         protected void bodyText(WriteContext context, String text)
196             throws IOException, SAXException {
197             recording.add(new Record(BODY_TEXT, context.getCurrentDescriptor()));
198         }
199 
200         protected void endElement(String uri, String localName, String qName)
201             throws IOException, SAXException {
202                 throw new RuntimeException("Deprecated method called");
203         }
204 
205         protected void endElement(
206             WriteContext context,
207             String uri,
208             String localName,
209             String qName)
210             throws IOException, SAXException {;
211                 recording.add(new Record(END_ELEMENT, context.getCurrentDescriptor()));
212         }
213 
214         protected void startElement(
215             String uri,
216             String localName,
217             String qName,
218             Attributes attr)
219             throws IOException, SAXException {
220                 throw new RuntimeException("Deprecated method called");
221         }
222 
223         protected void startElement(
224             WriteContext context,
225             String uri,
226             String localName,
227             String qName,
228             Attributes attr)
229             throws IOException, SAXException {
230             recording.add(new Record(START_ELEMENT, context.getCurrentDescriptor()));
231         }
232         
233         public static class Record {
234              ElementDescriptor currentDescriptor;
235              int type;
236              
237              Record(int type, ElementDescriptor currentDescriptor) {
238                  this.currentDescriptor = currentDescriptor;
239                  this.type = type;
240              }
241 
242             public int hashCode() {
243                 return type;
244             }
245 
246             public String toString() {
247                 return "[Record: type=" + type + "; " + currentDescriptor + "]";
248             }
249 
250             public boolean equals(Object arg) {
251                 boolean result = false;                
252                 if (arg instanceof Record) {
253                     Record record = (Record) arg;
254                     result = (type == type) 
255                         && currentDescriptor.equals(record.currentDescriptor);
256                 }
257                 return result;
258             }
259 
260         }
261     } 
262 }