From ad53cb1b703cd953b612b8c57bc5daccb338d551 Mon Sep 17 00:00:00 2001 From: Michael Edgar Date: Fri, 5 Jun 2020 16:45:07 -0400 Subject: [PATCH] Test Discriminator identity methods, error handling test cases --- .../schema/implementation/BaseImpl.java | 2 +- .../implementation/DiscriminatorImpl.java | 6 ++ .../schema/StaEDISchemaFactoryTest.java | 23 ++++++++ .../implementation/DiscriminatorImplTest.java | 59 +++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/xlate/edi/internal/schema/implementation/DiscriminatorImplTest.java diff --git a/src/main/java/io/xlate/edi/internal/schema/implementation/BaseImpl.java b/src/main/java/io/xlate/edi/internal/schema/implementation/BaseImpl.java index 2d0cdd0b..aeaaba21 100644 --- a/src/main/java/io/xlate/edi/internal/schema/implementation/BaseImpl.java +++ b/src/main/java/io/xlate/edi/internal/schema/implementation/BaseImpl.java @@ -26,7 +26,7 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (o == null || !getClass().equals(o.getClass())) { + if (!getClass().isInstance(o)) { return false; } diff --git a/src/main/java/io/xlate/edi/internal/schema/implementation/DiscriminatorImpl.java b/src/main/java/io/xlate/edi/internal/schema/implementation/DiscriminatorImpl.java index d7ca44ef..6ae85ebc 100644 --- a/src/main/java/io/xlate/edi/internal/schema/implementation/DiscriminatorImpl.java +++ b/src/main/java/io/xlate/edi/internal/schema/implementation/DiscriminatorImpl.java @@ -7,6 +7,7 @@ public class DiscriminatorImpl implements Discriminator { + private static final String TOSTRING_FORMAT = "position: [%d, %d], values: %s"; private final int elementPosition; private final int componentPosition; private final Set valueSet; @@ -40,6 +41,11 @@ public int hashCode() { return Objects.hash(elementPosition, componentPosition, valueSet); } + @Override + public String toString() { + return String.format(TOSTRING_FORMAT, elementPosition, componentPosition, valueSet); + } + @Override public int getElementPosition() { return elementPosition; diff --git a/src/test/java/io/xlate/edi/internal/schema/StaEDISchemaFactoryTest.java b/src/test/java/io/xlate/edi/internal/schema/StaEDISchemaFactoryTest.java index d6dcfc20..35b32f5b 100644 --- a/src/test/java/io/xlate/edi/internal/schema/StaEDISchemaFactoryTest.java +++ b/src/test/java/io/xlate/edi/internal/schema/StaEDISchemaFactoryTest.java @@ -17,12 +17,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; 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 java.io.ByteArrayInputStream; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URL; import java.util.List; import java.util.Objects; @@ -296,6 +298,13 @@ void testGetControlSchema() throws EDISchemaException { assertEquals(EDIType.Type.SEGMENT, schema.getType("ST").getType()); } + @Test + void testGetControlSchema_NotFound() throws EDISchemaException { + SchemaFactory factory = SchemaFactory.newFactory(); + Schema schema = factory.getControlSchema(Standards.EDIFACT, new String[] { "UNOA", "0" }); + assertNull(schema); + } + @Test void testReferenceUndeclared() { SchemaFactory factory = SchemaFactory.newFactory(); @@ -556,4 +565,18 @@ void testValidIncludeV4() throws EDISchemaException { Schema schema = factory.createSchema(stream); assertNotNull(schema); } + + @Test + void testInvalidUrlIncludeV4() { + SchemaFactory factory = SchemaFactory.newFactory(); + InputStream stream = new ByteArrayInputStream(("" + + "" + + " " + + "").getBytes()); + + EDISchemaException thrown = assertThrows(EDISchemaException.class, () -> factory.createSchema(stream)); + assertEquals("Exception reading included schema", thrown.getOriginalMessage()); + assertTrue(thrown.getCause() instanceof StaEDISchemaReadException); + assertTrue(thrown.getCause().getCause() instanceof MalformedURLException); + } } diff --git a/src/test/java/io/xlate/edi/internal/schema/implementation/DiscriminatorImplTest.java b/src/test/java/io/xlate/edi/internal/schema/implementation/DiscriminatorImplTest.java new file mode 100644 index 00000000..f832b3b6 --- /dev/null +++ b/src/test/java/io/xlate/edi/internal/schema/implementation/DiscriminatorImplTest.java @@ -0,0 +1,59 @@ +package io.xlate.edi.internal.schema.implementation; + +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 java.util.Collections; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import io.xlate.edi.schema.implementation.Discriminator; + +class DiscriminatorImplTest { + + DiscriminatorImpl target; + + @BeforeEach + void setUp() throws Exception { + target = new DiscriminatorImpl(1, 2, Collections.singleton("50")); + } + + @Test + void testHashCode() { + int expected = new DiscriminatorImpl(1, 2, Collections.singleton("50")).hashCode(); + assertEquals(expected, target.hashCode()); + } + + @Test + void testEquals_Same() { + assertEquals(target, target); + } + + @Test + void testEquals_Identical() { + Discriminator identical = new DiscriminatorImpl(1, 2, Collections.singleton("50")); + assertEquals(target, identical); + } + + @Test + void testEquals_NotInstance() { + assertFalse(target.equals(null)); + assertFalse(target.equals(new Object())); + } + + @Test + void testEquals_Different() { + assertNotEquals(new DiscriminatorImpl(1, 2, Collections.singleton("60")), target); + assertNotEquals(new DiscriminatorImpl(2, 2, Collections.singleton("50")), target); + assertNotEquals(new DiscriminatorImpl(1, 3, Collections.singleton("50")), target); + } + + @Test + void testToString() { + String expected = new DiscriminatorImpl(1, 2, Collections.singleton("50")).toString(); + assertEquals(expected, target.toString()); + } + +}