rave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfrank...@apache.org
Subject svn commit: r1157135 - in /incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa: AbstractJpaRepositoryTest.java JpaUtilTest.java PopulatedLocalContainerEntityManagerFactoryTest.java util/ util/JpaUtilTest.java
Date Fri, 12 Aug 2011 14:46:15 GMT
Author: mfranklin
Date: Fri Aug 12 14:46:15 2011
New Revision: 1157135

URL: http://svn.apache.org/viewvc?rev=1157135&view=rev
Log:
Added unit tests for jpa package (Supports RAVE-174)

Added:
    incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/AbstractJpaRepositoryTest.java
    incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactoryTest.java
    incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/util/
    incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/util/JpaUtilTest.java
      - copied, changed from r1156857, incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/JpaUtilTest.java
Removed:
    incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/JpaUtilTest.java

Added: incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/AbstractJpaRepositoryTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/AbstractJpaRepositoryTest.java?rev=1157135&view=auto
==============================================================================
--- incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/AbstractJpaRepositoryTest.java
(added)
+++ incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/AbstractJpaRepositoryTest.java
Fri Aug 12 14:46:15 2011
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.rave.persistence.jpa;
+
+import org.apache.rave.persistence.BasicEntity;
+import org.apache.rave.persistence.Repository;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.persistence.EntityManager;
+
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+/**
+ */
+public class AbstractJpaRepositoryTest {
+
+    private static final long ID = 1L;
+    private EntityManager manager;
+    private Repository<TestEntity> repository;
+
+    @Before
+    public void setup() {
+        manager = createNiceMock(EntityManager.class);
+        repository = new TestJpaRepository(manager);
+    }
+
+    @Test
+    public void getType() {
+        assertThat(repository.getType(), is(equalTo(TestEntity.class)));
+    }
+
+    @Test
+    public void get() {
+        expect(manager.find(TestEntity.class, ID)).andReturn(new TestEntity(ID));
+        replay(manager);
+
+        TestEntity entity = repository.get(ID);
+        assertThat(entity.getId(), is(equalTo(ID)));
+        verify(manager);
+    }
+
+    @Test
+    public void save_persist() {
+        TestEntity entity = new TestEntity(null);
+        manager.persist(entity);
+        expectLastCall();
+        replay(manager);
+
+        TestEntity result = repository.save(entity);
+        assertThat(result, is(sameInstance(entity)));
+        verify(manager);
+    }
+
+
+    @Test
+    public void save_merge() {
+        TestEntity entity = new TestEntity(ID);
+        expect(manager.merge(entity)).andReturn(new TestEntity(ID));
+        replay(manager);
+
+        TestEntity result = repository.save(entity);
+        assertThat(result, is(not(sameInstance(entity))));
+        verify(manager);
+    }
+
+
+    @Test
+    public void delete() {
+        TestEntity entity = new TestEntity(ID);
+        manager.remove(entity);
+        expectLastCall();
+        replay(manager);
+
+        repository.delete(entity);
+        verify(manager);
+    }
+
+
+    private class TestJpaRepository extends AbstractJpaRepository<TestEntity> {
+
+        protected TestJpaRepository(EntityManager manager) {
+            super(TestEntity.class);
+            this.manager = manager;
+        }
+    }
+
+    private class TestEntity implements BasicEntity{
+        private Long id;
+
+        private TestEntity(Long id) {
+            this.id = id;
+        }
+
+        @Override
+        public Long getId() {
+            return id;
+        }
+
+        @Override
+        public void setId(Long id) {
+            this.id = id;
+        }
+    }
+}

