|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license |
| 3 | + * agreements. See the NOTICE file distributed with this work for additional information regarding |
| 4 | + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the |
| 5 | + * "License"); you may not use this file except in compliance with the License. You may obtain a |
| 6 | + * copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 11 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 12 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 13 | + * the License. |
| 14 | + */ |
| 15 | +package org.apache.geode.pdx; |
| 16 | + |
| 17 | +import static com.googlecode.catchexception.CatchException.catchException; |
| 18 | +import static com.googlecode.catchexception.CatchException.caughtException; |
| 19 | +import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT; |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.experimental.categories.Category; |
| 26 | + |
| 27 | +import org.apache.geode.cache.Cache; |
| 28 | +import org.apache.geode.cache.CacheFactory; |
| 29 | +import org.apache.geode.cache.CacheLoader; |
| 30 | +import org.apache.geode.cache.CacheLoaderException; |
| 31 | +import org.apache.geode.cache.LoaderHelper; |
| 32 | +import org.apache.geode.cache.Region; |
| 33 | +import org.apache.geode.cache.RegionShortcut; |
| 34 | +import org.apache.geode.test.junit.categories.IntegrationTest; |
| 35 | +import org.apache.geode.test.junit.categories.SerializationTest; |
| 36 | + |
| 37 | +@Category({IntegrationTest.class, SerializationTest.class}) |
| 38 | +public class PdxInstanceLoaderIntegrationTest { |
| 39 | + |
| 40 | + private Cache cache; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() {} |
| 44 | + |
| 45 | + @After |
| 46 | + public void tearDown() { |
| 47 | + cache.close(); |
| 48 | + } |
| 49 | + |
| 50 | + private class PdxInstanceLoader implements CacheLoader { |
| 51 | + @Override |
| 52 | + public void close() {} |
| 53 | + |
| 54 | + @Override |
| 55 | + public Object load(LoaderHelper helper) throws CacheLoaderException { |
| 56 | + return cache.createPdxInstanceFactory("no class name").create(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void loadOfPdxInstanceWithReadSerializedFalseAttemptsToDeserialize() { |
| 62 | + cache = new CacheFactory().set(MCAST_PORT, "0").setPdxReadSerialized(false).create(); |
| 63 | + Region region = cache.createRegionFactory(RegionShortcut.LOCAL) |
| 64 | + .setCacheLoader(new PdxInstanceLoader()).create("region"); |
| 65 | + catchException(region).get("key"); |
| 66 | + assertThat((Exception) caughtException()).isInstanceOf(PdxSerializationException.class); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void loadOfPdxInstanceWithReadSerializedTrueReturnsPdxInstance() { |
| 71 | + cache = new CacheFactory().set(MCAST_PORT, "0").setPdxReadSerialized(true).create(); |
| 72 | + Region region = cache.createRegionFactory(RegionShortcut.LOCAL) |
| 73 | + .setCacheLoader(new PdxInstanceLoader()).create("region"); |
| 74 | + Object obj = region.get("key"); |
| 75 | + assertThat(obj).isInstanceOf(PdxInstance.class); |
| 76 | + } |
| 77 | +} |
0 commit comments