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,v 1.2 2004/06/13 21:32:48 rdonkin Exp $
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 testLastMappedClassBottomClass() throws Exception
98      {
99          ReadContext context = new ReadContext(
100                     new BindingConfiguration(), 
101                     new ReadConfiguration());
102 
103         context.markClassMap(Object.class);
104         context.pushElement("beta");
105         context.pushElement("delta");
106         context.pushElement("gamma");
107         assertEquals("One classes", Object.class, context.getLastMappedClass());
108     }
109     
110     public void testLastMappedClassTwoClasses() throws Exception
111     {
112         
113         ReadContext context = new ReadContext(
114                     new BindingConfiguration(), 
115                     new ReadConfiguration());    
116         context.markClassMap(Object.class);
117         context.pushElement("beta");
118         context.pushElement("delta");
119         context.markClassMap(String.class);
120         context.pushElement("gamma");
121         assertEquals("Two classes", String.class, context.getLastMappedClass());
122     }
123     
124     public void testLastMappedClassTopClass() throws Exception
125     {
126         ReadContext context = new ReadContext(
127                     new BindingConfiguration(), 
128                     new ReadConfiguration());    
129         context.markClassMap(Object.class);
130         context.pushElement("beta");
131         context.pushElement("delta");
132         context.markClassMap(String.class);
133         context.pushElement("gamma");
134         context.markClassMap(Integer.class);
135         assertEquals("Top class", Integer.class, context.getLastMappedClass());
136     }
137     
138     
139     public void testNullElementNameMatchesAll() throws Exception {
140         
141         ReadContext context = new ReadContext(
142                     new BindingConfiguration(), 
143                     new ReadConfiguration()); 
144                     
145         context.pushElement("LibraryBeanWithArraySetter");   
146         context.markClassMap(LibraryBeanWithArraySetter.class); 
147         context.pushElement("books");
148         context.pushElement("whatever");
149         assertNotNull("Null name should match any new element", context.getCurrentDescriptor());
150     }
151     
152     
153 /* Sad to say that the method tested has had to be made private.
154  * Maybe would be good to find a way to test the
155     public void testRelativeElementPathBase()
156     {
157         ReadContext context = new ReadContext(
158                     new BindingConfiguration(), 
159                     new ReadConfiguration());
160         ArrayList elements = new ArrayList();
161         
162         context.pushElement("alpha");
163         context.markClassMap(Object.class);
164         context.pushElement("beta");
165         context.pushElement("delta");
166         context.pushElement("gamma");
167     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
168         
169         assertEquals("Path element count (1)", 3 , elements.size());
170         assertEquals("Element name 0", "beta", elements.get(0));
171         assertEquals("Element name 1", "delta", elements.get(1));
172         assertEquals("Element name 2", "gamma", elements.get(2));
173     }
174     
175 
176     public void testRelativeElementPathTwoMarks()
177     {
178         ReadContext context = new ReadContext(
179                     new BindingConfiguration(), 
180                     new ReadConfiguration());
181         ArrayList elements = new ArrayList();
182         
183         context.pushElement("alpha");
184         context.markClassMap(Object.class);
185         context.pushElement("beta");
186         context.pushElement("delta");
187         context.markClassMap(Object.class);
188         context.pushElement("gamma");
189     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
190         
191         assertEquals("Path element count (1)", 1 , elements.size());
192         assertEquals("Element name", "gamma", elements.get(0));
193     }
194 
195 
196     public void testRelativeElementPathTopMark()
197     {
198         ReadContext context = new ReadContext(
199                     new BindingConfiguration(), 
200                     new ReadConfiguration());
201         ArrayList elements = new ArrayList();
202         
203         context.pushElement("alpha");
204         context.pushElement("beta");
205         context.pushElement("delta");
206         context.pushElement("gamma");
207         context.markClassMap(Object.class);
208     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
209         
210         assertEquals("Path element count (0)", 0 , elements.size());
211     }
212 
213     public void testRelativeElementPathRootMark()
214     {
215         ReadContext context = new ReadContext(
216                     new BindingConfiguration(), 
217                     new ReadConfiguration());
218         ArrayList elements = new ArrayList();
219  
220         context.markClassMap(Object.class);
221         context.pushElement("alpha");
222         context.pushElement("beta");
223         context.pushElement("delta");
224         context.pushElement("gamma");
225     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
226         
227         assertEquals("Path element count (4)", 4 , elements.size());
228         assertEquals("Element name (0)", "alpha", elements.get(0));
229         assertEquals("Element name (1)", "beta", elements.get(1));
230         assertEquals("Element name (2)", "delta", elements.get(2));
231         assertEquals("Element name (3)", "gamma", elements.get(3));
232 
233     }
234     
235     public void testRelativeElementPathNoMark()
236     {
237         ReadContext context = new ReadContext(
238                     new BindingConfiguration(), 
239                     new ReadConfiguration());
240         ArrayList elements = new ArrayList();
241  
242         context.pushElement("alpha");
243         context.pushElement("beta");
244         context.pushElement("delta");
245         context.pushElement("gamma");
246     	CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
247         
248         assertEquals("Path element count (4)", 4 , elements.size());
249         assertEquals("Element name (0)", "alpha", elements.get(0));
250         assertEquals("Element name (1)", "beta", elements.get(1));
251         assertEquals("Element name (2)", "delta", elements.get(2));
252         assertEquals("Element name (3)", "gamma", elements.get(3));
253 
254     }
255     */
256 }