Skip to content

Commit

Permalink
Test Discriminator identity methods, error handling test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeEdgar committed Jun 5, 2020
1 parent 077229b commit ad53cb1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> valueSet;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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((""
+ "<schema xmlns='" + StaEDISchemaFactory.XMLNS_V4 + "'>"
+ " <include schemaLocation='./src/test/resources/x12/EDISchema997.xml' />"
+ "</schema>").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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit ad53cb1

Please sign in to comment.