Fixes JIRA issus SGF-252 involving multiple, identically names Subregions and SDG's Repository extension improperly handling application domain objects associated by way of the @Region annotation.

This commit is contained in:
John Blum
2014-02-06 17:40:24 -08:00
parent 5660a78255
commit 65ae4b87fe
12 changed files with 491 additions and 31 deletions

View File

@@ -0,0 +1,171 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.mapping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.gemfire.repository.sample.GuestUser;
import org.springframework.data.gemfire.repository.sample.RootUser;
import org.springframework.data.gemfire.repository.sample.User;
import org.springframework.data.mapping.context.MappingContext;
import com.gemstone.gemfire.cache.Region;
/**
* The RegionsTest class is a test suite of test cases testing the contract and functionality of the Regions class.
* <p/>
* @author John J. Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.mapping.Regions
* @since 1.3.4
*/
public class RegionsTest {
private MappingContext mockMappingContext;
private Region mockUsers;
private Region mockAdminUsers;
private Region mockGuestUsers;
private Regions regions;
protected Region createMockRegion(final String fullPath) {
// NOTE if the Region path does not contain a "/" then lastIndexOf returns -1 and substring is appropriately
// based on a 0 index then by adding 1, ;-)
return createMockRegion(fullPath.substring(fullPath.lastIndexOf(Region.SEPARATOR) + 1), fullPath);
}
protected Region createMockRegion(final String name, final String fullPath) {
Region mockRegion = mock(com.gemstone.gemfire.cache.Region.class, name);
when(mockRegion.getName()).thenReturn(name);
when(mockRegion.getFullPath()).thenReturn(fullPath);
return mockRegion;
}
@Before
@SuppressWarnings("unchecked")
public void setup() {
mockMappingContext = mock(GemfireMappingContext.class, "GemfireMappingContext");
mockUsers = createMockRegion("/Users");
mockAdminUsers = createMockRegion("/Users/Admin");
mockGuestUsers = createMockRegion("/Users/Guest");
regions = new Regions(Arrays.<Region<?, ?>>asList(mockUsers, mockAdminUsers, mockGuestUsers),
mockMappingContext);
assertNotNull(regions);
}
@After
public void tearDown() {
mockMappingContext = null;
mockUsers = mockAdminUsers = mockGuestUsers = null;
regions = null;
}
@Test
public void testIterateRegions() {
List<Region> actualRegions = new ArrayList<Region>(3);
for (Region region : regions) {
actualRegions.add(region);
}
List<Region> expectedRegions = Arrays.asList(mockUsers, mockAdminUsers, mockGuestUsers);
assertEquals(expectedRegions.size() * 2, actualRegions.size());
assertTrue(actualRegions.containsAll(expectedRegions));
}
@Test
public void testGetRegionByNameOrPath() {
assertSame(mockUsers, regions.getRegion("Users"));
assertSame(mockUsers, regions.getRegion("/Users"));
assertSame(mockAdminUsers, regions.getRegion("Admin"));
assertSame(mockAdminUsers, regions.getRegion("/Users/Admin"));
assertSame(mockGuestUsers, regions.getRegion("Guest"));
assertSame(mockGuestUsers, regions.getRegion("/Users/Guest"));
}
@Test(expected = IllegalArgumentException.class)
public void testGetRegionByNameOrPathWithNullArgument() {
regions.getRegion((String) null);
}
@Test
public void testGetRegionByDomainClassAndUsingPersistentEntity() {
GemfirePersistentEntity mockUsersEntity = mock(GemfirePersistentEntity.class, "UsersGemfirePeristentEntity");
GemfirePersistentEntity mockAdminUserEntity = mock(GemfirePersistentEntity.class, "AdminUserGemfirePeristentEntity");
GemfirePersistentEntity mockGuestUserEntity = mock(GemfirePersistentEntity.class, "GuestUserGemfirePeristentEntity");
when(mockMappingContext.getPersistentEntity(User.class)).thenReturn(mockUsersEntity);
when(mockUsersEntity.getRegionName()).thenReturn("/Users");
when(mockMappingContext.getPersistentEntity(RootUser.class)).thenReturn(mockAdminUserEntity);
when(mockAdminUserEntity.getRegionName()).thenReturn("Admin");
when(mockMappingContext.getPersistentEntity(GuestUser.class)).thenReturn(mockGuestUserEntity);
when(mockGuestUserEntity.getRegionName()).thenReturn("/Users/Guest");
when(mockMappingContext.getPersistentEntity(Object.class)).thenReturn(null);
assertSame(mockUsers, regions.getRegion(User.class));
assertSame(mockUsers, regions.getRegion(Users.class));
assertSame(mockAdminUsers, regions.getRegion(RootUser.class));
assertSame(mockGuestUsers, regions.getRegion(GuestUser.class));
}
@Test
public void testGetRegionByDomainClass() {
when(mockMappingContext.getPersistentEntity(Object.class)).thenReturn(null);
assertSame(mockUsers, regions.getRegion(Users.class));
assertNull(regions.getRegion(User.class));
assertNull(regions.getRegion(RootUser.class));
assertNull(regions.getRegion(GuestUser.class));
}
@Test
public void testGetRegionByPersistentEntity() {
GemfirePersistentEntity mockPersistentEntity = mock(GemfirePersistentEntity.class, "GemfirePersistentEntity");
when(mockMappingContext.getPersistentEntity(any(Class.class))).thenReturn(mockPersistentEntity);
when(mockPersistentEntity.getRegionName()).thenReturn("/Non/Existing/Region");
assertNull(regions.getRegion(User.class));
assertNull(regions.getRegion(RootUser.class));
assertNull(regions.getRegion(GuestUser.class));
}
protected static interface Users {
}
}

