Search in sources :

Example 1 with EnumInfo

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);
        }
    }
}
Also used : PdxInstanceEnumInfo(org.apache.geode.pdx.internal.EnumInfo.PdxInstanceEnumInfo) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) BigDecimal(java.math.BigDecimal) Struct(org.apache.geode.cache.query.Struct) BigInteger(java.math.BigInteger) PdxInstance(org.apache.geode.pdx.PdxInstance) BigInteger(java.math.BigInteger) Collection(java.util.Collection) Map(java.util.Map)

Example 2 with EnumInfo

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");
}
Also used : DataOutputStream(java.io.DataOutputStream) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry) UnitTest(org.apache.geode.test.junit.categories.UnitTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with EnumInfo

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();
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) CacheFactory(org.apache.geode.cache.CacheFactory) Map(java.util.Map) Cache(org.apache.geode.cache.Cache) CacheWriterException(org.apache.geode.cache.CacheWriterException) EnumId(org.apache.geode.pdx.internal.EnumId) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 4 with EnumInfo

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();
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) CacheFactory(org.apache.geode.cache.CacheFactory) Map(java.util.Map) Cache(org.apache.geode.cache.Cache) EnumId(org.apache.geode.pdx.internal.EnumId) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 5 with EnumInfo

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());
}
Also used : MyObjectPdx(com.examples.snapshot.MyObjectPdx) PdxType(org.apache.geode.pdx.internal.PdxType) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) Region(org.apache.geode.cache.Region) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

EnumInfo (org.apache.geode.pdx.internal.EnumInfo)21 PdxType (org.apache.geode.pdx.internal.PdxType)14 IOException (java.io.IOException)8 Test (org.junit.Test)8 Cache (org.apache.geode.cache.Cache)6 Region (org.apache.geode.cache.Region)6 TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)6 CacheFactory (org.apache.geode.cache.CacheFactory)5 InternalCache (org.apache.geode.internal.cache.InternalCache)5 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)5 File (java.io.File)4 Map (java.util.Map)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 Properties (java.util.Properties)3 Message (org.apache.geode.internal.cache.tier.sockets.Message)3 EnumId (org.apache.geode.pdx.internal.EnumId)3 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2