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.read;
019:
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.betwixt.AbstractTestCase;
024: import org.apache.commons.betwixt.BindingConfiguration;
025: import org.apache.commons.betwixt.LibraryBeanWithArraySetter;
026:
027: /**
028: * Test harness for ReadContext
029: *
030: * @author Robert Burrell Donkin
031: * @version $Id: TestReadContext.java 438373 2006-08-30 05:17:21Z bayard $
032: */
033: public class TestReadContext extends AbstractTestCase {
034:
035: public TestReadContext(String name) {
036: super (name);
037: }
038:
039: public static Test suite() {
040: return new TestSuite(TestReadContext.class);
041: }
042:
043: public void testElementStackPushPop() throws Exception {
044: ReadContext context = new ReadContext(
045: new BindingConfiguration(), new ReadConfiguration());
046: context.pushElement("alpha");
047: assertEquals("Push then pop", "alpha", context.popElement());
048: assertEquals("Push then pop at bottom", null, context
049: .popElement());
050:
051: context.pushElement("beta");
052: context.pushElement("delta");
053: context.pushElement("gamma");
054: assertEquals("Triple push (1)", "gamma", context.popElement());
055: assertEquals("Triple push (2)", "delta", context.popElement());
056: assertEquals("Triple push (3)", "beta", context.popElement());
057: assertEquals("Triple push at bottom", null, context
058: .popElement());
059:
060: }
061:
062: public void testElementStackMarkedPushPop() throws Exception {
063: ReadContext context = new ReadContext(
064: new BindingConfiguration(), new ReadConfiguration());
065:
066: context.pushElement("beta");
067: context.pushElement("delta");
068: context.markClassMap(Object.class);
069: context.pushElement("gamma");
070: assertEquals("One mark (1)", "gamma", context.popElement());
071: assertEquals("One mark (2)", "delta", context.popElement());
072: assertEquals("One mark (3)", "beta", context.popElement());
073: assertEquals("One mark at bottom", null, context.popElement());
074:
075: context.markClassMap(Object.class);
076: context.pushElement("beta");
077: context.pushElement("delta");
078: context.markClassMap(Object.class);
079: context.pushElement("gamma");
080: context.markClassMap(Object.class);
081: assertEquals("Three marks (1)", "gamma", context.popElement());
082: assertEquals("Three marks (2)", "delta", context.popElement());
083: assertEquals("Three marks (3)", "beta", context.popElement());
084: assertEquals("Three marks at bottom", null, context
085: .popElement());
086: }
087:
088: public void testLastMappedClassNoClass() throws Exception {
089: ReadContext context = new ReadContext(
090: new BindingConfiguration(), new ReadConfiguration());
091: context.pushElement("beta");
092: context.pushElement("delta");
093: context.pushElement("gamma");
094: assertEquals("No class", null, context.getLastMappedClass());
095: }
096:
097: public void testGetCurrentElement() throws Exception {
098: ReadContext context = new ReadContext(
099: new BindingConfiguration(), new ReadConfiguration());
100: context.pushElement("element");
101: context.markClassMap(String.class);
102: assertEquals("Current element: ", "element", context
103: .getCurrentElement());
104: }
105:
106: public void testLastMappedClassBottomClass() throws Exception {
107: ReadContext context = new ReadContext(
108: new BindingConfiguration(), 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
115: .getLastMappedClass());
116: }
117:
118: public void testLastMappedClassTwoClasses() throws Exception {
119:
120: ReadContext context = new ReadContext(
121: new BindingConfiguration(), new ReadConfiguration());
122: context.markClassMap(Object.class);
123: context.pushElement("beta");
124: context.pushElement("delta");
125: context.markClassMap(String.class);
126: context.pushElement("gamma");
127: assertEquals("Two classes", String.class, context
128: .getLastMappedClass());
129: }
130:
131: public void testLastMappedClassTopClass() throws Exception {
132: ReadContext context = new ReadContext(
133: new BindingConfiguration(), new ReadConfiguration());
134: context.markClassMap(Object.class);
135: context.pushElement("beta");
136: context.pushElement("delta");
137: context.markClassMap(String.class);
138: context.pushElement("gamma");
139: context.markClassMap(Integer.class);
140: assertEquals("Top class", Integer.class, context
141: .getLastMappedClass());
142: }
143:
144: public void testNullElementNameMatchesAll() throws Exception {
145:
146: ReadContext context = new ReadContext(
147: new BindingConfiguration(), new ReadConfiguration());
148:
149: context.pushElement("LibraryBeanWithArraySetter");
150: context.markClassMap(LibraryBeanWithArraySetter.class);
151: context.pushElement("books");
152: context.pushElement("whatever");
153: assertNotNull("Null name should match any new element", context
154: .getCurrentDescriptor());
155: }
156:
157: /* Sad to say that the method tested has had to be made private.
158: * Maybe would be good to find a way to test the
159: public void testRelativeElementPathBase()
160: {
161: ReadContext context = new ReadContext(
162: new BindingConfiguration(),
163: new ReadConfiguration());
164: ArrayList elements = new ArrayList();
165:
166: context.pushElement("alpha");
167: context.markClassMap(Object.class);
168: context.pushElement("beta");
169: context.pushElement("delta");
170: context.pushElement("gamma");
171: CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
172:
173: assertEquals("Path element count (1)", 3 , elements.size());
174: assertEquals("Element name 0", "beta", elements.get(0));
175: assertEquals("Element name 1", "delta", elements.get(1));
176: assertEquals("Element name 2", "gamma", elements.get(2));
177: }
178:
179:
180: public void testRelativeElementPathTwoMarks()
181: {
182: ReadContext context = new ReadContext(
183: new BindingConfiguration(),
184: new ReadConfiguration());
185: ArrayList elements = new ArrayList();
186:
187: context.pushElement("alpha");
188: context.markClassMap(Object.class);
189: context.pushElement("beta");
190: context.pushElement("delta");
191: context.markClassMap(Object.class);
192: context.pushElement("gamma");
193: CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
194:
195: assertEquals("Path element count (1)", 1 , elements.size());
196: assertEquals("Element name", "gamma", elements.get(0));
197: }
198:
199:
200: public void testRelativeElementPathTopMark()
201: {
202: ReadContext context = new ReadContext(
203: new BindingConfiguration(),
204: new ReadConfiguration());
205: ArrayList elements = new ArrayList();
206:
207: context.pushElement("alpha");
208: context.pushElement("beta");
209: context.pushElement("delta");
210: context.pushElement("gamma");
211: context.markClassMap(Object.class);
212: CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
213:
214: assertEquals("Path element count (0)", 0 , elements.size());
215: }
216:
217: public void testRelativeElementPathRootMark()
218: {
219: ReadContext context = new ReadContext(
220: new BindingConfiguration(),
221: new ReadConfiguration());
222: ArrayList elements = new ArrayList();
223:
224: context.markClassMap(Object.class);
225: context.pushElement("alpha");
226: context.pushElement("beta");
227: context.pushElement("delta");
228: context.pushElement("gamma");
229: CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
230:
231: assertEquals("Path element count (4)", 4 , elements.size());
232: assertEquals("Element name (0)", "alpha", elements.get(0));
233: assertEquals("Element name (1)", "beta", elements.get(1));
234: assertEquals("Element name (2)", "delta", elements.get(2));
235: assertEquals("Element name (3)", "gamma", elements.get(3));
236:
237: }
238:
239: public void testRelativeElementPathNoMark()
240: {
241: ReadContext context = new ReadContext(
242: new BindingConfiguration(),
243: new ReadConfiguration());
244: ArrayList elements = new ArrayList();
245:
246: context.pushElement("alpha");
247: context.pushElement("beta");
248: context.pushElement("delta");
249: context.pushElement("gamma");
250: CollectionUtils.addAll(elements, context.getRelativeElementPathIterator());
251:
252: assertEquals("Path element count (4)", 4 , elements.size());
253: assertEquals("Element name (0)", "alpha", elements.get(0));
254: assertEquals("Element name (1)", "beta", elements.get(1));
255: assertEquals("Element name (2)", "delta", elements.get(2));
256: assertEquals("Element name (3)", "gamma", elements.get(3));
257:
258: }
259: */
260: }
|