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 17 package tests.api.org.xml.sax; 18 19 import junit.framework.TestCase; 20 21 import org.xml.sax.Locator; 22 import org.xml.sax.SAXParseException; 23 import org.xml.sax.helpers.LocatorImpl; 24 25 public class SAXParseExceptionTest extends TestCase { 26 27 public static final String ERR = "Houston, we have a problem"; 28 29 public static final String SYS = "mySystemID"; 30 31 public static final String PUB = "myPublicID"; 32 33 public static final int ROW = 1; 34 35 public static final int COL = 2; 36 37 public void testSAXParseException_String_Locator_Exception() { 38 LocatorImpl l = new LocatorImpl(); 39 l.setPublicId(PUB); 40 l.setSystemId(SYS); 41 l.setLineNumber(ROW); 42 l.setColumnNumber(COL); 43 44 Exception c = new Exception(); 45 46 // Ordinary case 47 SAXParseException e = new SAXParseException(ERR, l, c); 48 49 assertEquals(ERR, e.getMessage()); 50 assertEquals(c, e.getException()); 51 52 assertEquals(PUB, e.getPublicId()); 53 assertEquals(SYS, e.getSystemId()); 54 assertEquals(ROW, e.getLineNumber()); 55 assertEquals(COL, e.getColumnNumber()); 56 57 // No message 58 e = new SAXParseException(null, l, c); 59 60 assertNull(e.getMessage()); 61 assertEquals(c, e.getException()); 62 63 assertEquals(PUB, e.getPublicId()); 64 assertEquals(SYS, e.getSystemId()); 65 assertEquals(ROW, e.getLineNumber()); 66 assertEquals(COL, e.getColumnNumber()); 67 68 // No locator 69 e = new SAXParseException(ERR, null, c); 70 71 assertEquals(ERR, e.getMessage()); 72 assertEquals(c, e.getException()); 73 74 assertNull(e.getPublicId()); 75 assertNull(e.getSystemId()); 76 assertEquals(-1, e.getLineNumber()); 77 assertEquals(-1, e.getColumnNumber()); 78 79 // No cause 80 e = new SAXParseException(ERR, l, null); 81 82 assertEquals(ERR, e.getMessage()); 83 assertNull(e.getException()); 84 85 assertEquals(PUB, e.getPublicId()); 86 assertEquals(SYS, e.getSystemId()); 87 assertEquals(ROW, e.getLineNumber()); 88 assertEquals(COL, e.getColumnNumber()); 89 } 90 91 public void testSAXParseException_String_Locator() { 92 LocatorImpl l = new LocatorImpl(); 93 l.setPublicId(PUB); 94 l.setSystemId(SYS); 95 l.setLineNumber(ROW); 96 l.setColumnNumber(COL); 97 98 // Ordinary case 99 SAXParseException e = new SAXParseException(ERR, l); 100 101 assertEquals(ERR, e.getMessage()); 102 assertNull(e.getException()); 103 104 assertEquals(PUB, e.getPublicId()); 105 assertEquals(SYS, e.getSystemId()); 106 assertEquals(ROW, e.getLineNumber()); 107 assertEquals(COL, e.getColumnNumber()); 108 109 // No message 110 e = new SAXParseException(null, l); 111 112 assertNull(e.getMessage()); 113 assertNull(e.getException()); 114 115 assertEquals(PUB, e.getPublicId()); 116 assertEquals(SYS, e.getSystemId()); 117 assertEquals(ROW, e.getLineNumber()); 118 assertEquals(COL, e.getColumnNumber()); 119 120 // No locator 121 e = new SAXParseException(ERR, null); 122 123 assertEquals(ERR, e.getMessage()); 124 assertNull(e.getException()); 125 126 assertNull(e.getPublicId()); 127 assertNull(e.getSystemId()); 128 assertEquals(-1, e.getLineNumber()); 129 assertEquals(-1, e.getColumnNumber()); 130 131 } 132 133 public void testSAXParseException_String_String_String_int_int_Exception() { 134 Exception c = new Exception(); 135 136 // Ordinary case 137 SAXParseException e = new SAXParseException(ERR, PUB, SYS, ROW, COL, c); 138 139 assertEquals(ERR, e.getMessage()); 140 assertEquals(c, e.getException()); 141 142 assertEquals(PUB, e.getPublicId()); 143 assertEquals(SYS, e.getSystemId()); 144 assertEquals(ROW, e.getLineNumber()); 145 assertEquals(COL, e.getColumnNumber()); 146 147 // No message 148 e = new SAXParseException(null, PUB, SYS, ROW, COL, c); 149 150 assertNull(e.getMessage()); 151 assertEquals(c, e.getException()); 152 153 assertEquals(PUB, e.getPublicId()); 154 assertEquals(SYS, e.getSystemId()); 155 assertEquals(ROW, e.getLineNumber()); 156 assertEquals(COL, e.getColumnNumber()); 157 158 // No locator 159 e = new SAXParseException(ERR, null, null, -1, -1, c); 160 161 assertEquals(ERR, e.getMessage()); 162 assertEquals(c, e.getException()); 163 164 assertNull(e.getPublicId()); 165 assertNull(e.getSystemId()); 166 assertEquals(-1, e.getLineNumber()); 167 assertEquals(-1, e.getColumnNumber()); 168 169 // No cause 170 e = new SAXParseException(ERR, PUB, SYS, ROW, COL, null); 171 172 assertEquals(ERR, e.getMessage()); 173 assertNull(e.getException()); 174 175 assertEquals(PUB, e.getPublicId()); 176 assertEquals(SYS, e.getSystemId()); 177 assertEquals(ROW, e.getLineNumber()); 178 assertEquals(COL, e.getColumnNumber()); 179 } 180 181 public void testSAXParseException_String_String_String_int_int() { 182 // Ordinary case 183 SAXParseException e = new SAXParseException(ERR, PUB, SYS, ROW, COL); 184 185 assertEquals(ERR, e.getMessage()); 186 assertNull(e.getException()); 187 188 assertEquals(PUB, e.getPublicId()); 189 assertEquals(SYS, e.getSystemId()); 190 assertEquals(ROW, e.getLineNumber()); 191 assertEquals(COL, e.getColumnNumber()); 192 193 // No message 194 e = new SAXParseException(null, PUB, SYS, ROW, COL); 195 196 assertNull(e.getMessage()); 197 assertNull(e.getException()); 198 199 assertEquals(PUB, e.getPublicId()); 200 assertEquals(SYS, e.getSystemId()); 201 assertEquals(ROW, e.getLineNumber()); 202 assertEquals(COL, e.getColumnNumber()); 203 204 // No locator 205 e = new SAXParseException(ERR, null, null, -1, -1); 206 207 assertEquals(ERR, e.getMessage()); 208 assertNull(e.getException()); 209 210 assertNull(e.getPublicId()); 211 assertNull(e.getSystemId()); 212 assertEquals(-1, e.getLineNumber()); 213 assertEquals(-1, e.getColumnNumber()); 214 } 215 216 } 217