View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.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.test;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.lifecycle.Lifecycle;
20  import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
21  import org.kuali.rice.krad.datadictionary.DataDictionary;
22  import org.kuali.rice.test.BaselineTestCase;
23  import org.kuali.rice.test.SQLDataLoader;
24  import org.kuali.rice.test.TestHarnessServiceLocator;
25  import org.kuali.rice.test.TestUtilities;
26  import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
27  
28  import javax.xml.namespace.QName;
29  import java.util.HashSet;
30  import java.util.List;
31  
32  /**
33   * Default test base for a full KRAD enabled integration test
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
38  public abstract class KRADTestCase extends BaselineTestCase {
39  
40      private static final String SQL_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.sql";
41      private static final String XML_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.xml";
42      private static final String KRAD_MODULE_NAME = "krad";
43  
44      private SpringResourceLoader kradTestSpringResourceLoader;
45  
46      protected DataDictionary dd = null;
47  
48      public KRADTestCase() {
49          super(KRAD_MODULE_NAME);
50      }
51  
52      @Override
53      protected void setUpInternal() throws Exception {
54          super.setUpInternal();
55  
56          List<Class> classes = TestUtilities.getHierarchyClassesToHandle(getClass(),
57                  new Class[]{TestDictionaryConfig.class}, new HashSet<String>());
58  
59          // if annotation is present then initialize test data dictionary
60          if (!classes.isEmpty()) {
61              dd = (DataDictionary) kradTestSpringResourceLoader.getContext().getBean("testDataDictionary");
62  
63              // add any additional dictionary files required by the test
64              for (Class c : classes) {
65                  if (c.isAnnotationPresent(TestDictionaryConfig.class)) {
66                      TestDictionaryConfig testDictionaryConfig = (TestDictionaryConfig) c.getAnnotation(
67                              TestDictionaryConfig.class);
68  
69                      String dictionaryFileString = testDictionaryConfig.dataDictionaryFiles();
70                      String[] dictionaryFiles = StringUtils.split(dictionaryFileString, ",");
71                      for (String dictionaryFile : dictionaryFiles) {
72                          dd.addConfigFileLocation(dictionaryFile);
73                      }
74                  }
75              }
76  
77              dd.parseDataDictionaryConfigurationFiles(true);
78          }
79      }
80  
81      /**
82       * Returns an instance of the bean with the given id that has been configured in the test dictionary
83       *
84       * @param id - id of the bean definition
85       * @return Object instance of the given bean class, or null if not found or dictionary is not loaded
86       */
87      protected Object getTestDictionaryObject(String id) {
88          if (dd != null) {
89              return dd.getDictionaryObject(id);
90          }
91  
92          return null;
93      }
94  
95      @Override
96      protected List<Lifecycle> getSuiteLifecycles() {
97          List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
98          suiteLifecycles.add(new KEWXmlDataLoaderLifecycle(XML_FILE));
99          return suiteLifecycles;
100     }
101 
102     @Override
103     protected void loadSuiteTestData() throws Exception {
104         super.loadSuiteTestData();
105         new SQLDataLoader(SQL_FILE, ";").runSql();
106     }
107 
108     @Override
109     protected Lifecycle getLoadApplicationLifecycle() {
110         return getKRADTestSpringResourceLoader();
111     }
112 
113     public SpringResourceLoader getKRADTestSpringResourceLoader() {
114         if (kradTestSpringResourceLoader == null) {
115             kradTestSpringResourceLoader = new SpringResourceLoader(new QName("KRADTestResourceLoader"),
116                     "classpath:KRADTestSpringBeans.xml", null);
117         }
118         return kradTestSpringResourceLoader;
119     }
120 }