forked from apache/ignite
/
BinaryFieldExtractionSelfTest.java
350 lines (275 loc) · 10.3 KB
/
BinaryFieldExtractionSelfTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.binary;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.sql.Time;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.ignite.binary.BinaryObjectException;
import org.apache.ignite.configuration.BinaryConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.util.IgniteUtils;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.marshaller.MarshallerContextTestImpl;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.junit.Test;
import static org.apache.ignite.testframework.GridTestUtils.assertThrows;
/**
*
*/
public class BinaryFieldExtractionSelfTest extends GridCommonAbstractTest {
/**
* Create marshaller.
*
* @return Binary marshaller.
* @throws Exception If failed.
*/
protected BinaryMarshaller createMarshaller() throws Exception {
BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(),
log());
BinaryMarshaller marsh = new BinaryMarshaller();
BinaryConfiguration bCfg = new BinaryConfiguration();
IgniteConfiguration iCfg = new IgniteConfiguration();
iCfg.setBinaryConfiguration(bCfg);
marsh.setContext(new MarshallerContextTestImpl(null));
IgniteUtils.invoke(BinaryMarshaller.class, marsh, "setBinaryContext", ctx, iCfg);
return marsh;
}
/**
* @throws Exception If failed.
*/
@Test
public void testPrimitiveMarshalling() throws Exception {
BinaryMarshaller marsh = createMarshaller();
ThreadLocalRandom rnd = ThreadLocalRandom.current();
TestObject obj = new TestObject(0);
BinaryObjectImpl binObj = toBinary(obj, marsh);
BinaryFieldEx[] fields = new BinaryFieldEx[] {
(BinaryFieldEx)binObj.type().field("bVal"),
(BinaryFieldEx)binObj.type().field("cVal"),
(BinaryFieldEx)binObj.type().field("sVal"),
(BinaryFieldEx)binObj.type().field("iVal"),
(BinaryFieldEx)binObj.type().field("lVal"),
(BinaryFieldEx)binObj.type().field("fVal"),
(BinaryFieldEx)binObj.type().field("dVal")
};
ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);
for (int i = 0; i < 100; i++) {
TestObject to = new TestObject(rnd.nextLong());
BinaryObjectImpl bObj = toBinary(to, marsh);
for (BinaryFieldEx field : fields)
field.writeField(bObj, buf);
buf.flip();
for (BinaryFieldEx field : fields)
assertEquals(field.value(bObj), field.readField(buf));
buf.flip();
}
}
/**
* @throws Exception If failed.
*/
@Test
public void testTimeMarshalling() throws Exception {
BinaryMarshaller marsh = createMarshaller();
TimeValue obj = new TimeValue(11111L);
BinaryObjectImpl binObj = toBinary(obj, marsh);
BinaryFieldEx field = (BinaryFieldEx)binObj.type().field("time");
ByteBuffer buf = ByteBuffer.allocate(16);
field.writeField(binObj, buf);
buf.flip();
assertEquals(field.value(binObj), field.<Time>readField(buf));
}
/**
* Checking the exception and its text when changing the typeId of a
* BinaryField.
*
* @throws Exception If failed.
*/
@Test
public void testChangeTypeIdOfBinaryField() throws Exception {
BinaryMarshaller marsh = createMarshaller();
TimeValue timeVal = new TimeValue(11111L);
DecimalValue decimalVal = new DecimalValue(BigDecimal.ZERO);
BinaryObjectImpl timeValBinObj = toBinary(timeVal, marsh);
BinaryObjectImpl decimalValBinObj = toBinary(decimalVal, marsh);
BinaryFieldEx timeBinField = (BinaryFieldEx)timeValBinObj.type().field("time");
Field typeIdField = U.findField(timeBinField.getClass(), "typeId");
typeIdField.set(timeBinField, decimalValBinObj.typeId());
String expMsg = exceptionMessageOfDifferentTypeIdBinaryField(
decimalValBinObj.typeId(),
decimalVal.getClass().getName(),
timeValBinObj.typeId(),
timeVal.getClass().getName(),
U.field(timeBinField, "fieldId"),
timeBinField.name(),
null
);
assertThrows(log, () -> timeBinField.value(timeValBinObj), BinaryObjectException.class, expMsg);
}
/**
* Checking the exception and its text when changing the typeId of a
* BinaryField in case of not finding the expected BinaryType.
*
* @throws Exception If failed.
*/
@Test
public void testChangeTypeIdOfBinaryFieldCaseNotFoundExpectedTypeId() throws Exception {
BinaryMarshaller marsh = createMarshaller();
TimeValue timeVal = new TimeValue(11111L);
BinaryObjectImpl timeValBinObj = toBinary(timeVal, marsh);
BinaryFieldEx timeBinField = (BinaryFieldEx)timeValBinObj.type().field("time");
int newTypeId = timeValBinObj.typeId() + 1;
Field typeIdField = U.findField(timeBinField.getClass(), "typeId");
typeIdField.set(timeBinField, newTypeId);
String expMsg = exceptionMessageOfDifferentTypeIdBinaryField(
newTypeId,
null,
timeValBinObj.typeId(),
timeVal.getClass().getName(),
U.field(timeBinField, "fieldId"),
timeBinField.name(),
null
);
assertThrows(log, () -> timeBinField.value(timeValBinObj), BinaryObjectException.class, expMsg);
}
/**
* @throws Exception If failed.
*/
@Test
public void testDecimalFieldMarshalling() throws Exception {
BinaryMarshaller marsh = createMarshaller();
BigDecimal values[] = new BigDecimal[] { BigDecimal.ZERO, BigDecimal.ONE, BigDecimal.TEN,
new BigDecimal("-100.5"), BigDecimal.valueOf(Long.MAX_VALUE, 0),
BigDecimal.valueOf(Long.MIN_VALUE, 0), BigDecimal.valueOf(Long.MAX_VALUE, 8),
BigDecimal.valueOf(Long.MIN_VALUE, 8)};
DecimalValue decVal = new DecimalValue(values[0]);
BinaryObjectImpl binObj = toBinary(decVal, marsh);
BinaryFieldEx field = (BinaryFieldEx)binObj.type().field("decVal");
ByteBuffer buf = ByteBuffer.allocate(64);
for (BigDecimal value : values) {
decVal = new DecimalValue(value);
binObj = toBinary(decVal, marsh);
field.writeField(binObj, buf);
buf.flip();
assertEquals(field.value(binObj), field.readField(buf));
buf.clear();
}
}
/**
* @param obj Object to transform to a binary object.
* @param marsh Binary marshaller.
* @return Binary object.
*/
protected BinaryObjectImpl toBinary(Object obj, BinaryMarshaller marsh) throws Exception {
byte[] bytes = marsh.marshal(obj);
return new BinaryObjectImpl(binaryContext(marsh), bytes, 0);
}
/**
* Get binary context for the current marshaller.
*
* @param marsh Marshaller.
* @return Binary context.
*/
protected static BinaryContext binaryContext(BinaryMarshaller marsh) {
GridBinaryMarshaller impl = U.field(marsh, "impl");
return impl.context();
}
/**
*
*/
private static class TestObject {
/** */
private byte bVal;
/** */
private char cVal;
/** */
private short sVal;
/** */
private int iVal;
/** */
private long lVal;
/** */
private float fVal;
/** */
private double dVal;
/**
* @param seed Seed.
*/
private TestObject(long seed) {
bVal = (byte)seed;
cVal = (char)seed;
sVal = (short)seed;
iVal = (int)seed;
lVal = seed;
fVal = seed;
dVal = seed;
}
}
/** */
private static class TimeValue {
/** */
private Time time;
/**
* @param time Time.
*/
TimeValue(long time) {
this.time = new Time(time);
}
}
/**
*
*/
private static class DecimalValue {
/** */
private BigDecimal decVal;
/**
*
* @param decVal Value to use
*/
private DecimalValue(BigDecimal decVal) {
this.decVal = decVal;
}
}
/**
* Creates an exception text for the case when the typeId differs in the
* BinaryField and the BinaryObject.
*
* @param expTypeId Expected typeId.
* @param expTypeName Expected typeName.
* @param actualTypeId Actual typeId.
* @param actualTypeName Actual typeName.
* @param fieldId FieldId.
* @param fieldName FieldName.
* @param fieldType FieldType.
* @return Exception message.
*/
private String exceptionMessageOfDifferentTypeIdBinaryField(
int expTypeId,
String expTypeName,
int actualTypeId,
String actualTypeName,
int fieldId,
String fieldName,
String fieldType
) {
return "Failed to get field because type ID of passed object differs from type ID this " +
"BinaryField belongs to [expected=[typeId=" + expTypeId + ", typeName=" + expTypeName +
"], actual=[typeId=" + actualTypeId + ", typeName=" + actualTypeName + "], fieldId=" + fieldId +
", fieldName=" + fieldName + ", fieldType=" + fieldType + "]";
}
}