Skip to content

Commit

Permalink
Update tests to work with YAML wire #509 (#666)
Browse files Browse the repository at this point in the history
Add test for YAML method writer
Fix compatibility marshallable test
Fix nested maps test
Fix some reordering tests
Fix text binary wire test
Case insensitive field name matching to match TEXT behavior
Fix type literal parsing
Fixed handling of sequences
Partially support scenario where not all fields are read
Improve test coverage of tricky scenarios

Co-authored-by: Ilya Kaznacheev <ilya.kaznacheev@chronicle.software>
  • Loading branch information
alamar and Ilya Kaznacheev committed May 19, 2023
1 parent a2ee30a commit 76491d9
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public void readMarshallableInputOrder(T t, @NotNull WireIn in, T defaults, bool
}

public boolean matchesFieldName(StringBuilder sb, FieldAccess field) {
return sb.length() == 0 || StringUtils.isEqual(field.field.getName(), sb);
return sb.length() == 0 || StringUtils.equalsCaseIgnore(field.field.getName(), sb);
}

public void writeKey(T t, Bytes<?> bytes) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/openhft/chronicle/wire/YamlTokeniser.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void reset() {
blockQuote = 0;
hasSequenceEntry = false;
lastKeyPosition = -1;
pushed.clear();
last = YamlToken.STREAM_START;
pushContext0(YamlToken.STREAM_START, NO_INDENT);
}
Expand Down Expand Up @@ -660,6 +661,10 @@ public long lineStart() {
return lineStart;
}

public void lineStart(long lineStart) {
this.lineStart = lineStart;
}

public long blockStart() {
return blockStart;
}
Expand Down
42 changes: 32 additions & 10 deletions src/main/java/net/openhft/chronicle/wire/YamlWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public <T> T methodWriter(@NotNull Class<T> tClass, Class... additional) {
() -> newTextMethodWriterInvocationHandler(tClass));
for (Class aClass : additional)
builder.addInterface(aClass);
useTextDocuments();
builder.marshallableOut(this);
return builder.build();
}
Expand Down Expand Up @@ -252,6 +253,7 @@ protected void initReadContext() {
if (readContext == null)
useBinaryDocuments();
readContext.start();
yt.lineStart(bytes.readPosition());
}

