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