import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import com.google.common.base.Optional;
-import com.google.common.collect.Maps;
import java.io.ByteArrayOutputStream;
-import java.io.FileNotFoundException;
+import java.io.IOException;
import java.net.URI;
-import java.net.URISyntaxException;
import java.util.AbstractMap;
-import java.util.Date;
+import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLOutputFactory;
+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.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
import org.w3c.dom.Document;
public class XmlStreamUtilsTest {
+ @FunctionalInterface
+ interface XMLStreamWriterConsumer {
+ void accept(XMLStreamWriter writer) throws XMLStreamException;
+ }
public static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
private static Module leafRefModule;
@BeforeClass
- public static void initialize() throws URISyntaxException, FileNotFoundException, ReactorException {
- schemaContext = YangParserTestUtils.parseYangSource("/leafref-test.yang");
+ public static void initialize() {
+ schemaContext = YangParserTestUtils.parseYangResource("/leafref-test.yang");
assertNotNull(schemaContext);
assertEquals(1, schemaContext.getModules().size());
leafRefModule = schemaContext.getModules().iterator().next();
assertNotNull(leafRefModule);
}
+ @AfterClass
+ public static void cleanup() {
+ leafRefModule = null;
+ schemaContext = null;
+ }
+
@Test
public void testWriteAttribute() throws Exception {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
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.absent());
+ 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();
- XmlStreamUtils.writeAttribute(writer, attributeEntry, randomPrefix);
- XmlStreamUtils.writeAttribute(writer, attributeEntryNoPrefix, randomPrefix);
+ final RandomPrefix randomPrefix = new RandomPrefix(null);
+ XMLStreamWriterUtils.writeAttribute(writer, attributeEntry, randomPrefix);
+ XMLStreamWriterUtils.writeAttribute(writer, attributeEntryNoPrefix, randomPrefix);
writer.writeEndElement();
writer.close();
@Test
public void testWriteIdentityRef() throws Exception {
- final ByteArrayOutputStream out = new ByteArrayOutputStream();
- final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
+ final QNameModule parent = QNameModule.create(URI.create("parent:uri"), Revision.of("2000-01-01"));
- writer.writeStartElement("element");
- final QNameModule parent = QNameModule.create(URI.create("parent:uri"), new Date());
- XmlStreamUtils.write(writer, null, QName.create(parent, "identity"), Optional.of(parent));
- writer.writeEndElement();
+ String xmlAsString = createXml(writer -> {
+ writer.writeStartElement("element");
+ XMLStreamWriterUtils.write(writer, null, QName.create(parent, "identity"), parent);
+ writer.writeEndElement();
+ });
- writer.writeStartElement("elementDifferent");
- XmlStreamUtils.write(writer, null, QName.create("different:namespace", "identity"), Optional.of(parent));
- writer.writeEndElement();
+ assertThat(xmlAsString, containsString("element>identity"));
- writer.close();
- out.close();
+ xmlAsString = createXml(writer -> {
+ writer.writeStartElement("elementDifferent");
+ XMLStreamWriterUtils.write(writer, null, QName.create("different:namespace", "identity"), parent);
+ writer.writeEndElement();
- final String xmlAsString = new String(out.toByteArray()).replaceAll("\\s*", "");
- assertThat(xmlAsString, containsString("element>identity"));
+ });
final Pattern prefixedIdentityPattern = Pattern.compile(".*\"different:namespace\">(.*):identity.*");
final Matcher matcher = prefixedIdentityPattern.matcher(xmlAsString);
assertTrue("Xml: " + xmlAsString + " should match: " + prefixedIdentityPattern, matcher.matches());
}
+ private static String createXml(XMLStreamWriterConsumer consumer) throws XMLStreamException, IOException {
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
+
+ consumer.accept(writer);
+
+ writer.close();
+ out.close();
+
+ return new String(out.toByteArray()).replaceAll("\\s*", "");
+ }
+
/**
- * One leafref reference to other leafref via relative references
+ * One leafref reference to other leafref via relative references.
*/
@Test
public void testLeafRefRelativeChaining() {
}
private static Map<String, String> mapPrefixed(final Iterable<Map.Entry<URI, String>> prefixes) {
- final Map<String, String> mappedPrefixes = Maps.newHashMap();
+ final Map<String, String> mappedPrefixes = new HashMap<>();
for (final Map.Entry<URI, String> prefix : prefixes) {
mappedPrefixes.put(prefix.getKey().toString(), prefix.getValue());
}
final QName moduleQName = QName.create(namespace, revision, "module");
final QNameModule module = QNameModule.create(moduleQName.getNamespace(), moduleQName.getRevision());
return QName.create(module, localName);
- } else {
- return QName.create(namespace, revision, localName);
}
+ return QName.create(namespace, revision, localName);
}
private LeafSchemaNode findSchemaNodeWithLeafrefType(final DataNodeContainer module, final String nodeName) {