< prev index next >

test/javax/xml/jaxp/functional/org/xml/sax/ptests/AttributesNSTest.java

Print this page




   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.File;
  26 import java.io.IOException;
  27 import java.nio.file.Files;
  28 import java.nio.file.Path;
  29 import java.nio.file.Paths;
  30 import javax.xml.parsers.ParserConfigurationException;
  31 import javax.xml.parsers.SAXParser;
  32 import javax.xml.parsers.SAXParserFactory;

  33 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  34 import static jaxp.library.JAXPTestUtilities.failCleanup;
  35 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  36 import static org.testng.Assert.assertTrue;
  37 import org.testng.annotations.Test;
  38 import org.xml.sax.SAXException;
  39 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
  40 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  41 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  42 
  43 /**
  44  * This tests the Attributes interface. Here the startElement() callback of
  45  * ContentHandler has Attributes as one of its arguments. Attributes
  46  * pertaining to an element are taken into this argument and various methods
  47  * of Attributes interfaces are tested. This program uses Namespace processing
  48  * with namespaces in xml file. This program does not use Validation
  49  */
  50 public class AttributesNSTest {
  51     /**
  52      * Test for Attribute Interface's setter/getter.







  53      */
  54     @Test
  55     public void testcase01() {
  56         String outputFile = CLASS_DIR + "AttributesNS.out";
  57         String goldFile = GOLDEN_DIR + "AttributesNSGF.out";
  58         String xmlFile = XML_DIR + "namespace1.xml";
  59         try {
  60             SAXParserFactory spf = SAXParserFactory.newInstance();
  61             spf.setNamespaceAware(true);
  62             // http://www.saxproject.com/?selected=namespaces namespace-prefixes
  63             //set to false to supress xmlns attributes
  64             spf.setFeature("http://xml.org/sax/features/namespace-prefixes",
  65                                         false);
  66             SAXParser saxParser = spf.newSAXParser();
  67             MyAttrCHandler myAttrCHandler = new MyAttrCHandler(outputFile);
  68             saxParser.parse(new File(xmlFile), myAttrCHandler);
  69             myAttrCHandler.flushAndClose();
  70             assertTrue(compareWithGold(goldFile, outputFile));
  71         } catch (IOException | ParserConfigurationException | SAXException ex) {
  72             failUnexpected(ex);
  73         } finally {
  74             try {
  75                 Path outputPath = Paths.get(outputFile);
  76                 if(Files.exists(outputPath))
  77                     Files.delete(outputPath);
  78             } catch (IOException ex) {
  79                 failCleanup(ex, outputFile);
  80             }
  81         }
  82     }
  83 }


   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.File;
  26 import java.io.IOException;



  27 import javax.xml.parsers.ParserConfigurationException;
  28 import javax.xml.parsers.SAXParser;
  29 import javax.xml.parsers.SAXParserFactory;
  30 import jaxp.library.JAXPFileBaseTest;
  31 import static jaxp.library.JAXPTestUtilities.compareWithGold;


  32 import static org.testng.Assert.assertTrue;
  33 import org.testng.annotations.Test;
  34 import org.xml.sax.SAXException;
  35 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
  36 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  37 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  38 
  39 /**
  40  * This tests the Attributes interface. Here the startElement() callback of
  41  * ContentHandler has Attributes as one of its arguments. Attributes
  42  * pertaining to an element are taken into this argument and various methods
  43  * of Attributes interfaces are tested. This program uses Namespace processing
  44  * with namespaces in XML file. This program does not use Validation
  45  */
  46 public class AttributesNSTest extends JAXPFileBaseTest {
  47     /**
  48      * Test for Attribute Interface's setter/getter.
  49      * 
  50      * @throws SAXException If any parse errors occur.
  51      * @throws IOException if the file exists but is a directory rather than
  52      *         a regular file, does not exist but cannot be created, or cannot 
  53      *         be opened for any other reason.
  54      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  55      *         created which satisfies the configuration requested.
  56      */
  57     @Test
  58     public void testcase01() throws ParserConfigurationException, SAXException, IOException {
  59         String outputFile = CLASS_DIR + "AttributesNS.out";
  60         String goldFile = GOLDEN_DIR + "AttributesNSGF.out";
  61         String xmlFile = XML_DIR + "namespace1.xml";

  62         SAXParserFactory spf = SAXParserFactory.newInstance();
  63         spf.setNamespaceAware(true);
  64         // http://www.saxproject.com/?selected=namespaces namespace-prefixes
  65         //set to false to supress xmlns attributes
  66         spf.setFeature("http://xml.org/sax/features/namespace-prefixes",
  67                                     false);
  68         SAXParser saxParser = spf.newSAXParser();
  69         MyAttrCHandler myAttrCHandler = new MyAttrCHandler(outputFile);
  70         saxParser.parse(new File(xmlFile), myAttrCHandler);
  71         myAttrCHandler.flushAndClose();
  72         assertTrue(compareWithGold(goldFile, outputFile));











  73     }
  74 }
< prev index next >