1 /*
   2  * Copyright (c) 2014, 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 
  24 package dom;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 
  29 import javax.xml.parsers.DocumentBuilder;
  30 import javax.xml.parsers.DocumentBuilderFactory;
  31 import javax.xml.parsers.ParserConfigurationException;
  32 
  33 import jaxp.library.JAXPTestUtilities;
  34 
  35 import org.testng.Assert;
  36 import org.testng.annotations.Listeners;
  37 import org.testng.annotations.Test;
  38 import org.w3c.dom.Document;
  39 import org.w3c.dom.NamedNodeMap;
  40 import org.w3c.dom.NodeList;
  41 import org.xml.sax.SAXException;
  42 
  43 /*
  44  * @bug 6582545
  45  * @summary Test the value is correct when iterating attributes.
  46  */
  47 @Listeners({jaxp.library.FilePolicy.class})
  48 public class Bug6582545Test {
  49     private DocumentBuilder xmlParser = null;
  50     private Document document = null;
  51     private String FWS1 = "FWS1";
  52     private String KEY_ARROW_UP = "KEY_ARROW_UP";
  53     private String VALUE_ARROW_UP = "root%LRM%Tmp_CPIOM-C1%VLIN_For_ECP%ECP_IN_Port_1%IOM-A7_Msg_cd30%FDS_1_ECP_to_FWS-1%A31_ECP_ARROW_UP";
  54 
  55     @Test
  56     public void testAttributeCaching() {
  57 
  58         File xmlFile = new File(getClass().getResource("Bug6582545.xml").getFile());
  59 
  60         try {
  61             DocumentBuilderFactory aDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
  62             xmlParser = aDocumentBuilderFactory.newDocumentBuilder();
  63 
  64             // works fine with JDK 1.4.2, 1.5
  65             // does not work with JDK 1.6
  66             document = xmlParser.parse(xmlFile);
  67             printNode(FWS1);
  68         } catch (SAXException saxException) {
  69             saxException.printStackTrace();
  70         } catch (ParserConfigurationException parserConfigurationException) {
  71             parserConfigurationException.printStackTrace();
  72         } catch (IOException ioException) {
  73             ioException.printStackTrace();
  74         } catch (IllegalArgumentException illegalArgumentException) {
  75             illegalArgumentException.printStackTrace();
  76         }
  77     }
  78 
  79     private void printNode(String aNode) {
  80         boolean error = true;
  81         NodeList nodeList;
  82         NamedNodeMap attributes;
  83 
  84         nodeList = document.getElementsByTagName(aNode);
  85         attributes = nodeList.item(0).getAttributes();
  86 
  87         String name;
  88         String value;
  89         // Print all nodes
  90         for (int k = 0; k < attributes.getLength(); k++) {
  91             name = attributes.item(k).getNodeName();
  92             value = attributes.item(k).getNodeValue();
  93             System.out.println(name + "=" + value);
  94         }
  95 
  96         // Test specifique a node
  97         String javaSpecificationVersion = System.getProperty("java.specification.version");
  98         for (int k = 0; k < attributes.getLength(); k++) {
  99             name = attributes.item(k).getNodeName();
 100             value = attributes.item(k).getNodeValue();
 101             if (KEY_ARROW_UP.equals(name)) {
 102                 if (VALUE_ARROW_UP.equals(value)) {
 103                     // Parser OK
 104                     System.out.println("Parser in Java " + javaSpecificationVersion + " returned correct value.");
 105                     error = false;
 106                 } else {
 107                     // Parser NOK
 108                     System.out.println("Parser in Java " + javaSpecificationVersion + " returned wrong value");
 109                 }
 110                 System.out.println("for node         = " + KEY_ARROW_UP);
 111                 System.out.println("expecting value  =" + VALUE_ARROW_UP);
 112                 System.out.println("value from parser=" + value);
 113             }
 114         }
 115 
 116         Assert.assertTrue(!error);
 117     }
 118 
 119 }