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.apache.log4j.Logger;
20  import org.kuali.rice.core.api.lifecycle.Lifecycle;
21  import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
22  import org.kuali.rice.krad.datadictionary.DataDictionary;
23  import org.kuali.rice.test.BaselineTestCase;
24  import org.kuali.rice.test.SQLDataLoader;
25  import org.kuali.rice.test.TestUtilities;
26  import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
27  import org.springframework.context.ConfigurableApplicationContext;
28  import org.springframework.context.support.ClassPathXmlApplicationContext;
29  
30  import javax.xml.namespace.QName;
31  import java.util.HashSet;
32  import java.util.List;
33  
34  /**
35   * Default test base for a full KRAD enabled integration test
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
40  public abstract class KRADTestCase extends BaselineTestCase {
41      private static final String SQL_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.sql";
42      private static final String XML_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.xml";
43      private static final String KRAD_MODULE_NAME = "krad";
44  
45      protected DataDictionary dd;
46  
47      public KRADTestCase() {
48          super(KRAD_MODULE_NAME);
49      }
50  
51      /**
52       * propagate constructor
53       * @param moduleName - the name of the module
54       */
55      public KRADTestCase(String moduleName) {
56          super(moduleName);
57      }
58  
59      @Override
60      protected void setUpInternal() throws Exception {
61          super.setUpInternal();
62  
63          List<Class> classes = TestUtilities.getHierarchyClassesToHandle(getClass(),
64                  new Class[]{TestDictionaryConfig.class}, new HashSet<String>());
65  
66          // if annotation is present then initialize test data dictionary (setup once per suite)
67          if (!classes.isEmpty()) {
68              ConfigurableApplicationContext context  = new ClassPathXmlApplicationContext("TestDataDictionary.xml");
69              dd = (DataDictionary) context.getBean("testDataDictionary");
70  
71              // add any additional dictionary files required by the test
72              for (Class c : classes) {
73                  if (c.isAnnotationPresent(TestDictionaryConfig.class)) {
74                      TestDictionaryConfig testDictionaryConfig = (TestDictionaryConfig) c.getAnnotation(
75                              TestDictionaryConfig.class);
76  
77                      String namespaceCode = testDictionaryConfig.namespaceCode();
78                      String dictionaryFileString = testDictionaryConfig.dataDictionaryFiles();
79  
80                      String[] dictionaryFiles = StringUtils.split(dictionaryFileString, ",");
81                      for (String dictionaryFile : dictionaryFiles) {
82                          LOG.info("Adding test data dictionary file: " + dictionaryFile);
83  
84                          dd.addConfigFileLocation(namespaceCode, dictionaryFile);
85                      }
86                  }
87              }
88  
89              dd.parseDataDictionaryConfigurationFiles(true);
90          }
91      }
92  
93      /**
94       * Returns an instance of the bean with the given id that has been configured in the test dictionary
95       *
96       * @param id - id of the bean definition
97       * @return Object instance of the given bean class, or null if not found or dictionary is not loaded
98       */
99      protected Object getTestDictionaryObject(String id) {
100         if (dd != null) {
101             return dd.getDictionaryObject(id);
102         }
103 
104         return null;
105     }
106 
107     @Override
108     protected List<Lifecycle> getSuiteLifecycles() {
109         List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
110         suiteLifecycles.add(new KEWXmlDataLoaderLifecycle(XML_FILE));
111 
112         return suiteLifecycles;
113     }
114 
115     @Override
116     protected void loadSuiteTestData() throws Exception {
117         super.loadSuiteTestData();
118         new SQLDataLoader(SQL_FILE, ";").runSql();
119     }
120 
121     @Override
122     protected Lifecycle getLoadApplicationLifecycle() {
123         SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KRADTestResourceLoader"),
124                 "classpath:KRADTestSpringBeans.xml", null);
125         springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
126         return springResourceLoader;
127     }
128 }