Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API-1250] Rename cloneWithBuilder to newBuilderWithClone for GenericRecords #21730

Merged
merged 2 commits into from Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -161,7 +161,7 @@ public GenericRecordBuilder newBuilder() {

@Override
@Nonnull
public GenericRecordBuilder cloneWithBuilder() {
public GenericRecordBuilder newBuilderWithClone() {
TreeMap<String, Object> objects = new TreeMap<>();
for (String fieldName : getFieldNames()) {
objects.put(fieldName, readAny(fieldName));
Expand Down
Expand Up @@ -104,7 +104,7 @@ public GenericRecordBuilder newBuilder() {

@Nonnull
@Override
public GenericRecordBuilder cloneWithBuilder() {
public GenericRecordBuilder newBuilderWithClone() {
return new DeserializedGenericRecordCloner(schema, objects);
}

Expand Down
Expand Up @@ -63,7 +63,7 @@ public GenericRecordBuilder newBuilder() {

@Nonnull
@Override
public GenericRecordBuilder cloneWithBuilder() {
public GenericRecordBuilder newBuilderWithClone() {
return new PortableGenericRecordBuilder(classDefinition, Arrays.copyOf(objects, objects.length));
}

Expand Down
Expand Up @@ -515,7 +515,7 @@ public GenericRecordBuilder newBuilder() {

@Nonnull
@Override
public GenericRecordBuilder cloneWithBuilder() {
public GenericRecordBuilder newBuilderWithClone() {
throw new UnsupportedOperationException();
}

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Expand Up @@ -43,8 +43,8 @@
import static com.hazelcast.internal.serialization.impl.compact.CompactTestUtil.createCompactGenericRecord;
import static com.hazelcast.internal.serialization.impl.compact.CompactTestUtil.createMainDTO;
import static com.hazelcast.nio.serialization.GenericRecordBuilder.compact;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

@RunWith(HazelcastParallelClassRunner.class)
Expand Down Expand Up @@ -99,7 +99,7 @@ public void testGenericRecordToStringValidJson() throws IOException {
public void testCloneDeserializedGenericRecord() {
GenericRecordBuilder builder = compact("fooBarTypeName");
builder.setInt32("foo", 1);
assertTrue(trySetAndGetMessage("foo", 5, builder).startsWith("Field can only be written once"));
assertSetterThrows(builder, "foo", 5, "Field can only be written once");
builder.setInt64("bar", 1231L);
DeserializedGenericRecord genericRecord = (DeserializedGenericRecord) builder.build();

Expand All @@ -112,7 +112,7 @@ public void testCloneCompactInternalGenericRecord() throws IOException {

GenericRecordBuilder builder = compact("fooBarTypeName");
builder.setInt32("foo", 1);
assertTrue(trySetAndGetMessage("foo", 5, builder).startsWith("Field can only be written once"));
assertSetterThrows(builder, "foo", 5, "Field can only be written once");
builder.setInt64("bar", 1231L);
GenericRecord expectedGenericRecord = builder.build();

Expand All @@ -124,11 +124,11 @@ public void testCloneCompactInternalGenericRecord() throws IOException {
}

private void verifyCloneWithBuilder(GenericRecord genericRecord) {
GenericRecordBuilder cloneBuilder = genericRecord.cloneWithBuilder();
GenericRecordBuilder cloneBuilder = genericRecord.newBuilderWithClone();
cloneBuilder.setInt32("foo", 2);
assertTrue(trySetAndGetMessage("foo", 5, cloneBuilder).startsWith("Field can only be written once"));

assertTrue(trySetAndGetMessage("notExisting", 3, cloneBuilder).startsWith("Invalid field name"));
assertSetterThrows(cloneBuilder, "foo", 5, "Field can only be written once");
assertSetterThrows(cloneBuilder, "notExisting", 3, "Invalid field name");

GenericRecord clone = cloneBuilder.build();

Expand All @@ -151,7 +151,7 @@ public void testBuildFromCompactInternalGenericRecord() throws IOException {

GenericRecordBuilder builder = compact("fooBarTypeName");
builder.setInt32("foo", 1);
assertTrue(trySetAndGetMessage("foo", 5, builder).startsWith("Field can only be written once"));
assertSetterThrows(builder, "foo", 5, "Field can only be written once");
builder.setInt64("bar", 1231L);
GenericRecord expectedGenericRecord = builder.build();

Expand All @@ -166,10 +166,13 @@ public void testBuildFromCompactInternalGenericRecord() throws IOException {
private void verifyNewBuilder(GenericRecord genericRecord) {
GenericRecordBuilder recordBuilder = genericRecord.newBuilder();
recordBuilder.setInt32("foo", 2);
assertTrue(trySetAndGetMessage("foo", 5, recordBuilder).startsWith("Field can only be written once"));

assertTrue(trySetAndGetMessage("notExisting", 3, recordBuilder).startsWith("Invalid field name"));
assertTrue(tryBuildAndGetMessage(recordBuilder).startsWith("Found an unset field"));
assertSetterThrows(recordBuilder, "foo", 5, "Field can only be written once");
assertSetterThrows(recordBuilder, "notExisting", 3, "Invalid field name");

assertThatThrownBy(recordBuilder::build)
.isInstanceOf(HazelcastSerializationException.class)
.hasMessageStartingWith("Found an unset field");

recordBuilder.setInt64("bar", 100);
GenericRecord newRecord = recordBuilder.build();
Expand All @@ -182,7 +185,7 @@ private void verifyNewBuilder(GenericRecord genericRecord) {
public void testBuildFromDeserializedGenericRecord() {
GenericRecordBuilder builder = compact("fooBarTypeName");
builder.setInt32("foo", 1);
assertTrue(trySetAndGetMessage("foo", 5, builder).startsWith("Field can only be written once"));
assertSetterThrows(builder, "foo", 5, "Field can only be written once");
builder.setInt64("bar", 1231L);
GenericRecord genericRecord = builder.build();

Expand All @@ -191,61 +194,43 @@ public void testBuildFromDeserializedGenericRecord() {

@Test
public void testReadWriteChar() {
assertThrows(UnsupportedOperationException.class, () -> {
compact("writeChar").setChar("c", 'a');
});
assertThatThrownBy(() -> compact("writeChar").setChar("c", 'a'))
.isInstanceOf(UnsupportedOperationException.class);

assertThrows(UnsupportedOperationException.class, () -> {
assertThatThrownBy(() -> {
GenericRecord record = compact("readChar").build();
record.getChar("c");
});
}).isInstanceOf(UnsupportedOperationException.class);
}

@Test
public void testReadWriteCharArray() {
assertThrows(UnsupportedOperationException.class, () -> {
compact("writeCharArray").setArrayOfChar("ca", new char[]{'c'});
});
assertThatThrownBy(() -> compact("writeCharArray").setArrayOfChar("ca", new char[]{'c'}))
.isInstanceOf(UnsupportedOperationException.class);

assertThrows(UnsupportedOperationException.class, () -> {
assertThatThrownBy(() -> {
GenericRecord record = compact("readCharArray").build();
record.getArrayOfChar("ca");
});
}).isInstanceOf(UnsupportedOperationException.class);
}

private String trySetAndGetMessage(String fieldName, int value, GenericRecordBuilder cloneBuilder) {
try {
cloneBuilder.setInt32(fieldName, value);
} catch (HazelcastSerializationException e) {
return e.getMessage();
}
return null;
}

private String tryBuildAndGetMessage(GenericRecordBuilder builder) {
try {
builder.build();
} catch (HazelcastSerializationException e) {
return e.getMessage();
}
return null;
}


@Test
public void testGetFieldKindThrowsExceptionWhenFieldDoesNotExist() throws IOException {
GenericRecord record = compact("test").build();
assertThrows(IllegalArgumentException.class, () -> {
record.getFieldKind("doesNotExist");
});

assertThatThrownBy(() -> record.getFieldKind("doesNotExist"))
.isInstanceOf(IllegalArgumentException.class);

InternalSerializationService serializationService = (InternalSerializationService) createSerializationService();
Data data = serializationService.toData(record);

InternalGenericRecord internalGenericRecord = serializationService.readAsInternalGenericRecord(data);
assertThrows(IllegalArgumentException.class, () -> {
internalGenericRecord.getFieldKind("doesNotExist");
});
assertThatThrownBy(() -> internalGenericRecord.getFieldKind("doesNotExist"))
.isInstanceOf(IllegalArgumentException.class);
}

private void assertSetterThrows(GenericRecordBuilder builder, String fieldName, int value, String errorMessage) {
assertThatThrownBy(() -> builder.setInt32(fieldName, value))
.isInstanceOf(HazelcastSerializationException.class)
.hasMessageStartingWith(errorMessage);
}
}
Expand Up @@ -183,7 +183,7 @@ static class GenericIncreaseAgeEntryProcessor implements EntryProcessor<Integer,
@Override
public Object process(Map.Entry<Integer, GenericRecord> entry) {
GenericRecord value = entry.getValue();
GenericRecord newValue = value.cloneWithBuilder()
GenericRecord newValue = value.newBuilderWithClone()
.setInt32("age", value.getInt32("age") + 1000)
.build();
entry.setValue(newValue);
Expand Down
Expand Up @@ -68,7 +68,7 @@ public void testOverwritingSameFieldMultipleTimes() {
.setString("name", "foo")
.setInt32("myint", 123).build();

GenericRecordBuilder builder = record.cloneWithBuilder().setString("name", "foo2");
GenericRecordBuilder builder = record.newBuilderWithClone().setString("name", "foo2");
assertThrows(HazelcastSerializationException.class, () -> builder.setString("name", "foo3"));
}

Expand Down
Expand Up @@ -201,7 +201,7 @@ public void testCloneWithGenericBuilderOnEntryProcessor() {
Object value = entry.getValue();
GenericRecord genericRecord = (GenericRecord) value;

GenericRecord modifiedGenericRecord = genericRecord.cloneWithBuilder()
GenericRecord modifiedGenericRecord = genericRecord.newBuilderWithClone()
.setInt32("myint", 4).build();

entry.setValue(modifiedGenericRecord);
Expand Down