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