1/* 2 * Copyright (C) 2007 The Android Open Source Project 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 17package org.apache.harmony.tests.org.xml.sax.helpers; 18 19import java.io.IOException; 20import java.util.Locale; 21 22import junit.framework.TestCase; 23 24import org.apache.harmony.tests.org.xml.sax.support.MethodLogger; 25import org.apache.harmony.tests.org.xml.sax.support.MockHandler; 26import org.apache.harmony.tests.org.xml.sax.support.MockReader; 27import org.apache.harmony.tests.org.xml.sax.support.MockResolver; 28import org.xml.sax.AttributeList; 29import org.xml.sax.Attributes; 30import org.xml.sax.DTDHandler; 31import org.xml.sax.DocumentHandler; 32import org.xml.sax.EntityResolver; 33import org.xml.sax.ErrorHandler; 34import org.xml.sax.InputSource; 35import org.xml.sax.Locator; 36import org.xml.sax.SAXException; 37import org.xml.sax.XMLReader; 38import org.xml.sax.helpers.AttributesImpl; 39import org.xml.sax.helpers.LocatorImpl; 40import org.xml.sax.helpers.XMLReaderAdapter; 41 42@SuppressWarnings("deprecation") 43public class XMLReaderAdapterTest extends TestCase { 44 45 // Note: In many cases we can only test that delegation works 46 // properly. The rest is outside the scope of the specification. 47 48 private MethodLogger logger = new MethodLogger(); 49 50 private MockHandler handler = new MockHandler(logger); 51 52 private XMLReader reader = new MockReader(logger); 53 54 private XMLReaderAdapter adapter = new XMLReaderAdapter(reader); 55 56 private void assertEquals(Object[] a, Object[] b) { 57 assertEquals(a.length, b.length); 58 59 for (int i = 0; i < a.length; i++) { 60 assertEquals("Element #" + i + " must be equal", a[i], b[i]); 61 } 62 } 63 64 @Override 65 public void setUp() { 66 adapter.setDocumentHandler(handler); 67 adapter.setDTDHandler(handler); 68 adapter.setErrorHandler(handler); 69 } 70 71 @Override protected void tearDown() throws Exception { 72 super.tearDown(); 73 } 74 75 public void testXMLReaderAdapter() { 76 System.setProperty("org.xml.sax.driver", 77 "org.apache.harmony.tests.org.xml.sax.support.DoNothingXMLReader"); 78 79 try { 80 new XMLReaderAdapter(); 81 } catch (SAXException e) { 82 throw new RuntimeException("Unexpected exception", e); 83 } 84 } 85 86 public void testXMLReaderAdapterXMLReader() { 87 // Ordinary case 88 @SuppressWarnings("unused") 89 XMLReaderAdapter adapter = new XMLReaderAdapter(reader); 90 91 // Null case 92 try { 93 adapter = new XMLReaderAdapter(null); 94 fail("NullPointerException expected"); 95 } catch (NullPointerException e) { 96 // Expected 97 } 98 } 99 100 public void testSetLocale() { 101 // SAX RI does not support this, hence always expect exception 102 try { 103 adapter.setLocale(Locale.getDefault()); 104 fail("SAXException expected"); 105 } catch (SAXException e) { 106 // Expected 107 } 108 } 109 110 public void testSetEntityResolver() { 111 EntityResolver resolver = new MockResolver(); 112 113 // Ordinary case 114 adapter.setEntityResolver(resolver); 115 assertEquals(resolver, reader.getEntityResolver()); 116 117 // null case 118 adapter.setEntityResolver(null); 119 assertEquals(null, reader.getEntityResolver()); 120 } 121 122 public void testSetDTDHandler() { 123 // Ordinary case 124 assertEquals(handler, reader.getDTDHandler()); 125 126 // null case 127 adapter.setDTDHandler(null); 128 assertEquals(null, reader.getDTDHandler()); 129 } 130 131 public void testSetDocumentHandler() { 132 // There is no getter for the DocumentHandler, so we can only test 133 // indirectly whether is has been set correctly. 134 try { 135 adapter.startDocument(); 136 } catch (SAXException e) { 137 throw new RuntimeException("Unexpected exception", e); 138 } 139 140 assertEquals("startDocument", logger.getMethod()); 141 assertEquals(new Object[] { }, logger.getArgs()); 142 143 // null case 144 adapter.setDocumentHandler(null); 145 } 146 147 public void testSetErrorHandler() { 148 // Ordinary case 149 assertEquals(handler, reader.getErrorHandler()); 150 151 // null case 152 adapter.setErrorHandler(null); 153 assertEquals(null, reader.getErrorHandler()); 154 } 155 156 public void testParseString() { 157 try { 158 adapter.parse("foo"); 159 } catch (SAXException e) { 160 throw new RuntimeException("Unexpected exception", e); 161 } catch (IOException e) { 162 throw new RuntimeException("Unexpected exception", e); 163 } 164 165 // The SAX RI creates an InputSource itself and then delegates to the 166 // "other" parse method. 167 assertEquals("parse", logger.getMethod(0)); 168 assertEquals(InputSource.class, logger.getArgs(0)[0].getClass()); 169 } 170 171 public void testParseInputSource() { 172 InputSource source = new InputSource("foo"); 173 174 try { 175 adapter.parse(source); 176 } catch (SAXException e) { 177 throw new RuntimeException("Unexpected exception", e); 178 } catch (IOException e) { 179 throw new RuntimeException("Unexpected exception", e); 180 } 181 182 assertEquals("parse", logger.getMethod()); 183 assertEquals(new Object[] { source }, logger.getArgs()); 184 } 185 186 public void testSetDocumentLocator() { 187 // Ordinary case 188 LocatorImpl locator = new LocatorImpl(); 189 adapter.setDocumentLocator(locator); 190 191 assertEquals("setDocumentLocator", logger.getMethod()); 192 assertEquals(new Object[] { locator }, logger.getArgs()); 193 194 // null case (for the DocumentHandler itself!) 195 adapter.setDocumentHandler(null); 196 adapter.setDocumentLocator(locator); 197 } 198 199 public void testStartDocument() { 200 try { 201 adapter.startDocument(); 202 } catch (SAXException e) { 203 throw new RuntimeException("Unexpected exception", e); 204 } 205 206 assertEquals(logger.size(), 1); 207 assertEquals("startDocument", logger.getMethod()); 208 assertEquals(new Object[] {}, logger.getArgs()); 209 } 210 211 public void testEndDocument() { 212 try { 213 adapter.endDocument(); 214 } catch (SAXException e) { 215 throw new RuntimeException("Unexpected exception", e); 216 } 217 218 assertEquals(logger.size(), 1); 219 assertEquals("endDocument", logger.getMethod()); 220 assertEquals(new Object[] {}, logger.getArgs()); 221 } 222 223 public void testStartPrefixMapping() { 224 adapter.startPrefixMapping("foo", "http://some.uri"); 225 assertEquals(logger.size(), 0); 226 } 227 228 public void testEndPrefixMapping() { 229 adapter.endPrefixMapping("foo"); 230 assertEquals(logger.size(), 0); 231 } 232 233 public void testStartElement() { 234 AttributesImpl atts = new AttributesImpl(); 235 atts.addAttribute("http://some.other.uri", "gabba", "gabba:hey", 236 "int", "42"); 237 238 try { 239 adapter.startElement("http://some.uri", "bar", "foo:bar", atts); 240 } catch (SAXException e) { 241 throw new RuntimeException("Unexpected exception", e); 242 } 243 244 assertEquals(logger.size(), 1); 245 assertEquals("startElement", logger.getMethod()); 246 assertEquals("foo:bar", logger.getArgs()[0]); 247 assertEquals("gabba:hey", 248 ((AttributeList)logger.getArgs()[1]).getName(0)); 249 } 250 251 public void testEndElement() { 252 try { 253 adapter.endElement("http://some.uri", "bar", "foo:bar"); 254 } catch (SAXException e) { 255 throw new RuntimeException("Unexpected exception", e); 256 } 257 258 assertEquals(logger.size(), 1); 259 assertEquals("endElement", logger.getMethod()); 260 assertEquals(new Object[] { "foo:bar" }, logger.getArgs()); 261 } 262 263 public void testCharacters() { 264 char[] ch = "Android".toCharArray(); 265 266 try { 267 adapter.characters(ch, 2, 5); 268 } catch (SAXException e) { 269 throw new RuntimeException("Unexpected exception", e); 270 } 271 272 assertEquals(logger.size(), 1); 273 assertEquals("characters", logger.getMethod()); 274 assertEquals(new Object[] { ch, 2, 5 }, logger.getArgs()); 275 } 276 277 public void testIgnorableWhitespace() { 278 char[] ch = " ".toCharArray(); 279 280 try { 281 adapter.ignorableWhitespace(ch, 0, 5); 282 } catch (SAXException e) { 283 throw new RuntimeException("Unexpected exception", e); 284 } 285 286 assertEquals(logger.size(), 1); 287 assertEquals("ignorableWhitespace", logger.getMethod()); 288 assertEquals(new Object[] { ch, 0, 5 }, logger.getArgs()); 289 } 290 291 public void testProcessingInstruction() { 292 try { 293 adapter.processingInstruction("foo", "bar"); 294 } catch (SAXException e) { 295 throw new RuntimeException("Unexpected exception", e); 296 } 297 298 assertEquals(logger.size(), 1); 299 assertEquals("processingInstruction", logger.getMethod()); 300 assertEquals(new Object[] { "foo" , "bar" }, logger.getArgs()); 301 } 302 303 public void testSkippedEntity() { 304 try { 305 adapter.skippedEntity("foo"); 306 } catch (SAXException e) { 307 throw new RuntimeException("Unexpected exception", e); 308 } 309 310 assertEquals(logger.size(), 0); 311 } 312 313} 314