View Javadoc

1   /*
2    * Copyright 2012 The Kuali Foundation
3    *
4    * Licensed under the the Educational Community License, Version 1.0
5    * (the "License"); you may not use this file except in compliance
6    * with the License.  You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.common.test.mock.data;
17  
18  import java.util.Date;
19  import java.util.Map;
20  
21  import org.kuali.student.common.mock.MockService;
22  import org.kuali.student.common.test.TestAwareDataLoader;
23  import org.kuali.student.r2.common.dto.ContextInfo;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.beans.BeansException;
27  import org.springframework.context.ApplicationContext;
28  import org.springframework.context.ApplicationContextAware;
29  
30  /**
31   * Using this data loader it is possible to have the entire stack of mock
32   * services reset their state back to the initial conditions in between unit
33   * test method runs.
34   * 
35   * This allows for destructive cases to be tested without concern about the test
36   * data state for subsequent tests.
37   * 
38   * @author Kuali Student Team
39   * 
40   */
41  public abstract class AbstractMockServicesAwareDataLoader  implements ApplicationContextAware, TestAwareDataLoader {
42      
43      private static final Logger log = LoggerFactory
44              .getLogger(AbstractMockServicesAwareDataLoader.class);
45      
46      protected ApplicationContext applicationContext;
47  
48      protected boolean initialized;
49      
50      protected final ContextInfo context;
51      
52      public AbstractMockServicesAwareDataLoader() {
53      
54          context = new ContextInfo();
55          
56          context.setPrincipalId("123");
57          context.setCurrentDate(new Date());
58      }
59  
60      @Override
61      public final void setApplicationContext(ApplicationContext applicationContext)
62              throws BeansException {
63                  this.applicationContext = applicationContext;
64          
65      }
66  
67      @Override
68      public final void beforeTest() throws Exception {
69          
70          initializeData();
71          
72          initialized = true;
73          
74      }
75  
76      /**
77       * Child classes implement this method to add data into the mock services.
78       */
79      protected abstract void initializeData() throws Exception;
80      
81      
82  
83      /**
84       * @return the initialized
85       */
86      public final boolean isInitialized() {
87          return initialized;
88      }
89  
90      @Override
91      public final void afterTest() throws Exception {
92          
93          Map<String, MockService> map = applicationContext.getBeansOfType(MockService.class);
94          
95          for (MockService mock : map.values()) {
96          
97              // clear all mock services of state at the end of the test
98              mock.clear();
99          }
100         
101     }
102     
103     
104     
105     
106 }