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.kuali.rice.core.api.lifecycle.Lifecycle;
020    import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
021    import org.kuali.rice.krad.datadictionary.DataDictionary;
022    import org.kuali.rice.test.BaselineTestCase;
023    import org.kuali.rice.test.SQLDataLoader;
024    import org.kuali.rice.test.TestHarnessServiceLocator;
025    import org.kuali.rice.test.TestUtilities;
026    import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
027    
028    import javax.xml.namespace.QName;
029    import java.util.HashSet;
030    import java.util.List;
031    
032    /**
033     * Default test base for a full KRAD enabled integration test
034     *
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     */
037    @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
038    public abstract class KRADTestCase extends BaselineTestCase {
039    
040        private static final String SQL_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.sql";
041        private static final String XML_FILE = "classpath:org/kuali/rice/krad/test/DefaultSuiteTestData.xml";
042        private static final String KRAD_MODULE_NAME = "krad";
043    
044        private SpringResourceLoader kradTestSpringResourceLoader;
045    
046        protected DataDictionary dd = null;
047    
048        public KRADTestCase() {
049            super(KRAD_MODULE_NAME);
050        }
051    
052        @Override
053        protected void setUpInternal() throws Exception {
054            super.setUpInternal();
055    
056            List<Class> classes = TestUtilities.getHierarchyClassesToHandle(getClass(),
057                    new Class[]{TestDictionaryConfig.class}, new HashSet<String>());
058    
059            // if annotation is present then initialize test data dictionary
060            if (!classes.isEmpty()) {
061                dd = (DataDictionary) kradTestSpringResourceLoader.getContext().getBean("testDataDictionary");
062    
063                // add any additional dictionary files required by the test
064                for (Class c : classes) {
065                    if (c.isAnnotationPresent(TestDictionaryConfig.class)) {
066                        TestDictionaryConfig testDictionaryConfig = (TestDictionaryConfig) c.getAnnotation(
067                                TestDictionaryConfig.class);
068    
069                        String dictionaryFileString = testDictionaryConfig.dataDictionaryFiles();
070                        String[] dictionaryFiles = StringUtils.split(dictionaryFileString, ",");
071                        for (String dictionaryFile : dictionaryFiles) {
072                            dd.addConfigFileLocation(dictionaryFile);
073                        }
074                    }
075                }
076    
077                dd.parseDataDictionaryConfigurationFiles(true);
078            }
079        }
080    
081        /**
082         * Returns an instance of the bean with the given id that has been configured in the test dictionary
083         *
084         * @param id - id of the bean definition
085         * @return Object instance of the given bean class, or null if not found or dictionary is not loaded
086         */
087        protected Object getTestDictionaryObject(String id) {
088            if (dd != null) {
089                return dd.getDictionaryObject(id);
090            }
091    
092            return null;
093        }
094    
095        @Override
096        protected List<Lifecycle> getSuiteLifecycles() {
097            List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
098            suiteLifecycles.add(new KEWXmlDataLoaderLifecycle(XML_FILE));
099            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    }