From c027e7313147fc3945f6b76abe42326006666606 Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 25 Mar 2014 13:23:19 -0700 Subject: [PATCH] Completes testing for SGF-260, which enables the Spring Data Commons @Id mapping annotation to be used on methods of domain interfaces or classes in addition to domain entity class fields. These test additions require DATACMNS-476 (https://jira.spring.io/browse/DATACMNS-476), with details in GitHub PR #76 (https://github.com/spring-projects/spring-data-commons/pull/76) to be included in the recent SDC 1.8.0.BUILD-SNAPSHOT, slated for the Spring Data Dijkstra release as M1. --- .../gemfire/repository/sample/Algorithm.java | 36 +++++++ .../sample/AlgorithmRepository.java | 34 ++++++ .../sample/AlgorithmRepositoryTest.java | 100 ++++++++++++++++++ .../AlgorithmRepositoryTest-context.xml | 31 ++++++ 4 files changed, 201 insertions(+) create mode 100644 src/test/java/org/springframework/data/gemfire/repository/sample/Algorithm.java create mode 100644 src/test/java/org/springframework/data/gemfire/repository/sample/AlgorithmRepository.java create mode 100644 src/test/java/org/springframework/data/gemfire/repository/sample/AlgorithmRepositoryTest.java create mode 100644 src/test/resources/org/springframework/data/gemfire/repository/sample/AlgorithmRepositoryTest-context.xml diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/Algorithm.java b/src/test/java/org/springframework/data/gemfire/repository/sample/Algorithm.java new file mode 100644 index 00000000..9f44408c --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/Algorithm.java @@ -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 org.springframework.data.annotation.Id; +import org.springframework.data.gemfire.mapping.Region; + +/** + * The Algorithm interface define abstract data type modeling a computer algorithm. + *

+ * @author John Blum + * @see org.springframework.data.annotation.Id + * @see org.springframework.data.gemfire.mapping.Region + * @since 1.4.0 + */ +@Region("Algorithms") +@SuppressWarnings("unused") +public interface Algorithm { + + @Id String getName(); + +} diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/AlgorithmRepository.java b/src/test/java/org/springframework/data/gemfire/repository/sample/AlgorithmRepository.java new file mode 100644 index 00000000..ff8b2e2c --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/AlgorithmRepository.java @@ -0,0 +1,34 @@ +/* + * 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.repository.GemfireRepository; + +/** + * The AlgorithmRepository class is a Data Access Object (DAO) for accessing and persistent data/state about Algorithms + * to a GemFire Cache/Region. + *

+ * @author John Blum + * @see org.springframework.data.gemfire.repository.GemfireRepository + * @since 1.4.0 + */ +@SuppressWarnings("unused") +public interface AlgorithmRepository extends GemfireRepository { + + T findByName(String name); + +} diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/AlgorithmRepositoryTest.java b/src/test/java/org/springframework/data/gemfire/repository/sample/AlgorithmRepositoryTest.java new file mode 100644 index 00000000..e53a5433 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/AlgorithmRepositoryTest.java @@ -0,0 +1,100 @@ +/* + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.gemstone.gemfire.cache.Region; + +/** + * The AlgorithmRepositoryTest class is a test suite of test cases testing the contract and functionality of GemFire's + * Repository extension when using a plain old Java interface for defining the application domain object/entity type, + * rather than a Java class, that is the subject of the persistence operations. + *

+ * @author John Blum + * @see org.junit.Test + * @see org.junit.runner.RunWith + * @see org.springframework.test.context.ContextConfiguration + * @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner + * @since 1.4.0 + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +@SuppressWarnings("unused") +public class AlgorithmRepositoryTest { + + @Autowired + private AlgorithmRepository algorithmRepo; + + @Resource(name = "Algorithms") + private Region algorithmsRegion; + + @Test + public void testAlgorithmsRepo() { + assertNotNull("A reference to the AlgorithmRepository was not properly configured!", algorithmRepo); + assertNotNull("A reference to the 'Algorithms' GemFire Cache Region was not properly configured!", + algorithmsRegion); + assertEquals("Algorithms", algorithmsRegion.getName()); + assertEquals("/Algorithms", algorithmsRegion.getFullPath()); + assertTrue(algorithmsRegion.isEmpty()); + + algorithmRepo.save(new BinarySearch()); + algorithmRepo.save(new HeapSort()); + + assertFalse(algorithmsRegion.isEmpty()); + assertEquals(2, algorithmsRegion.size()); + + assertTrue(algorithmsRegion.get(BinarySearch.class.getSimpleName()) instanceof BinarySearch); + assertTrue(algorithmsRegion.get(HeapSort.class.getSimpleName()) instanceof HeapSort); + + HeapSort heapSort = algorithmRepo.findByName(HeapSort.class.getSimpleName()); + + assertNotNull(heapSort); + assertEquals(HeapSort.class.getSimpleName(), heapSort.getName()); + + BinarySearch binarySearch = algorithmRepo.findByName(BinarySearch.class.getSimpleName()); + + assertNotNull(binarySearch); + assertEquals(BinarySearch.class.getSimpleName(), binarySearch.getName()); + } + + protected static abstract class AbstractAlgorithm implements Algorithm { + + @Override + public String getName() { + return getClass().getSimpleName(); + } + } + + protected static final class BinarySearch extends AbstractAlgorithm { + } + + protected static final class HeapSort extends AbstractAlgorithm { + } + +} diff --git a/src/test/resources/org/springframework/data/gemfire/repository/sample/AlgorithmRepositoryTest-context.xml b/src/test/resources/org/springframework/data/gemfire/repository/sample/AlgorithmRepositoryTest-context.xml new file mode 100644 index 00000000..13885704 --- /dev/null +++ b/src/test/resources/org/springframework/data/gemfire/repository/sample/AlgorithmRepositoryTest-context.xml @@ -0,0 +1,31 @@ + + + + + springGemFireAlgorithmRepositoryTest + config + 0 + + + + + + + + + + +