001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.core;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.jelly.JellyContext;
024: import org.apache.commons.jelly.JellyException;
025: import org.apache.commons.jelly.JellyTagException;
026: import org.apache.commons.jelly.Script;
027: import org.apache.commons.jelly.TagSupport;
028: import org.apache.commons.jelly.XMLOutput;
029: import org.apache.commons.jelly.tags.core.ArgTag;
030: import org.apache.commons.jelly.tags.core.ArgTagParent;
031: import org.apache.commons.jelly.test.BaseJellyTest;
032:
033: /**
034: * @author Rodney Waldhoff
035: * @version $Revision: 155420 $ $Date: 2005-02-27 00:06:03 +1100 (Sun, 27 Feb 2005) $
036: */
037: public class TestArgTag extends BaseJellyTest {
038:
039: public TestArgTag(String name) {
040: super (name);
041: }
042:
043: public static TestSuite suite() throws Exception {
044: return new TestSuite(TestArgTag.class);
045: }
046:
047: public void setUp() throws Exception {
048: super .setUp();
049: parentTag = new MockArgTagParent();
050: argTag = new ArgTag();
051: argTag.setContext(getJellyContext());
052: argTag.setParent(parentTag);
053: argTag.setBody(new MockScript());
054: }
055:
056: public void tearDown() throws Exception {
057: super .tearDown();
058: parentTag = null;
059: argTag = null;
060: }
061:
062: public void testToBooleanFromString() throws Exception {
063: argTag.setType("boolean");
064: argTag.setValue("true");
065: argTag.doTag(getXMLOutput());
066: assertEquals(Boolean.TYPE, parentTag.getType(0));
067: assertEquals(Boolean.TRUE, parentTag.getValue(0));
068: }
069:
070: public void testToCharFromString() throws Exception {
071: argTag.setType("char");
072: argTag.setValue("X");
073: argTag.doTag(getXMLOutput());
074: assertEquals(Character.TYPE, parentTag.getType(0));
075: assertEquals(new Character('X'), parentTag.getValue(0));
076: }
077:
078: public void testToByteFromString() throws Exception {
079: argTag.setType("byte");
080: argTag.setValue("17");
081: argTag.doTag(getXMLOutput());
082: assertEquals(Byte.TYPE, parentTag.getType(0));
083: assertEquals(new Byte((byte) 17), parentTag.getValue(0));
084: }
085:
086: public void testToByteFromNumber() throws Exception {
087: argTag.setType("byte");
088: argTag.setValue(new Double(17.3d));
089: argTag.doTag(getXMLOutput());
090: assertEquals(Byte.TYPE, parentTag.getType(0));
091: assertEquals(new Byte((byte) 17), parentTag.getValue(0));
092: }
093:
094: public void testToShortFromString() throws Exception {
095: argTag.setType("short");
096: argTag.setValue("17");
097: argTag.doTag(getXMLOutput());
098: assertEquals(Short.TYPE, parentTag.getType(0));
099: 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",
176: "float", "long", "double" };
177: for (int i = 0; i < types.length; i++) {
178: argTag.setType(types[i]);
179: argTag.setValue(null);
180: try {
181: argTag.doTag(getXMLOutput());
182: fail("Expected JellyException");
183: } catch (JellyException e) {
184: // expected
185: }
186: }
187: }
188:
189: public void testFromNull() throws Exception {
190: Class[] types = { Boolean.class, Character.class, Byte.class,
191: Short.class, Integer.class, Float.class, Long.class,
192: Double.class, String.class, Object.class };
193: for (int i = 0; i < types.length; i++) {
194: argTag.setType(types[i].getName());
195: argTag.setValue(null);
196: argTag.doTag(getXMLOutput());
197: assertEquals(types[i], parentTag.getType(i));
198: assertNull(parentTag.getValue(i));
199: }
200: }
201:
202: private MockArgTagParent parentTag = null;
203: private ArgTag argTag = null;
204:
205: class MockArgTagParent extends TagSupport implements ArgTagParent {
206: public void addArgument(Class type, Object value) {
207: typeList.add(type);
208: valueList.add(value);
209: }
210:
211: public void doTag(XMLOutput output) {
212: }
213:
214: private Class getType(int i) {
215: return (Class) (typeList.get(i));
216: }
217:
218: private Object getValue(int i) {
219: return valueList.get(i);
220: }
221:
222: private List typeList = new ArrayList();
223: private List valueList = new ArrayList();
224: }
225:
226: class MockScript implements Script {
227: public Script compile() throws JellyException {
228: return this ;
229: }
230:
231: public void run(JellyContext context, XMLOutput output)
232: throws JellyTagException {
233: }
234: }
235:
236: }
|