|
20 | 20 | package org.apache.druid.data.input.protobuf;
|
21 | 21 |
|
22 | 22 | import com.google.common.io.Files;
|
23 |
| -import com.google.protobuf.Descriptors; |
24 |
| -import nl.jqno.equalsverifier.EqualsVerifier; |
25 | 23 | import org.apache.druid.java.util.common.IAE;
|
26 | 24 | import org.apache.druid.java.util.common.StringUtils;
|
27 | 25 | import org.apache.druid.java.util.common.parsers.ParseException;
|
28 |
| -import org.junit.Before; |
29 |
| -import org.junit.Test; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
30 | 28 |
|
31 | 29 | import java.io.File;
|
| 30 | +import java.net.URL; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
32 | 37 |
|
33 | 38 | public class InlineDescriptorProtobufBytesDecoderTest
|
34 | 39 | {
|
35 | 40 | private String descString;
|
36 | 41 |
|
37 |
| - @Before |
| 42 | + @BeforeEach |
38 | 43 | public void initDescriptorString() throws Exception
|
39 | 44 | {
|
40 |
| - File descFile = new File(this.getClass() |
41 |
| - .getClassLoader() |
42 |
| - .getResource("prototest.desc") |
43 |
| - .toURI()); |
| 45 | + final URL resource = this.getClass() |
| 46 | + .getClassLoader() |
| 47 | + .getResource("prototest.desc"); |
| 48 | + assertNotNull(resource); |
| 49 | + |
| 50 | + final var descFile = new File(resource.toURI()); |
44 | 51 | descString = StringUtils.encodeBase64String(Files.toByteArray(descFile));
|
45 | 52 | }
|
46 | 53 |
|
47 | 54 | @Test
|
48 | 55 | public void testShortMessageType()
|
49 | 56 | {
|
50 |
| - @SuppressWarnings("unused") // expected to create parser without exception |
51 |
| - InlineDescriptorProtobufBytesDecoder decoder = new InlineDescriptorProtobufBytesDecoder( |
| 57 | + final var decoder = new InlineDescriptorProtobufBytesDecoder( |
52 | 58 | descString,
|
53 | 59 | "ProtoTestEvent"
|
54 | 60 | );
|
55 |
| - decoder.initDescriptor(); |
| 61 | + |
| 62 | + assertDoesNotThrow(decoder::initDescriptor); |
56 | 63 | }
|
57 | 64 |
|
58 | 65 | @Test
|
59 | 66 | public void testLongMessageType()
|
60 | 67 | {
|
61 |
| - @SuppressWarnings("unused") // expected to create parser without exception |
62 |
| - InlineDescriptorProtobufBytesDecoder decoder = new InlineDescriptorProtobufBytesDecoder( |
| 68 | + final var decoder = new InlineDescriptorProtobufBytesDecoder( |
63 | 69 | descString,
|
64 | 70 | "prototest.ProtoTestEvent"
|
65 | 71 | );
|
66 |
| - decoder.initDescriptor(); |
| 72 | + |
| 73 | + assertDoesNotThrow(decoder::initDescriptor); |
67 | 74 | }
|
68 | 75 |
|
69 |
| - @Test(expected = ParseException.class) |
| 76 | + @Test |
70 | 77 | public void testBadProto()
|
71 | 78 | {
|
72 |
| - @SuppressWarnings("unused") // expected exception |
73 |
| - InlineDescriptorProtobufBytesDecoder decoder = new InlineDescriptorProtobufBytesDecoder(descString, "BadName"); |
74 |
| - decoder.initDescriptor(); |
| 79 | + assertThrows( |
| 80 | + ParseException.class, |
| 81 | + () -> { |
| 82 | + final var decoder = new InlineDescriptorProtobufBytesDecoder(descString, "BadName"); |
| 83 | + |
| 84 | + decoder.initDescriptor(); |
| 85 | + } |
| 86 | + ); |
75 | 87 | }
|
76 | 88 |
|
77 |
| - @Test(expected = IAE.class) |
| 89 | + @Test |
78 | 90 | public void testMalformedDescriptorBase64()
|
79 | 91 | {
|
80 |
| - @SuppressWarnings("unused") // expected exception |
81 |
| - InlineDescriptorProtobufBytesDecoder decoder = new InlineDescriptorProtobufBytesDecoder("invalidString", "BadName"); |
82 |
| - decoder.initDescriptor(); |
| 92 | + assertThrows( |
| 93 | + IAE.class, |
| 94 | + () -> { |
| 95 | + final var decoder = new InlineDescriptorProtobufBytesDecoder("invalidString", "BadName"); |
| 96 | + |
| 97 | + decoder.initDescriptor(); |
| 98 | + } |
| 99 | + ); |
83 | 100 | }
|
84 | 101 |
|
85 |
| - @Test(expected = ParseException.class) |
| 102 | + @Test |
86 | 103 | public void testMalformedDescriptorValidBase64InvalidDescriptor()
|
87 | 104 | {
|
88 |
| - @SuppressWarnings("unused") // expected exception |
89 |
| - InlineDescriptorProtobufBytesDecoder decoder = new InlineDescriptorProtobufBytesDecoder( |
90 |
| - "aGVsbG8gd29ybGQ=", |
91 |
| - "BadName" |
| 105 | + assertThrows( |
| 106 | + ParseException.class, |
| 107 | + () -> { |
| 108 | + final var decoder = new InlineDescriptorProtobufBytesDecoder( |
| 109 | + "aGVsbG8gd29ybGQ=", |
| 110 | + "BadName" |
| 111 | + ); |
| 112 | + |
| 113 | + decoder.initDescriptor(); |
| 114 | + } |
92 | 115 | );
|
93 |
| - decoder.initDescriptor(); |
94 | 116 | }
|
95 | 117 |
|
| 118 | + /** |
| 119 | + * For the backward compatibility, protoMessageType allows null when the desc file has only one message type. |
| 120 | + */ |
96 | 121 | @Test
|
97 | 122 | public void testSingleDescriptorNoMessageType()
|
98 | 123 | {
|
99 |
| - // For the backward compatibility, protoMessageType allows null when the desc file has only one message type. |
100 |
| - @SuppressWarnings("unused") // expected to create parser without exception |
101 |
| - InlineDescriptorProtobufBytesDecoder decoder = new InlineDescriptorProtobufBytesDecoder(descString, null); |
102 |
| - decoder.initDescriptor(); |
| 124 | + final var decoder = new InlineDescriptorProtobufBytesDecoder(descString, null); |
| 125 | + assertDoesNotThrow(decoder::initDescriptor); |
103 | 126 | }
|
104 | 127 |
|
105 | 128 | @Test
|
106 | 129 | public void testEquals()
|
107 | 130 | {
|
108 |
| - InlineDescriptorProtobufBytesDecoder decoder = new InlineDescriptorProtobufBytesDecoder(descString, "ProtoTestEvent"); |
109 |
| - decoder.initDescriptor(); |
110 |
| - Descriptors.Descriptor descriptorA = decoder.getDescriptor(); |
111 |
| - |
112 |
| - decoder = new InlineDescriptorProtobufBytesDecoder(descString, "ProtoTestEvent.Foo"); |
113 |
| - decoder.initDescriptor(); |
114 |
| - Descriptors.Descriptor descriptorB = decoder.getDescriptor(); |
115 |
| - |
116 |
| - EqualsVerifier.forClass(InlineDescriptorProtobufBytesDecoder.class) |
117 |
| - .usingGetClass() |
118 |
| - .withIgnoredFields("descriptor") |
119 |
| - .withPrefabValues(Descriptors.Descriptor.class, descriptorA, descriptorB) |
120 |
| - .verify(); |
| 131 | + // Test basic equality |
| 132 | + final var decoder1 = new InlineDescriptorProtobufBytesDecoder( |
| 133 | + descString, |
| 134 | + "ProtoTestEvent" |
| 135 | + ); |
| 136 | + final var decoder2 = new InlineDescriptorProtobufBytesDecoder( |
| 137 | + descString, |
| 138 | + "ProtoTestEvent" |
| 139 | + ); |
| 140 | + final var decoder3 = new InlineDescriptorProtobufBytesDecoder( |
| 141 | + descString, |
| 142 | + "ProtoTestEvent.Foo" |
| 143 | + ); |
| 144 | + final var decoder4 = new InlineDescriptorProtobufBytesDecoder( |
| 145 | + descString, |
| 146 | + null |
| 147 | + ); |
| 148 | + |
| 149 | + // Symmetry: x.equals(y) == y.equals(x) |
| 150 | + assertEquals(decoder1, decoder2); |
| 151 | + assertEquals(decoder2, decoder1); |
| 152 | + |
| 153 | + // Inequality tests |
| 154 | + assertNotEquals(decoder1, decoder3); // different protoMessageType |
| 155 | + assertNotEquals(decoder1, decoder4); // different protoMessageType (non-null vs null) |
| 156 | + assertNotEquals(null, decoder1); |
| 157 | + |
| 158 | + // HashCode consistency |
| 159 | + assertEquals(decoder1.hashCode(), decoder2.hashCode()); |
| 160 | + assertNotEquals(decoder1.hashCode(), decoder3.hashCode()); |
| 161 | + assertNotEquals(decoder1.hashCode(), decoder4.hashCode()); |
121 | 162 | }
|
122 | 163 |
|
123 | 164 | }
|
0 commit comments