|
21 | 21 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
22 | 22 | import static org.assertj.core.api.Assertions.fail; |
23 | 23 |
|
| 24 | +import com.datastax.oss.driver.api.core.type.codec.TypeCodec; |
24 | 25 | import com.datastax.oss.driver.api.core.type.codec.TypeCodecs; |
| 26 | +import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry; |
25 | 27 | import com.datastax.oss.driver.internal.SerializationHelper; |
26 | 28 | import com.datastax.oss.driver.shaded.guava.common.collect.Iterators; |
27 | 29 | import java.io.ByteArrayInputStream; |
28 | 30 | import java.io.ObjectInputStream; |
29 | 31 | import java.io.ObjectStreamException; |
| 32 | +import java.time.LocalTime; |
30 | 33 | import java.util.AbstractList; |
31 | 34 | import java.util.ArrayList; |
32 | 35 | import java.util.Arrays; |
33 | 36 | import java.util.Collections; |
| 37 | +import java.util.Iterator; |
34 | 38 | import java.util.List; |
35 | 39 | import java.util.stream.Collectors; |
| 40 | + |
| 41 | +import com.tngtech.java.junit.dataprovider.DataProvider; |
| 42 | +import com.tngtech.java.junit.dataprovider.DataProviderRunner; |
| 43 | +import com.tngtech.java.junit.dataprovider.UseDataProvider; |
36 | 44 | import org.apache.commons.codec.DecoderException; |
37 | 45 | import org.apache.commons.codec.binary.Hex; |
38 | 46 | import org.assertj.core.util.Lists; |
39 | 47 | import org.junit.Test; |
| 48 | +import org.junit.runner.RunWith; |
40 | 49 |
|
| 50 | +@RunWith(DataProviderRunner.class) |
41 | 51 | public class CqlVectorTest { |
42 | 52 |
|
43 | 53 | private static final Float[] VECTOR_ARGS = {1.0f, 2.5f}; |
44 | 54 |
|
45 | | - private void validate_built_vector(CqlVector<Float> vec) { |
| 55 | + @DataProvider |
| 56 | + public static Object[][] dataProvider() { |
| 57 | + return new Object[][]{ |
| 58 | + {new Float[]{1.0f, 2.5f}}, |
| 59 | + {new LocalTime[]{LocalTime.of(1, 2), LocalTime.of(3, 4)}}, |
| 60 | + {new List[]{Arrays.asList(1, 2), Arrays.asList(3, 4)}}, |
| 61 | + {new CqlVector[]{CqlVector.newInstance("a", "bc"), CqlVector.newInstance("d", "ef")}} |
| 62 | + }; |
| 63 | + } |
46 | 64 |
|
| 65 | + private void validate_built_vector(CqlVector<?> vec, Object[] expectedVals) { |
47 | 66 | assertThat(vec.size()).isEqualTo(2); |
48 | 67 | assertThat(vec.isEmpty()).isFalse(); |
49 | | - assertThat(vec.get(0)).isEqualTo(VECTOR_ARGS[0]); |
50 | | - assertThat(vec.get(1)).isEqualTo(VECTOR_ARGS[1]); |
| 68 | + assertThat(vec.get(0)).isEqualTo(expectedVals[0]); |
| 69 | + assertThat(vec.get(1)).isEqualTo(expectedVals[1]); |
51 | 70 | } |
52 | 71 |
|
| 72 | + @UseDataProvider("dataProvider") |
53 | 73 | @Test |
54 | | - public void should_build_vector_from_elements() { |
55 | | - |
56 | | - validate_built_vector(CqlVector.newInstance(VECTOR_ARGS)); |
| 74 | + public void should_build_vector_from_elements(Object[] vals) { |
| 75 | + validate_built_vector(CqlVector.newInstance(vals), vals); |
57 | 76 | } |
58 | 77 |
|
59 | 78 | @Test |
60 | | - public void should_build_vector_from_list() { |
61 | | - |
62 | | - validate_built_vector(CqlVector.newInstance(Lists.newArrayList(VECTOR_ARGS))); |
| 79 | + @UseDataProvider("dataProvider") |
| 80 | + public void should_build_vector_from_list(Object[] vals) { |
| 81 | + validate_built_vector(CqlVector.newInstance(Lists.newArrayList(vals)), vals); |
63 | 82 | } |
64 | 83 |
|
65 | 84 | @Test |
66 | | - public void should_build_vector_from_tostring_output() { |
67 | | - |
68 | | - CqlVector<Float> vector1 = CqlVector.newInstance(VECTOR_ARGS); |
69 | | - CqlVector<Float> vector2 = CqlVector.from(vector1.toString(), TypeCodecs.FLOAT); |
| 85 | + @UseDataProvider("dataProvider") |
| 86 | + public void should_build_vector_from_tostring_output(Object[] vals) { |
| 87 | + CqlVector<?> vector1 = CqlVector.newInstance(vals); |
| 88 | + TypeCodec<?> codec = CodecRegistry.DEFAULT.codecFor(vals[0]); |
| 89 | + CqlVector<?> vector2 = CqlVector.from(vector1.toString(), codec); |
70 | 90 | assertThat(vector2).isEqualTo(vector1); |
71 | 91 | } |
72 | 92 |
|
73 | 93 | @Test |
74 | 94 | public void should_throw_from_null_string() { |
75 | | - |
76 | 95 | assertThatThrownBy( |
77 | 96 | () -> { |
78 | 97 | CqlVector.from(null, TypeCodecs.FLOAT); |
@@ -123,94 +142,89 @@ public void should_build_empty_vector() { |
123 | 142 | } |
124 | 143 |
|
125 | 144 | @Test |
126 | | - public void should_behave_mostly_like_a_list() { |
127 | | - |
128 | | - CqlVector<Float> vector = CqlVector.newInstance(VECTOR_ARGS); |
129 | | - assertThat(vector.get(0)).isEqualTo(VECTOR_ARGS[0]); |
130 | | - Float newVal = VECTOR_ARGS[0] * 2; |
131 | | - vector.set(0, newVal); |
132 | | - assertThat(vector.get(0)).isEqualTo(newVal); |
| 145 | + @UseDataProvider("dataProvider") |
| 146 | + public <T> void should_behave_mostly_like_a_list(T[] vals) { |
| 147 | + CqlVector<T> vector = CqlVector.newInstance(vals); |
| 148 | + assertThat(vector.get(0)).isEqualTo(vals[0]); |
| 149 | + vector.set(0, vals[1]); |
| 150 | + assertThat(vector.get(0)).isEqualTo(vals[1]); |
133 | 151 | assertThat(vector.isEmpty()).isFalse(); |
134 | 152 | assertThat(vector.size()).isEqualTo(2); |
135 | | - assertThat(Iterators.toArray(vector.iterator(), Float.class)).isEqualTo(VECTOR_ARGS); |
| 153 | + Iterator<?> iterator = vector.iterator(); |
| 154 | + assertThat(iterator.next()).isEqualTo(vals[1]); |
| 155 | + assertThat(iterator.next()).isEqualTo(vals[1]); |
136 | 156 | } |
137 | 157 |
|
138 | 158 | @Test |
139 | | - public void should_play_nicely_with_streams() { |
140 | | - |
141 | | - CqlVector<Float> vector = CqlVector.newInstance(VECTOR_ARGS); |
142 | | - List<Float> results = |
| 159 | + @UseDataProvider("dataProvider") |
| 160 | + public <T> void should_play_nicely_with_streams(T[] vals) { |
| 161 | + CqlVector<T> vector = CqlVector.newInstance(vals); |
| 162 | + List<String> results = |
143 | 163 | vector.stream() |
144 | | - .map((f) -> f * 2) |
145 | | - .collect(Collectors.toCollection(() -> new ArrayList<Float>())); |
| 164 | + .map(Object::toString) |
| 165 | + .collect(Collectors.toCollection(() -> new ArrayList<String>())); |
146 | 166 | for (int i = 0; i < vector.size(); ++i) { |
147 | | - assertThat(results.get(i)).isEqualTo(vector.get(i) * 2); |
| 167 | + assertThat(results.get(i)).isEqualTo(vector.get(i).toString()); |
148 | 168 | } |
149 | 169 | } |
150 | 170 |
|
151 | 171 | @Test |
152 | | - public void should_reflect_changes_to_mutable_list() { |
153 | | - |
154 | | - List<Float> theList = Lists.newArrayList(1.1f, 2.2f, 3.3f); |
155 | | - CqlVector<Float> vector = CqlVector.newInstance(theList); |
156 | | - assertThat(vector.size()).isEqualTo(3); |
157 | | - assertThat(vector.get(2)).isEqualTo(3.3f); |
158 | | - |
159 | | - float newVal1 = 4.4f; |
160 | | - theList.set(2, newVal1); |
161 | | - assertThat(vector.size()).isEqualTo(3); |
162 | | - assertThat(vector.get(2)).isEqualTo(newVal1); |
163 | | - |
164 | | - float newVal2 = 5.5f; |
165 | | - theList.add(newVal2); |
166 | | - assertThat(vector.size()).isEqualTo(4); |
167 | | - assertThat(vector.get(3)).isEqualTo(newVal2); |
| 172 | + @UseDataProvider("dataProvider") |
| 173 | + public <T> void should_reflect_changes_to_mutable_list(T[] vals) { |
| 174 | + List<T> theList = Lists.newArrayList(vals); |
| 175 | + CqlVector<T> vector = CqlVector.newInstance(theList); |
| 176 | + assertThat(vector.size()).isEqualTo(2); |
| 177 | + assertThat(vector.get(1)).isEqualTo(vals[1]); |
| 178 | + |
| 179 | + T newVal = vals[0]; |
| 180 | + theList.set(1, newVal); |
| 181 | + assertThat(vector.size()).isEqualTo(2); |
| 182 | + assertThat(vector.get(1)).isEqualTo(newVal); |
168 | 183 | } |
169 | 184 |
|
170 | 185 | @Test |
171 | | - public void should_reflect_changes_to_array() { |
172 | | - |
173 | | - Float[] theArray = new Float[] {1.1f, 2.2f, 3.3f}; |
174 | | - CqlVector<Float> vector = CqlVector.newInstance(theArray); |
175 | | - assertThat(vector.size()).isEqualTo(3); |
176 | | - assertThat(vector.get(2)).isEqualTo(3.3f); |
| 186 | + @UseDataProvider("dataProvider") |
| 187 | + public <T> void should_reflect_changes_to_array(T[] theArray) { |
| 188 | + CqlVector<T> vector = CqlVector.newInstance(theArray); |
| 189 | + assertThat(vector.size()).isEqualTo(2); |
| 190 | + assertThat(vector.get(1)).isEqualTo(theArray[1]); |
177 | 191 |
|
178 | | - float newVal1 = 4.4f; |
179 | | - theArray[2] = newVal1; |
180 | | - assertThat(vector.size()).isEqualTo(3); |
181 | | - assertThat(vector.get(2)).isEqualTo(newVal1); |
| 192 | + T newVal = theArray[0]; |
| 193 | + theArray[1] = newVal; |
| 194 | + assertThat(vector.size()).isEqualTo(2); |
| 195 | + assertThat(vector.get(1)).isEqualTo(newVal); |
182 | 196 | } |
183 | 197 |
|
184 | 198 | @Test |
185 | | - public void should_correctly_compare_vectors() { |
186 | | - |
187 | | - Float[] args = VECTOR_ARGS.clone(); |
188 | | - CqlVector<Float> vector1 = CqlVector.newInstance(args); |
189 | | - CqlVector<Float> vector2 = CqlVector.newInstance(args); |
190 | | - CqlVector<Float> vector3 = CqlVector.newInstance(Lists.newArrayList(args)); |
| 199 | + @UseDataProvider("dataProvider") |
| 200 | + public <T> void should_correctly_compare_vectors(T[] vals) { |
| 201 | + CqlVector<T> vector1 = CqlVector.newInstance(vals); |
| 202 | + CqlVector<T> vector2 = CqlVector.newInstance(vals); |
| 203 | + CqlVector<T> vector3 = CqlVector.newInstance(Lists.newArrayList(vals)); |
191 | 204 | assertThat(vector1).isNotSameAs(vector2); |
192 | 205 | assertThat(vector1).isEqualTo(vector2); |
193 | 206 | assertThat(vector1).isNotSameAs(vector3); |
194 | 207 | assertThat(vector1).isEqualTo(vector3); |
195 | 208 |
|
196 | | - Float[] differentArgs = args.clone(); |
197 | | - float newVal = differentArgs[0] * 2; |
| 209 | + T[] differentArgs = Arrays.copyOf(vals, vals.length); |
| 210 | + T newVal = differentArgs[1]; |
198 | 211 | differentArgs[0] = newVal; |
199 | | - CqlVector<Float> vector4 = CqlVector.newInstance(differentArgs); |
| 212 | + CqlVector<T> vector4 = CqlVector.newInstance(differentArgs); |
200 | 213 | assertThat(vector1).isNotSameAs(vector4); |
201 | 214 | assertThat(vector1).isNotEqualTo(vector4); |
202 | 215 |
|
203 | | - Float[] biggerArgs = Arrays.copyOf(args, args.length + 1); |
| 216 | + T[] biggerArgs = Arrays.copyOf(vals, vals.length + 1); |
204 | 217 | biggerArgs[biggerArgs.length - 1] = newVal; |
205 | | - CqlVector<Float> vector5 = CqlVector.newInstance(biggerArgs); |
| 218 | + CqlVector<T> vector5 = CqlVector.newInstance(biggerArgs); |
206 | 219 | assertThat(vector1).isNotSameAs(vector5); |
207 | 220 | assertThat(vector1).isNotEqualTo(vector5); |
208 | 221 | } |
209 | 222 |
|
210 | 223 | @Test |
211 | | - public void should_serialize_and_deserialize() throws Exception { |
212 | | - CqlVector<Float> initial = CqlVector.newInstance(VECTOR_ARGS); |
213 | | - CqlVector<Float> deserialized = SerializationHelper.serializeAndDeserialize(initial); |
| 224 | + @UseDataProvider("dataProvider") |
| 225 | + public <T> void should_serialize_and_deserialize(T[] vals) throws Exception { |
| 226 | + CqlVector<T> initial = CqlVector.newInstance(vals); |
| 227 | + CqlVector<T> deserialized = SerializationHelper.serializeAndDeserialize(initial); |
214 | 228 | assertThat(deserialized).isEqualTo(initial); |
215 | 229 | } |
216 | 230 |
|
|
0 commit comments