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