onami-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sclas...@apache.org
Subject svn commit: r1632185 - /onami/trunk/persist/src/test/java/org/apache/onami/persist/test/PersistenceServletFilterTest.java
Date Wed, 15 Oct 2014 21:59:27 GMT
Author: sclassen
Date: Wed Oct 15 21:59:27 2014
New Revision: 1632185

URL: http://svn.apache.org/r1632185
Log:
ONAMI-115: test to ensure that the servlet filter starts all PUs

Modified:
    onami/trunk/persist/src/test/java/org/apache/onami/persist/test/PersistenceServletFilterTest.java

Modified: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/PersistenceServletFilterTest.java
URL: http://svn.apache.org/viewvc/onami/trunk/persist/src/test/java/org/apache/onami/persist/test/PersistenceServletFilterTest.java?rev=1632185&r1=1632184&r2=1632185&view=diff
==============================================================================
--- onami/trunk/persist/src/test/java/org/apache/onami/persist/test/PersistenceServletFilterTest.java
(original)
+++ onami/trunk/persist/src/test/java/org/apache/onami/persist/test/PersistenceServletFilterTest.java
Wed Oct 15 21:59:27 2014
@@ -21,27 +21,56 @@ package org.apache.onami.persist.test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
+import com.google.inject.Key;
 import com.google.inject.Scopes;
+import org.apache.onami.persist.EntityManagerProvider;
 import org.apache.onami.persist.PersistenceFilter;
 import org.apache.onami.persist.PersistenceModule;
+import org.apache.onami.persist.test.multipersistenceunits.FirstPU;
+import org.apache.onami.persist.test.multipersistenceunits.SecondPU;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
 
 import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
 
 /**
  * Test which ensures that the @{link PersistenceFilter} fulfills the requirements of a guice
servlet filter.
  */
 public class PersistenceServletFilterTest
 {
+    private EntityManagerProvider firstEmp;
+
+    private EntityManagerProvider secondEmp;
+
+    private PersistenceFilter persistenceFilter;
+
     private Injector injector;
 
     @Before
-    public void setUp()
+    public final void setUp()
+        throws Exception
     {
         final PersistenceModule pm = createPersistenceModuleForTest();
         injector = Guice.createInjector( pm );
+
+        persistenceFilter = injector.getInstance( Key.get( PersistenceFilter.class ) );
+        persistenceFilter.init( mock( FilterConfig.class ) );
+
+        firstEmp = injector.getInstance( Key.get( EntityManagerProvider.class, FirstPU.class
) );
+        secondEmp = injector.getInstance( Key.get( EntityManagerProvider.class, SecondPU.class
) );
     }
 
     private PersistenceModule createPersistenceModuleForTest()
@@ -52,11 +81,20 @@ public class PersistenceServletFilterTes
             @Override
             protected void configurePersistence()
             {
-                bindApplicationManagedPersistenceUnit( "firstUnit" );
+                bindApplicationManagedPersistenceUnit( "firstUnit" ).annotatedWith( FirstPU.class
);
+                bindApplicationManagedPersistenceUnit( "secondUnit" ).annotatedWith( SecondPU.class
);
             }
         };
     }
 
+    @After
+    public final void tearDown()
+        throws Exception
+    {
+        persistenceFilter.destroy();
+    }
+
+
     @Test
     public void persistenceFilterShouldBeSingleton()
     {
@@ -67,4 +105,43 @@ public class PersistenceServletFilterTes
     {
         return Scopes.isSingleton( injector.getBinding( type ) );
     }
+
+    @Test
+    public void shouldFilter()
+        throws Exception
+    {
+        // given
+        final ServletRequest request = mock( ServletRequest.class );
+        final ServletResponse response = mock( ServletResponse.class );
+        final FilterChain filterChain = mock( FilterChain.class );
+        doAnswer( new ServletMock() ).when( filterChain ).doFilter( request, response );
+
+        // when
+        persistenceFilter.doFilter( request, response, filterChain );
+    }
+
+    private class ServletMock
+        implements Answer<Void>
+    {
+
+        public Void answer( InvocationOnMock invocation )
+            throws Throwable
+        {
+            // given
+            final TestEntity firstEntity = new TestEntity();
+            final TestEntity secondEntity = new TestEntity();
+
+            // when
+            firstEmp.get().persist( firstEntity );
+            secondEmp.get().persist( secondEntity );
+
+            // then
+            assertNotNull( firstEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+            assertNotNull( secondEmp.get().find( TestEntity.class, secondEntity.getId() )
);
+            assertNull( firstEmp.get().find( TestEntity.class, secondEntity.getId() ) );
+            assertNull( secondEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+
+            return null;
+        }
+    }
 }



Mime
View raw message