@SuppressWarnings({ "rawtypes", "unchecked" })
public Region mockRegion(String name) {
regionAttributes = attributesFactory.create();
RegionService mockRegionService = mockRegionService();
Region region = mock(Region.class);
when(region.getAttributes()).thenReturn(regionAttributes);
String regionFullPath = (name.startsWith(Region.SEPARATOR) ? name : Region.SEPARATOR + name);
String regionName = (name.lastIndexOf(Region.SEPARATOR) > 0 ?
name.substring(name.lastIndexOf(Region.SEPARATOR) + Region.SEPARATOR.length()) : name);
when(region.getFullPath()).thenReturn(regionFullPath);
when(region.getName()).thenReturn(regionName);
when(region.getRegionService()).thenReturn(mockRegionService);
when(region.getSubregion(anyString())).thenAnswer(new Answer<Region>() {
@Override
public Region answer(InvocationOnMock invocation) throws Throwable {
Region parent = (Region) invocation.getMock();
String parentRegionName = parent.getFullPath();
String subRegionName = (String) invocation.getArguments()[0];
String subRegionPath = (parentRegionName.startsWith("/") ? parentRegionName+"/"+subRegionName
: "/"+parentRegionName+"/"+subRegionName);
return cache.getRegion(subRegionPath);
}
});
when(region.createSubregion(anyString(), any(RegionAttributes.class))).thenAnswer(new Answer<Region>() {
@Override
public Region answer(InvocationOnMock invocation) throws Throwable {
String name = (String) invocation.getArguments()[0];
RegionAttributes attributes = (RegionAttributes) invocation.getArguments()[1];
Region parent = (Region) invocation.getMock();
String parentName = parent.getName();
String regionName = parentName.startsWith("/") ? parentName+"/"+name : "/"+parentName+"/"+ name;
Region subRegion = new MockRegionFactory(cache).createMockRegionFactory(attributes).create(regionName);
when(subRegion.getFullPath()).thenReturn(regionName);
cache.allRegions().put(regionName, subRegion);
return subRegion;
}