Skip to content

Commit

Permalink
Added test cases for enum value naming style without prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
S1artie committed Nov 22, 2024
1 parent 324e766 commit e001627
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* mapstruct-spi-protobuf
*
* Copyright (C) 2024
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package de.firehead.mapstruct.spi.protobuf.test.immutables;

public enum TestEnumWithoutPrefixes {

VALUE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public interface TestImmutableObject {

TestEnum getEnumField();

TestEnumWithoutPrefixes getEnumWithoutPrefixesField();

java.util.Map<String, String> getStringMapField();

java.util.Map<String, Integer> getIntMapField();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public abstract class AbstractMappingTest {
.booleanField(true)
.bytesField(new byte[]{0x01, 0x02, 0x03})
.enumField(TestEnum.VALUE)
.enumWithoutPrefixesField(TestEnumWithoutPrefixes.VALUE)
.putStringMapField("key1", "value1")
.putStringMapField("key2", "value2")
.putIntMapField("key1", 42)
Expand Down Expand Up @@ -48,6 +49,7 @@ public abstract class AbstractMappingTest {
.setBooleanField(true)
.setBytesField(ByteString.copyFrom(new byte[]{0x01, 0x02, 0x03}))
.setEnumField(TestProtos.TestEnum.TEST_ENUM_VALUE)
.setEnumWithoutPrefixesField(TestProtos.TestEnumWithoutPrefixes.VALUE)
.putStringMapField("key1", "value1")
.putStringMapField("key2", "value2")
.putIntMapField("key1", 42)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void testSimpleMappingProtoToImmutable() {
Assertions.assertEquals(TEST_IMMUTABLE_OBJECT.getBooleanField(), mappedImmutable.getBooleanField());
Assertions.assertArrayEquals(TEST_IMMUTABLE_OBJECT.getBytesField(), mappedImmutable.getBytesField());
Assertions.assertEquals(TEST_IMMUTABLE_OBJECT.getEnumField(), mappedImmutable.getEnumField());
Assertions.assertEquals(TEST_IMMUTABLE_OBJECT.getEnumWithoutPrefixesField(), mappedImmutable.getEnumWithoutPrefixesField());
Assertions.assertEquals(TEST_IMMUTABLE_OBJECT.getStringMapField(), mappedImmutable.getStringMapField());
Assertions.assertEquals(TEST_IMMUTABLE_OBJECT.getIntMapField(), mappedImmutable.getIntMapField());
Assertions.assertEquals(TEST_IMMUTABLE_OBJECT.getLongMapField(), mappedImmutable.getLongMapField());
Expand All @@ -51,6 +52,7 @@ public void testSimpleMappingImmutableToProto() {
Assertions.assertEquals(TEST_PROTO_MESSAGE.getBooleanField(), mappedProto.getBooleanField());
Assertions.assertArrayEquals(TEST_PROTO_MESSAGE.getBytesField().toByteArray(), mappedProto.getBytesField().toByteArray());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getEnumField(), mappedProto.getEnumField());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getEnumWithoutPrefixesField(), mappedProto.getEnumWithoutPrefixesField());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getStringMapFieldMap(), mappedProto.getStringMapFieldMap());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getIntMapFieldMap(), mappedProto.getIntMapFieldMap());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getLongMapFieldMap(), mappedProto.getLongMapFieldMap());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* mapstruct-spi-protobuf
*
* Copyright (C) 2024
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package de.firehead.mapstruct.spi.protobuf.test.pojo;

public enum TestEnumWithoutPrefixes {

VALUE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class TestObject {

private TestEnum enumField;

private TestEnumWithoutPrefixes enumFieldWithoutPrefixes;

private java.util.Map<String, String> stringMapField;

private java.util.Map<String, Integer> intMapField;
Expand Down Expand Up @@ -130,6 +132,14 @@ public void setEnumField(TestEnum enumField) {
this.enumField = enumField;
}

public TestEnumWithoutPrefixes getEnumFieldWithoutPrefixes() {
return enumFieldWithoutPrefixes;
}

public void setEnumFieldWithoutPrefixes(TestEnumWithoutPrefixes enumFieldWithoutPrefixes) {
this.enumFieldWithoutPrefixes = enumFieldWithoutPrefixes;
}

public Map<String, String> getStringMapField() {
return stringMapField;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void testSimpleMappingProtoToPojo() {
Assertions.assertEquals(TEST_OBJECT.getBooleanField(), mappedPojo.getBooleanField());
Assertions.assertArrayEquals(TEST_OBJECT.getBytesField(), mappedPojo.getBytesField());
Assertions.assertEquals(TEST_OBJECT.getEnumField(), mappedPojo.getEnumField());
Assertions.assertEquals(TEST_OBJECT.getEnumFieldWithoutPrefixes(), mappedPojo.getEnumFieldWithoutPrefixes());
Assertions.assertEquals(TEST_OBJECT.getStringMapField(), mappedPojo.getStringMapField());
Assertions.assertEquals(TEST_OBJECT.getIntMapField(), mappedPojo.getIntMapField());
Assertions.assertEquals(TEST_OBJECT.getLongMapField(), mappedPojo.getLongMapField());
Expand All @@ -51,6 +52,7 @@ public void testSimpleMappingPojoToProto() {
Assertions.assertEquals(TEST_PROTO_MESSAGE.getBooleanField(), mappedProto.getBooleanField());
Assertions.assertArrayEquals(TEST_PROTO_MESSAGE.getBytesField().toByteArray(), mappedProto.getBytesField().toByteArray());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getEnumField(), mappedProto.getEnumField());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getEnumWithoutPrefixesField(), mappedProto.getEnumWithoutPrefixesField());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getStringMapFieldMap(), mappedProto.getStringMapFieldMap());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getIntMapFieldMap(), mappedProto.getIntMapFieldMap());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getLongMapFieldMap(), mappedProto.getLongMapFieldMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ message OtherTestProtoMessage {
bool booleanField = 6;
bytes bytesField = 7;
OtherTestEnum enumField = 8;
map<string, string> stringMapField = 9;
map<string, int32> intMapField = 10;
map<string, int64> longMapField = 11;
map<string, float> floatMapField = 12;
map<string, double> doubleMapField = 13;
map<string, bool> boolMapField = 14;
map<string, bytes> bytesMapField = 15;
map<string, OtherTestEnum> enumMapField = 16;
OtherTestEnumWithoutPrefixes enumWithoutPrefixesField = 9;
map<string, string> stringMapField = 10;
map<string, int32> intMapField = 11;
map<string, int64> longMapField = 12;
map<string, float> floatMapField = 13;
map<string, double> doubleMapField = 14;
map<string, bool> boolMapField = 15;
map<string, bytes> bytesMapField = 16;
map<string, OtherTestEnum> enumMapField = 17;
}

message OtherDeepTestProtoMessage {
Expand All @@ -32,3 +33,8 @@ enum OtherTestEnum {
OTHER_TEST_ENUM_UNSPECIFIED = 0;
OTHER_TEST_ENUM_VALUE = 1;
}

enum OtherTestEnumWithoutPrefixes {
OTHER_TEST_ENUM_WITHOUT_PREFIXES_UNSPECIFIED = 0;
VALUE = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public abstract class AbstractMappingTest {
.setBooleanField(true)
.setBytesField(ByteString.copyFrom(new byte[]{0x01, 0x02, 0x03}))
.setEnumField(TestProtos.TestEnum.TEST_ENUM_VALUE)
.setEnumWithoutPrefixesField(TestProtos.TestEnumWithoutPrefixes.VALUE)
.putStringMapField("key1", "value1")
.putStringMapField("key2", "value2")
.putIntMapField("key1", 42)
Expand Down Expand Up @@ -49,6 +50,7 @@ public abstract class AbstractMappingTest {
.setBooleanField(true)
.setBytesField(ByteString.copyFrom(new byte[]{0x01, 0x02, 0x03}))
.setEnumField(Proto2ProtoTestProtos.OtherTestEnum.OTHER_TEST_ENUM_VALUE)
.setEnumWithoutPrefixesField(Proto2ProtoTestProtos.OtherTestEnumWithoutPrefixes.VALUE)
.putStringMapField("key1", "value1")
.putStringMapField("key2", "value2")
.putIntMapField("key1", 42)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ message TestProtoMessage {
bool booleanField = 6;
bytes bytesField = 7;
TestEnum enumField = 8;
map<string, string> stringMapField = 9;
map<string, int32> intMapField = 10;
map<string, int64> longMapField = 11;
map<string, float> floatMapField = 12;
map<string, double> doubleMapField = 13;
map<string, bool> boolMapField = 14;
map<string, bytes> bytesMapField = 15;
map<string, TestEnum> enumMapField = 16;
TestEnumWithoutPrefixes enumWithoutPrefixesField = 9;
map<string, string> stringMapField = 10;
map<string, int32> intMapField = 11;
map<string, int64> longMapField = 12;
map<string, float> floatMapField = 13;
map<string, double> doubleMapField = 14;
map<string, bool> boolMapField = 15;
map<string, bytes> bytesMapField = 16;
map<string, TestEnum> enumMapField = 17;
}

message DeepTestProtoMessage {
Expand All @@ -28,7 +29,17 @@ message DeepTestProtoMessage {
map<string, TestProtoMessage> testProtoMessageMap = 3;
}

// For enums there are two slightly different naming conventions in use: one requires
// a name-dependent prefix only on the _UNSPECIFIED default value, and another one
// requires this prefix on all values (to avoid value name conflicts). These should
// both be handled correctly.

enum TestEnum {
TEST_ENUM_UNSPECIFIED = 0;
TEST_ENUM_VALUE = 1;
}

enum TestEnumWithoutPrefixes {
TEST_ENUM_WITHOUT_PREFIXES_UNSPECIFIED = 0;
VALUE = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* mapstruct-spi-protobuf
*
* Copyright (C) 2024
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package de.firehead.mapstruct.spi.protobuf.test.records;

public enum TestEnumWithoutPrefixes {

VALUE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
* bool booleanField = 6;
* bytes bytesField = 7;
* TestEnum enumField = 8;
* map<string, string> stringMapField = 9;
* map<string, int32> intMapField = 10;
* map<string, int64> longMapField = 11;
* map<string, float> floatMapField = 12;
* map<string, double> doubleMapField = 13;
* map<string, bool> boolMapField = 14;
* map<string, bytes> bytesMapField = 15;
* map<string, TestEnum> enumMapField = 16;
* TestEnumWithoutPrefixes enumWithoutPrefixesField = 9;
* map<string, string> stringMapField = 10;
* map<string, int32> intMapField = 11;
* map<string, int64> longMapField = 12;
* map<string, float> floatMapField = 13;
* map<string, double> doubleMapField = 14;
* map<string, bool> boolMapField = 15;
* map<string, bytes> bytesMapField = 16;
* map<string, TestEnum> enumMapField = 17;
* }
*/
public record TestRecord(String stringField, int intField, long longField, float floatField, double doubleField,
boolean booleanField, byte[] bytesField, TestEnum enumField,
TestEnumWithoutPrefixes enumWithoutPrefixesField,
Map<String, String> stringMapField,
Map<String, Integer> intMapField, Map<String, Long> longMapField,
Map<String, Float> floatMapField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public abstract class AbstractMappingTest {

protected static final TestRecord TEST_RECORD = new TestRecord("test", 42, 42L, 42.0f, 42.0, true, new byte[]{0x01, 0x02, 0x03}, TestEnum.VALUE, Map.of("key1", "value1", "key2", "value2"), Map.of("key1", 42, "key2", 43), Map.of("key1", 42L, "key2", 43L), Map.of("key1", 42.0f, "key2", 42.1f), Map.of("key1", 42.0, "key2", 42.1), Map.of("key1", true, "key2", false), Map.of("key1", new byte[]{0x01, 0x02, 0x03}, "key2", new byte[]{0x04, 0x05, 0x06}), Map.of("key1", TestEnum.VALUE, "key2", TestEnum.VALUE));
protected static final TestRecord TEST_RECORD = new TestRecord("test", 42, 42L, 42.0f, 42.0, true, new byte[]{0x01, 0x02, 0x03}, TestEnum.VALUE, TestEnumWithoutPrefixes.VALUE, Map.of("key1", "value1", "key2", "value2"), Map.of("key1", 42, "key2", 43), Map.of("key1", 42L, "key2", 43L), Map.of("key1", 42.0f, "key2", 42.1f), Map.of("key1", 42.0, "key2", 42.1), Map.of("key1", true, "key2", false), Map.of("key1", new byte[]{0x01, 0x02, 0x03}, "key2", new byte[]{0x04, 0x05, 0x06}), Map.of("key1", TestEnum.VALUE, "key2", TestEnum.VALUE));

protected static final TestProtos.TestProtoMessage TEST_PROTO_MESSAGE = TestProtos.TestProtoMessage.newBuilder()
.setStringField("test")
Expand All @@ -25,6 +25,7 @@ public abstract class AbstractMappingTest {
.setBooleanField(true)
.setBytesField(ByteString.copyFrom(new byte[]{0x01, 0x02, 0x03}))
.setEnumField(TestProtos.TestEnum.TEST_ENUM_VALUE)
.setEnumWithoutPrefixesField(TestProtos.TestEnumWithoutPrefixes.VALUE)
.putStringMapField("key1", "value1")
.putStringMapField("key2", "value2")
.putIntMapField("key1", 42)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void testSimpleMappingProtoToPojo() {
Assertions.assertEquals(TEST_RECORD.booleanField(), mappedRecord.booleanField());
Assertions.assertArrayEquals(TEST_RECORD.bytesField(), mappedRecord.bytesField());
Assertions.assertEquals(TEST_RECORD.enumField(), mappedRecord.enumField());
Assertions.assertEquals(TEST_RECORD.enumWithoutPrefixesField(), mappedRecord.enumWithoutPrefixesField());
Assertions.assertEquals(TEST_RECORD.stringMapField(), mappedRecord.stringMapField());
Assertions.assertEquals(TEST_RECORD.intMapField(), mappedRecord.intMapField());
Assertions.assertEquals(TEST_RECORD.longMapField(), mappedRecord.longMapField());
Expand All @@ -51,6 +52,7 @@ public void testSimpleMappingPojoToProto() {
Assertions.assertEquals(TEST_PROTO_MESSAGE.getBooleanField(), mappedProto.getBooleanField());
Assertions.assertArrayEquals(TEST_PROTO_MESSAGE.getBytesField().toByteArray(), mappedProto.getBytesField().toByteArray());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getEnumField(), mappedProto.getEnumField());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getEnumWithoutPrefixesField(), mappedProto.getEnumWithoutPrefixesField());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getStringMapFieldMap(), mappedProto.getStringMapFieldMap());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getIntMapFieldMap(), mappedProto.getIntMapFieldMap());
Assertions.assertEquals(TEST_PROTO_MESSAGE.getLongMapFieldMap(), mappedProto.getLongMapFieldMap());
Expand Down

0 comments on commit e001627

Please sign in to comment.