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  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  
42      private static final String SQL_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.sql";
43      private static final String XML_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.xml";
44      private static final String KRAD_MODULE_NAME = "krad";
45  
46      protected DataDictionary dd;
47  
48      public KRADTestCase() {
49          super(KRAD_MODULE_NAME);
50      }
51  
52      /**
53       * propagate constructor
54       * @param moduleName - the name of the module
55       */
56      public KRADTestCase(String moduleName) {
57          super(moduleName);
58      }
59  
60      @Override
61      protected void setUpInternal() throws Exception {
62          super.setUpInternal();
63  
64          List<Class> classes = TestUtilities.getHierarchyClassesToHandle(getClass(),
65                  new Class[]{TestDictionaryConfig.class}, new HashSet<String>());
66  
67          // if annotation is present then initialize test data dictionary (setup once per suite)
68          if (!classes.isEmpty()) {
69              ConfigurableApplicationContext context  = new ClassPathXmlApplicationContext("TestDataDictionary.xml");
70              dd = (DataDictionary) context.getBean("testDataDictionary");
71  
72              // add any additional dictionary files required by the test
73              for (Class c : classes) {
74                  if (c.isAnnotationPresent(TestDictionaryConfig.class)) {
75                      TestDictionaryConfig testDictionaryConfig = (TestDictionaryConfig) c.getAnnotation(
76                              TestDictionaryConfig.class);
77  
78                      String dictionaryFileString = testDictionaryConfig.dataDictionaryFiles();
79                      String[] dictionaryFiles = StringUtils.split(dictionaryFileString, ",");
80                      for (String dictionaryFile : dictionaryFiles) {
81                          dd.addConfigFileLocation(dictionaryFile);
82                      }
83                  }
84              }
85  
86              dd.parseDataDictionaryConfigurationFiles(true);
87          }
88      }
89  
90      /**
91       * Returns an instance of the bean with the given id that has been configured in the test dictionary
92       *
93       * @param id - id of the bean definition
94       * @return Object instance of the given bean class, or null if not found or dictionary is not loaded
95       */
96      protected Object getTestDictionaryObject(String id) {
97          if (dd != null) {
98              return dd.getDictionaryObject(id);
99          }
100 
101         return null;
102     }
103 
104     @Override
105     protected List<Lifecycle> getSuiteLifecycles() {
106         List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
107         suiteLifecycles.add(new KEWXmlDataLoaderLifecycle(XML_FILE));
108 
109         return suiteLifecycles;
110     }
111 
112     @Override
113     protected void loadSuiteTestData() throws Exception {
114         super.loadSuiteTestData();
115         new SQLDataLoader(SQL_FILE, ";").runSql();
116     }
117 
118     @Override
119     protected Lifecycle getLoadApplicationLifecycle() {
120         SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KRADTestResourceLoader"),
121                 "classpath:KRADTestSpringBeans.xml", null);
122         springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
123         return springResourceLoader;
124     }
125 }