KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > core > TestArgTag


1 /*
2  * Copyright 2002,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 package org.apache.commons.jelly.core;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
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 /**
34  * @author Rodney Waldhoff
35  * @version $Revision: 155420 $ $Date: 2005-02-27 00:06:03 +1100 (Sun, 27 Feb 2005) $
36  */

37 public class TestArgTag extends BaseJellyTest {
38
39     public TestArgTag(String JavaDoc name) {
40         super(name);
41     }
42
43     public static TestSuite suite() throws Exception JavaDoc {
44         return new TestSuite(TestArgTag.class);
45     }
46
47     public void setUp() throws Exception JavaDoc {
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 JavaDoc {
57         super.tearDown();
58         parentTag = null;
59         argTag = null;
60     }
61
62     public void testToBooleanFromString() throws Exception JavaDoc {
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 JavaDoc {
71         argTag.setType("char");
72         argTag.setValue("X");
73         argTag.doTag(getXMLOutput());
74         assertEquals(Character.TYPE,parentTag.getType(0));
75         assertEquals(new Character JavaDoc('X'),parentTag.getValue(0));
76     }
77
78     public void testToByteFromString() throws Exception JavaDoc {
79         argTag.setType("byte");
80         argTag.setValue("17");
81         argTag.doTag(getXMLOutput());
82         assertEquals(Byte.TYPE,parentTag.getType(0));
83         assertEquals(new Byte JavaDoc((byte)17),parentTag.getValue(0));
84     }
85
86     public void testToByteFromNumber() throws Exception JavaDoc {
87         argTag.setType("byte");
88         argTag.setValue(new Double JavaDoc(17.3d));
89         argTag.doTag(getXMLOutput());
90         assertEquals(Byte.TYPE,parentTag.getType(0));
91         assertEquals(new Byte JavaDoc((byte)17),parentTag.getValue(0));
92     }
93
94     public void testToShortFromString() throws Exception JavaDoc {
95         argTag.setType("short");
96         argTag.setValue("17");
97         argTag.doTag(getXMLOutput());
98         assertEquals(Short.TYPE,parentTag.getType(0));
99         assertEquals(new Short JavaDoc((short)17),parentTag.getValue(0));
100     }
101
102     public void testToShortFromNumber() throws Exception JavaDoc {
103         argTag.setType("short");
104         argTag.setValue(new Double JavaDoc(17.3d));
105         argTag.doTag(getXMLOutput());
106         assertEquals(Short.TYPE,parentTag.getType(0));
107         assertEquals(new Short JavaDoc((short)17),parentTag.getValue(0));
108     }
109
110     public void testToIntFromString() throws Exception JavaDoc {
111         argTag.setType("int");
112         argTag.setValue("17");
113         argTag.doTag(getXMLOutput());
114         assertEquals(Integer.TYPE,parentTag.getType(0));
115         assertEquals(new Integer JavaDoc((int)17),parentTag.getValue(0));
116     }
117
118     public void testToIntFromNumber() throws Exception JavaDoc {
119         argTag.setType("int");
120         argTag.setValue(new Double JavaDoc(17.3d));
121         argTag.doTag(getXMLOutput());
122         assertEquals(Integer.TYPE,parentTag.getType(0));
123         assertEquals(new Integer JavaDoc((int)17),parentTag.getValue(0));
124     }
125
126     public void testToFloatFromString() throws Exception JavaDoc {
127         argTag.setType("float");
128         argTag.setValue("17.3");
129         argTag.doTag(getXMLOutput());
130         assertEquals(Float.TYPE,parentTag.getType(0));
131         assertEquals(new Float JavaDoc((float)17.3),parentTag.getValue(0));
132     }
133
134     public void testToFloatFromNumber() throws Exception JavaDoc {
135         argTag.setType("float");
136         argTag.setValue(new Double JavaDoc(17.3d));
137         argTag.doTag(getXMLOutput());
138         assertEquals(Float.TYPE,parentTag.getType(0));
139         assertEquals(new Float JavaDoc((float)17.3),parentTag.getValue(0));
140     }
141
142     public void testToLongFromString() throws Exception JavaDoc {
143         argTag.setType("long");
144         argTag.setValue("17");
145         argTag.doTag(getXMLOutput());
146         assertEquals(Long.TYPE,parentTag.getType(0));
147         assertEquals(new Long JavaDoc((int)17),parentTag.getValue(0));
148     }
149
150     public void testToLongFromNumber() throws Exception JavaDoc {
151         argTag.setType("long");
152         argTag.setValue(new Double JavaDoc(17.3d));
153         argTag.doTag(getXMLOutput());
154         assertEquals(Long.TYPE,parentTag.getType(0));
155         assertEquals(new Long JavaDoc((long)17),parentTag.getValue(0));
156     }
157
158     public void testToDoubleFromString() throws Exception JavaDoc {
159         argTag.setType("double");
160         argTag.setValue("17.3");
161         argTag.doTag(getXMLOutput());
162         assertEquals(Double.TYPE,parentTag.getType(0));
163         assertEquals(new Double JavaDoc((double)17.3),parentTag.getValue(0));
164     }
165
166     public void testToDoubleFromNumber() throws Exception JavaDoc {
167         argTag.setType("double");
168         argTag.setValue(new Long JavaDoc(17L));
169         argTag.doTag(getXMLOutput());
170         assertEquals(Double.TYPE,parentTag.getType(0));
171         assertEquals(new Double JavaDoc((double)17),parentTag.getValue(0));
172     }
173
174     public void testToPrimitiveFromNull() throws Exception JavaDoc {
175         String JavaDoc[] 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                 // expected
184
}
185         }
186     }
187
188     public void testFromNull() throws Exception JavaDoc {
189         Class JavaDoc[] types = { Boolean JavaDoc.class, Character JavaDoc.class, Byte JavaDoc.class, Short JavaDoc.class, Integer JavaDoc.class, Float JavaDoc.class, Long JavaDoc.class, Double JavaDoc.class, String JavaDoc.class, Object JavaDoc.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 JavaDoc type, Object JavaDoc value) {
204             typeList.add(type);
205             valueList.add(value);
206         }
207
208         public void doTag(XMLOutput output) {
209         }
210
211         private Class JavaDoc getType(int i) {
212             return (Class JavaDoc)(typeList.get(i));
213         }
214
215         private Object JavaDoc getValue(int i) {
216             return valueList.get(i);
217         }
218
219         private List JavaDoc typeList = new ArrayList JavaDoc();
220         private List JavaDoc valueList = new ArrayList JavaDoc();
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