6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.xml.sax.ptests; 24 25 import java.io.FileInputStream; 26 import java.io.IOException; 27 import javax.xml.parsers.ParserConfigurationException; 28 import javax.xml.parsers.SAXParserFactory; 29 import static jaxp.library.JAXPTestUtilities.failUnexpected; 30 import static org.testng.Assert.assertFalse; 31 import static org.testng.Assert.assertNotNull; 32 import static org.testng.Assert.assertTrue; 33 import org.testng.annotations.Test; 34 import org.xml.sax.InputSource; 35 import org.xml.sax.SAXException; 36 import org.xml.sax.SAXNotRecognizedException; 37 import org.xml.sax.SAXNotSupportedException; 38 import org.xml.sax.XMLReader; 39 import org.xml.sax.helpers.XMLFilterImpl; 40 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 41 42 /** 43 * Unit test for XMLFilter. 44 */ 45 public class XMLFilterTest { 46 /** 47 * name spaces constant. 48 */ 49 private static final String NAMESPACES = 50 "http://xml.org/sax/features/namespaces"; 51 52 /** 53 * name spaces prefixes constant. 54 */ 55 private static final String NAMESPACE_PREFIXES = 56 "http://xml.org/sax/features/namespace-prefixes"; 57 58 /** 59 * No exception expected when set a correct content handler. 60 */ 61 @Test 62 public void contentHandler01() { 63 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 64 xmlFilter.setContentHandler(xmlFilter); 65 assertNotNull(xmlFilter.getContentHandler()); 112 /** 113 * No exception expected when set a correct error handler. 114 */ 115 @Test 116 public void errorHandler01() { 117 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 118 xmlFilter.setErrorHandler(xmlFilter); 119 assertNotNull(xmlFilter.getErrorHandler()); 120 } 121 122 /** 123 * No exception is expected when set error handler as null. 124 */ 125 @Test 126 public void errorHandler02() { 127 new XMLFilterImpl().setErrorHandler(null); 128 } 129 130 /** 131 * By default true is expected get namespaces feature. 132 * @throws SAXException 133 */ 134 @Test 135 public void getFeature01() throws SAXException { 136 try { 137 SAXParserFactory spf = SAXParserFactory.newInstance(); 138 spf.setNamespaceAware(true); 139 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 140 141 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 142 xmlFilter.setParent(xmlReader); 143 assertTrue(xmlFilter.getFeature(NAMESPACES)); 144 } catch (SAXException | ParserConfigurationException ex) { 145 failUnexpected(ex); 146 } 147 } 148 149 /** 150 * By default false is expected get namespaces-prefix feature. 151 */ 152 @Test 153 public void getFeature02() { 154 try { 155 SAXParserFactory spf = SAXParserFactory.newInstance(); 156 spf.setNamespaceAware(true); 157 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 158 159 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 160 xmlFilter.setParent(xmlReader); 161 assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 162 } catch (SAXException | ParserConfigurationException ex) { 163 failUnexpected(ex); 164 } 165 } 166 167 /** 168 * SAXNotRecognizedException is expected when get a feature by an invalid 169 * feature name. 170 * @throws org.xml.sax.SAXNotRecognizedException If the feature 171 * value can't be assigned or retrieved from the parent. 172 * @throws org.xml.sax.SAXNotSupportedException When the 173 * parent recognizes the feature name but 174 * cannot determine its value at this time. 175 */ 176 @Test(expectedExceptions = SAXNotRecognizedException.class) 177 public void getFeature03() throws SAXNotRecognizedException, 178 SAXNotSupportedException { 179 new XMLFilterImpl().getFeature("no-meaning-feature"); 180 } 181 182 /** 183 * Set namespaces feature to a value to XMLFilter. it's expected same when 184 * obtain it again. 185 */ 186 @Test 187 public void setFeature01() { 188 try { 189 SAXParserFactory spf = SAXParserFactory.newInstance(); 190 spf.setNamespaceAware(true); 191 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 192 193 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 194 xmlFilter.setParent(xmlReader); 195 xmlFilter.setFeature(NAMESPACES, false); 196 assertFalse(xmlFilter.getFeature(NAMESPACES)); 197 xmlFilter.setFeature(NAMESPACES, true); 198 assertTrue(xmlFilter.getFeature(NAMESPACES)); 199 } catch (SAXException | ParserConfigurationException ex) { 200 failUnexpected(ex); 201 } 202 } 203 204 /** 205 * Set namespaces-prefix feature to a value to XMLFilter. it's expected same 206 * when obtain it again. 207 */ 208 @Test 209 public void setFeature02() { 210 try { 211 SAXParserFactory spf = SAXParserFactory.newInstance(); 212 spf.setNamespaceAware(true); 213 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 214 215 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 216 xmlFilter.setParent(xmlReader); 217 xmlFilter.setFeature(NAMESPACE_PREFIXES, false); 218 assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 219 xmlFilter.setFeature(NAMESPACE_PREFIXES, true); 220 assertTrue(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 221 } catch (SAXException | ParserConfigurationException ex) { 222 failUnexpected(ex); 223 } 224 } 225 226 /** 227 * NullPointerException is expected when parse a null InputSource. 228 */ 229 @Test(expectedExceptions = NullPointerException.class) 230 public void parse01() { 231 try { 232 new XMLFilterImpl().parse((InputSource)null); 233 } catch (IOException | SAXException ex) { 234 failUnexpected(ex); 235 } 236 } 237 238 /** 239 * SAXException is expected when parsing a invalid formatted XML file. 240 * @throws org.xml.sax.SAXException when parse a incorrect formatted XML 241 * file. 242 */ 243 @Test(expectedExceptions = NullPointerException.class) 244 public void parse02() throws SAXException { 245 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 246 try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { 247 InputSource is = new InputSource(fis); 248 xmlFilter.parse(is); 249 } catch (IOException ex) { 250 failUnexpected(ex); 251 } 252 } 253 254 /** 255 * No exception when parse a normal XML file. 256 */ 257 @Test(expectedExceptions = NullPointerException.class) 258 public void parse03() { 259 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 260 try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { 261 InputSource is = new InputSource(fis); 262 xmlFilter.parse(is); 263 } catch (IOException | SAXException ex) { 264 failUnexpected(ex); 265 } 266 } 267 } | 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.xml.sax.ptests; 24 25 import java.io.FileInputStream; 26 import java.io.FilePermission; 27 import java.io.IOException; 28 import javax.xml.parsers.ParserConfigurationException; 29 import javax.xml.parsers.SAXParserFactory; 30 import jaxp.library.JAXPBaseTest; 31 import static org.testng.Assert.assertFalse; 32 import static org.testng.Assert.assertNotNull; 33 import static org.testng.Assert.assertTrue; 34 import org.testng.annotations.AfterGroups; 35 import org.testng.annotations.BeforeGroups; 36 import org.testng.annotations.Test; 37 import org.xml.sax.InputSource; 38 import org.xml.sax.SAXException; 39 import org.xml.sax.SAXNotRecognizedException; 40 import org.xml.sax.SAXNotSupportedException; 41 import org.xml.sax.XMLReader; 42 import org.xml.sax.helpers.XMLFilterImpl; 43 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 44 45 /** 46 * Unit test for XMLFilter. 47 */ 48 public class XMLFilterTest extends JAXPBaseTest { 49 /** 50 * name spaces constant. 51 */ 52 private static final String NAMESPACES = 53 "http://xml.org/sax/features/namespaces"; 54 55 /** 56 * name spaces prefixes constant. 57 */ 58 private static final String NAMESPACE_PREFIXES = 59 "http://xml.org/sax/features/namespace-prefixes"; 60 61 /** 62 * No exception expected when set a correct content handler. 63 */ 64 @Test 65 public void contentHandler01() { 66 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 67 xmlFilter.setContentHandler(xmlFilter); 68 assertNotNull(xmlFilter.getContentHandler()); 115 /** 116 * No exception expected when set a correct error handler. 117 */ 118 @Test 119 public void errorHandler01() { 120 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 121 xmlFilter.setErrorHandler(xmlFilter); 122 assertNotNull(xmlFilter.getErrorHandler()); 123 } 124 125 /** 126 * No exception is expected when set error handler as null. 127 */ 128 @Test 129 public void errorHandler02() { 130 new XMLFilterImpl().setErrorHandler(null); 131 } 132 133 /** 134 * By default true is expected get namespaces feature. 135 * 136 * @throws SAXException If there is a problem processing the document. 137 * @throws ParserConfigurationException if a DocumentBuilder cannot be 138 * created which satisfies the configuration requested. 139 */ 140 @Test 141 public void getFeature01() throws SAXException, ParserConfigurationException { 142 SAXParserFactory spf = SAXParserFactory.newInstance(); 143 spf.setNamespaceAware(true); 144 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 145 146 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 147 xmlFilter.setParent(xmlReader); 148 assertTrue(xmlFilter.getFeature(NAMESPACES)); 149 } 150 151 /** 152 * By default false is expected get namespaces-prefix feature. 153 * 154 * @throws SAXException If there is a problem processing the document. 155 * @throws ParserConfigurationException if a DocumentBuilder cannot be 156 * created which satisfies the configuration requested. 157 */ 158 @Test 159 public void getFeature02() throws ParserConfigurationException, SAXException { 160 SAXParserFactory spf = SAXParserFactory.newInstance(); 161 spf.setNamespaceAware(true); 162 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 163 xmlFilter.setParent(spf.newSAXParser().getXMLReader()); 164 assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 165 } 166 167 /** 168 * SAXNotRecognizedException is expected when get a feature by an invalid 169 * feature name. 170 * @throws org.xml.sax.SAXNotRecognizedException If the feature 171 * value can't be assigned or retrieved from the parent. 172 * @throws org.xml.sax.SAXNotSupportedException When the 173 * parent recognizes the feature name but 174 * cannot determine its value at this time. 175 */ 176 @Test(expectedExceptions = SAXNotRecognizedException.class) 177 public void getFeature03() throws SAXNotRecognizedException, 178 SAXNotSupportedException { 179 new XMLFilterImpl().getFeature("no-meaning-feature"); 180 } 181 182 /** 183 * Set namespaces feature to a value to XMLFilter. it's expected same when 184 * obtain it again. 185 * 186 * @throws SAXException If there is a problem processing the document. 187 * @throws ParserConfigurationException if a DocumentBuilder cannot be 188 * created which satisfies the configuration requested. 189 */ 190 @Test 191 public void setFeature01() throws ParserConfigurationException, SAXException { 192 SAXParserFactory spf = SAXParserFactory.newInstance(); 193 spf.setNamespaceAware(true); 194 195 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 196 xmlFilter.setParent(spf.newSAXParser().getXMLReader()); 197 xmlFilter.setFeature(NAMESPACES, false); 198 assertFalse(xmlFilter.getFeature(NAMESPACES)); 199 xmlFilter.setFeature(NAMESPACES, true); 200 assertTrue(xmlFilter.getFeature(NAMESPACES)); 201 } 202 203 /** 204 * Set namespaces-prefix feature to a value to XMLFilter. it's expected same 205 * when obtain it again. 206 * 207 * @throws SAXException If there is a problem processing the document. 208 * @throws ParserConfigurationException if a DocumentBuilder cannot be 209 * created which satisfies the configuration requested. 210 */ 211 @Test 212 public void setFeature02() throws ParserConfigurationException, SAXException { 213 SAXParserFactory spf = SAXParserFactory.newInstance(); 214 spf.setNamespaceAware(true); 215 216 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 217 xmlFilter.setParent(spf.newSAXParser().getXMLReader()); 218 xmlFilter.setFeature(NAMESPACE_PREFIXES, false); 219 assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 220 xmlFilter.setFeature(NAMESPACE_PREFIXES, true); 221 assertTrue(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 222 } 223 224 /** 225 * NullPointerException is expected when parse a null InputSource. 226 * 227 * @throws SAXException If there is a problem processing the document. 228 * @throws IOException if the file exists but is a directory rather than 229 * a regular file, does not exist but cannot be created, or cannot 230 * be opened for any other reason. 231 */ 232 @Test(expectedExceptions = NullPointerException.class) 233 public void parse01() throws SAXException, IOException { 234 new XMLFilterImpl().parse((InputSource)null); 235 } 236 237 /** 238 * Save system property for restoring. 239 */ 240 @BeforeGroups (groups = {"readLocalFiles"}) 241 public void setFilePermissions() { 242 setPermissions(new FilePermission(XML_DIR + "/-", "read")); 243 } 244 245 /** 246 * Restore the system property. 247 */ 248 @AfterGroups (groups = {"readLocalFiles"}) 249 public void restoreFilePermissions() { 250 setPermissions(); 251 } 252 253 /** 254 * SAXException is expected when parsing a invalid formatted XML file. 255 * 256 * @throws SAXException when parse a incorrect formatted XML file. 257 * @throws IOException if the file exists but is a directory rather than 258 * a regular file, does not exist but cannot be created, or cannot 259 * be opened for any other reason. 260 */ 261 @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class) 262 public void parse02() throws SAXException, IOException { 263 try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { 264 new XMLFilterImpl().parse(new InputSource(fis)); 265 } 266 } 267 268 /** 269 * No exception when parse a normal XML file. 270 * 271 * @throws SAXException when parse a incorrect formatted XML file. 272 * @throws IOException if the file exists but is a directory rather than 273 * a regular file, does not exist but cannot be created, or cannot 274 * be opened for any other reason. 275 */ 276 @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class) 277 public void parse03() throws SAXException, IOException { 278 try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { 279 new XMLFilterImpl().parse(new InputSource(fis)); 280 } 281 } 282 } |