View Javadoc
1   package org.apache.onami.persist;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.mockito.InOrder;
25  
26  import javax.servlet.FilterChain;
27  import javax.servlet.FilterConfig;
28  import javax.servlet.ServletRequest;
29  import javax.servlet.ServletResponse;
30  
31  import static org.mockito.Mockito.doThrow;
32  import static org.mockito.Mockito.inOrder;
33  import static org.mockito.Mockito.mock;
34  import static org.mockito.Mockito.verify;
35  
36  /**
37   * Test for {@link PersistenceFilterImpl}.
38   */
39  public class PersistenceFilterImplTest
40  {
41  
42      private PersistenceFilterImpl sut;
43  
44      private AllPersistenceServices allPersistenceServices;
45      private AllUnitsOfWork allUnitsOfWork;
46  
47      @Before
48      public void setUp()
49          throws Exception
50      {
51          allPersistenceServices = mock( AllPersistenceServices.class );
52          allUnitsOfWork = mock( AllUnitsOfWork.class );
53          sut = new PersistenceFilterImpl( allPersistenceServices, allUnitsOfWork );
54      }
55  
56      @Test
57      public void initShouldStartService()
58          throws Exception
59      {
60          sut.init( mock( FilterConfig.class ) );
61          verify( allPersistenceServices ).startAllStoppedPersistenceServices();
62      }
63  
64      @Test
65      public void destroyShouldStopService()
66      {
67          sut.destroy();
68          verify( allPersistenceServices ).stopAllPersistenceServices();
69      }
70  
71      @Test
72      public void doFilterShouldSpanUnitOfWork()
73          throws Exception
74      {
75          // given
76          final FilterChain chain = mock( FilterChain.class );
77          final InOrder inOrder = inOrder( allUnitsOfWork, chain );
78  
79          final ServletRequest request = mock( ServletRequest.class );
80          final ServletResponse response = mock( ServletResponse.class );
81  
82          // when
83          sut.doFilter( request, response, chain );
84  
85          // then
86          inOrder.verify( allUnitsOfWork ).beginAllInactiveUnitsOfWork();
87          inOrder.verify( chain ).doFilter( request, response );
88          inOrder.verify( allUnitsOfWork ).endAllUnitsOfWork();
89      }
90  
91      @Test(expected = RuntimeException.class)
92      public void doFilterShouldEndUnitOfWorkInCaseOfException()
93          throws Exception
94      {
95          // given
96          final FilterChain chain = mock( FilterChain.class );
97          final InOrder inOrder = inOrder( allUnitsOfWork, chain );
98  
99          final ServletRequest request = mock( ServletRequest.class );
100         final ServletResponse response = mock( ServletResponse.class );
101 
102         doThrow( new RuntimeException() ).when( chain ).doFilter( request, response );
103 
104         // when
105         try
106         {
107             sut.doFilter( request, response, chain );
108         }
109         // then
110         finally
111         {
112             inOrder.verify( allUnitsOfWork ).beginAllInactiveUnitsOfWork();
113             inOrder.verify( chain ).doFilter( request, response );
114             inOrder.verify( allUnitsOfWork ).endAllUnitsOfWork();
115         }
116     }
117 
118 }