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