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