1 16 package org.apache.commons.jelly.core; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import junit.framework.TestSuite; 22 23 import org.apache.commons.jelly.JellyContext; 24 import org.apache.commons.jelly.JellyException; 25 import org.apache.commons.jelly.JellyTagException; 26 import org.apache.commons.jelly.Script; 27 import org.apache.commons.jelly.TagSupport; 28 import org.apache.commons.jelly.XMLOutput; 29 import org.apache.commons.jelly.tags.core.ArgTag; 30 import org.apache.commons.jelly.tags.core.ArgTagParent; 31 import org.apache.commons.jelly.test.BaseJellyTest; 32 33 37 public class TestArgTag extends BaseJellyTest { 38 39 public TestArgTag(String name) { 40 super(name); 41 } 42 43 public static TestSuite suite() throws Exception { 44 return new TestSuite(TestArgTag.class); 45 } 46 47 public void setUp() throws Exception { 48 super.setUp(); 49 parentTag = new MockArgTagParent(); 50 argTag = new ArgTag(); 51 argTag.setContext(getJellyContext()); 52 argTag.setParent(parentTag); 53 argTag.setBody(new MockScript()); 54 } 55 56 public void tearDown() throws Exception { 57 super.tearDown(); 58 parentTag = null; 59 argTag = null; 60 } 61 62 public void testToBooleanFromString() throws Exception { 63 argTag.setType("boolean"); 64 argTag.setValue("true"); 65 argTag.doTag(getXMLOutput()); 66 assertEquals(Boolean.TYPE,parentTag.getType(0)); 67 assertEquals(Boolean.TRUE,parentTag.getValue(0)); 68 } 69 70 public void testToCharFromString() throws Exception { 71 argTag.setType("char"); 72 argTag.setValue("X"); 73 argTag.doTag(getXMLOutput()); 74 assertEquals(Character.TYPE,parentTag.getType(0)); 75 assertEquals(new Character ('X'),parentTag.getValue(0)); 76 } 77 78 public void testToByteFromString() throws Exception { 79 argTag.setType("byte"); 80 argTag.setValue("17"); 81 argTag.doTag(getXMLOutput()); 82 assertEquals(Byte.TYPE,parentTag.getType(0)); 83 assertEquals(new Byte ((byte)17),parentTag.getValue(0)); 84 } 85 86 public void testToByteFromNumber() throws Exception { 87 argTag.setType("byte"); 88 argTag.setValue(new Double (17.3d)); 89 argTag.doTag(getXMLOutput()); 90 assertEquals(Byte.TYPE,parentTag.getType(0)); 91 assertEquals(new Byte ((byte)17),parentTag.getValue(0)); 92 } 93 94 public void testToShortFromString() throws Exception { 95 argTag.setType("short"); 96 argTag.setValue("17"); 97 argTag.doTag(getXMLOutput()); 98 assertEquals(Short.TYPE,parentTag.getType(0)); 99 assertEquals(new Short ((short)17),parentTag.getValue(0)); 100 } 101 102 public void testToShortFromNumber() throws Exception { 103 argTag.setType("short"); 104 argTag.setValue(new Double (17.3d)); 105 argTag.doTag(getXMLOutput()); 106 assertEquals(Short.TYPE,parentTag.getType(0)); 107 assertEquals(new Short ((short)17),parentTag.getValue(0)); 108 } 109 110 public void testToIntFromString() throws Exception { 111 argTag.setType("int"); 112 argTag.setValue("17"); 113 argTag.doTag(getXMLOutput()); 114 assertEquals(Integer.TYPE,parentTag.getType(0)); 115 assertEquals(new Integer ((int)17),parentTag.getValue(0)); 116 } 117 118 public void testToIntFromNumber() throws Exception { 119 argTag.setType("int"); 120 argTag.setValue(new Double (17.3d)); 121 argTag.doTag(getXMLOutput()); 122 assertEquals(Integer.TYPE,parentTag.getType(0)); 123 assertEquals(new Integer ((int)17),parentTag.getValue(0)); 124 } 125 126 public void testToFloatFromString() throws Exception { 127 argTag.setType("float"); 128 argTag.setValue("17.3"); 129 argTag.doTag(getXMLOutput()); 130 assertEquals(Float.TYPE,parentTag.getType(0)); 131 assertEquals(new Float ((float)17.3),parentTag.getValue(0)); 132 } 133 134 public void testToFloatFromNumber() throws Exception { 135 argTag.setType("float"); 136 argTag.setValue(new Double (17.3d)); 137 argTag.doTag(getXMLOutput()); 138 assertEquals(Float.TYPE,parentTag.getType(0)); 139 assertEquals(new Float ((float)17.3),parentTag.getValue(0)); 140 } 141 142 public void testToLongFromString() throws Exception { 143 argTag.setType("long"); 144 argTag.setValue("17"); 145 argTag.doTag(getXMLOutput()); 146 assertEquals(Long.TYPE,parentTag.getType(0)); 147 assertEquals(new Long ((int)17),parentTag.getValue(0)); 148 } 149 150 public void testToLongFromNumber() throws Exception { 151 argTag.setType("long"); 152 argTag.setValue(new Double (17.3d)); 153 argTag.doTag(getXMLOutput()); 154 assertEquals(Long.TYPE,parentTag.getType(0)); 155 assertEquals(new Long ((long)17),parentTag.getValue(0)); 156 } 157 158 public void testToDoubleFromString() throws Exception { 159 argTag.setType("double"); 160 argTag.setValue("17.3"); 161 argTag.doTag(getXMLOutput()); 162 assertEquals(Double.TYPE,parentTag.getType(0)); 163 assertEquals(new Double ((double)17.3),parentTag.getValue(0)); 164 } 165 166 public void testToDoubleFromNumber() throws Exception { 167 argTag.setType("double"); 168 argTag.setValue(new Long (17L)); 169 argTag.doTag(getXMLOutput()); 170 assertEquals(Double.TYPE,parentTag.getType(0)); 171 assertEquals(new Double ((double)17),parentTag.getValue(0)); 172 } 173 174 public void testToPrimitiveFromNull() throws Exception { 175 String [] types = { "boolean", "char", "byte", "short", "int", "float", "long", "double" }; 176 for(int i=0;i<types.length;i++) { 177 argTag.setType(types[i]); 178 argTag.setValue(null); 179 try { 180 argTag.doTag(getXMLOutput()); 181 fail("Expected JellyException"); 182 } catch (JellyException e) { 183 } 185 } 186 } 187 188 public void testFromNull() throws Exception { 189 Class [] types = { Boolean .class, Character .class, Byte .class, Short .class, Integer .class, Float .class, Long .class, Double .class, String .class, Object .class }; 190 for(int i=0;i<types.length;i++) { 191 argTag.setType(types[i].getName()); 192 argTag.setValue(null); 193 argTag.doTag(getXMLOutput()); 194 assertEquals(types[i],parentTag.getType(i)); 195 assertNull(parentTag.getValue(i)); 196 } 197 } 198 199 private MockArgTagParent parentTag = null; 200 private ArgTag argTag = null; 201 202 class MockArgTagParent extends TagSupport implements ArgTagParent { 203 public void addArgument(Class type, Object value) { 204 typeList.add(type); 205 valueList.add(value); 206 } 207 208 public void doTag(XMLOutput output) { 209 } 210 211 private Class getType(int i) { 212 return (Class )(typeList.get(i)); 213 } 214 215 private Object getValue(int i) { 216 return valueList.get(i); 217 } 218 219 private List typeList = new ArrayList (); 220 private List valueList = new ArrayList (); 221 } 222 223 class MockScript implements Script { 224 public Script compile() throws JellyException { 225 return this; 226 } 227 228 public void run(JellyContext context, XMLOutput output) throws JellyTagException { 229 } 230 } 231 232 } | Popular Tags |