+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
+
+ nnout.writeNormalizedNode(anyXmlContainer);
+
+ final byte[] bytes = bos.toByteArray();
+ assertEquals(229, bytes.length);
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
+
+ ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
+
+ Optional<DataContainerChild<? extends PathArgument, ?>> child =
+ deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
+ assertEquals("AnyXml child present", true, child.isPresent());
+
+ StreamResult xmlOutput = new StreamResult(new StringWriter());
+ Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ transformer.transform(((AnyXmlNode)child.get()).getValue(), xmlOutput);
+
+ assertEquals("XML", xml, xmlOutput.getWriter().toString());
+ assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
+ }
+
+ @Test
+ public void testSchemaPathSerialization() throws IOException {
+ final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
+ nnout.writeSchemaPath(expected);
+ }
+
+ final byte[] bytes = bos.toByteArray();
+ assertEquals(99, bytes.length);
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
+ assertEquals(expected, nnin.readSchemaPath());
+ }
+
+ @Test
+ public void testWritePathArgument() throws IOException {
+ final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+ try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
+ nnout.writePathArgument(expected);
+ }
+
+ final byte[] bytes = bos.toByteArray();
+ assertEquals(103, bytes.length);
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
+ assertEquals(expected, nnin.readPathArgument());
+ }
+
+ /*
+ * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
+ * and an integer.
+ */
+ @Test
+ public void testHugeEntries() throws IOException {
+ final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders.mapBuilder()
+ .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME));
+ final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> entryBuilder =
+ Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42));
+
+ for (int i = 0; i < 100_000; ++i) {
+ final String key = "xyzzy" + i;
+ mapBuilder.addChild(entryBuilder
+ .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME,
+ TestModel.CHILD_NAME_QNAME, key))
+ .withChild(ImmutableNodes.leafNode(TestModel.CHILD_NAME_QNAME, key))
+ .build());
+ }
+
+ final MapNode expected = mapBuilder.build();
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+ try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
+ nnout.writeNormalizedNode(expected);
+ }
+
+ final byte[] bytes = bos.toByteArray();
+ assertEquals(2_289_103, bytes.length);
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
+ assertEquals(expected, nnin.readNormalizedNode());
+ }
+
+ private static String largeString(final int pow) {
+ StringBuilder sb = new StringBuilder("X");
+ for (int i = 0; i < pow; i++) {
+ sb.append(sb);
+ }
+ return sb.toString();