View File

@@ -15,9 +15,11 @@
*/
package org.springframework.data.gemfire.repository.query;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
@@ -27,12 +29,14 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.gemfire.repository.sample.Person;
import org.springframework.data.gemfire.repository.sample.RootUser;
import com.gemstone.gemfire.cache.Region;
/**
*
* @author Oliver Gierke
* @author John Blum
*/
@RunWith(MockitoJUnitRunner.class)
public class QueryStringUnitTests {
@@ -41,8 +45,9 @@ public class QueryStringUnitTests {
@SuppressWarnings("rawtypes")
Region region;
// SGF-251
@Test
public void replacesDomainObjectWithRegionNameCorrectly() {
public void replacesDomainObjectWithRegionPathCorrectly() {
QueryString query = new QueryString("SELECT * FROM /Person p WHERE p.firstname = $1");
when(region.getFullPath()).thenReturn("/foo");
@@ -52,9 +57,10 @@ public class QueryStringUnitTests {
verify(region, never()).getName();
}
//SGF-156
// SGF-156
// SGF-251
@Test
public void replacesDomainObjectWithPluralRegionNameCorrectly() {
public void replacesDomainObjectWithPluralRegionPathCorrectly() {
QueryString query = new QueryString("SELECT * FROM /Persons p WHERE p.firstname = $1");
when(region.getFullPath()).thenReturn("/Persons");
@@ -64,9 +70,20 @@ public class QueryStringUnitTests {
verify(region, never()).getName();
}
// SGF-252
@Test
public void replacesFullyQualifiedSubegionReferenceWithRegionPathCorrectly() {
QueryString query = new QueryString("SELECT * FROM //Local/Root/Users u WHERE u.username = $1");
when(region.getFullPath()).thenReturn("/Local/Root/Users");
assertThat(query.forRegion(RootUser.class, region).toString(), is("SELECT * FROM /Local/Root/Users u WHERE u.username = $1"));
verify(region, never()).getName();
}
@Test
public void bindsInValuesCorrectly() {
QueryString query = new QueryString("SELECT * FROM /Person p WHERE p.firstname IN SET $1");
List<Integer> values = Arrays.asList(1, 2, 3);
assertThat(query.bindIn(values).toString(), is("SELECT * FROM /Person p WHERE p.firstname IN SET ('1', '2', '3')"));
@@ -74,9 +91,9 @@ public class QueryStringUnitTests {
@Test
public void detectsInParameterIndexesCorrectly() {
QueryString query = new QueryString("IN SET $1 OR IN SET $2");
Iterable<Integer> indexes = query.getInParameterIndexes();
assertThat(indexes, is((Iterable<Integer>) Arrays.asList(1, 2)));
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.gemfire.mapping.Region;
/**
* The GuestUser class represents an authorized restricted user of a service or computer system, etc.
* <p/>
* @author John Blum
* @see org.springframework.data.gemfire.mapping.Region
* @see org.springframework.data.gemfire.repository.sample.User
* @since 1.3.4
*/
@Region("/Local/Guest/Users")
@SuppressWarnings("unused")
public class GuestUser extends User {
public GuestUser(final String username) {
super(username);
}
@Override
public String toString() {
return String.format("Guest User '%1$s'", getUsername());
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository.sample;
import java.util.List;
import org.springframework.data.gemfire.repository.GemfireRepository;
/**
* The GuestUserRepository class is a DAO for accessing and persisting GuestUsers.
* <p/>
* @author John Blum
* @see org.springframework.data.gemfire.repository.GemfireRepository
* @see org.springframework.data.gemfire.repository.sample.GuestUser
* @since 1.3.4
*/
@SuppressWarnings("unused")
public interface GuestUserRepository extends GemfireRepository<GuestUser, String> {
public List<GuestUser> findDistinctByUsername(String username);
}

View File

@@ -25,7 +25,7 @@ import org.springframework.util.StringUtils;
* @author John J. Blum
* @see org.springframework.data.gemfire.mapping.Region
* @see org.springframework.data.gemfire.repository.sample.User
* @since 1.0.0
* @since 1.3.4
*/
@Region("Programmers")
@SuppressWarnings("unused")

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository.sample;
import org.springframework.data.gemfire.mapping.Region;
/**
* The RootUser class represents an authorized administrative user of a service or computer system, etc.
* <p/>
* @author John Blum
* @see org.springframework.data.gemfire.mapping.Region
* @see org.springframework.data.gemfire.repository.sample.User
* @since 1.3.4
*/
@Region("/Local/Admin/Users")
@SuppressWarnings("unused")
public class RootUser extends User {
public RootUser(final String username) {
super(username);
}
@Override
public String toString() {
return String.format("Root User '%1$s'", getUsername());
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository.sample;
import java.util.List;
import org.springframework.data.gemfire.repository.GemfireRepository;
/**
* The RootUserRepository class is a DAO for accessing and persisting RootUsers.
* <p/>
* @author John Blum
* @see org.springframework.data.gemfire.repository.GemfireRepository
* @see org.springframework.data.gemfire.repository.sample.RootUser
* @since 1.3.4
*/
@SuppressWarnings("unused")
public interface RootUserRepository extends GemfireRepository<RootUser, String> {
public List<RootUser> findDistinctByUsername(String username);
}

View File

@@ -51,6 +51,8 @@ import com.gemstone.gemfire.cache.Region;
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @see com.gemstone.gemfire.cache.Region
* @link https://jira.springsource.org/browse/SGF-251
* @link https://jira.springsource.org/browse/SGF-252
* @since 1.0.0
*/
@ContextConfiguration("subregionRepository.xml")
@@ -58,9 +60,18 @@ import com.gemstone.gemfire.cache.Region;
@SuppressWarnings("unused")
public class SubRegionRepositoryIntegrationTest {
private static final Map<String, Programmer> PROGRAMMER_DATA = new HashMap<String, Programmer>(23, 0.90f);
private static final Map<String, RootUser> ADMIN_USER_DATA = new HashMap<String, RootUser>(5, 0.90f);
private static final Map<String, GuestUser> GUEST_USER_DATA = new HashMap<String, GuestUser>(3, 0.90f);
private static final Map<String, Programmer> PROGRAMMER_USER_DATA = new HashMap<String, Programmer>(23, 0.90f);
static {
createAdminUser("supertool");
createAdminUser("thor");
createAdminUser("zeus");
createGuestUser("bubba");
createGuestUser("joeblow");
createProgrammer("AdaLovelace", "Ada");
createProgrammer("AlanKay", "Smalltalk");
createProgrammer("BjarneStroustrup", "C++");
@@ -87,10 +98,68 @@ public class SubRegionRepositoryIntegrationTest {
@Resource(name = "/Users/Programmers")
private Region<String, Programmer> programmers;
@Resource(name = "/Local/Admin/Users")
private Region<String, RootUser> adminUsers;
@Resource(name = "/Local/Guest/Users")
private Region<String, GuestUser> guestUsers;
@Autowired
private GuestUserRepository guestUserRepo;
@Autowired
private RootUserRepository adminUserRepo;
protected static RootUser createAdminUser(final String username) {
RootUser user = new RootUser(username);
ADMIN_USER_DATA.put(username, user);
return user;
}
protected static GuestUser createGuestUser(final String username) {
GuestUser user = new GuestUser(username);
GUEST_USER_DATA.put(username, user);
return user;
}
protected static RootUser getAdminUser(final String username) {
List<RootUser> users = getAdminUsers(username);
return (users.isEmpty() ? null : users.get(0));
}
protected static List<RootUser> getAdminUsers(final String... usernames) {
return getUsers(ADMIN_USER_DATA, usernames);
}
protected static GuestUser getGuestUser(final String username) {
List<GuestUser> users = getGuestUsers(username);
return (users.isEmpty() ? null : users.get(0));
}
protected static List<GuestUser> getGuestUsers(final String... usernames) {
return getUsers(GUEST_USER_DATA, usernames);
}
protected static <T extends User> List<T> getUsers(final Map<String, T> userData, final String... usernames) {
List<T> users = new ArrayList<T>(usernames.length);
for (String username : usernames) {
T user = userData.get(username);
if (user != null) {
users.add(user);
}
}
Collections.sort(users);
return users;
}
protected static Programmer createProgrammer(final String username, final String programmingLanguage) {
Programmer programmer = new Programmer(username);
programmer.setProgrammingLanguage(programmingLanguage);
PROGRAMMER_DATA.put(username, programmer);
PROGRAMMER_USER_DATA.put(username, programmer);
return programmer;
}
@@ -98,10 +167,10 @@ public class SubRegionRepositoryIntegrationTest {
List<Programmer> programmers = new ArrayList<Programmer>(usernames.length);
for (String username : usernames) {
Programmer programmer = PROGRAMMER_DATA.get(username);
Programmer programmer = PROGRAMMER_USER_DATA.get(username);
if (programmer != null) {
programmers.add(PROGRAMMER_DATA.get(username));
programmers.add(PROGRAMMER_USER_DATA.get(username));
}
}
@@ -115,15 +184,29 @@ public class SubRegionRepositoryIntegrationTest {
assertNotNull("The /Users/Programmers Subregion was null!", programmers);
if (programmers.isEmpty()) {
programmers.putAll(PROGRAMMER_DATA);
programmers.putAll(PROGRAMMER_USER_DATA);
}
assertEquals(PROGRAMMER_DATA.size(), programmers.size());
assertEquals(PROGRAMMER_USER_DATA.size(), programmers.size());
assertNotNull("The /Local/Admins/Users Subregion was null!", adminUsers);
if (adminUsers.isEmpty()) {
adminUsers.putAll(ADMIN_USER_DATA);
}
assertEquals(ADMIN_USER_DATA.size(), adminUsers.size());
assertNotNull("The /Local/Guest/Users Subregion was null!", guestUsers);
if (guestUsers.isEmpty()) {
guestUsers.putAll(GUEST_USER_DATA);
}
assertEquals(GUEST_USER_DATA.size(), guestUsers.size());
}
@Test
public void testSubregionRepositoryInteractions() {
assertEquals(PROGRAMMER_DATA.get("JamesGosling"), programmersRepo.findOne("JamesGosling"));
assertEquals(PROGRAMMER_USER_DATA.get("JamesGosling"), programmersRepo.findOne("JamesGosling"));
List<Programmer> javaProgrammers = programmersRepo.findDistinctByProgrammingLanguageOrderByUsernameAsc("Java");
@@ -170,4 +253,25 @@ public class SubRegionRepositoryIntegrationTest {
assertTrue(pseudoCodeProgrammers.isEmpty());
}
@Test
public void testIdenticallyNamedSubregionDataAccess() {
assertEquals(getAdminUser("supertool"), adminUserRepo.findOne("supertool"));
assertEquals(getGuestUser("joeblow"), guestUserRepo.findOne("joeblow"));
List<RootUser> rootUsers = adminUserRepo.findDistinctByUsername("zeus");
assertNotNull(rootUsers);
assertFalse(rootUsers.isEmpty());
assertEquals(1, rootUsers.size());
assertEquals(getAdminUser("zeus"), rootUsers.get(0));
List<GuestUser> guestUsers = guestUserRepo.findDistinctByUsername("bubba");
assertNotNull(guestUsers);
assertFalse(guestUsers.isEmpty());
assertEquals(1, guestUsers.size());
assertEquals(getGuestUser("bubba"), guestUsers.get(0));
}
}