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 tests.api.org.xml.sax.helpers; 18 19import dalvik.annotation.TestLevel; 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargetNew; 22 23import junit.framework.TestCase; 24 25import org.xml.sax.Attributes; 26import org.xml.sax.SAXException; 27import org.xml.sax.SAXParseException; 28import org.xml.sax.helpers.AttributesImpl; 29import org.xml.sax.helpers.DefaultHandler; 30import org.xml.sax.helpers.LocatorImpl; 31 32import java.io.IOException; 33 34@TestTargetClass(DefaultHandler.class) 35public class DefaultHandlerTest extends TestCase { 36 37 /* 38 * Note: most of the tests have to check for an empty implementation of the 39 * respective methods and, as a result, are somewhat trivial. 40 */ 41 42 private DefaultHandler h = new DefaultHandler(); 43 44 @TestTargetNew( 45 level = TestLevel.COMPLETE, 46 method = "resolveEntity", 47 args = { String.class, String.class } 48 ) 49 public void testResolveEntity() { 50 try { 51 h.resolveEntity("publicID", "systemID"); 52 } catch (SAXException e) { 53 throw new RuntimeException(e); 54 } catch (IOException e) { 55 throw new RuntimeException(e); 56 } 57 } 58 59 @TestTargetNew( 60 level = TestLevel.COMPLETE, 61 method = "notationDecl", 62 args = { String.class, String.class, String.class } 63 ) 64 public void testNotationDecl() { 65 try { 66 h.notationDecl("name", "publicID", "systemID"); 67 } catch (SAXException e) { 68 throw new RuntimeException(e); 69 } 70 } 71 72 @TestTargetNew( 73 level = TestLevel.COMPLETE, 74 method = "unparsedEntityDecl", 75 args = { String.class, String.class, String.class, String.class } 76 ) 77 public void testUnparsedEntityDecl() { 78 try { 79 h.unparsedEntityDecl("name", "publicID", "systemID", 80 "notationName"); 81 } catch (SAXException e) { 82 throw new RuntimeException(e); 83 } 84 } 85 86 @TestTargetNew( 87 level = TestLevel.COMPLETE, 88 method = "setDocumentLocator", 89 args = { org.xml.sax.Locator.class } 90 ) 91 public void testSetDocumentLocator() { 92 h.setDocumentLocator(new LocatorImpl()); 93 } 94 95 @TestTargetNew( 96 level = TestLevel.COMPLETE, 97 method = "startDocument", 98 args = { } 99 ) 100 public void testStartDocument() { 101 try { 102 h.startDocument(); 103 } catch (SAXException e) { 104 throw new RuntimeException(e); 105 } 106 } 107 108 @TestTargetNew( 109 level = TestLevel.COMPLETE, 110 method = "endDocument", 111 args = { } 112 ) 113 public void testEndDocument() { 114 try { 115 h.endDocument(); 116 } catch (SAXException e) { 117 throw new RuntimeException(e); 118 } 119 } 120 121 @TestTargetNew( 122 level = TestLevel.COMPLETE, 123 method = "startPrefixMapping", 124 args = { String.class, String.class } 125 ) 126 public void testStartPrefixMapping() { 127 try { 128 h.startPrefixMapping("prefix", "uri"); 129 } catch (SAXException e) { 130 throw new RuntimeException(e); 131 } 132 } 133 134 @TestTargetNew( 135 level = TestLevel.COMPLETE, 136 method = "endPrefixMapping", 137 args = { String.class } 138 ) 139 public void testEndPrefixMapping() { 140 try { 141 h.endPrefixMapping("prefix"); 142 } catch (SAXException e) { 143 throw new RuntimeException(e); 144 } 145 } 146 147 @TestTargetNew( 148 level = TestLevel.COMPLETE, 149 method = "startElement", 150 args = { String.class, String.class, String.class, 151 Attributes.class } 152 ) 153 public void testStartElement() { 154 try { 155 h.startElement("uri", "name", "qname", new AttributesImpl()); 156 } catch (SAXException e) { 157 throw new RuntimeException(e); 158 } 159 } 160 161 @TestTargetNew( 162 level = TestLevel.COMPLETE, 163 method = "endElement", 164 args = { String.class, String.class, String.class } 165 ) 166 public void testEndElement() { 167 try { 168 h.endElement("uri", "name", "qname"); 169 } catch (SAXException e) { 170 throw new RuntimeException(e); 171 } 172 } 173 174 @TestTargetNew( 175 level = TestLevel.COMPLETE, 176 method = "characters", 177 args = { char[].class, int.class, int.class } 178 ) 179 public void testCharacters() { 180 try { 181 h.characters("The quick brown fox".toCharArray(), 4, 11); 182 } catch (SAXException e) { 183 throw new RuntimeException(e); 184 } 185 } 186 187 @TestTargetNew( 188 level = TestLevel.COMPLETE, 189 method = "ignorableWhitespace", 190 args = { char[].class, int.class, int.class } 191 ) 192 public void testIgnorableWhitespace() { 193 try { 194 h.ignorableWhitespace(" ".toCharArray(), 4, 11); 195 } catch (SAXException e) { 196 throw new RuntimeException(e); 197 } 198 } 199 200 @TestTargetNew( 201 level = TestLevel.COMPLETE, 202 method = "processingInstruction", 203 args = { String.class, String.class } 204 ) 205 public void testProcessingInstruction() { 206 try { 207 h.processingInstruction("target", "data"); 208 } catch (SAXException e) { 209 throw new RuntimeException(e); 210 } 211 } 212 213 @TestTargetNew( 214 level = TestLevel.COMPLETE, 215 method = "skippedEntity", 216 args = { String.class } 217 ) 218 public void testSkippedEntity() { 219 try { 220 h.skippedEntity("name"); 221 } catch (SAXException e) { 222 throw new RuntimeException(e); 223 } 224 } 225 226 @TestTargetNew( 227 level = TestLevel.COMPLETE, 228 method = "warning", 229 args = { org.xml.sax.SAXParseException.class } 230 ) 231 public void testWarning() { 232 try { 233 h.warning(new SAXParseException("Foo", new LocatorImpl())); 234 } catch (SAXException e) { 235 throw new RuntimeException(e); 236 } 237 } 238 239 @TestTargetNew( 240 level = TestLevel.COMPLETE, 241 method = "error", 242 args = { org.xml.sax.SAXParseException.class } 243 ) 244 public void testError() { 245 try { 246 h.error(new SAXParseException("Foo", new LocatorImpl())); 247 } catch (SAXException e) { 248 throw new RuntimeException(e); 249 } 250 } 251 252 @TestTargetNew( 253 level = TestLevel.COMPLETE, 254 method = "fatalError", 255 args = { org.xml.sax.SAXParseException.class } 256 ) 257 public void testFatalError() { 258 // Ordinary case 259 try { 260 h.fatalError(new SAXParseException("Foo", new LocatorImpl())); 261 fail("SAXException expected"); 262 } catch (SAXException e) { 263 // Expected 264 } 265 266 // No exception 267 try { 268 h.fatalError(null); 269 fail("NullPointerException expected"); 270 } catch (SAXException e) { 271 fail("NullPointerException expected"); 272 } catch (NullPointerException e) { 273 // Expected 274 } 275 276 } 277 278} 279