001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.test;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.log4j.Logger;
020    import org.kuali.rice.core.api.lifecycle.Lifecycle;
021    import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
022    import org.kuali.rice.krad.datadictionary.DataDictionary;
023    import org.kuali.rice.test.BaselineTestCase;
024    import org.kuali.rice.test.SQLDataLoader;
025    import org.kuali.rice.test.TestUtilities;
026    import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
027    import org.springframework.context.ConfigurableApplicationContext;
028    import org.springframework.context.support.ClassPathXmlApplicationContext;
029    
030    import javax.xml.namespace.QName;
031    import java.util.HashSet;
032    import java.util.List;
033    
034    /**
035     * Default test base for a full KRAD enabled integration test
036     *
037     * @author Kuali Rice Team (rice.collab@kuali.org)
038     */
039    @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
040    public abstract class KRADTestCase extends BaselineTestCase {
041        private static final String SQL_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.sql";
042        private static final String XML_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.xml";
043        private static final String KRAD_MODULE_NAME = "krad";
044    
045        protected DataDictionary dd;
046    
047        public KRADTestCase() {
048            super(KRAD_MODULE_NAME);
049        }
050    
051        /**
052         * propagate constructor
053         * @param moduleName - the name of the module
054         */
055        public KRADTestCase(String moduleName) {
056            super(moduleName);
057        }
058    
059        @Override
060        protected void setUpInternal() throws Exception {
061            super.setUpInternal();
062    
063            List<Class> classes = TestUtilities.getHierarchyClassesToHandle(getClass(),
064                    new Class[]{TestDictionaryConfig.class}, new HashSet<String>());
065    
066            // if annotation is present then initialize test data dictionary (setup once per suite)
067            if (!classes.isEmpty()) {
068                ConfigurableApplicationContext context  = new ClassPathXmlApplicationContext("TestDataDictionary.xml");
069                dd = (DataDictionary) context.getBean("testDataDictionary");
070    
071                // add any additional dictionary files required by the test
072                for (Class c : classes) {
073                    if (c.isAnnotationPresent(TestDictionaryConfig.class)) {
074                        TestDictionaryConfig testDictionaryConfig = (TestDictionaryConfig) c.getAnnotation(
075                                TestDictionaryConfig.class);
076    
077                        String namespaceCode = testDictionaryConfig.namespaceCode();
078                        String dictionaryFileString = testDictionaryConfig.dataDictionaryFiles();
079    
080                        String[] dictionaryFiles = StringUtils.split(dictionaryFileString, ",");
081                        for (String dictionaryFile : dictionaryFiles) {
082                            LOG.info("Adding test data dictionary file: " + dictionaryFile);
083    
084                            dd.addConfigFileLocation(namespaceCode, dictionaryFile);
085                        }
086                    }
087                }
088    
089                dd.parseDataDictionaryConfigurationFiles(true);
090            }
091        }
092    
093        /**
094         * Returns an instance of the bean with the given id that has been configured in the test dictionary
095         *
096         * @param id - id of the bean definition
097         * @return Object instance of the given bean class, or null if not found or dictionary is not loaded
098         */
099        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    }