use of org.apache.geode.pdx.internal.PdxType in project geode by apache.
the class DataTypeJUnitTest method getDataTypeShouldReturnUnknownIfPDXTypeIsNull.
@Test
public void getDataTypeShouldReturnUnknownIfPDXTypeIsNull() throws IOException {
int somePdxTypeInt = 1;
PdxType somePdxType = null;
TypeRegistry mockTypeRegistry = mock(TypeRegistry.class);
when(mockTypeRegistry.getType(somePdxTypeInt)).thenReturn(somePdxType);
GemFireCacheImpl pdxInstance = mock(GemFireCacheImpl.class);
when(pdxInstance.getPdxRegistry()).thenReturn(mockTypeRegistry);
PowerMockito.mockStatic(GemFireCacheImpl.class);
when(GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.")).thenReturn(pdxInstance);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
out.writeByte(DSCODE.PDX);
out.writeInt(somePdxTypeInt);
byte[] bytes = baos.toByteArray();
String type = DataType.getDataType(bytes);
assertThat(type).isEqualTo("org.apache.geode.pdx.PdxInstance: unknown id=" + somePdxTypeInt);
}
use of org.apache.geode.pdx.internal.PdxType in project geode by apache.
the class DataTypeJUnitTest method getDataTypeShouldReturnPDXType.
@Test
public void getDataTypeShouldReturnPDXType() throws IOException {
int somePdxTypeInt = 1;
PdxType somePdxType = mock(PdxType.class);
doReturn("PDXType").when(somePdxType).getClassName();
TypeRegistry mockTypeRegistry = mock(TypeRegistry.class);
when(mockTypeRegistry.getType(somePdxTypeInt)).thenReturn(somePdxType);
GemFireCacheImpl pdxInstance = mock(GemFireCacheImpl.class);
when(pdxInstance.getPdxRegistry()).thenReturn(mockTypeRegistry);
PowerMockito.mockStatic(GemFireCacheImpl.class);
when(GemFireCacheImpl.getForPdx(anyString())).thenReturn(pdxInstance);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
out.writeByte(DSCODE.PDX);
out.writeInt(somePdxTypeInt);
byte[] bytes = baos.toByteArray();
String type = DataType.getDataType(bytes);
assertThat(type).isEqualTo("org.apache.geode.pdx.PdxInstance:PDXType");
}
use of org.apache.geode.pdx.internal.PdxType in project geode by apache.
the class PdxSerializableJUnitTest method testFieldInsert.
@Test
public void testFieldInsert() throws Exception {
MyEvolvablePdx.setVersion(1);
MyEvolvablePdx pdx = new MyEvolvablePdx(7);
assertEquals(7, pdx.f1);
assertEquals(0, pdx.f2);
MyEvolvablePdx.setVersion(3);
pdx = new MyEvolvablePdx(7);
assertEquals(7, pdx.f1);
assertEquals(8, pdx.f2);
byte[] v3actual = createBlob(pdx);
int v3typeId = getBlobPdxTypeId(v3actual);
c.getPdxRegistry().removeLocal(pdx);
MyEvolvablePdx.setVersion(1);
MyEvolvablePdx pdxv1 = deblob(v3actual);
assertEquals(7, pdxv1.f1);
assertEquals(0, pdxv1.f2);
int numPdxTypes = getNumPdxTypes();
// now reserialize and make sure f2 is preserved
byte[] v1actual = createBlob(pdxv1);
int mergedTypeId = getBlobPdxTypeId(v1actual);
assertEquals(numPdxTypes + 1, getNumPdxTypes());
TypeRegistry tr = c.getPdxRegistry();
PdxType v3Type = tr.getType(v3typeId);
PdxType mergedType = tr.getType(mergedTypeId);
assertFalse(mergedType.equals(v3Type));
assertTrue(mergedType.compatible(v3Type));
MyEvolvablePdx.setVersion(3);
c.getPdxRegistry().removeLocal(pdxv1);
MyEvolvablePdx pdxv3 = deblob(v1actual);
assertEquals(7, pdxv3.f1);
assertEquals(8, pdxv3.f2);
}
use of org.apache.geode.pdx.internal.PdxType in project geode by apache.
the class PdxSerializableJUnitTest method testByteFormatForStrings.
@Test
public void testByteFormatForStrings() throws Exception {
boolean myFlag = true;
short myShort = 25;
String myString1 = "Class4_myString1";
long myLong = 15654;
String myString2 = "Class4_myString2";
String myString3 = "Class4_myString3";
int myInt = 1420;
float myFloat = 123.023f;
HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
SimpleClass1 pdx = new SimpleClass1(myFlag, myShort, myString1, myLong, myString2, myString3, myInt, myFloat);
DataSerializer.writeObject(pdx, out);
int typeId = getPdxTypeIdForClass(SimpleClass1.class);
HeapDataOutputStream hdos1 = new HeapDataOutputStream(Version.CURRENT);
DataSerializer.writeString(myString1, hdos1);
byte[] str1Bytes = hdos1.toByteArray();
HeapDataOutputStream hdos2 = new HeapDataOutputStream(Version.CURRENT);
DataSerializer.writeString(myString2, hdos2);
byte[] str2Bytes = hdos2.toByteArray();
HeapDataOutputStream hdos3 = new HeapDataOutputStream(Version.CURRENT);
DataSerializer.writeString(myString3, hdos3);
byte[] str3Bytes = hdos3.toByteArray();
int length = 1 + /* myFlag */
2 + /* myShort */
8 + /* myLong */
4 + /* myInt */
4 + /* myFloat */
str1Bytes.length + str2Bytes.length + str3Bytes.length + 2;
int offset1 = 1 + 2;
int offset2 = offset1 + 8 + str1Bytes.length;
int offset3 = offset1 + 8 + str1Bytes.length + str2Bytes.length;
byte[] actual = out.toByteArray();
int floatBytes = Float.floatToRawIntBits(myFloat);
Byte[] expected = new Byte[] { // byte
DSCODE.PDX, // int -
(byte) (length >> 24), // int -
(byte) (length >> 16), // int -
(byte) (length >> 8), // int -
(byte) length, // int -
(byte) (typeId >> 24), // int -
(byte) (typeId >> 16), // int -
(byte) (typeId >> 8), // int -
(byte) typeId, // boolean - myFlag = true
1, // short - myShort
(byte) (myShort >> 8), // short - myShort
(byte) myShort, (byte) (myLong >> 56), (byte) (myLong >> 48), (byte) (myLong >> 40), (byte) (myLong >> 32), // long -
(byte) (myLong >> 24), // long -
(byte) (myLong >> 16), // long -
(byte) (myLong >> 8), // long -
(byte) myLong, // int -
(byte) (myInt >> 24), // int -
(byte) (myInt >> 16), // int -
(byte) (myInt >> 8), // int -
(byte) myInt, // myInt
(byte) (floatBytes >> 24), (byte) (floatBytes >> 16), (byte) (floatBytes >> 8), // float - myFloat
(byte) floatBytes, // offset of myString3
(byte) offset3, // offset of myString2
(byte) offset2 };
for (int i = (str1Bytes.length - 1); i >= 0; i--) {
expected = // +
(Byte[]) ArrayUtils.insert(expected, offset1 + PdxWriterImpl.HEADER_SIZE, str1Bytes[i]);
// 5
// for:
// 1
// for
// DSCODE.PDX
// and
// 4
// for
// byte
// stream
// length
}
for (int i = (str2Bytes.length - 1); i >= 0; i--) {
expected = (Byte[]) ArrayUtils.insert(expected, offset2 + PdxWriterImpl.HEADER_SIZE, str2Bytes[i]);
}
for (int i = (str3Bytes.length - 1); i >= 0; i--) {
expected = (Byte[]) ArrayUtils.insert(expected, offset3 + PdxWriterImpl.HEADER_SIZE, str3Bytes[i]);
}
StringBuffer msg = new StringBuffer("Actual output: ");
for (byte val : actual) {
msg.append(val + ", ");
}
msg.append("\nExpected output: ");
for (byte val : expected) {
msg.append(val + ", ");
}
if (actual.length != expected.length) {
System.out.println(msg.toString());
}
assertTrue("Mismatch in length, actual.length: " + actual.length + " and expected length: " + expected.length, actual.length == expected.length);
for (int i = 0; i < actual.length; i++) {
if (actual[i] != expected[i]) {
System.out.println(msg.toString());
}
assertTrue("Mismatch at index " + i, actual[i] == expected[i]);
}
System.out.println("\n");
DataInput in = new DataInputStream(new ByteArrayInputStream(actual));
SimpleClass1 actualVal = (SimpleClass1) DataSerializer.readObject(in);
// System.out.println("actualVal..."+actualVal);
assertTrue("Mismatch in write and read value: Value Write..." + pdx + " Value Read..." + actualVal, pdx.equals(actualVal));
c.setReadSerialized(true);
try {
in = new DataInputStream(new ByteArrayInputStream(actual));
PdxInstance pi = (PdxInstance) DataSerializer.readObject(in);
actualVal = (SimpleClass1) pi.getObject();
assertTrue("Mismatch in write and read value: Value Write..." + pdx + " Value Read..." + actualVal, pdx.equals(actualVal));
assertTrue(pi.hasField("myFlag"));
assertTrue(pi.hasField("myShort"));
assertTrue(pi.hasField("myString1"));
assertTrue(pi.hasField("myLong"));
assertTrue(pi.hasField("myString2"));
assertTrue(pi.hasField("myString3"));
assertTrue(pi.hasField("myInt"));
assertTrue(pi.hasField("myFloat"));
assertEquals(pdx.isMyFlag(), pi.getField("myFlag"));
assertEquals(pdx.getMyShort(), pi.getField("myShort"));
assertEquals(pdx.getMyString1(), pi.getField("myString1"));
assertEquals(pdx.getMyLong(), pi.getField("myLong"));
assertEquals(pdx.getMyString2(), pi.getField("myString2"));
assertEquals(pdx.getMyString3(), pi.getField("myString3"));
assertEquals(pdx.getMyInt(), pi.getField("myInt"));
assertEquals(pdx.getMyFloat(), pi.getField("myFloat"));
PdxReaderImpl reader = (PdxReaderImpl) pi;
PdxType type = reader.getPdxType();
assertEquals(SimpleClass1.class.getName(), type.getClassName());
assertEquals(8, type.getFieldCount());
assertEquals(2, type.getVariableLengthFieldCount());
assertEquals(0, type.getPdxField("myFlag").getFieldIndex());
assertEquals(1, type.getPdxField("myShort").getFieldIndex());
assertEquals(2, type.getPdxField("myString1").getFieldIndex());
assertEquals(3, type.getPdxField("myLong").getFieldIndex());
assertEquals(4, type.getPdxField("myString2").getFieldIndex());
assertEquals(5, type.getPdxField("myString3").getFieldIndex());
assertEquals(6, type.getPdxField("myInt").getFieldIndex());
assertEquals(7, type.getPdxField("myFloat").getFieldIndex());
assertEquals(FieldType.BOOLEAN, type.getPdxField("myFlag").getFieldType());
assertEquals(FieldType.SHORT, type.getPdxField("myShort").getFieldType());
assertEquals(FieldType.STRING, type.getPdxField("myString1").getFieldType());
assertEquals(FieldType.LONG, type.getPdxField("myLong").getFieldType());
assertEquals(FieldType.STRING, type.getPdxField("myString2").getFieldType());
assertEquals(FieldType.STRING, type.getPdxField("myString3").getFieldType());
assertEquals(FieldType.INT, type.getPdxField("myInt").getFieldType());
assertEquals(FieldType.FLOAT, type.getPdxField("myFloat").getFieldType());
assertEquals("myFlag", type.getPdxField("myFlag").getFieldName());
assertEquals("myShort", type.getPdxField("myShort").getFieldName());
assertEquals("myString1", type.getPdxField("myString1").getFieldName());
assertEquals("myLong", type.getPdxField("myLong").getFieldName());
assertEquals("myString2", type.getPdxField("myString2").getFieldName());
assertEquals("myString3", type.getPdxField("myString3").getFieldName());
assertEquals("myInt", type.getPdxField("myInt").getFieldName());
assertEquals("myFloat", type.getPdxField("myFloat").getFieldName());
assertEquals(0, type.getPdxField("myFlag").getVarLenFieldSeqId());
assertEquals(0, type.getPdxField("myShort").getVarLenFieldSeqId());
assertEquals(0, type.getPdxField("myString1").getVarLenFieldSeqId());
assertEquals(0, type.getPdxField("myLong").getVarLenFieldSeqId());
assertEquals(1, type.getPdxField("myString2").getVarLenFieldSeqId());
assertEquals(2, type.getPdxField("myString3").getVarLenFieldSeqId());
assertEquals(2, type.getPdxField("myInt").getVarLenFieldSeqId());
assertEquals(2, type.getPdxField("myFloat").getVarLenFieldSeqId());
assertEquals(false, type.getPdxField("myFlag").isVariableLengthType());
assertEquals(false, type.getPdxField("myShort").isVariableLengthType());
assertEquals(true, type.getPdxField("myString1").isVariableLengthType());
assertEquals(false, type.getPdxField("myLong").isVariableLengthType());
assertEquals(true, type.getPdxField("myString2").isVariableLengthType());
assertEquals(true, type.getPdxField("myString3").isVariableLengthType());
assertEquals(false, type.getPdxField("myInt").isVariableLengthType());
assertEquals(false, type.getPdxField("myFloat").isVariableLengthType());
assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.isMyFlag() ? 1 : 0) }), reader.getRaw(0));
assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.getMyShort() >> 8), (byte) pdx.getMyShort() }), reader.getRaw(1));
assertEquals(ByteSourceFactory.wrap(str1Bytes), reader.getRaw(2));
assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.getMyLong() >> 56), (byte) (pdx.getMyLong() >> 48), (byte) (pdx.getMyLong() >> 40), (byte) (pdx.getMyLong() >> 32), (byte) (pdx.getMyLong() >> 24), (byte) (pdx.getMyLong() >> 16), (byte) (pdx.getMyLong() >> 8), (byte) pdx.getMyLong() }), reader.getRaw(3));
assertEquals(ByteSourceFactory.wrap(str2Bytes), reader.getRaw(4));
assertEquals(ByteSourceFactory.wrap(str3Bytes), reader.getRaw(5));
assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.getMyInt() >> 24), (byte) (pdx.getMyInt() >> 16), (byte) (pdx.getMyInt() >> 8), (byte) pdx.getMyInt() }), reader.getRaw(6));
assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (floatBytes >> 24), (byte) (floatBytes >> 16), (byte) (floatBytes >> 8), (byte) floatBytes }), reader.getRaw(7));
} finally {
c.setReadSerialized(false);
}
}
use of org.apache.geode.pdx.internal.PdxType in project geode by apache.
the class PdxAttributesJUnitTest method testPdxTypeId.
@Test
public void testPdxTypeId() throws Exception {
int dsId = 5;
CacheFactory cf = new CacheFactory();
cf.set(MCAST_PORT, "0");
cf.set(ConfigurationProperties.DISTRIBUTED_SYSTEM_ID, String.valueOf(dsId));
Cache cache = cf.create();
// define a type.
defineAType();
Region pdxRegion = cache.getRegion(PeerTypeRegistration.REGION_NAME);
Iterator itr = pdxRegion.entrySet().iterator();
boolean found = false;
boolean foundEnum = false;
while (itr.hasNext()) {
Map.Entry ent = (Map.Entry) itr.next();
if (ent.getKey() instanceof Integer) {
int pdxTypeId = (int) ent.getKey();
PdxType pdxType = (PdxType) ent.getValue();
int pdxTypeHashcode = pdxType.hashCode();
System.out.println("pdx hashcode " + pdxTypeHashcode);
int expectedPdxTypeId = (dsId << 24) | (PeerTypeRegistration.PLACE_HOLDER_FOR_TYPE_ID & pdxTypeHashcode);
assertEquals(expectedPdxTypeId, pdxTypeId);
found = true;
} else {
EnumId enumId = (EnumId) ent.getKey();
EnumInfo enumInfo = (EnumInfo) ent.getValue();
EnumInfo expectedEnumInfo = new EnumInfo(SimpleEnum.TWO);
int expectKey = (dsId << 24) | (PeerTypeRegistration.PLACE_HOLDER_FOR_TYPE_ID & expectedEnumInfo.hashCode());
;
assertEquals(expectKey, enumId.intValue());
foundEnum = true;
}
}
assertEquals(true, found);
assertEquals(true, foundEnum);
cache.close();
}
Aggregations