< prev index next >

test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/SAXParserTest02.java

Print this page




   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 
  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.assertNull;
  29 import static org.testng.Assert.assertTrue;
  30 
  31 import javax.xml.parsers.FactoryConfigurationError;
  32 import javax.xml.parsers.ParserConfigurationException;
  33 import javax.xml.parsers.SAXParser;
  34 import javax.xml.parsers.SAXParserFactory;
  35 
  36 import org.testng.annotations.DataProvider;
  37 import org.testng.annotations.Test;
  38 import org.xml.sax.Parser;
  39 import org.xml.sax.SAXException;
  40 import org.xml.sax.SAXNotRecognizedException;
  41 import org.xml.sax.SAXNotSupportedException;
  42 import org.xml.sax.XMLReader;
  43 import org.xml.sax.ext.DeclHandler;
  44 import org.xml.sax.ext.LexicalHandler;
  45 
  46 /**
  47  * Class contains the test cases for SAXParser API
  48  */
  49 public class SAXParserTest02 {
  50     final String DOM_NODE = "http://xml.org/sax/properties/dom-node";
  51     final String XML_STRING = "http://xml.org/sax/properties/xml-string";
  52     final String DECL_HANDLER = "http://xml.org/sax/properties/declaration-handler";
  53     final String LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler";
  54 
  55     /**
  56      * Provide SAXParser.
  57      *
  58      * @throws SAXException
  59      * @throws ParserConfigurationException



  60      */
  61     @DataProvider(name = "parser-provider")
  62     public Object[][] getParser() throws ParserConfigurationException, SAXException {
  63         SAXParserFactory spf = SAXParserFactory.newInstance();
  64         SAXParser saxparser = spf.newSAXParser();
  65         return new Object[][] { { saxparser } };
  66     }
  67 
  68     /**
  69      * Testcase to test the default functionality (No validation) of the parser.


  70      */
  71     @Test(dataProvider = "parser-provider")
  72     public void testValidate01(SAXParser saxparser) {
  73         try {
  74             assertFalse(saxparser.isValidating());
  75         } catch (FactoryConfigurationError e) {
  76             failUnexpected(e);
  77         }
  78 
  79     }
  80 
  81     /**
  82      * Testcase to test the functionality of setValidating and isvalidating
  83      * methods.





  84      */
  85     @Test
  86     public void testValidate02() {
  87         try {
  88             SAXParserFactory spf = SAXParserFactory.newInstance();
  89             spf.setValidating(true);
  90             spf.newSAXParser();
  91             assertTrue(spf.isValidating());
  92         } catch (FactoryConfigurationError | ParserConfigurationException | SAXException e) {
  93             failUnexpected(e);
  94         }
  95 
  96     }
  97 
  98     /**
  99      * Test case to test isNamespaceAware() method. By default, namespaces are
 100      * not supported.


 101      */
 102     @Test(dataProvider = "parser-provider")
 103     public void testNamespace01(SAXParser saxparser) {
 104         try {
 105             assertFalse(saxparser.isNamespaceAware());
 106         } catch (FactoryConfigurationError e) {
 107             failUnexpected(e);
 108         }
 109 
 110     }
 111 
 112     /**
 113      * Test case to test setnamespaceAware() method.





 114      */
 115     @Test
 116     public void testNamespace02() {
 117         try {
 118             SAXParserFactory spf = SAXParserFactory.newInstance();
 119             spf.setNamespaceAware(true);
 120             SAXParser saxparser = spf.newSAXParser();
 121             assertTrue(saxparser.isNamespaceAware());
 122         } catch (FactoryConfigurationError | ParserConfigurationException | SAXException e) {
 123             failUnexpected(e);
 124         }
 125 
 126     }
 127 
 128     /**
 129      * Test case to test if the getParser() method returns instance of Parser.



 130      */
 131     @Test(dataProvider = "parser-provider")
 132     public void testParser01(SAXParser saxparser) {
 133         try {
 134             Parser parser = saxparser.getParser();
 135         } catch (FactoryConfigurationError | SAXException e) {
 136             failUnexpected(e);
 137         }
 138 
 139     }
 140 
 141     /**
 142      * Test case to test if the getXMLReader() method returns instance of
 143      * XMLReader.



 144      */
 145     @Test(dataProvider = "parser-provider")
 146     public void testXmlReader01(SAXParser saxparser) {
 147         try {
 148             XMLReader xmlReader = saxparser.getXMLReader();
 149         } catch (FactoryConfigurationError | SAXException e) {
 150             failUnexpected(e);
 151         }
 152     }
 153 
 154     /**
 155      * Test whether the xml-string property is not supported.
 156      *
 157      * @throws SAXNotSupportedException

 158      */
 159     @Test(expectedExceptions = SAXNotSupportedException.class, dataProvider = "parser-provider")
 160     public void testProperty01(SAXParser saxparser) throws SAXNotSupportedException {
 161         try {
 162             Object object = saxparser.getProperty(XML_STRING);
 163         } catch (SAXNotRecognizedException e) {
 164             failUnexpected(e);
 165         }
 166     }
 167 
 168     /**
 169      * Test whether the dom-node property is not supported.
 170      *
 171      * @throws SAXNotSupportedException

 172      */
 173     @Test(expectedExceptions = SAXNotSupportedException.class, dataProvider = "parser-provider")
 174     public void testProperty02(SAXParser saxparser) throws SAXNotSupportedException {
 175         try {
 176             Object object = saxparser.getProperty(DOM_NODE);
 177         } catch (SAXNotRecognizedException e) {
 178             failUnexpected(e);
 179         }
 180     }
 181 
 182     /**
 183      * Test the default lexical-handler not exists.



 184      */
 185     @Test(dataProvider = "parser-provider")
 186     public void testProperty03(SAXParser saxparser) {
 187         try {
 188             assertNull(saxparser.getProperty(LEXICAL_HANDLER));
 189         } catch (SAXException e) {
 190             failUnexpected(e);
 191         }
 192 
 193     }
 194 
 195     /**
 196      * Test the default declaration-handler not exists.



 197      */
 198     @Test(dataProvider = "parser-provider")
 199     public void testProperty04(SAXParser saxparser) {
 200 
 201         try {
 202             assertNull(saxparser.getProperty(DECL_HANDLER));
 203         } catch (SAXException e) {
 204             failUnexpected(e);
 205         }
 206     }
 207 
 208     /**
 209      * Test to set and get the lexical-handler.



 210      */
 211     @Test(dataProvider = "parser-provider")
 212     public void testProperty05(SAXParser saxparser) {
 213         try {
 214             MyLexicalHandler myLexicalHandler = new MyLexicalHandler();
 215             saxparser.setProperty(LEXICAL_HANDLER, myLexicalHandler);
 216             Object object = saxparser.getProperty(LEXICAL_HANDLER);
 217             assertTrue(object instanceof LexicalHandler);
 218         } catch (SAXException e) {
 219             failUnexpected(e);
 220         }
 221     }
 222 
 223     /**
 224      * Test to set and get the declaration-handler.



 225      */
 226     @Test(dataProvider = "parser-provider")
 227     public void testProperty06(SAXParser saxparser) {
 228         try {
 229             MyDeclHandler myDeclHandler = new MyDeclHandler();
 230             saxparser.setProperty(DECL_HANDLER, myDeclHandler);
 231             Object object = saxparser.getProperty(DECL_HANDLER);
 232             assertTrue(object instanceof DeclHandler);
 233         } catch (SAXException e) {
 234             failUnexpected(e);
 235         }
 236 
 237     }
 238 
 239     /**
 240      * Customized LexicalHandler used for test.

 241      */
 242     private class MyLexicalHandler implements LexicalHandler {
 243 

 244         public void comment(char[] ch, int start, int length) {
 245         }
 246 

 247         public void endCDATA() {
 248         }
 249 

 250         public void endDTD() {
 251         }
 252 

 253         public void endEntity(String name) {
 254         }
 255 

 256         public void startCDATA() {
 257         }
 258 

 259         public void startDTD(String name, String publicId, String systemId) {
 260         }
 261 

 262         public void startEntity(String name) {
 263         }
 264     }
 265 
 266     /**
 267      * Customized DeclHandler used for test.

 268      */
 269     private class MyDeclHandler implements DeclHandler {
 270 

 271         public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) {
 272         }
 273 

 274         public void elementDecl(String name, String model) {
 275         }
 276 

 277         public void externalEntityDecl(String name, String publicId, String systemId) {
 278         }
 279 

 280         public void internalEntityDecl(String name, String value) {
 281         }
 282     }
 283 }


   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 
  24 package javax.xml.parsers.ptests;
  25 

  26 import static org.testng.Assert.assertFalse;
  27 import static org.testng.Assert.assertNull;
  28 import static org.testng.Assert.assertTrue;


  29 import javax.xml.parsers.ParserConfigurationException;
  30 import javax.xml.parsers.SAXParser;
  31 import javax.xml.parsers.SAXParserFactory;
  32 import jaxp.library.JAXPBaseTest;
  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;

  35 import org.xml.sax.SAXException;

  36 import org.xml.sax.SAXNotSupportedException;

  37 import org.xml.sax.ext.DeclHandler;
  38 import org.xml.sax.ext.LexicalHandler;
  39 
  40 /**
  41  * Class contains the test cases for SAXParser API
  42  */
  43 public class SAXParserTest02 extends JAXPBaseTest {
  44     private static final String DOM_NODE = "http://xml.org/sax/properties/dom-node";
  45     private static final String XML_STRING = "http://xml.org/sax/properties/xml-string";
  46     private static final String DECL_HANDLER = "http://xml.org/sax/properties/declaration-handler";
  47     private static final String LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler";
  48 
  49     /**
  50      * Provide SAXParser.
  51      * 
  52      * @return a data provider contains a SAXParser instance.
  53      * @throws SAXException If any parse errors occur.
  54      * @throws ParserConfigurationException in case of ServiceConfigurationError
  55      * service configuration error or if the implementation is not available or
  56      * cannot be instantiated.
  57      */
  58     @DataProvider(name = "parser-provider")
  59     public Object[][] getParser() throws ParserConfigurationException, SAXException {
  60         SAXParserFactory spf = SAXParserFactory.newInstance();
  61         SAXParser saxparser = spf.newSAXParser();
  62         return new Object[][] { { saxparser } };
  63     }
  64 
  65     /**
  66      * Test to test the default functionality (No validation) of the parser.
  67      * 
  68      * @param saxparser a SAXParser instance.
  69      */
  70     @Test(dataProvider = "parser-provider")
  71     public void testValidate01(SAXParser saxparser) {

  72         assertFalse(saxparser.isValidating());




  73     }
  74 
  75     /**
  76      * Test to test the functionality of setValidating and isValidating
  77      * methods.
  78      * 
  79      * @throws ParserConfigurationException in case of ServiceConfigurationError
  80      * service configuration error or if the implementation is not available or
  81      * cannot be instantiated.
  82      * @throws SAXException If any parse errors occur.
  83      */
  84     @Test
  85     public void testValidate02() throws ParserConfigurationException, SAXException {

  86         SAXParserFactory spf = SAXParserFactory.newInstance();
  87         spf.setValidating(true);
  88         spf.newSAXParser();
  89         assertTrue(spf.isValidating());




  90     }
  91 
  92     /**
  93      * Test isNamespaceAware() method. By default, namespaces are
  94      * not supported.
  95      * 
  96      * @param saxparser a SAXParser instance.
  97      */
  98     @Test(dataProvider = "parser-provider")
  99     public void testNamespace01(SAXParser saxparser) {

 100         assertFalse(saxparser.isNamespaceAware());




 101     }
 102 
 103     /**
 104      * Test case to test setnamespaceAware() method.
 105      *
 106      * @throws SAXException If any parse errors occur.
 107      * @throws ParserConfigurationException in case of ServiceConfigurationError
 108      * service configuration error or if the implementation is not available or
 109      * cannot be instantiated.
 110      */
 111     @Test
 112     public void testNamespace02() throws SAXException, ParserConfigurationException {

 113         SAXParserFactory spf = SAXParserFactory.newInstance();
 114         spf.setNamespaceAware(true);
 115         SAXParser saxparser = spf.newSAXParser();
 116         assertTrue(saxparser.isNamespaceAware());




 117     }
 118 
 119     /**
 120      * Test case to test if the getParser() method returns instance of Parser.
 121      * 
 122      * @param saxparser a SAXParser instance.
 123      * @throws SAXException If any parse errors occur.
 124      */
 125     @Test(dataProvider = "parser-provider")
 126     public void testParser01(SAXParser saxparser) throws SAXException {
 127         saxparser.getParser();





 128     }
 129 
 130     /**
 131      * Test case to test if the getXMLReader() method returns instance of
 132      * XMLReader.
 133      * 
 134      * @param saxparser a SAXParser instance.
 135      * @throws SAXException If any parse errors occur.
 136      */
 137     @Test(dataProvider = "parser-provider")
 138     public void testXmlReader01(SAXParser saxparser) throws SAXException {
 139         saxparser.getXMLReader();




 140     }
 141 
 142     /**
 143      * Test whether the xml-string property is not supported.
 144      *
 145      * @param saxparser a SAXParser instance.
 146      * @throws SAXException If any parse errors occur.
 147      */
 148     @Test(expectedExceptions = SAXNotSupportedException.class, 
 149             dataProvider = "parser-provider")
 150     public void testProperty01(SAXParser saxparser) throws SAXException {
 151         saxparser.getProperty(XML_STRING);



 152     }
 153 
 154     /**
 155      * Test whether the dom-node property is not supported.
 156      *
 157      * @param saxparser a SAXParser instance.
 158      * @throws SAXException If any parse errors occur.
 159      */
 160     @Test(expectedExceptions = SAXNotSupportedException.class,
 161             dataProvider = "parser-provider")
 162     public void testProperty02(SAXParser saxparser) throws SAXException {
 163         saxparser.getProperty(DOM_NODE);



 164     }
 165 
 166     /**
 167      * Test the default lexical-handler not exists.
 168      *
 169      * @param saxparser a SAXParser instance.
 170      * @throws SAXException If any parse errors occur.
 171      */
 172     @Test(dataProvider = "parser-provider")
 173     public void testProperty03(SAXParser saxparser) throws SAXException {

 174         assertNull(saxparser.getProperty(LEXICAL_HANDLER));




 175     }
 176 
 177     /**
 178      * Test the default declaration-handler not exists.
 179      *
 180      * @param saxparser a SAXParser instance.
 181      * @throws SAXException If any parse errors occur.
 182      */
 183     @Test(dataProvider = "parser-provider")
 184     public void testProperty04(SAXParser saxparser) throws SAXException {


 185         assertNull(saxparser.getProperty(DECL_HANDLER));



 186     }
 187 
 188     /**
 189      * Test to set and get the lexical-handler.
 190      *
 191      * @param saxparser a SAXParser instance.
 192      * @throws SAXException If any parse errors occur.
 193      */
 194     @Test(dataProvider = "parser-provider")
 195     public void testProperty05(SAXParser saxparser) throws SAXException {

 196         MyLexicalHandler myLexicalHandler = new MyLexicalHandler();
 197         saxparser.setProperty(LEXICAL_HANDLER, myLexicalHandler);
 198         assertTrue(saxparser.getProperty(LEXICAL_HANDLER) instanceof LexicalHandler);




 199     }
 200 
 201     /**
 202      * Test to set and get the declaration-handler.
 203      *
 204      * @param saxparser a SAXParser instance.
 205      * @throws SAXException If any parse errors occur.
 206      */
 207     @Test(dataProvider = "parser-provider")
 208     public void testProperty06(SAXParser saxparser) throws SAXException {

 209         MyDeclHandler myDeclHandler = new MyDeclHandler();
 210         saxparser.setProperty(DECL_HANDLER, myDeclHandler);
 211         assertTrue(saxparser.getProperty(DECL_HANDLER) instanceof DeclHandler);





 212     }
 213 
 214     /**
 215      * Customized LexicalHandler used for test. An empty implementation for 
 216      * LexicalHandler.
 217      */
 218     private class MyLexicalHandler implements LexicalHandler {
 219 
 220         @Override
 221         public void comment(char[] ch, int start, int length) {
 222         }
 223 
 224         @Override
 225         public void endCDATA() {
 226         }
 227 
 228         @Override
 229         public void endDTD() {
 230         }
 231 
 232         @Override
 233         public void endEntity(String name) {
 234         }
 235 
 236         @Override
 237         public void startCDATA() {
 238         }
 239 
 240         @Override
 241         public void startDTD(String name, String publicId, String systemId) {
 242         }
 243 
 244         @Override
 245         public void startEntity(String name) {
 246         }
 247     }
 248 
 249     /**
 250      * Customized DeclHandler used for test. An empty implementation for 
 251      * DeclHandler. 
 252      */
 253     private class MyDeclHandler implements DeclHandler {
 254 
 255         @Override
 256         public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) {
 257         }
 258 
 259         @Override
 260         public void elementDecl(String name, String model) {
 261         }
 262 
 263         @Override
 264         public void externalEntityDecl(String name, String publicId, String systemId) {
 265         }
 266 
 267         @Override
 268         public void internalEntityDecl(String name, String value) {
 269         }
 270     }
 271 }
< prev index next >