import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.annotations.VisibleForTesting;
-import java.util.Map.Entry;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.eclipse.jdt.annotation.NonNull;
abstract class XMLStreamWriterUtils {
private static final Logger LOG = LoggerFactory.getLogger(XMLStreamWriterUtils.class);
- @VisibleForTesting
- static void writeAttribute(final XMLStreamWriter writer, final Entry<QName, String> attribute,
- final RandomPrefix randomPrefix) throws XMLStreamException {
- final QName key = attribute.getKey();
- final String prefix = randomPrefix.encodePrefix(key.getNamespace());
- writer.writeAttribute("xmlns:" + prefix, key.getNamespace().toString());
- writer.writeAttribute(prefix, key.getNamespace().toString(), key.getLocalName(), attribute.getValue());
- }
-
/**
* Write a value into a XML stream writer. This method assumes the start and end of element is
* emitted by the caller.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
-import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
-import org.custommonkey.xmlunit.Diff;
-import org.custommonkey.xmlunit.XMLUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-import org.w3c.dom.Document;
public class XmlStreamUtilsTest {
@FunctionalInterface
schemaContext = null;
}
- @Test
- public void testWriteAttribute() throws Exception {
- final ByteArrayOutputStream out = new ByteArrayOutputStream();
- final XMLStreamWriter writer = TestFactories.DEFAULT_OUTPUT_FACTORY.createXMLStreamWriter(out);
- writer.writeStartElement("element");
-
- QName name = getAttrQName("namespace", "2012-12-12", "attr", Optional.of("prefix"));
- final Map.Entry<QName, String> attributeEntry = new AbstractMap.SimpleEntry<>(name, "value");
-
- name = getAttrQName("namespace2", "2012-12-12", "attr", Optional.empty());
- final Map.Entry<QName, String> attributeEntryNoPrefix = new AbstractMap.SimpleEntry<>(name, "value");
-
- final RandomPrefix randomPrefix = new RandomPrefix(null);
- XMLStreamWriterUtils.writeAttribute(writer, attributeEntry, randomPrefix);
- XMLStreamWriterUtils.writeAttribute(writer, attributeEntryNoPrefix, randomPrefix);
-
- writer.writeEndElement();
- writer.close();
- out.close();
-
- final String xmlAsString = new String(out.toByteArray());
-
- final Map<String, String> mappedPrefixes = mapPrefixed(randomPrefix.getPrefixes());
- assertEquals(2, mappedPrefixes.size());
- final String randomPrefixValue = mappedPrefixes.get("namespace2");
-
- final String expectedXmlAsString = "<element xmlns:a=\"namespace\" a:attr=\"value\" xmlns:" + randomPrefixValue
- + "=\"namespace2\" " + randomPrefixValue + ":attr=\"value\"></element>";
-
- XMLUnit.setIgnoreAttributeOrder(true);
- final Document control = XMLUnit.buildControlDocument(expectedXmlAsString);
- final Document test = XMLUnit.buildTestDocument(xmlAsString);
- final Diff diff = XMLUnit.compareXML(control, test);
-
- final boolean identical = diff.identical();
- assertTrue("Xml differs: " + diff.toString(), identical);
- }
-
@Test
public void testWriteIdentityRef() throws Exception {
final QNameModule parent = QNameModule.create(URI.create("parent:uri"), Revision.of("2000-01-01"));
assertTrue("Xml: " + xmlAsString + " should match: " + prefixedIdentityPattern, matcher.matches());
}
- private static String createXml(XMLStreamWriterConsumer consumer) throws XMLStreamException, IOException {
+ private static String createXml(final XMLStreamWriterConsumer consumer) throws XMLStreamException, IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final XMLStreamWriter writer = TestFactories.DEFAULT_OUTPUT_FACTORY.createXMLStreamWriter(out);