diff --git a/asdf-core/src/main/java/org/asdfformat/asdf/node/AsdfNode.java b/asdf-core/src/main/java/org/asdfformat/asdf/node/AsdfNode.java index 683b851..eea3ee4 100644 --- a/asdf-core/src/main/java/org/asdfformat/asdf/node/AsdfNode.java +++ b/asdf-core/src/main/java/org/asdfformat/asdf/node/AsdfNode.java @@ -105,6 +105,13 @@ public interface AsdfNode extends Iterable { */ boolean containsKey(AsdfNode key); + /** + * Does the chain of MAPPING nodes contain the specified keys? + * @param keys mapping key for each successive node + * @return true if all keys are present + */ + boolean containsKey(Object... keys); + /** * Get the size of this MAPPING or SEQUENCE node. * @return size @@ -139,6 +146,13 @@ public interface AsdfNode extends Iterable { */ AsdfNode get(AsdfNode key); + /** + * Get a sequence value or mapping value as AsdfNode, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + AsdfNode get(Object... keys); + /** * Get an optional mapping value as AsdfNode, indexed by String key. * @param key mapping key @@ -167,6 +181,13 @@ public interface AsdfNode extends Iterable { */ Optional getOptional(AsdfNode key); + /** + * Get an optional sequence value or mapping value as AsdfNode, indexed by a chain of keys. + * @param keys sequences indexes and mapping keys + * @return value + */ + Optional getOptional(Object... keys); + /** * Get a NUMBER mapping value as BigDecimal, indexed by String key. * @param key mapping key @@ -195,6 +216,13 @@ public interface AsdfNode extends Iterable { */ BigDecimal getBigDecimal(AsdfNode key); + /** + * Get a NUMBER sequence value or mapping value as BigDecimal, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + BigDecimal getBigDecimal(Object... keys); + /** * Get a NUMBER mapping value as BigInteger, indexed by String key. * @param key mapping key @@ -223,6 +251,13 @@ public interface AsdfNode extends Iterable { */ BigInteger getBigInteger(AsdfNode key); + /** + * Get a NUMBER sequence value or mapping value as BigInteger, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + BigInteger getBigInteger(Object... keys); + /** * Get a BOOLEAN mapping value as boolean, indexed by String key. * @param key mapping key @@ -251,6 +286,13 @@ public interface AsdfNode extends Iterable { */ boolean getBoolean(AsdfNode key); + /** + * Get a BOOLEAN sequence value or mapping value as boolean, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + boolean getBoolean(Object... keys); + /** * Get a NUMBER mapping value as byte, indexed by String key. * @param key mapping key @@ -279,6 +321,13 @@ public interface AsdfNode extends Iterable { */ byte getByte(AsdfNode key); + /** + * Get a NUMBER sequence value or mapping value as byte, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + byte getByte(Object... keys); + /** * Get a NUMBER mapping value as double, indexed by String key. * @param key mapping key @@ -307,6 +356,13 @@ public interface AsdfNode extends Iterable { */ double getDouble(AsdfNode key); + /** + * Get a NUMBER sequence value or mapping value as double, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + double getDouble(Object... keys); + /** * Get a NUMBER mapping value as float, indexed by String key. * @param key mapping key @@ -335,6 +391,13 @@ public interface AsdfNode extends Iterable { */ float getFloat(AsdfNode key); + /** + * Get a NUMBER sequence value or mapping value as float, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + float getFloat(Object... keys); + /** * Get a TIMESTAMP mapping value as Instant, indexed by String key. * @param key mapping key @@ -363,6 +426,13 @@ public interface AsdfNode extends Iterable { */ Instant getInstant(AsdfNode key); + /** + * Get a TIMESTAMP sequence value or mapping value as Instant, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + Instant getInstant(Object... keys); + /** * Get a NUMBER mapping value as int, indexed by String key. * @param key mapping key @@ -391,13 +461,23 @@ public interface AsdfNode extends Iterable { */ int getInt(AsdfNode key); + /** + * Get a NUMBER sequence value or mapping value as int, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + int getInt(Object... keys); + /** * Get a SEQUENCE mapping value as List, indexed by String key. * @param key mapping key * @param elementClass element class * @return value * @param List element + * + * @deprecated in favor of {@link #getList(Class, String)} */ + @Deprecated List getList(String key, Class elementClass); /** @@ -406,7 +486,10 @@ public interface AsdfNode extends Iterable { * @param elementClass element class * @return value * @param List element + * + * @deprecated in favor of {@link #getList(Class, long)} */ + @Deprecated List getList(long key, Class elementClass); /** @@ -415,7 +498,10 @@ public interface AsdfNode extends Iterable { * @param elementClass element class * @return value * @param List element + * + * @deprecated in favor of {@link #getList(Class, boolean)} */ + @Deprecated List getList(boolean key, Class elementClass); /** @@ -424,9 +510,57 @@ public interface AsdfNode extends Iterable { * @param elementClass element class * @return value * @param List element + * + * @deprecated in favor of {@link #getList(Class, AsdfNode)} */ + @Deprecated List getList(AsdfNode key, Class elementClass); + /** + * Get a SEQUENCE mapping value as List, indexed by String key. + * @param elementClass element class + * @param key mapping key + * @return value + * @param List element + */ + List getList(Class elementClass, String key); + + /** + * Get a SEQUENCE sequence value or mapping value as List, indexed by long key. + * @param key sequence index or mapping key + * @param elementClass element class + * @return value + * @param List element + */ + List getList(Class elementClass, long key); + + /** + * Get a SEQUENCE mapping value as List, indexed by boolean key. + * @param key mapping key + * @param elementClass element class + * @return value + * @param List element + */ + List getList(Class elementClass, boolean key); + + /** + * Get a SEQUENCE sequence value or mapping value as List, indexed by AsdfNode key. + * @param key sequence index or mapping key + * @param elementClass element class + * @return value + * @param List element + */ + List getList(Class elementClass, AsdfNode key); + + /** + * Get a SEQUENCE sequence value or mapping value as List, indexed by a chain of keys. + * @param elementClass element class + * @param keys sequence indexes and mapping keys + * @return value + * @param List element + */ + List getList(Class elementClass, Object... keys); + /** * Get a NUMBER mapping value as long, indexed by String key. * @param key mapping key @@ -455,6 +589,13 @@ public interface AsdfNode extends Iterable { */ long getLong(AsdfNode key); + /** + * Get a NUMBER sequence value or mapping value as long, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + long getLong(Object... keys); + /** * Get a NUMBER mapping value as a Number, indexed by String key. * @param key mapping key @@ -483,6 +624,13 @@ public interface AsdfNode extends Iterable { */ Number getNumber(AsdfNode key); + /** + * Get a NUMBER sequence value or mapping value as Number, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + Number getNumber(Object... keys); + /** * Get a MAPPING mapping value as Map, indexed by String key. * @param key mapping key @@ -491,7 +639,10 @@ public interface AsdfNode extends Iterable { * @return value * @param Map key * @param Map value + * + * @deprecated in favor of {@link #getMap(Class, Class, String)} */ + @Deprecated Map getMap(String key, Class keyClass, Class valueClass); /** @@ -502,7 +653,10 @@ public interface AsdfNode extends Iterable { * @return value * @param Map key * @param Map value + * + * @deprecated in favor of {@link #getMap(Class, Class, long)} */ + @Deprecated Map getMap(long key, Class keyClass, Class valueClass); /** @@ -513,7 +667,10 @@ public interface AsdfNode extends Iterable { * @return value * @param Map key * @param Map value + * + * @deprecated in favor of {@link #getMap(Class, Class, boolean)} */ + @Deprecated Map getMap(boolean key, Class keyClass, Class valueClass); /** @@ -524,9 +681,67 @@ public interface AsdfNode extends Iterable { * @return value * @param Map key * @param Map value + * + * @deprecated in favor of {@link #getMap(Class, Class, AsdfNode)} */ + @Deprecated Map getMap(AsdfNode key, Class keyClass, Class valueClass); + /** + * Get a MAPPING mapping value as Map, indexed by String key. + * @param keyClass key class + * @param valueClass value class + * @param key mapping key + * @return value + * @param Map key + * @param Map value + */ + Map getMap(Class keyClass, Class valueClass, String key); + + /** + * Get a MAPPING sequence value or mapping value as Map, indexed by long key. + * @param keyClass key class + * @param valueClass value class + * @param key sequence index or mapping key + * @return value + * @param Map key + * @param Map value + */ + Map getMap(Class keyClass, Class valueClass, long key); + + /** + * Get a MAPPING mapping value as Map, indexed by String key. + * @param keyClass key class + * @param valueClass value class + * @param key mapping key + * @return value + * @param Map key + * @param Map value + */ + Map getMap(Class keyClass, Class valueClass, boolean key); + + /** + * Get a MAPPING mapping value as Map, indexed by AsdfNode key. + * @param keyClass key class + * @param valueClass value class + * @param key sequence index or mapping key + * @return value + * @param Map key + * @param Map value + */ + Map getMap(Class keyClass, Class valueClass, AsdfNode key); + + /** + * Get a MAPPING sequence value or mapping value as Map, indexed by a chain of keys. + * @param keyClass key class + * @param valueClass value class + * @param keys sequence indexes and mapping keys + * @return value + * @param Map key + * @param Map value * + */ + Map getMap(Class keyClass, Class valueClass, Object... keys); + /** * Get a NDARRAY mapping value, indexed by String key. * @param key mapping key @@ -555,6 +770,13 @@ public interface AsdfNode extends Iterable { */ NdArray getNdArray(AsdfNode key); + /** + * Get a NDARRAY sequence value or mapping value, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + NdArray getNdArray(Object... keys); + /** * Get a NUMBER mapping value as short, indexed by String key. * @param key mapping key @@ -583,6 +805,13 @@ public interface AsdfNode extends Iterable { */ short getShort(AsdfNode key); + /** + * Get a NUMBER sequence value or mapping value as short, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + short getShort(Object... keys); + /** * Get a STRING mapping value, indexed by String key. * @param key mapping key @@ -611,6 +840,13 @@ public interface AsdfNode extends Iterable { */ String getString(AsdfNode key); + /** + * Get a STRING sequence value or mapping value, indexed by a chain of keys. + * @param keys sequence indexes and mapping keys + * @return value + */ + String getString(Object... keys); + /** * Get this NUMBER node's value as BigDecimal. * @return value diff --git a/asdf-core/src/main/java/org/asdfformat/asdf/node/impl/AsdfNodeBase.java b/asdf-core/src/main/java/org/asdfformat/asdf/node/impl/AsdfNodeBase.java index a4f65eb..9344d57 100644 --- a/asdf-core/src/main/java/org/asdfformat/asdf/node/impl/AsdfNodeBase.java +++ b/asdf-core/src/main/java/org/asdfformat/asdf/node/impl/AsdfNodeBase.java @@ -7,6 +7,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.time.Instant; +import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -75,6 +76,18 @@ public boolean containsKey(final AsdfNode key) { return false; } + @Override + public boolean containsKey(final Object... keys) { + if (keys.length == 0) { + return true; + } + + final AsdfNode currentKey = constructKeyNode(keys[0]); + + return getOptional(currentKey).map(node -> node.containsKey(Arrays.copyOfRange(keys, 1, keys.length))) + .orElse(false); + } + @Override public int size() { return 0; @@ -100,6 +113,17 @@ public AsdfNode get(final AsdfNode key) { throw new IllegalStateException(makeGetErrorMessage("AsdfNode")); } + @Override + public AsdfNode get(final Object... keys) { + if (keys.length == 0) { + return this; + } + + final AsdfNode currentKey = constructKeyNode(keys[0]); + + return get(currentKey).get(Arrays.copyOfRange(keys, 1, keys.length)); + } + @Override public Optional getOptional(final String key) { if (containsKey(key)) { @@ -156,6 +180,17 @@ public Optional getOptional(final AsdfNode key) { } } + @Override + public Optional getOptional(final Object... keys) { + if (keys.length == 0) { + return Optional.of(this); + } + + final AsdfNode currentKey = constructKeyNode(keys[0]); + + return getOptional(currentKey).flatMap(node -> node.getOptional(Arrays.copyOfRange(keys, 1, keys.length))); + } + @Override public BigDecimal getBigDecimal(final String key) { return get(key).asBigDecimal(); @@ -176,6 +211,11 @@ public BigDecimal getBigDecimal(final AsdfNode key) { return get(key).asBigDecimal(); } + @Override + public BigDecimal getBigDecimal(final Object... keys) { + return get(keys).asBigDecimal(); + } + @Override public BigInteger getBigInteger(final String key) { return get(key).asBigInteger(); @@ -196,6 +236,11 @@ public BigInteger getBigInteger(final AsdfNode key) { return get(key).asBigInteger(); } + @Override + public BigInteger getBigInteger(final Object... keys) { + return get(keys).asBigInteger(); + } + @Override public boolean getBoolean(final String key) { return get(key).asBoolean(); @@ -216,6 +261,11 @@ public boolean getBoolean(final AsdfNode key) { return get(key).asBoolean(); } + @Override + public boolean getBoolean(final Object... keys) { + return get(keys).asBoolean(); + } + @Override public byte getByte(final String key) { return get(key).asByte(); @@ -236,6 +286,11 @@ public byte getByte(final AsdfNode key) { return get(key).asByte(); } + @Override + public byte getByte(final Object... keys) { + return get(keys).asByte(); + } + @Override public double getDouble(final String key) { return get(key).asDouble(); @@ -256,6 +311,11 @@ public double getDouble(final AsdfNode key) { return get(key).asDouble(); } + @Override + public double getDouble(final Object... keys) { + return get(keys).asDouble(); + } + @Override public float getFloat(final String key) { return get(key).asFloat(); @@ -276,6 +336,11 @@ public float getFloat(final AsdfNode key) { return get(key).asFloat(); } + @Override + public float getFloat(final Object... keys) { + return get(keys).asFloat(); + } + @Override public Instant getInstant(final String key) { return get(key).asInstant(); @@ -296,6 +361,11 @@ public Instant getInstant(final AsdfNode key) { return get(key).asInstant(); } + @Override + public Instant getInstant(final Object... keys) { + return get(keys).asInstant(); + } + @Override public int getInt(final String key) { return get(key).asInt(); @@ -316,6 +386,11 @@ public int getInt(final AsdfNode key) { return get(key).asInt(); } + @Override + public int getInt(final Object... keys) { + return get(keys).asInt(); + } + @Override public List getList(final String key, final Class elementClass) { return get(key).asList(elementClass); @@ -336,6 +411,31 @@ public List getList(final AsdfNode key, final Class elementClass) { return get(key).asList(elementClass); } + @Override + public List getList(final Class elementClass, final String key) { + return get(key).asList(elementClass); + } + + @Override + public List getList(final Class elementClass, final long key) { + return get(key).asList(elementClass); + } + + @Override + public List getList(final Class elementClass, final boolean key) { + return get(key).asList(elementClass); + } + + @Override + public List getList(final Class elementClass, final AsdfNode key) { + return get(key).asList(elementClass); + } + + @Override + public List getList(final Class elementClass, final Object... keys) { + return get(keys).asList(elementClass); + } + @Override public long getLong(final String key) { return get(key).asLong(); @@ -356,6 +456,11 @@ public long getLong(final AsdfNode key) { return get(key).asLong(); } + @Override + public long getLong(final Object... keys) { + return get(keys).asLong(); + } + @Override public Number getNumber(final String key) { return get(key).asNumber(); @@ -376,6 +481,11 @@ public Number getNumber(final AsdfNode key) { return get(key).asNumber(); } + @Override + public Number getNumber(final Object... keys) { + return get(keys).asNumber(); + } + @Override public Map getMap(final String key, final Class keyClass, final Class valueClass) { return get(key).asMap(keyClass, valueClass); @@ -396,6 +506,31 @@ public Map getMap(final AsdfNode key, final Class keyClass, fina return get(key).asMap(keyClass, valueClass); } + @Override + public Map getMap(final Class keyClass, final Class valueClass, final String key) { + return get(key).asMap(keyClass, valueClass); + } + + @Override + public Map getMap(final Class keyClass, final Class valueClass, final long key) { + return get(key).asMap(keyClass, valueClass); + } + + @Override + public Map getMap(final Class keyClass, final Class valueClass, final boolean key) { + return get(key).asMap(keyClass, valueClass); + } + + @Override + public Map getMap(final Class keyClass, final Class valueClass, final AsdfNode key) { + return get(key).asMap(keyClass, valueClass); + } + + @Override + public Map getMap(final Class keyClass, final Class valueClass, final Object... keys) { + return get(keys).asMap(keyClass, valueClass); + } + @Override public NdArray getNdArray(final String key) { return get(key).asNdArray(); @@ -416,6 +551,11 @@ public NdArray getNdArray(final AsdfNode key) { return get(key).asNdArray(); } + @Override + public NdArray getNdArray(final Object... keys) { + return get(keys).asNdArray(); + } + @Override public short getShort(final String key) { return get(key).asShort(); @@ -436,6 +576,11 @@ public short getShort(final AsdfNode key) { return get(key).asShort(); } + @Override + public short getShort(final Object... keys) { + return get(keys).asShort(); + } + @Override public String getString(final String key) { return get(key).asString(); @@ -456,6 +601,11 @@ public String getString(final AsdfNode key) { return get(key).asString(); } + @Override + public String getString(final Object... keys) { + return get(keys).asString(); + } + @Override public BigDecimal asBigDecimal() { throw new IllegalStateException(makeAsErrorMessage("BigDecimal")); @@ -552,4 +702,18 @@ protected String makeGetErrorMessage(final String keyType) { protected String makeAsErrorMessage(final String asType) { return String.format("%s node cannot be represented as %s", getNodeType(), asType); } + + protected AsdfNode constructKeyNode(final Object key) { + if (key instanceof String) { + return StringAsdfNode.of((String)key); + } else if (key instanceof AsdfNode) { + return (AsdfNode)key; + } else if (key instanceof Number) { + return NumberAsdfNode.of((Number)key); + } else if (key instanceof Boolean) { + return BooleanAsdfNode.of((Boolean)key); + } else { + throw new IllegalArgumentException("Unhandled key class: " + key.getClass().getSimpleName()); + } + } } diff --git a/asdf-core/src/main/java/org/asdfformat/asdf/node/impl/NumberAsdfNode.java b/asdf-core/src/main/java/org/asdfformat/asdf/node/impl/NumberAsdfNode.java index e5d0864..a920b49 100644 --- a/asdf-core/src/main/java/org/asdfformat/asdf/node/impl/NumberAsdfNode.java +++ b/asdf-core/src/main/java/org/asdfformat/asdf/node/impl/NumberAsdfNode.java @@ -109,7 +109,9 @@ public BigInteger asBigInteger() { @Override public byte asByte() { if (value instanceof Byte) { - return (Byte)value; + return (Byte) value; + } else if (value instanceof Integer && (int)value >= Byte.MIN_VALUE && (int)value <= Byte.MAX_VALUE) { + return value.byteValue(); } else { throw new IllegalStateException("Node cannot be represented as byte"); } @@ -152,7 +154,9 @@ public Number asNumber() { public short asShort() { if (value instanceof Byte || value instanceof Short) { return value.shortValue(); - } else { + } else if (value instanceof Integer && (int)value >= Short.MIN_VALUE && (int)value <= Short.MAX_VALUE) { + return value.shortValue(); + }else { throw new IllegalStateException("Node cannot be represented as short"); } } @@ -162,16 +166,39 @@ public boolean equals(final Object other) { if (this == other) { return true; } - if (other == null || getClass() != other.getClass()) { + + if (!(other instanceof NumberAsdfNode)) { + return false; + } + + final NumberAsdfNode otherNode = (NumberAsdfNode)other; + + if (!Objects.equals(this.tag, otherNode.tag)) { return false; } - final NumberAsdfNode typedOther = (NumberAsdfNode) other; - return Objects.equals(tag, typedOther.tag) && Objects.equals(value, typedOther.value); + + if (this.value == null || otherNode.value == null) { + return this.value == otherNode.value; + } + + if (this.value.getClass() == otherNode.value.getClass() && this.value.equals(otherNode.value)) { + return true; + } + + if (isSpecialFloatingPointValue(this.value) || isSpecialFloatingPointValue(otherNode.value)) { + return Double.compare(this.value.doubleValue(), otherNode.value.doubleValue()) == 0; + } + + return asBigDecimal().compareTo(otherNode.asBigDecimal()) == 0; } @Override public int hashCode() { - return Objects.hash(tag, value); + if (isSpecialFloatingPointValue(value)) { + return Double.hashCode(value.doubleValue()); + } + + return asBigDecimal().stripTrailingZeros().hashCode(); } @Override @@ -188,4 +215,9 @@ public String toString() { return NodeUtils.nodeToString(this, fields); } + + private boolean isSpecialFloatingPointValue(final Number number) { + return (number instanceof Double && (((Double)number).isInfinite() || ((Double)number).isNaN())) || + (number instanceof Float && (((Float)number).isInfinite() || ((Float)number).isNaN())); + } } diff --git a/asdf-core/src/main/java/org/asdfformat/asdf/testing/ReferenceFileUtils.java b/asdf-core/src/main/java/org/asdfformat/asdf/testing/ReferenceFileUtils.java index 3214ea3..f54fe3c 100644 --- a/asdf-core/src/main/java/org/asdfformat/asdf/testing/ReferenceFileUtils.java +++ b/asdf-core/src/main/java/org/asdfformat/asdf/testing/ReferenceFileUtils.java @@ -10,11 +10,14 @@ import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import static org.junit.Assume.assumeTrue; + public class ReferenceFileUtils { private static final String PYTHON_PATH = System.getenv("ASDF_JAVA_TESTS_PYTHON_PATH"); private static final Path TEST_FILE_GENERATOR_PY_PATH = getTestFileGeneratorPyPath(); @@ -59,6 +62,13 @@ private static Path generateTestFile(final ReferenceFile referenceFile, final Ve file.deleteOnExit(); final Path path = file.toPath(); + assumeTrue( + "ASDF_JAVA_TESTS_PYTHON_PATH missing or unset", + Optional.ofNullable(PYTHON_PATH) + .map(p -> p.isEmpty() ? null : p) + .map(p -> Files.exists(Paths.get(p))) + .orElse(false) + ); final Process process = new ProcessBuilder(PYTHON_PATH, TEST_FILE_GENERATOR_PY_PATH.toString(), "--version", asdfStandardVersion.toString()).start(); try (final OutputStream outputStream = process.getOutputStream()) { IOUtils.transferTo(scriptInputStream, outputStream); diff --git a/asdf-core/src/test/java/org/asdfformat/asdf/node/BooleanAsdfNodeTest.java b/asdf-core/src/test/java/org/asdfformat/asdf/node/BooleanAsdfNodeTest.java index a018b32..9d2941d 100644 --- a/asdf-core/src/test/java/org/asdfformat/asdf/node/BooleanAsdfNodeTest.java +++ b/asdf-core/src/test/java/org/asdfformat/asdf/node/BooleanAsdfNodeTest.java @@ -13,6 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -141,6 +142,8 @@ void testBaseMethods() { assertFalse(node.containsKey(1L)); assertFalse(node.containsKey(false)); assertFalse(node.containsKey(node)); + assertFalse(node.containsKey("foo", 1L, false, node)); + assertTrue(node.containsKey()); assertEquals(0, node.size()); @@ -150,81 +153,113 @@ void testBaseMethods() { assertThrows(IllegalStateException.class, () -> node.get(1L)); assertThrows(IllegalStateException.class, () -> node.get(false)); assertThrows(IllegalStateException.class, () -> node.get(node)); + assertThrows(IllegalStateException.class, () -> node.get("foo", 1L, false, node)); + assertSame(node, node.get()); assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo")); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(1L)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(false)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(node)); + assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBigDecimal); assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo")); assertThrows(IllegalStateException.class, () -> node.getBigInteger(1L)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(false)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(node)); + assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBigInteger); assertThrows(IllegalStateException.class, () -> node.getBoolean("foo")); assertThrows(IllegalStateException.class, () -> node.getBoolean(1L)); assertThrows(IllegalStateException.class, () -> node.getBoolean(false)); assertThrows(IllegalStateException.class, () -> node.getBoolean(node)); + assertThrows(IllegalStateException.class, () -> node.getBoolean("foo", 1L, false, node)); + assertTrue(node.getBoolean()); assertThrows(IllegalStateException.class, () -> node.getByte("foo")); assertThrows(IllegalStateException.class, () -> node.getByte(1L)); assertThrows(IllegalStateException.class, () -> node.getByte(false)); assertThrows(IllegalStateException.class, () -> node.getByte(node)); + assertThrows(IllegalStateException.class, () -> node.getByte("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getByte); assertThrows(IllegalStateException.class, () -> node.getDouble("foo")); assertThrows(IllegalStateException.class, () -> node.getDouble(1L)); assertThrows(IllegalStateException.class, () -> node.getDouble(false)); assertThrows(IllegalStateException.class, () -> node.getDouble(node)); + assertThrows(IllegalStateException.class, () -> node.getDouble("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getDouble); assertThrows(IllegalStateException.class, () -> node.getFloat("foo")); assertThrows(IllegalStateException.class, () -> node.getFloat(1L)); assertThrows(IllegalStateException.class, () -> node.getFloat(false)); assertThrows(IllegalStateException.class, () -> node.getFloat(node)); + assertThrows(IllegalStateException.class, () -> node.getFloat("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getFloat); assertThrows(IllegalStateException.class, () -> node.getInstant("foo")); assertThrows(IllegalStateException.class, () -> node.getInstant(1L)); assertThrows(IllegalStateException.class, () -> node.getInstant(false)); assertThrows(IllegalStateException.class, () -> node.getInstant(node)); + assertThrows(IllegalStateException.class, () -> node.getInstant("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getInstant); assertThrows(IllegalStateException.class, () -> node.getInt("foo")); assertThrows(IllegalStateException.class, () -> node.getInt(1L)); assertThrows(IllegalStateException.class, () -> node.getInt(false)); assertThrows(IllegalStateException.class, () -> node.getInt(node)); + assertThrows(IllegalStateException.class, () -> node.getInt("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getInt); - assertThrows(IllegalStateException.class, () -> node.getList("foo", String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(1L, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(false, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(node, String.class)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class)); assertThrows(IllegalStateException.class, () -> node.getLong("foo")); assertThrows(IllegalStateException.class, () -> node.getLong(1L)); assertThrows(IllegalStateException.class, () -> node.getLong(false)); assertThrows(IllegalStateException.class, () -> node.getLong(node)); + assertThrows(IllegalStateException.class, () -> node.getLong("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getLong); assertThrows(IllegalStateException.class, () -> node.getNumber("foo")); assertThrows(IllegalStateException.class, () -> node.getNumber(1L)); assertThrows(IllegalStateException.class, () -> node.getNumber(false)); assertThrows(IllegalStateException.class, () -> node.getNumber(node)); + assertThrows(IllegalStateException.class, () -> node.getNumber("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getNumber); - assertThrows(IllegalStateException.class, () -> node.getMap("foo", String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(1L, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(false, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(node, String.class, String.class)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class)); assertThrows(IllegalStateException.class, () -> node.getNdArray("foo")); assertThrows(IllegalStateException.class, () -> node.getNdArray(1L)); assertThrows(IllegalStateException.class, () -> node.getNdArray(false)); assertThrows(IllegalStateException.class, () -> node.getNdArray(node)); + assertThrows(IllegalStateException.class, () -> node.getNdArray("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getNdArray); assertThrows(IllegalStateException.class, () -> node.getShort("foo")); assertThrows(IllegalStateException.class, () -> node.getShort(1L)); assertThrows(IllegalStateException.class, () -> node.getShort(false)); assertThrows(IllegalStateException.class, () -> node.getShort(node)); + assertThrows(IllegalStateException.class, () -> node.getShort("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getShort); assertThrows(IllegalStateException.class, () -> node.getString("foo")); assertThrows(IllegalStateException.class, () -> node.getString(1L)); assertThrows(IllegalStateException.class, () -> node.getString(false)); assertThrows(IllegalStateException.class, () -> node.getString(node)); + assertThrows(IllegalStateException.class, () -> node.getString("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getString); assertThrows(IllegalStateException.class, node::asBigDecimal); assertThrows(IllegalStateException.class, node::asBigInteger); diff --git a/asdf-core/src/test/java/org/asdfformat/asdf/node/NullAsdfNodeTest.java b/asdf-core/src/test/java/org/asdfformat/asdf/node/NullAsdfNodeTest.java index fb5a13a..c288eb2 100644 --- a/asdf-core/src/test/java/org/asdfformat/asdf/node/NullAsdfNodeTest.java +++ b/asdf-core/src/test/java/org/asdfformat/asdf/node/NullAsdfNodeTest.java @@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -62,6 +63,8 @@ void testBaseMethods() { assertFalse(node.containsKey(1L)); assertFalse(node.containsKey(false)); assertFalse(node.containsKey(node)); + assertFalse(node.containsKey("foo", 1L, false, node)); + assertTrue(node.containsKey()); assertEquals(0, node.size()); @@ -71,81 +74,113 @@ void testBaseMethods() { assertThrows(IllegalStateException.class, () -> node.get(1L)); assertThrows(IllegalStateException.class, () -> node.get(false)); assertThrows(IllegalStateException.class, () -> node.get(node)); + assertThrows(IllegalStateException.class, () -> node.get("foo", 1L, false, node)); + assertSame(node, node.get()); assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo")); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(1L)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(false)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(node)); + assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBigDecimal); assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo")); assertThrows(IllegalStateException.class, () -> node.getBigInteger(1L)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(false)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(node)); + assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBigInteger); assertThrows(IllegalStateException.class, () -> node.getBoolean("foo")); assertThrows(IllegalStateException.class, () -> node.getBoolean(1L)); assertThrows(IllegalStateException.class, () -> node.getBoolean(false)); assertThrows(IllegalStateException.class, () -> node.getBoolean(node)); + assertThrows(IllegalStateException.class, () -> node.getBoolean("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBoolean); assertThrows(IllegalStateException.class, () -> node.getByte("foo")); assertThrows(IllegalStateException.class, () -> node.getByte(1L)); assertThrows(IllegalStateException.class, () -> node.getByte(false)); assertThrows(IllegalStateException.class, () -> node.getByte(node)); + assertThrows(IllegalStateException.class, () -> node.getByte("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getByte); assertThrows(IllegalStateException.class, () -> node.getDouble("foo")); assertThrows(IllegalStateException.class, () -> node.getDouble(1L)); assertThrows(IllegalStateException.class, () -> node.getDouble(false)); assertThrows(IllegalStateException.class, () -> node.getDouble(node)); + assertThrows(IllegalStateException.class, () -> node.getDouble("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getDouble); assertThrows(IllegalStateException.class, () -> node.getFloat("foo")); assertThrows(IllegalStateException.class, () -> node.getFloat(1L)); assertThrows(IllegalStateException.class, () -> node.getFloat(false)); assertThrows(IllegalStateException.class, () -> node.getFloat(node)); + assertThrows(IllegalStateException.class, () -> node.getFloat("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getFloat); assertThrows(IllegalStateException.class, () -> node.getInstant("foo")); assertThrows(IllegalStateException.class, () -> node.getInstant(1L)); assertThrows(IllegalStateException.class, () -> node.getInstant(false)); assertThrows(IllegalStateException.class, () -> node.getInstant(node)); + assertThrows(IllegalStateException.class, () -> node.getInstant("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getInstant); assertThrows(IllegalStateException.class, () -> node.getInt("foo")); assertThrows(IllegalStateException.class, () -> node.getInt(1L)); assertThrows(IllegalStateException.class, () -> node.getInt(false)); assertThrows(IllegalStateException.class, () -> node.getInt(node)); + assertThrows(IllegalStateException.class, () -> node.getInt("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getInt); - assertThrows(IllegalStateException.class, () -> node.getList("foo", String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(1L, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(false, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(node, String.class)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class)); assertThrows(IllegalStateException.class, () -> node.getLong("foo")); assertThrows(IllegalStateException.class, () -> node.getLong(1L)); assertThrows(IllegalStateException.class, () -> node.getLong(false)); assertThrows(IllegalStateException.class, () -> node.getLong(node)); + assertThrows(IllegalStateException.class, () -> node.getLong("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getLong); assertThrows(IllegalStateException.class, () -> node.getNumber("foo")); assertThrows(IllegalStateException.class, () -> node.getNumber(1L)); assertThrows(IllegalStateException.class, () -> node.getNumber(false)); assertThrows(IllegalStateException.class, () -> node.getNumber(node)); + assertThrows(IllegalStateException.class, () -> node.getNumber("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getNumber); - assertThrows(IllegalStateException.class, () -> node.getMap("foo", String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(1L, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(false, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(node, String.class, String.class)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class)); assertThrows(IllegalStateException.class, () -> node.getNdArray("foo")); assertThrows(IllegalStateException.class, () -> node.getNdArray(1L)); assertThrows(IllegalStateException.class, () -> node.getNdArray(false)); assertThrows(IllegalStateException.class, () -> node.getNdArray(node)); + assertThrows(IllegalStateException.class, () -> node.getNdArray("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getNdArray); assertThrows(IllegalStateException.class, () -> node.getShort("foo")); assertThrows(IllegalStateException.class, () -> node.getShort(1L)); assertThrows(IllegalStateException.class, () -> node.getShort(false)); assertThrows(IllegalStateException.class, () -> node.getShort(node)); + assertThrows(IllegalStateException.class, () -> node.getShort("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getShort); assertThrows(IllegalStateException.class, () -> node.getString("foo")); assertThrows(IllegalStateException.class, () -> node.getString(1L)); assertThrows(IllegalStateException.class, () -> node.getString(false)); assertThrows(IllegalStateException.class, () -> node.getString(node)); + assertThrows(IllegalStateException.class, () -> node.getString("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getString); assertThrows(IllegalStateException.class, node::asBigDecimal); assertThrows(IllegalStateException.class, node::asBigInteger); diff --git a/asdf-core/src/test/java/org/asdfformat/asdf/node/NumberAsdfNodeTest.java b/asdf-core/src/test/java/org/asdfformat/asdf/node/NumberAsdfNodeTest.java index 4fc975b..7826f74 100644 --- a/asdf-core/src/test/java/org/asdfformat/asdf/node/NumberAsdfNodeTest.java +++ b/asdf-core/src/test/java/org/asdfformat/asdf/node/NumberAsdfNodeTest.java @@ -14,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -199,6 +200,88 @@ void testCustomTag() { } } + @Nested + class EqualsAndHashCode { + @Test + void testAgainstNull() { + final NumberAsdfNode node = NumberAsdfNode.of(1L); + + assertNotEquals(node, null); + } + + @Test + void testAgainstUnwrappedNumber() { + final NumberAsdfNode node = NumberAsdfNode.of(1L); + + assertNotEquals(node, 1L); + } + + @Test + void testAgainstSelf() { + final NumberAsdfNode node = NumberAsdfNode.of(1L); + + assertEquals(node, node); + } + + @Test + void testSameValueClass() { + final NumberAsdfNode node = NumberAsdfNode.of(1L); + + assertEquals(node, NumberAsdfNode.of(1L)); + assertEquals(node.hashCode(), NumberAsdfNode.of(1L).hashCode()); + + assertNotEquals(node, NumberAsdfNode.of(2L)); + assertNotEquals(node.hashCode(), NumberAsdfNode.of(2L).hashCode()); + } + + @Test + void testDifferentIntegerValueClass() { + final NumberAsdfNode node = NumberAsdfNode.of(1L); + + assertEquals(node, NumberAsdfNode.of(BigInteger.ONE)); + assertEquals(node.hashCode(), NumberAsdfNode.of(BigInteger.ONE).hashCode()); + + assertNotEquals(node, NumberAsdfNode.of(BigInteger.TEN)); + assertNotEquals(node.hashCode(), NumberAsdfNode.of(BigInteger.TEN).hashCode()); + } + + @Test + void testDifferentDecimalValueClass() { + final NumberAsdfNode node = NumberAsdfNode.of(3.14159); + + assertEquals(node, NumberAsdfNode.of(new BigDecimal("3.14159"))); + assertEquals(node.hashCode(), NumberAsdfNode.of(new BigDecimal("3.14159")).hashCode()); + + assertNotEquals(node, NumberAsdfNode.of(new BigDecimal("2.71828"))); + assertNotEquals(node.hashCode(), NumberAsdfNode.of(new BigDecimal("2.71828")).hashCode()); + } + + @Test + void testSpecialFloatingPointValues() { + final NumberAsdfNode nanNode = NumberAsdfNode.of(Double.NaN); + final NumberAsdfNode positiveInfinityNode = NumberAsdfNode.of(Double.POSITIVE_INFINITY); + final NumberAsdfNode negativeInfinityNode = NumberAsdfNode.of(Double.NEGATIVE_INFINITY); + + assertEquals(nanNode, NumberAsdfNode.of(Float.NaN)); + assertEquals(nanNode.hashCode(), NumberAsdfNode.of(Float.NaN).hashCode()); + + assertNotEquals(nanNode, positiveInfinityNode); + assertNotEquals(nanNode.hashCode(), positiveInfinityNode.hashCode()); + + assertEquals(positiveInfinityNode, NumberAsdfNode.of(Float.POSITIVE_INFINITY)); + assertEquals(positiveInfinityNode.hashCode(), NumberAsdfNode.of(Float.POSITIVE_INFINITY).hashCode()); + + assertNotEquals(positiveInfinityNode, negativeInfinityNode); + assertNotEquals(positiveInfinityNode.hashCode(), negativeInfinityNode.hashCode()); + + assertEquals(negativeInfinityNode, NumberAsdfNode.of(Float.NEGATIVE_INFINITY)); + assertEquals(negativeInfinityNode.hashCode(), NumberAsdfNode.of(Float.NEGATIVE_INFINITY).hashCode()); + + assertNotEquals(negativeInfinityNode, nanNode); + assertNotEquals(negativeInfinityNode.hashCode(), nanNode.hashCode()); + } + } + @Test void testConversionFromByte() { final byte value = Byte.MAX_VALUE; @@ -340,22 +423,6 @@ void testNodeType() { assertEquals(AsdfNodeType.NUMBER, NumberAsdfNode.of(1).getNodeType()); } - @Test - void testEqualsAndHashCode() { - final NumberAsdfNode node = NumberAsdfNode.of(1L); - - assertEquals(node, NumberAsdfNode.of(1L)); - assertEquals(node.hashCode(), NumberAsdfNode.of(1L).hashCode()); - - assertNotEquals(node, NumberAsdfNode.of(2L)); - assertNotEquals(node.hashCode(), NumberAsdfNode.of(2L).hashCode()); - - assertEquals(node, node); - assertNotEquals(node, 1L); - assertNotEquals(node, null); - - } - @Test void testToString() { assertEquals("NumberAsdfNode(value=1)", NumberAsdfNode.of(1L).toString()); @@ -379,6 +446,8 @@ void testBaseMethods() { assertFalse(node.containsKey(1L)); assertFalse(node.containsKey(false)); assertFalse(node.containsKey(node)); + assertFalse(node.containsKey("foo", 1L, false, node)); + assertTrue(node.containsKey()); assertEquals(0, node.size()); @@ -388,81 +457,113 @@ void testBaseMethods() { assertThrows(IllegalStateException.class, () -> node.get(1L)); assertThrows(IllegalStateException.class, () -> node.get(false)); assertThrows(IllegalStateException.class, () -> node.get(node)); + assertThrows(IllegalStateException.class, () -> node.get("foo", 1L, false, node)); + assertSame(node, node.get()); assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo")); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(1L)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(false)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(node)); + assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo", 1L, false, node)); + assertEquals(BigDecimal.ONE, node.getBigDecimal()); assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo")); assertThrows(IllegalStateException.class, () -> node.getBigInteger(1L)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(false)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(node)); + assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo", 1L, false, node)); + assertEquals(BigInteger.ONE, node.getBigInteger()); assertThrows(IllegalStateException.class, () -> node.getBoolean("foo")); assertThrows(IllegalStateException.class, () -> node.getBoolean(1L)); assertThrows(IllegalStateException.class, () -> node.getBoolean(false)); assertThrows(IllegalStateException.class, () -> node.getBoolean(node)); + assertThrows(IllegalStateException.class, () -> node.getBoolean("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBoolean); assertThrows(IllegalStateException.class, () -> node.getByte("foo")); assertThrows(IllegalStateException.class, () -> node.getByte(1L)); assertThrows(IllegalStateException.class, () -> node.getByte(false)); assertThrows(IllegalStateException.class, () -> node.getByte(node)); + assertThrows(IllegalStateException.class, () -> node.getByte("foo", 1L, false, node)); + assertEquals(1, node.getByte()); assertThrows(IllegalStateException.class, () -> node.getDouble("foo")); assertThrows(IllegalStateException.class, () -> node.getDouble(1L)); assertThrows(IllegalStateException.class, () -> node.getDouble(false)); assertThrows(IllegalStateException.class, () -> node.getDouble(node)); + assertThrows(IllegalStateException.class, () -> node.getDouble("foo", 1L, false, node)); + assertEquals(1.0, node.getDouble()); assertThrows(IllegalStateException.class, () -> node.getFloat("foo")); assertThrows(IllegalStateException.class, () -> node.getFloat(1L)); assertThrows(IllegalStateException.class, () -> node.getFloat(false)); assertThrows(IllegalStateException.class, () -> node.getFloat(node)); + assertThrows(IllegalStateException.class, () -> node.getFloat("foo", 1L, false, node)); + assertEquals(1.0, node.getFloat()); assertThrows(IllegalStateException.class, () -> node.getInstant("foo")); assertThrows(IllegalStateException.class, () -> node.getInstant(1L)); assertThrows(IllegalStateException.class, () -> node.getInstant(false)); assertThrows(IllegalStateException.class, () -> node.getInstant(node)); + assertThrows(IllegalStateException.class, () -> node.getInstant("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getInstant); assertThrows(IllegalStateException.class, () -> node.getInt("foo")); assertThrows(IllegalStateException.class, () -> node.getInt(1L)); assertThrows(IllegalStateException.class, () -> node.getInt(false)); assertThrows(IllegalStateException.class, () -> node.getInt(node)); + assertThrows(IllegalStateException.class, () -> node.getInt("foo", 1L, false, node)); + assertEquals(1, node.getInt()); - assertThrows(IllegalStateException.class, () -> node.getList("foo", String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(1L, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(false, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(node, String.class)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class)); assertThrows(IllegalStateException.class, () -> node.getLong("foo")); assertThrows(IllegalStateException.class, () -> node.getLong(1L)); assertThrows(IllegalStateException.class, () -> node.getLong(false)); assertThrows(IllegalStateException.class, () -> node.getLong(node)); + assertThrows(IllegalStateException.class, () -> node.getLong("foo", 1L, false, node)); + assertEquals(1, node.getLong()); assertThrows(IllegalStateException.class, () -> node.getNumber("foo")); assertThrows(IllegalStateException.class, () -> node.getNumber(1L)); assertThrows(IllegalStateException.class, () -> node.getNumber(false)); assertThrows(IllegalStateException.class, () -> node.getNumber(node)); + assertThrows(IllegalStateException.class, () -> node.getNumber("foo", 1L, false, node)); + assertEquals(1, node.getNumber()); - assertThrows(IllegalStateException.class, () -> node.getMap("foo", String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(1L, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(false, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(node, String.class, String.class)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class)); assertThrows(IllegalStateException.class, () -> node.getNdArray("foo")); assertThrows(IllegalStateException.class, () -> node.getNdArray(1L)); assertThrows(IllegalStateException.class, () -> node.getNdArray(false)); assertThrows(IllegalStateException.class, () -> node.getNdArray(node)); + assertThrows(IllegalStateException.class, () -> node.getNdArray("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getNdArray); assertThrows(IllegalStateException.class, () -> node.getShort("foo")); assertThrows(IllegalStateException.class, () -> node.getShort(1L)); assertThrows(IllegalStateException.class, () -> node.getShort(false)); assertThrows(IllegalStateException.class, () -> node.getShort(node)); + assertThrows(IllegalStateException.class, () -> node.getShort("foo", 1L, false, node)); + assertEquals(1, node.getShort()); assertThrows(IllegalStateException.class, () -> node.getString("foo")); assertThrows(IllegalStateException.class, () -> node.getString(1L)); assertThrows(IllegalStateException.class, () -> node.getString(false)); assertThrows(IllegalStateException.class, () -> node.getString(node)); + assertThrows(IllegalStateException.class, () -> node.getString("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getString); assertThrows(IllegalStateException.class, node::asBoolean); assertThrows(IllegalStateException.class, node::asInstant); diff --git a/asdf-core/src/test/java/org/asdfformat/asdf/node/StringAsdfNodeTest.java b/asdf-core/src/test/java/org/asdfformat/asdf/node/StringAsdfNodeTest.java index 3035e7a..980550f 100644 --- a/asdf-core/src/test/java/org/asdfformat/asdf/node/StringAsdfNodeTest.java +++ b/asdf-core/src/test/java/org/asdfformat/asdf/node/StringAsdfNodeTest.java @@ -11,6 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -55,6 +56,7 @@ void testToString() { @Nested class ConstructionFromSnakeYamlNode { + @Test void testBasic() { final ScalarNode snakeYamlNode = new ScalarNode(Tag.STR, "foo", null, null, DumperOptions.ScalarStyle.LITERAL); final StringAsdfNode asdfNode = StringAsdfNode.of(snakeYamlNode); @@ -105,6 +107,8 @@ void testBaseMethods() { assertFalse(node.containsKey(1L)); assertFalse(node.containsKey(false)); assertFalse(node.containsKey(node)); + assertFalse(node.containsKey("foo", 1L, false, node)); + assertTrue(node.containsKey()); assertEquals(0, node.size()); @@ -114,81 +118,113 @@ void testBaseMethods() { assertThrows(IllegalStateException.class, () -> node.get(1L)); assertThrows(IllegalStateException.class, () -> node.get(false)); assertThrows(IllegalStateException.class, () -> node.get(node)); + assertThrows(IllegalStateException.class, () -> node.get("foo", 1L, false, node)); + assertSame(node, node.get()); assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo")); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(1L)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(false)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(node)); + assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBigDecimal); assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo")); assertThrows(IllegalStateException.class, () -> node.getBigInteger(1L)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(false)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(node)); + assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBigInteger); assertThrows(IllegalStateException.class, () -> node.getBoolean("foo")); assertThrows(IllegalStateException.class, () -> node.getBoolean(1L)); assertThrows(IllegalStateException.class, () -> node.getBoolean(false)); assertThrows(IllegalStateException.class, () -> node.getBoolean(node)); + assertThrows(IllegalStateException.class, () -> node.getBoolean("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBoolean); assertThrows(IllegalStateException.class, () -> node.getByte("foo")); assertThrows(IllegalStateException.class, () -> node.getByte(1L)); assertThrows(IllegalStateException.class, () -> node.getByte(false)); assertThrows(IllegalStateException.class, () -> node.getByte(node)); + assertThrows(IllegalStateException.class, () -> node.getByte("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getByte); assertThrows(IllegalStateException.class, () -> node.getDouble("foo")); assertThrows(IllegalStateException.class, () -> node.getDouble(1L)); assertThrows(IllegalStateException.class, () -> node.getDouble(false)); assertThrows(IllegalStateException.class, () -> node.getDouble(node)); + assertThrows(IllegalStateException.class, () -> node.getDouble("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getDouble); assertThrows(IllegalStateException.class, () -> node.getFloat("foo")); assertThrows(IllegalStateException.class, () -> node.getFloat(1L)); assertThrows(IllegalStateException.class, () -> node.getFloat(false)); assertThrows(IllegalStateException.class, () -> node.getFloat(node)); + assertThrows(IllegalStateException.class, () -> node.getFloat("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getFloat); assertThrows(IllegalStateException.class, () -> node.getInstant("foo")); assertThrows(IllegalStateException.class, () -> node.getInstant(1L)); assertThrows(IllegalStateException.class, () -> node.getInstant(false)); assertThrows(IllegalStateException.class, () -> node.getInstant(node)); + assertThrows(IllegalStateException.class, () -> node.getInstant("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getInstant); assertThrows(IllegalStateException.class, () -> node.getInt("foo")); assertThrows(IllegalStateException.class, () -> node.getInt(1L)); assertThrows(IllegalStateException.class, () -> node.getInt(false)); assertThrows(IllegalStateException.class, () -> node.getInt(node)); + assertThrows(IllegalStateException.class, () -> node.getInt("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getInt); - assertThrows(IllegalStateException.class, () -> node.getList("foo", String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(1L, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(false, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(node, String.class)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class)); assertThrows(IllegalStateException.class, () -> node.getLong("foo")); assertThrows(IllegalStateException.class, () -> node.getLong(1L)); assertThrows(IllegalStateException.class, () -> node.getLong(false)); assertThrows(IllegalStateException.class, () -> node.getLong(node)); + assertThrows(IllegalStateException.class, () -> node.getLong("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getLong); assertThrows(IllegalStateException.class, () -> node.getNumber("foo")); assertThrows(IllegalStateException.class, () -> node.getNumber(1L)); assertThrows(IllegalStateException.class, () -> node.getNumber(false)); assertThrows(IllegalStateException.class, () -> node.getNumber(node)); + assertThrows(IllegalStateException.class, () -> node.getNumber("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getNumber); - assertThrows(IllegalStateException.class, () -> node.getMap("foo", String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(1L, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(false, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(node, String.class, String.class)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class)); assertThrows(IllegalStateException.class, () -> node.getNdArray("foo")); assertThrows(IllegalStateException.class, () -> node.getNdArray(1L)); assertThrows(IllegalStateException.class, () -> node.getNdArray(false)); assertThrows(IllegalStateException.class, () -> node.getNdArray(node)); + assertThrows(IllegalStateException.class, () -> node.getNdArray("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getNdArray); assertThrows(IllegalStateException.class, () -> node.getShort("foo")); assertThrows(IllegalStateException.class, () -> node.getShort(1L)); assertThrows(IllegalStateException.class, () -> node.getShort(false)); assertThrows(IllegalStateException.class, () -> node.getShort(node)); + assertThrows(IllegalStateException.class, () -> node.getShort("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getShort); assertThrows(IllegalStateException.class, () -> node.getString("foo")); assertThrows(IllegalStateException.class, () -> node.getString(1L)); assertThrows(IllegalStateException.class, () -> node.getString(false)); assertThrows(IllegalStateException.class, () -> node.getString(node)); + assertThrows(IllegalStateException.class, () -> node.getString("foo", 1L, false, node)); + assertEquals("foo", node.getString()); assertThrows(IllegalStateException.class, node::asBigDecimal); assertThrows(IllegalStateException.class, node::asBigInteger); diff --git a/asdf-core/src/test/java/org/asdfformat/asdf/node/TimestampAsdfNodeTest.java b/asdf-core/src/test/java/org/asdfformat/asdf/node/TimestampAsdfNodeTest.java index 8c240c6..255af67 100644 --- a/asdf-core/src/test/java/org/asdfformat/asdf/node/TimestampAsdfNodeTest.java +++ b/asdf-core/src/test/java/org/asdfformat/asdf/node/TimestampAsdfNodeTest.java @@ -14,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -63,6 +64,7 @@ class ConstructionFromSnakeYamlNode { final String value = "2001-12-15T02:59:43.100Z"; final Instant instant = Instant.parse(value); + @Test void testBasic() { final ScalarNode snakeYamlNode = new ScalarNode(Tag.TIMESTAMP, value, null, null, DumperOptions.ScalarStyle.LITERAL); final TimestampAsdfNode asdfNode = TimestampAsdfNode.of(snakeYamlNode, instant); @@ -98,7 +100,8 @@ void testNodeType() { @Test void testBaseMethods() { - final TimestampAsdfNode node = TimestampAsdfNode.of(Instant.now()); + final Instant instant = Instant.now(); + final TimestampAsdfNode node = TimestampAsdfNode.of(instant); assertFalse(node.isBoolean()); assertFalse(node.isMapping()); @@ -113,6 +116,8 @@ void testBaseMethods() { assertFalse(node.containsKey(1L)); assertFalse(node.containsKey(false)); assertFalse(node.containsKey(node)); + assertFalse(node.containsKey("foo", 1L, false, node)); + assertTrue(node.containsKey()); assertEquals(0, node.size()); @@ -122,81 +127,113 @@ void testBaseMethods() { assertThrows(IllegalStateException.class, () -> node.get(1L)); assertThrows(IllegalStateException.class, () -> node.get(false)); assertThrows(IllegalStateException.class, () -> node.get(node)); + assertThrows(IllegalStateException.class, () -> node.get("foo", 1L, false, node)); + assertSame(node, node.get()); assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo")); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(1L)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(false)); assertThrows(IllegalStateException.class, () -> node.getBigDecimal(node)); + assertThrows(IllegalStateException.class, () -> node.getBigDecimal("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBigDecimal); assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo")); assertThrows(IllegalStateException.class, () -> node.getBigInteger(1L)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(false)); assertThrows(IllegalStateException.class, () -> node.getBigInteger(node)); + assertThrows(IllegalStateException.class, () -> node.getBigInteger("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBigInteger); assertThrows(IllegalStateException.class, () -> node.getBoolean("foo")); assertThrows(IllegalStateException.class, () -> node.getBoolean(1L)); assertThrows(IllegalStateException.class, () -> node.getBoolean(false)); assertThrows(IllegalStateException.class, () -> node.getBoolean(node)); + assertThrows(IllegalStateException.class, () -> node.getBoolean("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getBoolean); assertThrows(IllegalStateException.class, () -> node.getByte("foo")); assertThrows(IllegalStateException.class, () -> node.getByte(1L)); assertThrows(IllegalStateException.class, () -> node.getByte(false)); assertThrows(IllegalStateException.class, () -> node.getByte(node)); + assertThrows(IllegalStateException.class, () -> node.getByte("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getByte); assertThrows(IllegalStateException.class, () -> node.getDouble("foo")); assertThrows(IllegalStateException.class, () -> node.getDouble(1L)); assertThrows(IllegalStateException.class, () -> node.getDouble(false)); assertThrows(IllegalStateException.class, () -> node.getDouble(node)); + assertThrows(IllegalStateException.class, () -> node.getDouble("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getDouble); assertThrows(IllegalStateException.class, () -> node.getFloat("foo")); assertThrows(IllegalStateException.class, () -> node.getFloat(1L)); assertThrows(IllegalStateException.class, () -> node.getFloat(false)); assertThrows(IllegalStateException.class, () -> node.getFloat(node)); + assertThrows(IllegalStateException.class, () -> node.getFloat("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getFloat); assertThrows(IllegalStateException.class, () -> node.getInstant("foo")); assertThrows(IllegalStateException.class, () -> node.getInstant(1L)); assertThrows(IllegalStateException.class, () -> node.getInstant(false)); assertThrows(IllegalStateException.class, () -> node.getInstant(node)); + assertThrows(IllegalStateException.class, () -> node.getInstant("foo", 1L, false, node)); + assertEquals(instant, node.getInstant()); assertThrows(IllegalStateException.class, () -> node.getInt("foo")); assertThrows(IllegalStateException.class, () -> node.getInt(1L)); assertThrows(IllegalStateException.class, () -> node.getInt(false)); assertThrows(IllegalStateException.class, () -> node.getInt(node)); + assertThrows(IllegalStateException.class, () -> node.getInt("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getInt); - assertThrows(IllegalStateException.class, () -> node.getList("foo", String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(1L, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(false, String.class)); - assertThrows(IllegalStateException.class, () -> node.getList(node, String.class)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getList(String.class)); assertThrows(IllegalStateException.class, () -> node.getLong("foo")); assertThrows(IllegalStateException.class, () -> node.getLong(1L)); assertThrows(IllegalStateException.class, () -> node.getLong(false)); assertThrows(IllegalStateException.class, () -> node.getLong(node)); + assertThrows(IllegalStateException.class, () -> node.getLong("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getLong); assertThrows(IllegalStateException.class, () -> node.getNumber("foo")); assertThrows(IllegalStateException.class, () -> node.getNumber(1L)); assertThrows(IllegalStateException.class, () -> node.getNumber(false)); assertThrows(IllegalStateException.class, () -> node.getNumber(node)); + assertThrows(IllegalStateException.class, () -> node.getNumber("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getNumber); - assertThrows(IllegalStateException.class, () -> node.getMap("foo", String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(1L, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(false, String.class, String.class)); - assertThrows(IllegalStateException.class, () -> node.getMap(node, String.class, String.class)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo")); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, 1L)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, false)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class, "foo", 1L, false, node)); + assertThrows(IllegalStateException.class, () -> node.getMap(String.class, String.class)); assertThrows(IllegalStateException.class, () -> node.getNdArray("foo")); assertThrows(IllegalStateException.class, () -> node.getNdArray(1L)); assertThrows(IllegalStateException.class, () -> node.getNdArray(false)); assertThrows(IllegalStateException.class, () -> node.getNdArray(node)); + assertThrows(IllegalStateException.class, () -> node.getNdArray("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getNdArray); assertThrows(IllegalStateException.class, () -> node.getShort("foo")); assertThrows(IllegalStateException.class, () -> node.getShort(1L)); assertThrows(IllegalStateException.class, () -> node.getShort(false)); assertThrows(IllegalStateException.class, () -> node.getShort(node)); + assertThrows(IllegalStateException.class, () -> node.getShort("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getShort); assertThrows(IllegalStateException.class, () -> node.getString("foo")); assertThrows(IllegalStateException.class, () -> node.getString(1L)); assertThrows(IllegalStateException.class, () -> node.getString(false)); assertThrows(IllegalStateException.class, () -> node.getString(node)); + assertThrows(IllegalStateException.class, () -> node.getString("foo", 1L, false, node)); + assertThrows(IllegalStateException.class, node::getString); assertThrows(IllegalStateException.class, node::asBigDecimal); assertThrows(IllegalStateException.class, node::asBigInteger); diff --git a/asdf-core/src/test/java/org/asdfformat/asdf/node/VarargMethodTest.java b/asdf-core/src/test/java/org/asdfformat/asdf/node/VarargMethodTest.java new file mode 100644 index 0000000..75ff65b --- /dev/null +++ b/asdf-core/src/test/java/org/asdfformat/asdf/node/VarargMethodTest.java @@ -0,0 +1,274 @@ +package org.asdfformat.asdf.node; + +import org.asdfformat.asdf.node.impl.BooleanAsdfNode; +import org.asdfformat.asdf.node.impl.MappingAsdfNode; +import org.asdfformat.asdf.node.impl.NdArrayAsdfNode; +import org.asdfformat.asdf.node.impl.NumberAsdfNode; +import org.asdfformat.asdf.node.impl.SequenceAsdfNode; +import org.asdfformat.asdf.node.impl.StringAsdfNode; +import org.asdfformat.asdf.node.impl.TimestampAsdfNode; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.nodes.MappingNode; +import org.yaml.snakeyaml.nodes.SequenceNode; +import org.yaml.snakeyaml.nodes.Tag; + +import java.math.BigDecimal; +import java.time.Instant; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class VarargMethodTest { + private AsdfNode leafNode; + private AsdfNode sequenceNode; + private AsdfNode rootNode; + + @BeforeEach + void beforeEach() { + final Map leafMap = new HashMap<>(); + leafMap.put(StringAsdfNode.of("string"), StringAsdfNode.of("string key value")); + leafMap.put(BooleanAsdfNode.of(true), StringAsdfNode.of("boolean key value")); + leafMap.put(NumberAsdfNode.of(1), StringAsdfNode.of("number key value")); + + leafMap.put(StringAsdfNode.of("big decimal"), NumberAsdfNode.of(BigDecimal.TEN)); + leafMap.put(StringAsdfNode.of("boolean"), BooleanAsdfNode.of(true)); + leafMap.put(StringAsdfNode.of("byte"), NumberAsdfNode.of(0x1F)); + leafMap.put(StringAsdfNode.of("double"), NumberAsdfNode.of(Double.MAX_VALUE)); + leafMap.put(StringAsdfNode.of("float"), NumberAsdfNode.of(Float.MAX_VALUE)); + leafMap.put(StringAsdfNode.of("instant"), TimestampAsdfNode.of(Instant.EPOCH)); + leafMap.put(StringAsdfNode.of("int"), NumberAsdfNode.of(Integer.MAX_VALUE)); + + leafMap.put(StringAsdfNode.of("list"), SequenceAsdfNode.of( + new SequenceNode(Tag.SEQ, Collections.emptyList(), DumperOptions.FlowStyle.FLOW), + Stream.of("a", "b", "c").map(StringAsdfNode::of).collect(Collectors.toList()) + )); + + leafMap.put(StringAsdfNode.of("long"), NumberAsdfNode.of(Long.MAX_VALUE)); + + leafMap.put(StringAsdfNode.of("map"), MappingAsdfNode.of( + new MappingNode(Tag.MAP, Collections.emptyList(), DumperOptions.FlowStyle.FLOW), + Stream.of("a", "b", "c").collect(Collectors.toMap( + StringAsdfNode::of, + v -> StringAsdfNode.of(v + " value") + )) + )); + + final NdArrayAsdfNode ndArrayAsdfNode = mock(); + when(ndArrayAsdfNode.get()).thenReturn(ndArrayAsdfNode); + leafMap.put(StringAsdfNode.of("ndarray"), ndArrayAsdfNode); + + leafMap.put(StringAsdfNode.of("number"), NumberAsdfNode.of(14)); + leafMap.put(StringAsdfNode.of("short"), NumberAsdfNode.of(Short.MAX_VALUE)); + + leafNode = MappingAsdfNode.of( + new MappingNode(Tag.MAP, Collections.emptyList(), DumperOptions.FlowStyle.FLOW), + leafMap + ); + + sequenceNode = SequenceAsdfNode.of( + new SequenceNode(Tag.SEQ, Collections.emptyList(), DumperOptions.FlowStyle.FLOW), + Stream.of(leafNode).collect(Collectors.toList()) + ); + + final Map rootMap = new HashMap<>(); + rootMap.put(StringAsdfNode.of("sequence"), sequenceNode); + + rootNode = MappingAsdfNode.of( + new MappingNode(Tag.MAP, Collections.emptyList(), DumperOptions.FlowStyle.FLOW), + rootMap + ); + } + + @Nested + class ContainsKey { + @Test + void testNoArgs() { + assertTrue(rootNode.containsKey()); + } + + @Test + void testFirstLevel() { + assertTrue(rootNode.containsKey("sequence")); + assertFalse(rootNode.containsKey("missing")); + } + + @Test + void testSecondLevel() { + assertTrue(rootNode.containsKey("sequence", 0)); + assertFalse(rootNode.containsKey("sequence", 1)); + } + + @Test + void testThirdLevel() { + assertTrue(rootNode.containsKey("sequence", 0, "string")); + assertFalse(rootNode.containsKey("sequence", 0, "missing")); + } + } + + @Nested + class Get { + @Test + void testNoArgs() { + assertSame(rootNode, rootNode.get()); + } + + @Test + void testFirstLevel() { + assertSame(sequenceNode, rootNode.get("sequence")); + assertThrows(IllegalArgumentException.class, () -> rootNode.get("missing")); + } + + @Test + void testSecondLevel() { + assertSame(leafNode, rootNode.get("sequence", 0)); + assertThrows(IndexOutOfBoundsException.class, () -> rootNode.get("sequence", 1)); + } + + @Test + void testThirdLevel() { + assertSame(leafNode.get("string"), rootNode.get("sequence", 0, "string")); + assertThrows(IllegalArgumentException.class, () -> rootNode.get("sequence", 0, "missing")); + } + } + + @Nested + class GetOptional { + @Test + void testNoArgs() { + assertEquals(Optional.of(rootNode), rootNode.getOptional()); + } + + @Test + void testFirstLevel() { + assertEquals(Optional.of(sequenceNode), rootNode.getOptional("sequence")); + assertFalse(rootNode.getOptional("missing").isPresent()); + } + + @Test + void testSecondLevel() { + assertEquals(Optional.of(leafNode), rootNode.getOptional("sequence", 0)); + assertFalse(rootNode.getOptional("sequence", 1).isPresent()); + } + + @Test + void testThirdLevel() { + assertEquals(Optional.of(leafNode.get("string")), rootNode.getOptional("sequence", 0, "string")); + assertFalse(rootNode.getOptional("sequence", 0, "missing").isPresent()); + } + } + + @Nested + class GetTyped { + @Test + void testGetBigDecimal() { + assertEquals(leafNode.getBigDecimal("big decimal"), rootNode.getBigDecimal("sequence", 0, "big decimal")); + } + + @Test + void testGetBoolean() { + assertEquals(leafNode.getBoolean("boolean"), rootNode.getBoolean("sequence", 0, "boolean")); + } + + @Test + void testGetByte() { + assertEquals(leafNode.getByte("byte"), rootNode.getByte("sequence", 0, "byte")); + } + + @Test + void testGetDouble() { + assertEquals(leafNode.getDouble("double"), rootNode.getDouble("sequence", 0, "double")); + } + + @Test + void testGetFloat() { + assertEquals(leafNode.getFloat("float"), rootNode.getFloat("sequence", 0, "float")); + } + + @Test + void testGetInstant() { + assertEquals(leafNode.getInstant("instant"), rootNode.getInstant("sequence", 0, "instant")); + } + + @Test + void testGetInt() { + assertEquals(leafNode.getInt("int"), rootNode.getInt("sequence", 0, "int")); + } + + @Test + void testGetList() { + assertEquals(leafNode.getList(String.class, "list"), rootNode.getList(String.class, "sequence", 0, "list")); + } + + @Test + void testGetLong() { + assertEquals(leafNode.getLong("long"), rootNode.getLong("sequence", 0, "long")); + } + + @Test + void testGetMap() { + assertEquals(leafNode.getMap(String.class, String.class, "map"), rootNode.getMap(String.class, String.class, "sequence", 0, "map")); + } + + @Test + void testGetNdArray() { + assertEquals(leafNode.getNdArray("ndarray"), rootNode.getNdArray("sequence", 0, "ndarray")); + } + + @Test + void testGetNumber() { + assertEquals(leafNode.getNumber("number"), rootNode.getNumber("sequence", 0, "number")); + } + + @Test + void testGetShort() { + assertEquals(leafNode.getShort("short"), rootNode.getShort("sequence", 0, "short")); + } + + @Test + void testGetString() { + assertEquals(leafNode.getString("string"), rootNode.getString("sequence", 0, "string")); + } + } + + @Nested + class KeyTypes { + @Test + void testStringKey() { + assertEquals(leafNode.getString("string"), rootNode.getString("sequence", 0, "string")); + } + + @Test + void testBooleanKey() { + assertEquals(leafNode.getString(true), rootNode.getString("sequence", 0, true)); + } + + @Test + void testNumberKey() { + assertEquals(leafNode.getString(1), rootNode.getString("sequence", 0, 1)); + } + + @Test + void testAsdfNodeKey() { + assertEquals(leafNode.getString(NumberAsdfNode.of(1)), rootNode.getString("sequence", 0, NumberAsdfNode.of(1))); + } + + @Test + void testInvalidKeyType() { + assertThrows(IllegalArgumentException.class, () -> leafNode.getString(new Object())); + } + } +}