use of org.apache.geode.pdx.internal.EnumInfo in project geode by apache.
the class JsonWriter method writeValueAsJson.
public static void writeValueAsJson(JsonGenerator generator, Object value, String pdxField) throws JsonGenerationException, IOException {
if (value == null) {
generator.writeNull();
} else if (value.getClass().equals(Boolean.class)) {
boolean b = (Boolean) value;
generator.writeBoolean(b);
} else if (value.getClass().equals(Byte.class)) {
Byte b = (Byte) value;
generator.writeNumber(b);
} else if (value.getClass().equals(Short.class)) {
Short b = (Short) value;
generator.writeNumber(b);
} else if (value.getClass().equals(Integer.class)) {
int i = (Integer) value;
generator.writeNumber(i);
} else if (value.getClass().equals(Long.class)) {
long i = (Long) value;
generator.writeNumber(i);
} else if (value.getClass().equals(BigInteger.class)) {
BigInteger i = (BigInteger) value;
generator.writeNumber(i);
} else if (value.getClass().equals(Float.class)) {
float i = (Float) value;
generator.writeNumber(i);
} else if (value.getClass().equals(BigDecimal.class)) {
BigDecimal i = (BigDecimal) value;
generator.writeNumber(i);
} else if (value.getClass().equals(Double.class)) {
double d = (Double) value;
generator.writeNumber(d);
} else if (value.getClass().equals(String.class)) {
String s = (String) value;
generator.writeString(s);
} else if (value.getClass().isArray()) {
writeArrayAsJson(generator, value, pdxField);
} else if (value.getClass().equals(Link.class)) {
writeLinkAsJson(generator, (Link) value, pdxField);
// } else if (value.getClass().equals(Date.class)) {
// generator.writeObject((Date) value);
} else if (value.getClass().equals(EnumInfo.class)) {
/*
* try { generator.writeString(((EnumInfo)value).getEnum().name()); } catch
* (ClassNotFoundException e) { throw new
* IllegalStateException("PdxInstance returns unknwon pdxfield " + pdxField + " for type " +
* value); }
*/
generator.writeString(value.toString());
} else if (value.getClass().equals(PdxInstanceEnumInfo.class)) {
generator.writeString(value.toString());
} else {
if (value instanceof Struct) {
writeStructAsJson(generator, (StructImpl) value);
} else if (value instanceof PdxInstance) {
writePdxInstanceAsJson(generator, (PdxInstance) value);
} else if (value instanceof Collection) {
writeCollectionAsJson(generator, (Collection<?>) value);
} else if (value instanceof Map) {
writeMapAsJson(generator, (Map) value, pdxField);
} else {
generator.writeObject(value);
}
}
}
use of org.apache.geode.pdx.internal.EnumInfo in project geode by apache.
the class DataTypeJUnitTest method getDataTypeShouldReturnPDXEnumType.
@Test
public void getDataTypeShouldReturnPDXEnumType() throws IOException {
int somePdxEnumId = 1;
EnumInfo somePdxEnumInfo = mock(EnumInfo.class);
doReturn("PDXENUM").when(somePdxEnumInfo).getClassName();
TypeRegistry mockTypeRegistry = mock(TypeRegistry.class);
when(mockTypeRegistry.getEnumInfoById(0)).thenReturn(somePdxEnumInfo);
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_ENUM);
out.writeInt(somePdxEnumId);
byte[] bytes = baos.toByteArray();
String type = DataType.getDataType(bytes);
assertThat(type).isEqualTo("PdxRegistry/java.lang.Enum:PDXENUM");
}
use of org.apache.geode.pdx.internal.EnumInfo in project geode by apache.
the class PdxAttributesJUnitTest method testDuplicatePdxTypeId.
@Test
public void testDuplicatePdxTypeId() 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 foundException = false;
boolean foundEnumException = false;
while (itr.hasNext()) {
Map.Entry ent = (Map.Entry) itr.next();
if (ent.getKey() instanceof Integer) {
int pdxTypeId = (int) ent.getKey();
try {
pdxRegion.put(pdxTypeId, new PdxType());
} catch (CacheWriterException cwe) {
foundException = true;
}
} else {
EnumId enumId = (EnumId) ent.getKey();
EnumInfo enumInfo = new EnumInfo(SimpleEnum.ONE);
try {
pdxRegion.put(enumId, enumInfo);
} catch (CacheWriterException cwe) {
foundEnumException = true;
}
}
}
assertEquals(true, foundException);
assertEquals(true, foundEnumException);
cache.close();
}
use of org.apache.geode.pdx.internal.EnumInfo in project geode by apache.
the class PdxAttributesJUnitTest method testPdxTypeIdWithNegativeDsId.
@Test
public void testPdxTypeIdWithNegativeDsId() throws Exception {
// in this case geode will use 0 as dsId
int dsId = -1;
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 = 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 = PeerTypeRegistration.PLACE_HOLDER_FOR_TYPE_ID & expectedEnumInfo.hashCode();
;
assertEquals(expectKey, enumId.intValue());
foundEnum = true;
}
}
assertEquals(true, found);
cache.close();
}
use of org.apache.geode.pdx.internal.EnumInfo in project geode by apache.
the class PdxTypeExportDUnitTest method testPeer.
@Test
public void testPeer() throws Exception {
Region r = getCache().getRegion("pdxtest");
r.get(1);
TypeRegistry tr = ((GemFireCacheImpl) getCache()).getPdxRegistry();
Collection<PdxType> types = tr.typeMap().values();
assertEquals(MyObjectPdx.class.getName(), types.iterator().next().getClassName());
Collection<EnumInfo> enums = tr.enumMap().values();
assertEquals(MyEnumPdx.const1.name(), enums.iterator().next().getEnum().name());
}
Aggregations