001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.jocl;
019
020 import junit.framework.Test;
021 import junit.framework.TestCase;
022 import junit.framework.TestSuite;
023
024 import org.xml.sax.helpers.AttributesImpl;
025 import org.xml.sax.SAXException;
026
027 import java.util.Collection;
028 import java.util.Iterator;
029 import java.util.List;
030 import java.io.InputStream;
031 import java.io.IOException;
032
033 /**
034 * @version $Revision: 1023401 $ $Date: 2010-10-16 21:54:24 -0400 (Sat, 16 Oct 2010) $
035 */
036 public class TestJOCLContentHandler extends TestCase {
037 public TestJOCLContentHandler(String testName) {
038 super(testName);
039 if (Float.parseFloat(System.getProperty("java.specification.version")) <
040 1.6f) {
041 if(null == System.getProperty("org.xml.sax.driver")) {
042 System.setProperty("org.xml.sax.driver","org.apache.xerces.parsers.SAXParser");
043 }
044 }
045 }
046
047 public static Test suite() {
048 return new TestSuite(TestJOCLContentHandler.class);
049 }
050
051 public static void main(String args[]) {
052 String[] testCaseName = { TestJOCLContentHandler.class.getName() };
053 junit.textui.TestRunner.main(testCaseName);
054 }
055
056 private JOCLContentHandler jocl = null;
057
058 public void setUp() {
059 jocl = new JOCLContentHandler();
060 }
061
062 public void testPrimitives() throws Exception {
063 jocl.startDocument();
064 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","jocl","jocl",new AttributesImpl());
065 {
066 AttributesImpl attr = new AttributesImpl();
067 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","true");
068 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","boolean","boolean",attr);
069 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","boolean","boolean");
070 }
071 {
072 AttributesImpl attr = new AttributesImpl();
073 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","1");
074 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","byte","byte",attr);
075 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","byte","byte");
076 }
077 {
078 AttributesImpl attr = new AttributesImpl();
079 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","c");
080 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","char","char",attr);
081 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","char","char");
082 }
083 {
084 AttributesImpl attr = new AttributesImpl();
085 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","2.0");
086 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","double","double",attr);
087 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","double","double");
088 }
089 {
090 AttributesImpl attr = new AttributesImpl();
091 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","3.0");
092 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","float","float",attr);
093 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","float","float");
094 }
095 {
096 AttributesImpl attr = new AttributesImpl();
097 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","5");
098 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","int","int",attr);
099 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","int","int");
100 }
101 {
102 AttributesImpl attr = new AttributesImpl();
103 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","7");
104 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long",attr);
105 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long");
106 }
107 {
108 AttributesImpl attr = new AttributesImpl();
109 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","11");
110 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","short","short",attr);
111 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","short","short");
112 }
113 {
114 AttributesImpl attr = new AttributesImpl();
115 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","All your base are belong to us.");
116 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","string","string",attr);
117 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","string","string");
118 }
119 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","jocl","jocl");
120 jocl.endDocument();
121
122 assertEquals(Boolean.TYPE,jocl.getType(0));
123 assertEquals(Byte.TYPE,jocl.getType(1));
124 assertEquals(Character.TYPE,jocl.getType(2));
125 assertEquals(Double.TYPE,jocl.getType(3));
126 assertEquals(Float.TYPE,jocl.getType(4));
127 assertEquals(Integer.TYPE,jocl.getType(5));
128 assertEquals(Long.TYPE,jocl.getType(6));
129 assertEquals(Short.TYPE,jocl.getType(7));
130 assertEquals(String.class,jocl.getType(8));
131
132 assertEquals(Boolean.TRUE,jocl.getValue(0));
133 assertEquals(new Byte("1"),jocl.getValue(1));
134 assertEquals(new Character('c'),jocl.getValue(2));
135 assertEquals(new Double("2.0"),jocl.getValue(3));
136 assertEquals(new Float("3.0"),jocl.getValue(4));
137 assertEquals(new Integer("5"),jocl.getValue(5));
138 assertEquals(new Long("7"),jocl.getValue(6));
139 assertEquals(new Short("11"),jocl.getValue(7));
140 assertEquals("All your base are belong to us.",jocl.getValue(8));
141 }
142
143 public void testObject() throws Exception {
144 jocl.startDocument();
145 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","jocl","jocl",new AttributesImpl());
146 {
147 AttributesImpl attr = new AttributesImpl();
148 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","null","null","CDATA","true");
149 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","class","class","CDATA","java.lang.String");
150 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object",attr);
151 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object");
152 }
153 {
154 AttributesImpl attr = new AttributesImpl();
155 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","class","class","CDATA","java.util.Date");
156 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object",attr);
157 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object");
158 }
159 {
160 AttributesImpl attr = new AttributesImpl();
161 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","class","class","CDATA","java.util.Date");
162 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object",attr);
163 }
164 {
165 AttributesImpl attr = new AttributesImpl();
166 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","345");
167 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long",attr);
168 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long");
169 }
170 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object");
171
172 // test array of 2 longs
173 {
174 AttributesImpl attr = new AttributesImpl();
175 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","array","array",attr);
176 }
177 {
178 AttributesImpl attr = new AttributesImpl();
179 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","12");
180 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long",attr);
181 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long");
182 }
183 {
184 AttributesImpl attr = new AttributesImpl();
185 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","34");
186 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long",attr);
187 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long");
188 }
189 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object");
190
191 // test collection of 2 Strings, one null
192 {
193 AttributesImpl attr = new AttributesImpl();
194 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","collection","collection",attr);
195 }
196 {
197 AttributesImpl attr = new AttributesImpl();
198 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","null","null","CDATA","true");
199 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","class","class","CDATA","java.lang.String");
200 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object",attr);
201 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object");
202 }
203 {
204 AttributesImpl attr = new AttributesImpl();
205 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","String #1");
206 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","string","string",attr);
207 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","string","string");
208 }
209 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object");
210
211 // test list of 1 Date, one Long
212 {
213 AttributesImpl attr = new AttributesImpl();
214 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","list","list",attr);
215 }
216 {
217 AttributesImpl attr = new AttributesImpl();
218 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","class","class","CDATA","java.util.Date");
219 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object",attr);
220 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object");
221 }
222 {
223 AttributesImpl attr = new AttributesImpl();
224 attr.addAttribute("http://apache.org/xml/xmlns/jakarta/commons/jocl","value","value","CDATA","12");
225 jocl.startElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long",attr);
226 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","long","long");
227 }
228 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","object","object");
229
230 jocl.endElement("http://apache.org/xml/xmlns/jakarta/commons/jocl","jocl","jocl");
231 jocl.endDocument();
232
233 assertEquals(String.class,jocl.getType(0));
234 assertEquals(java.util.Date.class,jocl.getType(1));
235 assertEquals(java.util.Date.class,jocl.getType(2));
236 assertTrue(jocl.getType(3).isArray());
237 assertEquals(Collection.class,jocl.getType(4));
238 assertEquals(List.class,jocl.getType(5));
239
240 assertTrue(null == jocl.getValue(0));
241 assertTrue(null != jocl.getValue(1));
242 assertEquals(new java.util.Date(345L),jocl.getValue(2));
243
244 Object[] objects = (Object[])jocl.getValue(3);
245 assertEquals(new Long(12L), objects[0]);
246 assertEquals(new Long(34L), objects[1]);
247
248 Iterator iterator = ((Collection)jocl.getValue(4)).iterator();
249 assertNull(iterator.next());
250 assertEquals("String #1", iterator.next());
251
252 List list = (List) jocl.getValue(5);
253 assertEquals(java.util.Date.class,list.get(0).getClass());
254 assertEquals(new Long(12L),list.get(1));
255 }
256
257 public void testParse()
258 throws IOException, SAXException
259 {
260 InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("testpool.jocl");
261 JOCLContentHandler.parse(stream);
262 }
263 }