@NotNull
Expand Down Expand Up @@ -453,6 +455,10 @@ protected StringBuilder readField(@NotNull StringBuilder sb) {
public <K> K readEvent(@NotNull Class<K> expectedClass) throws InvalidMarshallableException {
startEventIfTop();
switch (yt.current()) {
case MAPPING_START:
yt.next();
assert yt.current() == YamlToken.MAPPING_KEY;
// Deliberate fall-through
case MAPPING_KEY:
YamlToken next = yt.next();
if (next == YamlToken.MAPPING_KEY) {
Expand Down Expand Up @@ -539,6 +545,7 @@ public ValueIn read(String keyName) {
return rereadWire.valueIn;
}
}
// Next lines not covered by any tests
}
YamlTokeniser.YTContext yc = yt.topContext();
int minIndent = yc.indent;
Expand All @@ -550,7 +557,8 @@ public ValueIn read(String keyName) {

if (!StringUtils.startsWith(sb, "-"))
keys.push(lastKeyPosition);
valueIn.consumeAny(minIndent);
// Avoid consuming '}' but consume to next mapping key
valueIn.consumeAny(minIndent >= 0 ? minIndent : Integer.MAX_VALUE);
}

return defaultValueIn;
Expand Down Expand Up @@ -610,11 +618,10 @@ public ValueIn getValueIn() {
@NotNull
@Override
public Wire readComment(@NotNull StringBuilder s) {
sb.setLength(0);
s.setLength(0);
if (yt.current() == YamlToken.COMMENT) {
// Skip the initial '#'
YamlToken next = yt.next();
sb.append(yt.text());
s.append(yt.text());
yt.next();
}
return this;
}
Expand Down Expand Up @@ -753,11 +760,12 @@ public void endEvent() {
yt.next(Integer.MIN_VALUE);
} else {
while (yt.current() == YamlToken.MAPPING_KEY) {
yt.next();
valueIn.consumeAny(minIndent);
}
}
if (yt.current() == YamlToken.MAPPING_END || yt.current() == YamlToken.DOCUMENT_END || yt.current() == YamlToken.NONE) {
if (yt.current() == YamlToken.MAPPING_END ||
yt.current() == YamlToken.DOCUMENT_END ||
yt.current() == YamlToken.NONE) {
yt.next(Integer.MIN_VALUE);
return;
}
Expand Down Expand Up @@ -1344,7 +1352,6 @@ public <T, K> WireIn sequence(@NotNull T t, K kls, @NotNull TriConsumer<T, K, Va
while (true) {
switch (yt.current()) {
case SEQUENCE_ENTRY:
yt.next(Integer.MIN_VALUE);
tReader.accept(t, kls, YamlWire.this.valueIn);
continue;

Expand Down Expand Up @@ -1381,9 +1388,11 @@ public boolean hasNext() {
public boolean hasNextSequenceItem() {
consumePadding();
switch (yt.current()) {
// Perhaps should be negative selection instead of positive
case SEQUENCE_START:
case TEXT:
case SEQUENCE_ENTRY:
// Allows scalar value to be converted into singleton array
case TEXT:
return true;
}
return false;
Expand Down Expand Up @@ -1474,7 +1483,20 @@ String stringForCode(int code) {
@Override
public <T> WireIn typeLiteralAsText(T t, @NotNull BiConsumer<T, CharSequence> classNameConsumer)
throws IORuntimeException, BufferUnderflowException {
throw new UnsupportedOperationException(yt.toString());
if (yt.current() != YamlToken.TAG)
throw new UnsupportedOperationException(yt.toString());

if (!yt.isText("type"))
throw new UnsupportedOperationException(yt.text());

if (yt.next() != YamlToken.TEXT)
throw new UnsupportedOperationException(yt.toString());

StringBuilder stringBuilder = acquireStringBuilder();
textTo(stringBuilder);
classNameConsumer.accept(t, stringBuilder);

return YamlWire.this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,51 @@

import net.openhft.chronicle.bytes.BytesMarshallable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeFalse;

@RunWith(value = Parameterized.class)
public class DeserializeFromNakedFileTest extends WireTestCommon {
private final WireType wireType;

public DeserializeFromNakedFileTest(WireType wireType) {
this.wireType = wireType;
}

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> combinations() {
Object[][] list = {
{WireType.TEXT},
{WireType.YAML}
};
return Arrays.asList(list);
}

@Test
public void testPOJO() throws IOException {
PlainOldJavaClass res = Marshallable.fromFile(PlainOldJavaClass.class, "naked.yaml");
PlainOldJavaClass res = wireType.fromFile(PlainOldJavaClass.class, "naked.yaml");

assertEquals(20, res.heartBtInt);
}

@Test
public void testSelfDescribing() throws IOException {
SelfDescribingClass res = Marshallable.fromFile(SelfDescribingClass.class, "naked.yaml");
SelfDescribingClass res = wireType.fromFile(SelfDescribingClass.class, "naked.yaml");

assertEquals(20, res.heartBtInt);
}

@Test
public void testBytes() throws IOException {
BytesClass res = Marshallable.fromFile(BytesClass.class, "naked.yaml");
assumeFalse(wireType == WireType.YAML);
BytesClass res = wireType.fromFile(BytesClass.class, "naked.yaml");

// The result of parsing first 4 bytes as integer value
assertEquals(0x72616548, res.heartBtInt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.openhft.chronicle.core.util.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -41,13 +42,12 @@ public ForwardAndBackwardCompatibilityMarshallableTest(WireType wireType) {
this.wireType = wireType;
}

@Parameterized.Parameters
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{WireType.JSON},
{WireType.TEXT},
// TODO FIX
// {WireType.YAML},
{WireType.YAML},
{WireType.BINARY}
});
}
Expand All @@ -63,15 +63,12 @@ public void marshableStringBuilderTest() throws Exception {
wire.writeDocument(false, w -> new MDTO2(1, 2, "3").writeMarshallable(w));
// System.out.println(Wires.fromSizePrefixedBlobs(wire));

if (wire instanceof TextWire)
((TextWire) wire).useBinaryDocuments();

try (DocumentContext dc = wire.readingDocument()) {
if (!dc.isPresent())
Assert.fail();
@NotNull MDTO2 dto2 = new MDTO2();
dto2.readMarshallable(dc.wire());
Assert.assertEquals(1, dto2.one );
Assert.assertEquals(1, dto2.one);
Assert.assertEquals(2, dto2.two);
Assert.assertTrue("3".contentEquals(dto2.three));
}
Expand Down Expand Up @@ -112,6 +109,7 @@ public void backwardsCompatibility() {
public void forwardCompatibility() {

final Wire wire = wireType.apply(Bytes.elasticByteBuffer());
Assume.assumeFalse(wire instanceof YamlWire);
wire.usePadding(wire.isBinary());
ClassLookup wrap2 = CLASS_ALIASES.wrap();
wrap2.addAlias(MDTO2.class, "MDTO");
Expand All @@ -126,8 +124,6 @@ public void forwardCompatibility() {

if (wire instanceof TextWire)
((TextWire) wire).useBinaryDocuments();
if (wire instanceof YamlWire)
((YamlWire) wire).useBinaryDocuments();
try (DocumentContext dc = wire.readingDocument()) {
if (!dc.isPresent())
Assert.fail();
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/net/openhft/chronicle/wire/NestedMapsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public NestedMapsTest(WireType wireType) {
this.wireType = wireType;
}

@Parameterized.Parameters
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> wireTypes() {
return Arrays.asList(
new Object[]{WireType.JSON},
new Object[]{WireType.TEXT},
// TODO FIX
// new Object[]{WireType.YAML_ONLY},
new Object[]{WireType.YAML_ONLY},
new Object[]{WireType.BINARY},
new Object[]{WireType.FIELDLESS_BINARY}
);
Expand Down
34 changes: 18 additions & 16 deletions src/test/java/net/openhft/chronicle/wire/TextBinaryWireTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

@RunWith(value = Parameterized.class)
public class TextBinaryWireTest extends WireTestCommon {
Expand All @@ -42,13 +44,14 @@ public TextBinaryWireTest(WireType wireType) {
this.wireType = wireType;
}

@Parameterized.Parameters
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> combinations() {
Object[][] list = {
{WireType.BINARY},
{WireType.FIELDLESS_BINARY},
{WireType.RAW},
{WireType.TEXT},
{WireType.YAML},
{WireType.JSON}
};
return Arrays.asList(list);
Expand Down Expand Up @@ -87,21 +90,21 @@ public void readingDocumentLocation() {

@Test
public void testReadComment() {
if (wireType == WireType.TEXT || wireType == WireType.BINARY) {
Wire wire = createWire();
wire.writeComment("This is a comment");
@NotNull StringBuilder sb = new StringBuilder();
wire.readComment(sb);
assertEquals("This is a comment", sb.toString());

wire.bytes().releaseLast();
}
assumeTrue(wireType == WireType.TEXT || wireType == WireType.BINARY || wireType == WireType.YAML);

Wire wire = createWire();
wire.writeComment("This is a comment");
@NotNull StringBuilder sb = new StringBuilder();
wire.readComment(sb);
assertEquals("This is a comment", sb.toString());

wire.bytes().releaseLast();
}

@Test
public void readFieldAsObject() {
if (wireType == WireType.RAW || wireType == WireType.FIELDLESS_BINARY)
return;
assumeFalse(wireType == WireType.RAW || wireType == WireType.FIELDLESS_BINARY);

Wire wire = createWire();
wire.write("CLASS").text("class")
.write("RUNTIME").text("runtime");
Expand All @@ -117,8 +120,8 @@ public void readFieldAsObject() {

@Test
public void readFieldAsLong() {
if (wireType == WireType.RAW || wireType == WireType.FIELDLESS_BINARY)
return;
assumeFalse(wireType == WireType.RAW || wireType == WireType.FIELDLESS_BINARY);

Wire wire = createWire();
// todo fix to ensure a field number is used.
wire.writeEvent(Long.class, 1L).text("class")
Expand All @@ -138,8 +141,7 @@ public void readFieldAsLong() {

@Test
public void testConvertToNum() {
if (wireType == WireType.RAW)
return;
assumeFalse(wireType == WireType.RAW || /* No support for bool conversions */ wireType == WireType.YAML);

Wire wire = createWire();
wire.write("a").bool(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public void testTextWire() {
doTestText(WireType.TEXT);
}

@Ignore(/* TODO FIX */)
@Test
public void testYamlWire() {
doTestText(WireType.YAML_ONLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public class NestedClass implements Marshallable {
double number;
double number2;
double number4; // out of order
NestedClass doublyNested;

@Override
public void readMarshallable(@NotNull WireIn wire) throws IORuntimeException {
wire.read(() -> "text").text(this, (t, v) -> t.text = v)
.read(() -> "text2").text(this, (t, v) -> t.text2 = v)
.read(() -> "doublyNested").object(NestedClass.class, this, (t, v) -> t.doublyNested = v)
.read(() -> "number").float64(this, (t, v) -> t.number = v)
.read(() -> "number2").float64(this, (t, v) -> t.number2 = v)
.read(() -> "number4").float64(this, (t, v) -> t.number4 = v);
Expand All @@ -46,13 +48,19 @@ public void writeMarshallable(@NotNull WireOut wire) {
.write(() -> "text3").text("is text3")
.write(() -> "number4").float64(number4)
.write(() -> "number").float64(number)
.write(() -> "doublyNested").object(doublyNested)
.write(() -> "number3").float64(333.3);
}

public void setTextNumber(String text, double number) {
public NestedClass setTextNumber(String text, double number) {
this.text = text;
this.number = number;
this.number4 = number * 4;
return this;
}

public void nest(String text, double number) {
this.doublyNested = new NestedClass().setTextNumber(text, number);
}

@NotNull
Expand Down

0 comments on commit 76491d9

Please sign in to comment.