2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.codec.xml;
10 import static org.junit.Assert.assertFalse;
12 import java.util.Collection;
13 import javax.xml.stream.XMLOutputFactory;
14 import javax.xml.transform.dom.DOMResult;
15 import javax.xml.transform.dom.DOMSource;
16 import org.junit.AfterClass;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.junit.runners.Parameterized;
21 import org.opendaylight.yangtools.util.xml.UntrustedXML;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26 import org.xmlunit.builder.DiffBuilder;
28 @RunWith(Parameterized.class)
29 public class Bug8745Test extends AbstractXmlTest {
30 @Parameterized.Parameters(name = "{0}")
31 public static Collection<Object[]> data() {
32 return TestFactories.junitParameters();
35 private static EffectiveModelContext SCHEMA_CONTEXT;
37 private final XMLOutputFactory factory;
39 public Bug8745Test(final String factoryMode, final XMLOutputFactory factory) {
40 this.factory = factory;
44 public static void beforeClass() {
45 SCHEMA_CONTEXT = YangParserTestUtils.parseYang("""
50 container cont-with-attributes {
51 leaf leaf-with-attributes {
54 leaf-list leaf-list-with-attributes {
57 list list-with-attributes {
68 public static void afterClass() {
69 SCHEMA_CONTEXT = null;
73 public void testParsingAttributes() throws Exception {
74 final var doc = loadDocument("/bug8745/foo.xml");
75 final var domSource = new DOMSource(doc.getDocumentElement());
76 final var domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());
77 final var xmlStreamWriter = factory.createXMLStreamWriter(domResult);
78 final var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, SCHEMA_CONTEXT);
80 final var reader = new DOMSourceXMLStreamReader(domSource);
81 final var xmlParser = XmlParserStream.create(streamWriter,
82 Inference.ofDataTreePath(SCHEMA_CONTEXT, QName.create("foo", "cont-with-attributes")));
83 xmlParser.parse(reader);
85 final var diff = DiffBuilder.compare(toString(doc.getDocumentElement()))
86 .withTest(toString(domResult.getNode()))
90 assertFalse(diff.toString(), diff.hasDifferences());