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 24 package javax.xml.parsers.ptests; 25 26 import static jaxp.library.JAXPTestUtilities.failUnexpected; 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertTrue; 29 30 import java.io.File; 31 import java.io.IOException; 32 33 import javax.xml.parsers.ParserConfigurationException; 34 import javax.xml.parsers.SAXParser; 35 import javax.xml.parsers.SAXParserFactory; 36 37 import org.testng.annotations.DataProvider; 38 import org.testng.annotations.Test; 39 import org.xml.sax.SAXException; 40 41 /** 42 * Class contains the test cases for SAXParser API 43 */ 44 public class SAXParserTest03 { 45 46 /** 47 * Provide SAXParserFactory. 48 * 49 * @throws Exception 50 */ 51 @DataProvider(name = "input-provider") 52 public Object[][] getFactory() { 53 SAXParserFactory spf = SAXParserFactory.newInstance(); 54 spf.setValidating(true); 55 MyErrorHandler handler = MyErrorHandler.newInstance(); 56 return new Object[][] { { spf, handler } }; 57 } 58 59 /** 60 * parsertest.xml holds a valid document. This method tests the validating 61 * parser. 62 */ 63 @Test(dataProvider = "input-provider") 64 public void testParseValidate01(SAXParserFactory spf, MyErrorHandler handler) { 65 try { 66 SAXParser saxparser = spf.newSAXParser(); 67 saxparser.parse(new File(TestUtils.XML_DIR, "parsertest.xml"), handler); 68 assertFalse(handler.errorOccured); 69 } catch (ParserConfigurationException | SAXException | IOException e) { 70 failUnexpected(e); 71 } 72 } 73 74 /** 75 * validns.xml holds a valid document with XML namespaces in it. This method 76 * tests the Validating parser with namespace processing on. 77 */ 78 @Test(dataProvider = "input-provider") 79 public void testParseValidate02(SAXParserFactory spf, MyErrorHandler handler) { 80 try { 81 spf.setNamespaceAware(true); 82 SAXParser saxparser = spf.newSAXParser(); 83 saxparser.parse(new File(TestUtils.XML_DIR, "validns.xml"), handler); 84 assertFalse(handler.errorOccured); 85 } catch (ParserConfigurationException | SAXException | IOException e) { 86 failUnexpected(e); 87 } 88 } 89 90 /** 91 * invalidns.xml holds an invalid document with XML namespaces in it. This 92 * method tests the validating parser with namespace processing on. It 93 * should throw validation error. 94 */ 95 @Test(dataProvider = "input-provider") 96 public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler) { 97 try { 98 spf.setNamespaceAware(true); 99 SAXParser saxparser = spf.newSAXParser(); 100 saxparser.parse(new File(TestUtils.XML_DIR, "invalidns.xml"), handler); 101 failUnexpected(new RuntimeException()); 102 } catch (ParserConfigurationException | SAXException | IOException e) { 103 if (e instanceof SAXException) { 104 assertTrue(handler.errorOccured); 105 } 106 } 107 } 108 109 } | 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 24 package javax.xml.parsers.ptests; 25 26 import static jaxp.library.JAXPTestUtilities.failUnexpected; 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertTrue; 29 30 import java.io.File; 31 import java.io.FilePermission; 32 import java.io.IOException; 33 34 import javax.xml.parsers.ParserConfigurationException; 35 import javax.xml.parsers.SAXParser; 36 import javax.xml.parsers.SAXParserFactory; 37 import static javax.xml.parsers.ptests.TestUtils.XML_DIR; 38 import jaxp.library.JAXPBaseTest; 39 import org.testng.annotations.AfterGroups; 40 import org.testng.annotations.BeforeGroups; 41 42 import org.testng.annotations.DataProvider; 43 import org.testng.annotations.Test; 44 import org.xml.sax.SAXException; 45 46 /** 47 * Class contains the test cases for SAXParser API 48 */ 49 public class SAXParserTest03 extends JAXPBaseTest { 50 51 /** 52 * Provide SAXParserFactory. 53 * 54 * @return a dimensional contains. 55 */ 56 @DataProvider(name = "input-provider") 57 public Object[][] getFactory() { 58 SAXParserFactory spf = SAXParserFactory.newInstance(); 59 spf.setValidating(true); 60 return new Object[][] { { spf, MyErrorHandler.newInstance() } }; 61 } 62 63 /** 64 * Save system property for restoring. 65 */ 66 @BeforeGroups (groups = {"readLocalFiles"}) 67 public void setFilePermissions() { 68 setPermissions(new FilePermission(XML_DIR + "/-", 69 "read")); 70 } 71 72 /** 73 * Restore the system property. 74 */ 75 @AfterGroups (groups = {"readLocalFiles"}) 76 public void restoreFilePermissions() { 77 setPermissions(); 78 } 79 80 /** 81 * parsertest.xml holds a valid document. This method tests the validating 82 * parser. 83 * 84 * @param spf a Parser factory. 85 * @param handler an error handler for capturing events. 86 * @throws SAXException If any parse errors occur. 87 * @throws ParserConfigurationException in case of ServiceConfigurationError 88 * service configuration error or if the implementation is not available or 89 * cannot be instantiated. 90 * @throws IOException if any I/O operation error. 91 */ 92 @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider") 93 public void testParseValidate01(SAXParserFactory spf, MyErrorHandler handler) 94 throws ParserConfigurationException, SAXException, IOException { 95 spf.newSAXParser().parse(new File(XML_DIR, "parsertest.xml"), handler); 96 assertFalse(handler.isErrorOccured()); 97 } 98 99 /** 100 * validns.xml holds a valid document with XML namespaces in it. This method 101 * tests the Validating parser with namespace processing on. 102 * 103 * @param spf a Parser factory. 104 * @param handler an error handler for capturing events. 105 * @throws SAXException If any parse errors occur. 106 * @throws ParserConfigurationException in case of ServiceConfigurationError 107 * service configuration error or if the implementation is not available or 108 * cannot be instantiated. 109 * @throws IOException if any I/O operation error. 110 */ 111 @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider") 112 public void testParseValidate02(SAXParserFactory spf, MyErrorHandler handler) 113 throws ParserConfigurationException, SAXException, IOException { 114 spf.setNamespaceAware(true); 115 spf.newSAXParser().parse(new File(XML_DIR, "validns.xml"), handler); 116 assertFalse(handler.isErrorOccured()); 117 } 118 119 /** 120 * invalidns.xml holds an invalid document with XML namespaces in it. This 121 * method tests the validating parser with namespace processing on. It 122 * should throw validation error. 123 * 124 * @param spf a Parser factory. 125 * @param handler an error handler for capturing events. 126 */ 127 @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider") 128 public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler) { 129 try { 130 spf.setNamespaceAware(true); 131 SAXParser saxparser = spf.newSAXParser(); 132 saxparser.parse(new File(XML_DIR, "invalidns.xml"), handler); 133 failUnexpected(new RuntimeException()); 134 } catch (ParserConfigurationException | SAXException | IOException e) { 135 if (e instanceof SAXException) { 136 assertTrue(handler.isErrorOccured()); 137 } 138 } 139 } 140 141 } |