From 383a1f354845249d555b9eb0022ffeb07460085c Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Fri, 30 Aug 2024 14:22:45 +0300 Subject: [PATCH] More tests --- .../utils/deserializer/NodeDescriptor.java | 5 +- .../core/utils/JsonDeserializerTest.java | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cqfn/astranaut/core/utils/deserializer/NodeDescriptor.java b/src/main/java/org/cqfn/astranaut/core/utils/deserializer/NodeDescriptor.java index 07c829e1..d2996d8c 100644 --- a/src/main/java/org/cqfn/astranaut/core/utils/deserializer/NodeDescriptor.java +++ b/src/main/java/org/cqfn/astranaut/core/utils/deserializer/NodeDescriptor.java @@ -100,12 +100,11 @@ public Node convert(final Factory factory, final ActionList actions, */ private Node convertHole(final Factory factory, final ActionList actions, final Map holes) { - assert this.number != null; Node original = DummyNode.INSTANCE; - if (this.prototype != null) { + if (this.prototype != null && this.number != null) { original = this.prototype.convert(factory, actions, holes); + holes.put(original, this.number); } - holes.put(original, this.number); return original; } diff --git a/src/test/java/org/cqfn/astranaut/core/utils/JsonDeserializerTest.java b/src/test/java/org/cqfn/astranaut/core/utils/JsonDeserializerTest.java index ab0305c1..55f3f255 100644 --- a/src/test/java/org/cqfn/astranaut/core/utils/JsonDeserializerTest.java +++ b/src/test/java/org/cqfn/astranaut/core/utils/JsonDeserializerTest.java @@ -23,10 +23,15 @@ */ package org.cqfn.astranaut.core.utils; +import java.util.List; import java.util.Map; import java.util.TreeMap; +import org.cqfn.astranaut.core.base.Builder; import org.cqfn.astranaut.core.base.DefaultFactory; +import org.cqfn.astranaut.core.base.DummyNode; import org.cqfn.astranaut.core.base.EmptyTree; +import org.cqfn.astranaut.core.base.Factory; +import org.cqfn.astranaut.core.base.Fragment; import org.cqfn.astranaut.core.base.Node; import org.cqfn.astranaut.core.base.Tree; import org.cqfn.astranaut.core.base.Type; @@ -41,6 +46,7 @@ * Test for {@link JsonDeserializer} class. * @since 1.0.2 */ +@SuppressWarnings("PMD.TooManyMethods") class JsonDeserializerTest { /** * The "Addition" type. @@ -152,6 +158,66 @@ void deserializeWithNullFactory() { Assertions.assertSame(EmptyTree.INSTANCE, result); } + @Test + void deserializeWithFactoryThatProducesNullBuilder() { + final JsonDeserializer deserializer = new JsonDeserializer( + JsonDeserializerTest.SMALL_TREE, + language -> new Factory() { + @Override + public Type getType(final String name) { + return null; + } + + @Override + public Builder createBuilder(final String name) { + return null; + } + } + ); + final Tree result = deserializer.convert(); + Assertions.assertSame(DummyNode.INSTANCE, result.getRoot()); + } + + @Test + void deserializeWithFactoryThatProducesInvalidBuilder() { + final JsonDeserializer deserializer = new JsonDeserializer( + JsonDeserializerTest.SMALL_TREE, + language -> new Factory() { + @Override + public Type getType(final String name) { + return null; + } + + @Override + public Builder createBuilder(final String name) { + return new InvalidBuilder(); + } + } + ); + final Tree result = deserializer.convert(); + Assertions.assertSame(DummyNode.INSTANCE, result.getRoot()); + } + + @Test + void deserializeInvalidHoleWithoutNumber() { + final JsonDeserializer deserializer = new JsonDeserializer( + "{ \"root\": { \"type\": \"Hole\", \"prototype\": { \"type\": \"Node\" } } }", + language -> DefaultFactory.EMPTY + ); + final Tree result = deserializer.convert(); + Assertions.assertSame(DummyNode.INSTANCE, result.getRoot()); + } + + @Test + void deserializeInvalidHoleWithoutPrototype() { + final JsonDeserializer deserializer = new JsonDeserializer( + "{ \"root\": { \"type\": \"Hole\", \"number\": 19 } } }", + language -> DefaultFactory.EMPTY + ); + final Tree result = deserializer.convert(); + Assertions.assertSame(DummyNode.INSTANCE, result.getRoot()); + } + /** * Returns content of the specified file. * @param name The name of the file @@ -163,4 +229,35 @@ private String getFileContent(final String name) { Assertions.assertFalse(source.isEmpty()); return source; } + + /** + * Invalid builder for test purposes. + * @since 2.0.0 + */ + private static final class InvalidBuilder implements Builder { + @Override + public void setFragment(final Fragment fragment) { + this.getClass(); + } + + @Override + public boolean setData(final String str) { + return true; + } + + @Override + public boolean setChildrenList(final List list) { + return true; + } + + @Override + public boolean isValid() { + return false; + } + + @Override + public Node createNode() { + throw new UnsupportedOperationException(); + } + } }