1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 static jaxp.library.JAXPTestUtilities.USER_DIR;
  26 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  27 import static org.testng.Assert.assertTrue;
  28 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  29 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  30 
  31 import java.io.File;
  32 
  33 import javax.xml.parsers.SAXParser;
  34 import javax.xml.parsers.SAXParserFactory;
  35 
  36 import org.testng.annotations.Listeners;
  37 import org.testng.annotations.Test;
  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.
  44  * This program uses Namespace processing without any namepsaces in xml file.
  45  * This program uses Validation
  46  */
  47 @Listeners({jaxp.library.FilePolicy.class})
  48 public class AttributesTest {
  49     /**
  50      * Unit test for Attributes interface. Prints all attributes into output
  51      * file. Check it with golden file.
  52      *
  53      * @throws Exception If any errors occur.
  54      */
  55     @Test
  56     public void testcase01() throws Exception {
  57         String outputFile = USER_DIR + "Attributes.out";
  58         String goldFile = GOLDEN_DIR + "AttributesGF.out";
  59         String xmlFile = XML_DIR + "family.xml";
  60 
  61         SAXParserFactory spf = SAXParserFactory.newInstance();
  62         spf.setNamespaceAware(true);
  63         spf.setFeature("http://xml.org/sax/features/namespace-prefixes",
  64                                     true);
  65         spf.setValidating(true);
  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     }
  72 }