1 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 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 256 } 257 | Popular Tags |