RegionConverter extends ConfigurationConverter<Region, RegionConfig> { public RegionAttributesType createRegionAttributesByType(String type) { RegionAttributesType regionAttributes = new RegionAttributesType(); switch (type) { case "PARTITION": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PARTITION); break; } case "PARTITION_PERSISTENT": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PERSISTENT_PARTITION); break; } case "PARTITION_PROXY": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PARTITION); regionAttributes.setLocalMaxMemory("0"); break; } case "REPLICATE": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.REPLICATE); regionAttributes.setScope(RegionAttributesScope.DISTRIBUTED_ACK); break; } case "REPLICATE_PERSISTENT": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PERSISTENT_REPLICATE); regionAttributes.setScope(RegionAttributesScope.DISTRIBUTED_ACK); break; } case "REPLICATE_PROXY": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.EMPTY); regionAttributes.setScope(RegionAttributesScope.DISTRIBUTED_ACK); break; } case "PARTITION_REDUNDANT": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PARTITION); regionAttributes.setRedundantCopy("1"); break; } case "PARTITION_REDUNDANT_PERSISTENT": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PERSISTENT_PARTITION); regionAttributes.setRedundantCopy("1"); break; } case "PARTITION_OVERFLOW": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PARTITION); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.OVERFLOW_TO_DISK); break; } case "PARTITION_REDUNDANT_OVERFLOW": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PARTITION); regionAttributes.setRedundantCopy("1"); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.OVERFLOW_TO_DISK); break; } case "PARTITION_PERSISTENT_OVERFLOW": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PERSISTENT_PARTITION); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.OVERFLOW_TO_DISK); break; } case "PARTITION_REDUNDANT_PERSISTENT_OVERFLOW": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PERSISTENT_PARTITION); regionAttributes.setRedundantCopy("1"); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.OVERFLOW_TO_DISK); break; } case "PARTITION_HEAP_LRU": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PARTITION); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.LOCAL_DESTROY); break; } case "PARTITION_REDUNDANT_HEAP_LRU": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PARTITION); regionAttributes.setRedundantCopy("1"); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.LOCAL_DESTROY); break; } case "REPLICATE_OVERFLOW": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.REPLICATE); regionAttributes.setScope(RegionAttributesScope.DISTRIBUTED_ACK); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.OVERFLOW_TO_DISK); break; } case "REPLICATE_PERSISTENT_OVERFLOW": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PERSISTENT_REPLICATE); regionAttributes.setScope(RegionAttributesScope.DISTRIBUTED_ACK); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.OVERFLOW_TO_DISK); break; } case "REPLICATE_HEAP_LRU": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PRELOADED); regionAttributes.setScope(RegionAttributesScope.DISTRIBUTED_ACK); regionAttributes.setInterestPolicy("all"); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.LOCAL_DESTROY); break; } case "LOCAL": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.NORMAL); regionAttributes.setScope(RegionAttributesScope.LOCAL); break; } case "LOCAL_PERSISTENT": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PERSISTENT_REPLICATE); regionAttributes.setScope(RegionAttributesScope.LOCAL); break; } case "LOCAL_HEAP_LRU": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.NORMAL); regionAttributes.setScope(RegionAttributesScope.LOCAL); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.LOCAL_DESTROY); break; } case "LOCAL_OVERFLOW": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.NORMAL); regionAttributes.setScope(RegionAttributesScope.LOCAL); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.OVERFLOW_TO_DISK); break; } case "LOCAL_PERSISTENT_OVERFLOW": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PERSISTENT_REPLICATE); regionAttributes.setScope(RegionAttributesScope.LOCAL); regionAttributes .setLruHeapPercentageEvictionAction(EnumActionDestroyOverflow.OVERFLOW_TO_DISK); break; } case "PARTITION_PROXY_REDUNDANT": { regionAttributes.setDataPolicy(RegionAttributesDataPolicy.PARTITION); regionAttributes.setLocalMaxMemory("0"); regionAttributes.setRedundantCopy("1"); break; } default: throw new IllegalArgumentException("invalid type " + type); } return regionAttributes; }  RegionType getRegionType(String refid, RegionAttributesType regionAttributes); RegionAttributesType createRegionAttributesByType(String type);  }
@Test public void fromXmlWithNameType() { config.setName("test"); config.setRegionAttributes(converter.createRegionAttributesByType("REPLICATE")); Region region = converter.fromXmlObject(config); assertThat(region.getName()).isEqualTo("test"); assertThat(region.getType()).isEqualTo(RegionType.REPLICATE); }
@Test public void fromXmlWithAll() { config.setName("test"); config.setRegionAttributes(converter.createRegionAttributesByType("PARTITION")); RegionAttributesType attributesType = config.getRegionAttributes(); attributesType.setValueConstraint("foo"); attributesType.setKeyConstraint("bar"); attributesType.setDiskStoreName("diskstore"); config.setRegionAttributes(attributesType); RegionAttributesType.PartitionAttributes partitionAttributes = new RegionAttributesType.PartitionAttributes(); partitionAttributes.setRedundantCopies("2"); attributesType.setPartitionAttributes(partitionAttributes); Region region = converter.fromXmlObject(config); assertThat(region.getName()).isEqualTo("test"); assertThat(region.getType()).isEqualTo(RegionType.PARTITION); assertThat(region.getValueConstraint()).isEqualTo("foo"); assertThat(region.getKeyConstraint()).isEqualTo("bar"); assertThat(region.getDiskStoreName()).isEqualTo("diskstore"); assertThat(region.getRedundantCopies()).isEqualTo(2); assertThat(region.getExpirations()).isNull(); }
@Test public void fromXmlWithLocalType() { config.setName("test"); config.setRegionAttributes(converter.createRegionAttributesByType("LOCAL")); assertThat(converter.fromXmlObject(config).getType()).isEqualTo(RegionType.LEGACY); }
@Test public void checkDefaultRegionAttributesForShortcuts() throws Exception { URL xmlResource = RegionConverterTest.class.getResource("RegionConverterTest.xml"); assertThat(xmlResource).isNotNull(); CacheConfig master = new JAXBService(CacheConfig.class) .unMarshall(FileUtils.readFileToString(new File(xmlResource.getFile()), "UTF-8")); RegionShortcut[] shortcuts = RegionShortcut.values(); for (RegionShortcut shortcut : shortcuts) { RegionConfig config = new RegionConfig(); config.setType(shortcut.name()); config.setName(shortcut.name()); config.setRegionAttributes(converter.createRegionAttributesByType(shortcut.name())); RegionConfig masterRegion = find(master.getRegions(), shortcut.name()); assertThat(config).isEqualToComparingFieldByFieldRecursively(masterRegion); } }
@Test public void createRegionAttributesByInvalidType() { assertThatThrownBy(() -> converter.createRegionAttributesByType("abc")) .isInstanceOf(IllegalArgumentException.class); }