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.read;
19  
20  import junit.framework.Test;
21  import junit.framework.TestSuite;
22  
23  import org.apache.commons.betwixt.AbstractTestCase;
24  import org.apache.commons.betwixt.BindingConfiguration;
25  import org.apache.commons.betwixt.LibraryBeanWithArraySetter;
26  
27  /*** 
28   * Test harness for ReadContext
29   * 
30   * @author Robert Burrell Donkin
31   * @version $Id: TestReadContext.java 438373 2006-08-30 05:17:21Z bayard $
32   */
33  public class TestReadContext extends AbstractTestCase {
34  
35      public TestReadContext(String name) {
36          super(name);
37      }
38          
39      public static Test suite() {
40          return new TestSuite(TestReadContext.class);
41      }    
42      
43      public void testElementStackPushPop() throws Exception {
44          ReadContext context = new ReadContext(
45                      new BindingConfiguration(), 
46                      new ReadConfiguration());
47          context.pushElement("alpha");
48          assertEquals("Push then pop", "alpha", context.popElement());
49          assertEquals("Push then pop at bottom", null, context.popElement());
50          
51          context.pushElement("beta");
52          context.pushElement("delta");
53          context.pushElement("gamma");
54          assertEquals("Triple push (1)", "gamma", context.popElement());
55          assertEquals("Triple push (2)", "delta", context.popElement());
56          assertEquals("Triple push (3)", "beta", context.popElement());
57          assertEquals("Triple push at bottom", null, context.popElement());
58             
59      }
60         
61      public void testElementStackMarkedPushPop() throws Exception {
62          ReadContext context = new ReadContext(
63                      new BindingConfiguration(), 
64                      new ReadConfiguration());
65  
66          context.pushElement("beta");
67          context.pushElement("delta");
68          context.markClassMap(Object.class);
69          context.pushElement("gamma");
70          assertEquals("One mark (1)", "gamma", context.popElement());
71          assertEquals("One mark (2)", "delta", context.popElement());
72          assertEquals("One mark (3)", "beta", context.popElement());
73          assertEquals("One mark at bottom", null, context.popElement());
74             
75          context.markClassMap(Object.class);
76          context.pushElement("beta");
77          context.pushElement("delta");
78          context.markClassMap(Object.class);
79          context.pushElement("gamma");
80          context.markClassMap(Object.class);
81          assertEquals("Three marks (1)", "gamma", context.popElement());
82          assertEquals("Three marks (2)", "delta", context.popElement());
83          assertEquals("Three marks (3)", "beta", context.popElement());
84          assertEquals("Three marks at bottom", null, context.popElement());
85      }
86      
87      public void testLastMappedClassNoClass() throws Exception
88      {
89          ReadContext context = new ReadContext(
90                      new BindingConfiguration(), 
91                      new ReadConfiguration());
92          context.pushElement("beta");
93          context.pushElement("delta");
94          context.pushElement("gamma");
95          assertEquals("No class", null, context.getLastMappedClass());
96      }
97      
98      public void testGetCurrentElement() throws Exception {
99          ReadContext context = new ReadContext(new BindingConfiguration(), new ReadConfiguration());
100         context.pushElement("element");
101         context.markClassMap(String.class);
102         assertEquals("Current element: ", "element", context.getCurrentElement());
103     }
104     
105     public void testLastMappedClassBottomClass() throws Exception
106     {
107         ReadContext context = new ReadContext(
108                     new BindingConfiguration(), 
109                     new ReadConfiguration());
110 
111         context.markClassMap(Object.class);
112         context.pushElement("beta");
113         context.pushElement("delta");
114         context.pushElement("gamma");
115         assertEquals("One classes", Object.class, context.getLastMappedClass());
116     }
117     
118     public void testLastMappedClassTwoClasses() throws Exception
119     {
120         
121         ReadContext context = new ReadContext(
122                     new BindingConfiguration(), 
123                     new ReadConfiguration());    
124         context.markClassMap(Object.class);
125         context.pushElement("beta");
126         context.pushElement("delta");
127         context.markClassMap(String.class);
128         context.pushElement("gamma");
129         assertEquals("Two classes", String.class, context.getLastMappedClass());
130     }
131     
132     public void testLastMappedClassTopClass() throws Exception
133     {
134         ReadContext context = new ReadContext(
135                     new BindingConfiguration(), 
136                     new ReadConfiguration());    
137         context.markClassMap(Object.class);
138         context.pushElement("beta");
139         context.pushElement("delta");
140         context.markClassMap(String.class);
141         context.pushElement("gamma");
142         context.markClassMap(Integer.class);
143         assertEquals("Top class", Integer.class, context.getLastMappedClass());
144     }
145     
146     
147     public void testNullElementNameMatchesAll() throws Exception {
148         
149         ReadContext context = new ReadContext(
150                     new BindingConfiguration(), 
151                     new ReadConfiguration()); 
152                     
153         context.pushElement("LibraryBeanWithArraySetter");   
154         context.markClassMap(LibraryBeanWithArraySetter.class); 
155         context.pushElement("books");
156         context.pushElement("whatever");
157         assertNotNull("Null name should match any new element", context.getCurrentDescriptor());
158     }
159     
160     
161 /* Sad to say that the method tested has had to be made private.
162  * Maybe would be good to find a way to test the
163     public void testRelativeElementPathBase()
164     {
165         ReadContext context = new ReadContext(
166                     new BindingConfiguration(), 
167                     new ReadConfiguration());
168         ArrayList elements = new ArrayList();
169         
170         context.pushElement("alpha");
171         context.markClassMap(Object.class);
172         context.pushElement("beta");
173         context.pushElement("delta");
174         context.pushElement("gamma");
175     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
176         
177         assertEquals("Path element count (1)", 3 , elements.size());
178         assertEquals("Element name 0", "beta", elements.get(0));
179         assertEquals("Element name 1", "delta", elements.get(1));
180         assertEquals("Element name 2", "gamma", elements.get(2));
181     }
182     
183 
184     public void testRelativeElementPathTwoMarks()
185     {
186         ReadContext context = new ReadContext(
187                     new BindingConfiguration(), 
188                     new ReadConfiguration());
189         ArrayList elements = new ArrayList();
190         
191         context.pushElement("alpha");
192         context.markClassMap(Object.class);
193         context.pushElement("beta");
194         context.pushElement("delta");
195         context.markClassMap(Object.class);
196         context.pushElement("gamma");
197     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
198         
199         assertEquals("Path element count (1)", 1 , elements.size());
200         assertEquals("Element name", "gamma", elements.get(0));
201     }
202 
203 
204     public void testRelativeElementPathTopMark()
205     {
206         ReadContext context = new ReadContext(
207                     new BindingConfiguration(), 
208                     new ReadConfiguration());
209         ArrayList elements = new ArrayList();
210         
211         context.pushElement("alpha");
212         context.pushElement("beta");
213         context.pushElement("delta");
214         context.pushElement("gamma");
215         context.markClassMap(Object.class);
216     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
217         
218         assertEquals("Path element count (0)", 0 , elements.size());
219     }
220 
221     public void testRelativeElementPathRootMark()
222     {
223         ReadContext context = new ReadContext(
224                     new BindingConfiguration(), 
225                     new ReadConfiguration());
226         ArrayList elements = new ArrayList();
227  
228         context.markClassMap(Object.class);
229         context.pushElement("alpha");
230         context.pushElement("beta");
231         context.pushElement("delta");
232         context.pushElement("gamma");
233     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
234         
235         assertEquals("Path element count (4)", 4 , elements.size());
236         assertEquals("Element name (0)", "alpha", elements.get(0));
237         assertEquals("Element name (1)", "beta", elements.get(1));
238         assertEquals("Element name (2)", "delta", elements.get(2));
239         assertEquals("Element name (3)", "gamma", elements.get(3));
240 
241     }
242     
243     public void testRelativeElementPathNoMark()
244     {
245         ReadContext context = new ReadContext(
246                     new BindingConfiguration(), 
247                     new ReadConfiguration());
248         ArrayList elements = new ArrayList();
249  
250         context.pushElement("alpha");
251         context.pushElement("beta");
252         context.pushElement("delta");
253         context.pushElement("gamma");
254     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
255         
256         assertEquals("Path element count (4)", 4 , elements.size());
257         assertEquals("Element name (0)", "alpha", elements.get(0));
258         assertEquals("Element name (1)", "beta", elements.get(1));
259         assertEquals("Element name (2)", "delta", elements.get(2));
260         assertEquals("Element name (3)", "gamma", elements.get(3));
261 
262     }
263     */
264 }