Added: incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactoryTest.java?rev=1157135&view=auto
==============================================================================
--- incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactoryTest.java
(added)
+++ incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/PopulatedLocalContainerEntityManagerFactoryTest.java
Fri Aug 12 14:46:15 2011
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.rave.persistence.jpa;
+
+import org.apache.rave.jdbc.util.DataSourcePopulator;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.sql.DataSource;
+
+import static org.easymock.EasyMock.*;
+
+/**
+ */
+public class PopulatedLocalContainerEntityManagerFactoryTest {
+    private PopulatedLocalContainerEntityManagerFactory factory;
+    private DataSourcePopulator populator;
+    private EntityManagerFactory innerFactory;
+    private EntityManager manager;
+    private PersistenceUnitInfo info;
+    private DataSource dataSource;
+
+    @Before
+    public void setup() {
+        factory = new TestPopulatedLocalContainerEntityManagerFactory();
+        populator = createNiceMock(DataSourcePopulator.class);
+        innerFactory = createNiceMock(EntityManagerFactory.class);
+        manager = createNiceMock(EntityManager.class);
+        info = createNiceMock(PersistenceUnitInfo.class);
+        dataSource = createNiceMock(DataSource.class);
+    }
+
+    @Test
+    public void postProcess_nullPopulator() {
+        replay(info);
+        replay(innerFactory);
+        factory.postProcessEntityManagerFactory(innerFactory, info);
+        verify(info);
+        verify(innerFactory);
+    }
+
+    @Test
+    public void postProcess_validPopulator() {
+        populator.initialize(dataSource);
+        expectLastCall();
+        replay(populator);
+
+        manager.close();
+        expectLastCall();
+        replay(manager);
+
+        expect(innerFactory.createEntityManager()).andReturn(manager);
+        replay(innerFactory);
+        replay(info);
+
+        factory.setPopulator(populator);
+        factory.setDataSource(dataSource);
+
+        factory.postProcessEntityManagerFactory(innerFactory, info);
+
+        verify(info);
+        verify(innerFactory);
+        verify(populator);
+        verify(manager);
+    }
+
+    private class TestPopulatedLocalContainerEntityManagerFactory extends PopulatedLocalContainerEntityManagerFactory
{}
+}

Copied: incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/util/JpaUtilTest.java
(from r1156857, incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/JpaUtilTest.java)
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/util/JpaUtilTest.java?p2=incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/util/JpaUtilTest.java&p1=incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/JpaUtilTest.java&r1=1156857&r2=1157135&rev=1157135&view=diff
==============================================================================
--- incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/JpaUtilTest.java
(original)
+++ incubator/rave/trunk/rave-commons/src/test/java/org/apache/rave/persistence/jpa/util/JpaUtilTest.java
Fri Aug 12 14:46:15 2011
@@ -17,23 +17,39 @@
  * under the License.
  */
 
-package org.apache.rave.persistence.jpa;
+package org.apache.rave.persistence.jpa.util;
 
 import org.apache.rave.persistence.jpa.util.JpaUtil;
+import org.junit.Before;
 import org.junit.Test;
 import org.springframework.dao.IncorrectResultSizeDataAccessException;
 
+import javax.persistence.EntityManager;
 import java.util.ArrayList;
 import java.util.List;
 
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.sameInstance;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
 
 /**
  *
  * @author carlucci
  */
 public class JpaUtilTest {
+
+    private EntityManager mockManager;
+
+    @Before
+    public void setup() {
+        mockManager = createNiceMock(EntityManager.class);
+    }
+
+
     @Test
     public void getSingleResult_emptyList() {        
         List<Object> list = new ArrayList<Object>();        
@@ -58,7 +74,30 @@ public class JpaUtilTest {
     public void getSingleResult_multipleObjectPopulatedList() {                         
     
         List<Object> populatedList = generatePopulatedList(5);        
         JpaUtil.getSingleResult(populatedList);
-    }    
+    }
+
+    @Test
+    public void saveOrUpdate_save() {
+        Object o = new Object();
+        mockManager.persist(o);
+        expectLastCall();
+        replay(mockManager);
+
+        Object r =JpaUtil.saveOrUpdate(null, mockManager, o);
+        assertThat(r, is(sameInstance(o)));
+        verify(mockManager);
+    }
+
+    @Test
+    public void saveOrUpdate_update() {
+        Object o = new Object();
+        expect(mockManager.merge(o)).andReturn(new Object());
+        replay(mockManager);
+
+        Object r = JpaUtil.saveOrUpdate(new Object(), mockManager, o);
+        assertThat(r, is(not(sameInstance(o))));
+        verify(mockManager);
+    }
     
     // Private helper functions for the tests
     private List<Object> generatePopulatedList(int size) {



Mime
View raw message