Skip to content

Commit

Permalink
Minor code re-factoring, coverage improvements, and assertion reduction
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Apr 18, 2023
1 parent 58e8efc commit 089972b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 76 deletions.
28 changes: 10 additions & 18 deletions src/main/java/io/xlate/edi/internal/stream/StaEDIStreamWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,26 @@ private static void ensureArgs(int arrayLength, int start, int end) {
}
}

private void ensureState(State s) {
if (this.state != s) {
private void ensureFalse(boolean illegalState) {
if (illegalState) {
throw new IllegalStateException();
}
}

private void ensureState(State s) {
ensureFalse(this.state != s);
}

private void ensureLevel(int l) {
if (this.level != l) {
throw new IllegalStateException();
}
ensureFalse(this.level != l);
}

private void ensureLevelAtLeast(int lvl) {
if (this.level < lvl) {
throw new IllegalStateException();
}
ensureFalse(this.level < lvl);
}

private void ensureLevelBetween(int min, int max) {
if (this.level < min || this.level > max) {
throw new IllegalStateException();
}
ensureFalse(this.level < min || this.level > max);
}

@Override
Expand Down Expand Up @@ -366,9 +364,6 @@ void writeHeader(char output, boolean isPrettyPrint) throws EDIStreamException {
case HEADER_ELEMENT_END:
state = State.ELEMENT_END;
break;
case HEADER_COMPONENT_END:
state = State.COMPONENT_END;
break;
case HEADER_SEGMENT_END:
state = State.SEGMENT_END;
break;
Expand Down Expand Up @@ -627,10 +622,7 @@ public EDIStreamWriter endElement() throws EDIStreamException {
@Override
public EDIStreamWriter startComponent() throws EDIStreamException {
ensureLevelBetween(LEVEL_ELEMENT, LEVEL_COMPOSITE);

if (state == State.ELEMENT_DATA_BINARY) {
throw new IllegalStateException();
}
ensureFalse(state == State.ELEMENT_DATA_BINARY);

if (LEVEL_ELEMENT == level) {
// Level is LEVEL_ELEMENT only for the first component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ int validate(Dialect dialect, CharSequence value) {

case '-':
length--;
invalid = !validNegativeSymbol(i, value, invalid);
invalid = invalidNegativeSymbol(i, value, invalid);
break;

default:
if (dialect.isDecimalMark(value.charAt(i))) {
length--;
invalid = !validDecimalSymbol(++dec, exp, invalid);
invalid = invalidDecimalSymbol(++dec, exp, invalid);
} else {
invalid = true;
}
Expand All @@ -78,19 +78,11 @@ int validate(Dialect dialect, CharSequence value) {
return invalid ? -length : length;
}

boolean validNegativeSymbol(int currentIndex, CharSequence value, boolean currentlyInvalid) {
if (currentlyInvalid) {
return false;
}

return currentIndex == 0 || value.charAt(currentIndex - 1) == 'E';
boolean invalidNegativeSymbol(int currentIndex, CharSequence value, boolean currentlyInvalid) {
return currentlyInvalid || (currentIndex > 0 && value.charAt(currentIndex - 1) != 'E');
}

boolean validDecimalSymbol(int decimalCount, int exponentCount, boolean currentlyInvalid) {
if (currentlyInvalid) {
return false;
}

return !(decimalCount > 1 || exponentCount > 0);
boolean invalidDecimalSymbol(int decimalCount, int exponentCount, boolean currentlyInvalid) {
return currentlyInvalid || decimalCount > 1 || exponentCount > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import io.xlate.edi.internal.stream.tokenization.Dialect;
Expand Down Expand Up @@ -69,6 +71,13 @@ public static UsageNode getFirstChild(UsageNode node) {
return node != null ? node.getFirstChild() : null;
}

static <I, T, R> R withTypeOrElseGet(I reference, Class<T> type, Function<T, R> mapper, Supplier<R> defaultValue) {
if (type.isInstance(reference)) {
return mapper.apply(type.cast(reference));
}
return defaultValue.get();
}

public UsageNode getFirstSiblingSameType() {
EDIType type = getReferencedType();
UsageNode sibling = getFirstSibling();
Expand Down Expand Up @@ -131,18 +140,11 @@ boolean isImplementation() {
}

String getId() {
if (link instanceof EDITypeImplementation) {
return ((EDITypeImplementation) link).getId();
}

return link.getReferencedType().getId();
return withTypeOrElseGet(link, EDITypeImplementation.class, EDITypeImplementation::getId, link.getReferencedType()::getId);
}

EDISimpleType getSimpleType() {
if (link instanceof EDISimpleType) {
return (EDISimpleType) link;
}
return (EDISimpleType) link.getReferencedType();
return withTypeOrElseGet(link, EDISimpleType.class, EDISimpleType.class::cast, () -> (EDISimpleType) link.getReferencedType());
}

void validate(Dialect dialect, CharSequence value, List<EDIStreamValidationError> errors) {
Expand All @@ -155,12 +157,7 @@ void format(Dialect dialect, CharSequence value, StringBuilder result) {

List<EDISyntaxRule> getSyntaxRules() {
EDIType referencedNode = link.getReferencedType();

if (referencedNode instanceof EDIComplexType) {
return ((EDIComplexType) referencedNode).getSyntaxRules();
}

return Collections.emptyList();
return withTypeOrElseGet(referencedNode, EDIComplexType.class, EDIComplexType::getSyntaxRules, Collections::emptyList);
}

int getIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1382,13 +1382,7 @@ int getCurrentIndex(Location location, boolean isComposite) {
final int index;

if (isComposite) {
int componentPosition = location.getComponentPosition();

if (componentPosition < 1) {
index = 1;
} else {
index = componentPosition;
}
index = Math.max(location.getComponentPosition(), 1);
} else {
index = location.getElementPosition();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
******************************************************************************/
package io.xlate.edi.internal.stream;

import static io.xlate.edi.test.StaEDITestUtil.assertLocation;
import static io.xlate.edi.test.StaEDITestUtil.assertTextLocation;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -65,6 +67,7 @@
import io.xlate.edi.schema.SchemaFactory;
import io.xlate.edi.stream.EDIInputFactory;
import io.xlate.edi.stream.EDIStreamConstants.Delimiters;
import io.xlate.edi.test.StaEDITestUtil;
import io.xlate.edi.stream.EDIStreamEvent;
import io.xlate.edi.stream.EDIStreamException;
import io.xlate.edi.stream.EDIStreamReader;
Expand Down Expand Up @@ -1026,19 +1029,13 @@ void testGetLocation() throws EDIStreamException {
String tag = null;
int conditions = 0;

assertEquals(-1, reader.getLocation().getSegmentPosition());
assertEquals(-1, reader.getLocation().getElementPosition());
assertEquals(-1, reader.getLocation().getComponentPosition());
assertEquals(-1, reader.getLocation().getElementOccurrence());
assertLocation(reader, -1, -1, -1, -1);

while (reader.hasNext()) {
switch (reader.next()) {
case START_SEGMENT:
s += s == -1 ? 2 : 1;
assertEquals(s, reader.getLocation().getSegmentPosition());
assertEquals(-1, reader.getLocation().getElementPosition());
assertEquals(-1, reader.getLocation().getComponentPosition());
assertEquals(-1, reader.getLocation().getElementOccurrence());
assertLocation(reader, s, -1, -1, -1);
tag = reader.getText();
break;
case ELEMENT_DATA:
Expand All @@ -1047,23 +1044,21 @@ void testGetLocation() throws EDIStreamException {
if (l.getElementPosition() == 2) {
switch (l.getElementOccurrence()) {
case 1:
assertEquals("AK302-R1", reader.getText());
assertEquals(-1, l.getComponentPosition());
assertTextLocation(reader, "AK302-R1", 6, 2, 1, -1);
conditions++;
break;
case 2:
assertEquals("AK302-R2", reader.getText());
assertEquals(-1, l.getComponentPosition());
assertTextLocation(reader, "AK302-R2", 6, 2, 2, -1);
conditions++;
break;
case 3:
switch (l.getComponentPosition()) {
case 1:
assertEquals("AK302-R3-COMP1", reader.getText());
assertTextLocation(reader, "AK302-R3-COMP1", 6, 2, 3, 1);
conditions++;
break;
case 2:
assertEquals("AK302-R3-COMP2", reader.getText());
assertTextLocation(reader, "AK302-R3-COMP2", 6, 2, 3, 2);
conditions++;
break;
default:
Expand All @@ -1074,24 +1069,20 @@ void testGetLocation() throws EDIStreamException {
} else if (l.getElementPosition() == 4) {
switch (l.getElementOccurrence()) {
case 1:
assertEquals("AK304-R1", reader.getText());
assertEquals(-1, l.getComponentPosition());
assertTextLocation(reader, "AK304-R1", 6, 4, 1, -1);
conditions++;
break;
case 2:
assertEquals("AK304-R2", reader.getText());
assertEquals(-1, l.getComponentPosition());
assertTextLocation(reader, "AK304-R2", 6, 4, 2, -1);
conditions++;
break;
case 3:
assertEquals("AK304-R3", reader.getText());
assertEquals(-1, l.getComponentPosition());
assertTextLocation(reader, "AK304-R3", 6, 4, 3, -1);
conditions++;
break;
}
} else {
assertEquals(-1, l.getComponentPosition());
assertEquals(1, l.getElementOccurrence());
assertLocation(reader, 6, l.getElementPosition(), 1, -1);
}
} else if ("AK4".equals(tag)) {
if (l.getElementPosition() == 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ void testInvalidBooleanPropertyIsFalse() {
assertEquals(false, ((StaEDIStreamWriter) writer).emptyElementTruncation);
}

@Test
void testGetDelimitersIllegal() throws EDIStreamException {
EDIOutputFactory factory = EDIOutputFactory.newFactory();
OutputStream stream = new ByteArrayOutputStream(4096);
EDIStreamWriter writer = factory.createEDIStreamWriter(stream);
assertThrows(IllegalStateException.class, () -> writer.getDelimiters());
}

@Test
void testStartInterchange() {
try {
Expand Down Expand Up @@ -236,6 +244,15 @@ void testWriteEndSegmentIllegal() throws EDIStreamException {
assertThrows(IllegalStateException.class, () -> writer.writeEndSegment());
}

@Test
void testStartComponentIllegal() throws EDIStreamException {
EDIOutputFactory factory = EDIOutputFactory.newFactory();
OutputStream stream = new ByteArrayOutputStream(4096);
EDIStreamWriter writer = factory.createEDIStreamWriter(stream);
writer.startInterchange();
assertThrows(IllegalStateException.class, () -> writer.startComponent());
}

@Test
void testWriteStartElement() throws EDIStreamException {
EDIOutputFactory factory = EDIOutputFactory.newFactory();
Expand Down Expand Up @@ -342,6 +359,32 @@ void testWriteStartElementBinaryIllegal() throws IllegalStateException, EDIStrea
assertThrows(IllegalStateException.class, () -> writer.writeStartElementBinary());
}

@Test
void testWriteBinaryDataIllegal() throws IllegalStateException, EDIStreamException {
EDIOutputFactory factory = EDIOutputFactory.newFactory();
OutputStream stream = new ByteArrayOutputStream(4096);
EDIStreamWriter writer = factory.createEDIStreamWriter(stream);
writer.startInterchange();
writer.writeStartSegment("ISA");
writer.writeStartElement();
ByteBuffer binaryData = ByteBuffer.allocate(1);
assertThrows(IllegalStateException.class, () -> writer.writeBinaryData(binaryData));
}

@Test
void testStartComponentIllegalInElementBinary() throws IllegalStateException, EDIStreamException {
EDIOutputFactory factory = EDIOutputFactory.newFactory();
ByteArrayOutputStream stream = new ByteArrayOutputStream(4096);
EDIStreamWriter writer = factory.createEDIStreamWriter(stream);
writer.startInterchange();
writeHeader(writer);
writer.flush();
stream.reset();
writer.writeStartSegment("BIN");
writer.writeStartElementBinary();
assertThrows(IllegalStateException.class, () -> writer.startComponent());
}

@Test
void testComponent() throws IllegalStateException, EDIStreamException {
EDIOutputFactory factory = EDIOutputFactory.newFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ void testValidateExponentialsInvalid() {
assertEquals(-6, v.validate(dialectEdifact, "1,234E-5,6"));
assertEquals(-6, v.validate(dialectEdifact, "-1,234E-5E-6"));
assertEquals(-2, v.validate(dialectEdifact, "1E--2,"));
assertEquals(-6, v.validate(dialectEdifact, "1E,234E-5,6"));

assertEquals(-6, v.validate(dialectX12, "1.234E-5.6"));
assertEquals(-6, v.validate(dialectX12, "-1.234E-5E-6"));
assertEquals(-2, v.validate(dialectX12, "1E--2."));
assertEquals(-6, v.validate(dialectX12, "1E.234E-5.6"));
}

@Test
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/io/xlate/edi/test/StaEDITestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ public static void assertEvent(EDIStreamReader reader, EDIStreamEvent event, EDI
assertEquals(referenceCode, reader.getReferenceCode());
}

public static void assertLocation(EDIStreamReader reader, int segPos, int elePos, int eleOcc, int cmpPos)
throws EDIStreamException {
assertEquals(segPos, reader.getLocation().getSegmentPosition());
assertEquals(elePos, reader.getLocation().getElementPosition());
assertEquals(eleOcc, reader.getLocation().getElementOccurrence());
assertEquals(cmpPos, reader.getLocation().getComponentPosition());
}

public static void assertTextLocation(EDIStreamReader reader, String text, int segPos, int elePos, int eleOcc, int cmpPos)
throws EDIStreamException {
assertEquals(text, reader.getText());
assertEquals(segPos, reader.getLocation().getSegmentPosition());
assertEquals(elePos, reader.getLocation().getElementPosition());
assertEquals(eleOcc, reader.getLocation().getElementOccurrence());
assertEquals(cmpPos, reader.getLocation().getComponentPosition());
}

public static String[] getJavaVersion() {
String versionString = System.getProperty("java.version");
return versionString.split("[\\._]");
Expand Down

0 comments on commit 089972b

Please sign in to comment.