rave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfrank...@apache.org
Subject svn commit: r1148826 - in /incubator/rave/trunk/rave-portal/src: main/java/org/apache/rave/portal/repository/ main/java/org/apache/rave/portal/repository/impl/ test/java/org/apache/rave/portal/repository/
Date Wed, 20 Jul 2011 16:02:51 GMT
Author: mfranklin
Date: Wed Jul 20 16:02:49 2011
New Revision: 1148826

URL: http://svn.apache.org/viewvc?rev=1148826&view=rev
Log:
Made save, delete methods transactional on AbstractJpaRepository (Supports RAVE-27)

Modified:
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/Repository.java
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/AbstractJpaRepository.java
    incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/AbstractJpaRepositoryTest.java

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/Repository.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/Repository.java?rev=1148826&r1=1148825&r2=1148826&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/Repository.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/Repository.java
Wed Jul 20 16:02:49 2011
@@ -24,6 +24,12 @@ package org.apache.rave.portal.repositor
  */
 public interface Repository<T> {
     /**
+     * Gets the type of the object served by the repository
+     * @return a {@link Class} representing the type of object
+     */
+    Class<T> getType();
+
+    /**
      * Gets the specified object from the persistence context by its id
      *
      * @param id the id of the object of type T to retrieve

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/AbstractJpaRepository.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/AbstractJpaRepository.java?rev=1148826&r1=1148825&r2=1148826&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/AbstractJpaRepository.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/repository/impl/AbstractJpaRepository.java
Wed Jul 20 16:02:49 2011
@@ -21,6 +21,8 @@ package org.apache.rave.portal.repositor
 
 import org.apache.rave.portal.model.BasicEntity;
 import org.apache.rave.portal.repository.Repository;
+import org.springframework.core.type.ClassMetadata;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
@@ -42,17 +44,24 @@ public abstract class AbstractJpaReposit
     }
 
     @Override
-    public void delete(T item) {
-        manager.remove(item);
+    public Class<T> getType() {
+        return type;
     }
 
     @Override
+    public T get(long id) {
+        return manager.find(type, id);
+    }
+
+    @Override
+    @Transactional
     public T save(T item) {
         return saveOrUpdate(item.getId(), manager, item);
     }
 
     @Override
-    public T get(long id) {
-        return manager.find(type, id);
+    @Transactional
+    public void delete(T item) {
+        manager.remove(item);
     }
 }

Modified: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/AbstractJpaRepositoryTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/AbstractJpaRepositoryTest.java?rev=1148826&r1=1148825&r2=1148826&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/AbstractJpaRepositoryTest.java
(original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/repository/AbstractJpaRepositoryTest.java
Wed Jul 20 16:02:49 2011
@@ -51,15 +51,15 @@ public class AbstractJpaRepositoryTest {
     private EntityManager sharedManager;
 
     @Autowired
-    private List<AbstractJpaRepository> repositories;
+    private List<Repository> repositories;
     //NOTE: In order for tests to succeed, there must be an object with id of 1 in the store
for every repository
     private static final Long VALID_ENTITY_ID = 1L;
     private static final Long INVALID_ENTITY_ID = -1L;
 
     @Test
     public void getById_validId() {
-        for (AbstractJpaRepository repository : repositories) {
-            BasicEntity entity = repository.get(VALID_ENTITY_ID);
+        for (Repository repository : repositories) {
+            BasicEntity entity = (BasicEntity)repository.get(VALID_ENTITY_ID);
             assertThat(entity, is(notNullValue()));
             assertThat(entity.getId(), is(equalTo(VALID_ENTITY_ID)));
         }
@@ -67,8 +67,8 @@ public class AbstractJpaRepositoryTest {
 
     @Test
     public void getById_invalidId() {
-        for (AbstractJpaRepository repository : repositories) {
-            BasicEntity entity = repository.get(INVALID_ENTITY_ID);
+        for (Repository repository : repositories) {
+            BasicEntity entity = (BasicEntity)repository.get(INVALID_ENTITY_ID);
             assertThat(entity, is(nullValue()));
         }
     }
@@ -76,9 +76,9 @@ public class AbstractJpaRepositoryTest {
     @Test
     @Rollback(true)
     public void save_newEntity() {
-        for (AbstractJpaRepository repository : repositories) {
+        for (Repository repository : repositories) {
             BasicEntity entity = constructNewEntityForRepository(repository);
-            BasicEntity saved = repository.save(entity);
+            BasicEntity saved = (BasicEntity)repository.save(entity);
             sharedManager.flush();
             assertThat(saved, is(sameInstance(entity)));
             assertThat(saved.getId(), is(notNullValue()));
@@ -88,10 +88,10 @@ public class AbstractJpaRepositoryTest {
     @Test
     @Rollback(true)
     public void save_existingEntity() {
-        for (AbstractJpaRepository repository : repositories) {
+        for (Repository repository : repositories) {
             BasicEntity entity = constructNewEntityForRepository(repository);
             entity.setId(VALID_ENTITY_ID);
-            BasicEntity saved = repository.save(entity);
+            BasicEntity saved = (BasicEntity)repository.save(entity);
             sharedManager.flush();
             assertThat(saved, is(not(sameInstance(entity))));
             assertThat(saved.getId(), is(equalTo(entity.getId())));
@@ -102,20 +102,17 @@ public class AbstractJpaRepositoryTest {
     @Test
     @Rollback(true)
     public void delete() {
-        for(AbstractJpaRepository repository : repositories) {
-            BasicEntity entity = repository.get(VALID_ENTITY_ID);
+        for(Repository repository : repositories) {
+            Object entity = repository.get(VALID_ENTITY_ID);
             repository.delete(entity);
             sharedManager.flush();
             assertThat(repository.get(VALID_ENTITY_ID), is(nullValue()));
         }
     }
 
-    private BasicEntity constructNewEntityForRepository(AbstractJpaRepository repository)
{
+    private BasicEntity constructNewEntityForRepository(Repository repository) {
         try {
-            Field field = AbstractJpaRepository.class.getDeclaredField("type");
-            field.setAccessible(true);
-            Class<?> objectClass = (Class)field.get(repository);
-            return (BasicEntity) objectClass.newInstance();
+            return (BasicEntity)repository.getType().newInstance();
         } catch (Throwable e) {
             throw new RuntimeException(e);
         }



Mime
View raw message