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