Skip to content

Commit

Permalink
Ensure State is a known dialect when checking State.isHeaderState (#…
Browse files Browse the repository at this point in the history
…332)

* Test case to illustrate exception

```
io.xlate.edi.stream.EDIStreamException: Invalid X12 ISA segment: too short or elements missing
 at io.xlate.edi.internal.stream.StaEDIStreamWriter.writeEndSegment(StaEDIStreamWriter.java:549)
 at io.xlate.edi.internal.stream.StaEDIStreamWriterTest.writeFromReader(StaEDIStreamWriterTest.java:757)
 at io.xlate.edi.internal.stream.StaEDIStreamWriterTest.testInputEquivalence(StaEDIStreamWriterTest.java:717)
```

* Ensure State is a known dialect when checking `State.isHeaderState`

Signed-off-by: Michael Edgar <[email protected]>

---------

Signed-off-by: Michael Edgar <[email protected]>
Co-authored-by: Michael Edgar <[email protected]>
  • Loading branch information
hoffrocket and MikeEdgar authored Feb 13, 2023
1 parent 34e231d commit 16feb07
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public State transition(int dialect, CharacterClass clazz) {
}

public boolean isHeaderState() {
return Category.HEADER == code;
return Category.HEADER == code && DialectCode.UNKNOWN != table;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,13 @@ void testWriteBinaryDataByteBuffer() throws EDIStreamException {

@ParameterizedTest
@CsvSource({
"X12 with BIN Segment, /x12/sample275_with_HL7_valid_BIN01.edi, false",
"X12 with Non-ASCII Segment Terminator, /x12/issue109/ts214_ellipses_segterm.edi, false",
"Basic TRADACOMS, /TRADACOMS/order.edi, false",
"Basic TRADACOMS 40 Blocked, /TRADACOMS/order-blocked.edi, true",
"X12 837, /x12/sample837-original.edi, false, true",
"X12 with BIN Segment, /x12/sample275_with_HL7_valid_BIN01.edi, false, false",
"X12 with Non-ASCII Segment Terminator, /x12/issue109/ts214_ellipses_segterm.edi, false, false",
"Basic TRADACOMS, /TRADACOMS/order.edi, false, false",
"Basic TRADACOMS 40 Blocked, /TRADACOMS/order-blocked.edi, true, false",
})
void testInputEquivalence(String title, String resourceName, boolean blocked) throws Exception {
void testInputEquivalence(String title, String resourceName, boolean blocked, boolean indented) throws Exception {
EDIInputFactory inputFactory = EDIInputFactory.newFactory();
if (blocked) {
inputFactory.setProperty(EDIInputFactory.EDI_IGNORE_EXTRANEOUS_CHARACTERS, "true");
Expand Down Expand Up @@ -715,7 +716,8 @@ public int read() throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream(16384);
writeFromReader(reader, blocked, result);
final String expected = Stream.of(readBuffer.toString("UTF-8"))
.map(StaEDITestUtil::normalizeLines)
.map(StaEDITestUtil::normalizeLines)
.map(exp -> indented ? exp.replaceAll("(?m)^\\s+", "") : exp) // Remove leading spaces in indented examples
.map(exp -> blocked ? exp.replace("\n", "") : exp)
.map(String::trim)
.findFirst()
Expand Down

0 comments on commit 16feb07

Please sign in to comment.