001/**
002 * Copyright 2005-2013 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 */
016package org.kuali.rice.test.lifecycles;
017
018import org.kuali.rice.core.api.lifecycle.Lifecycle;
019import org.kuali.rice.core.api.lifecycle.Lifecycle;
020import org.kuali.rice.test.data.PerTestUnitTestData;
021import org.kuali.rice.test.data.UnitTestData;
022import org.kuali.rice.test.data.UnitTestDataUtils;
023
024import java.lang.reflect.Method;
025
026/**
027 * A lifecycle for loading SQL datasets based on the PerTestUnitTestData annotation. The individual SQL statements are
028 * loaded first, followed by the statements inside the files (files are loaded sequentially in the order listed in the
029 * annotation).
030 * 
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class PerTestDataLoaderLifecycle implements Lifecycle {
034        private boolean started;
035        private Method method;
036
037        public PerTestDataLoaderLifecycle(Method method) {
038                this.method = method;
039        }
040
041        public boolean isStarted() {
042                return started;
043        }
044
045        public void start() throws Exception {
046                if (method.getDeclaringClass().isAnnotationPresent(PerTestUnitTestData.class)) {
047                        UnitTestData[] data = method.getDeclaringClass().getAnnotation(PerTestUnitTestData.class).value();
048            UnitTestDataUtils.executeDataLoader(data);
049                }
050                if (method.isAnnotationPresent(UnitTestData.class)) {
051                        UnitTestData data = method.getAnnotation(UnitTestData.class);
052            UnitTestDataUtils.executeDataLoader(data);
053                }
054                started = true;
055        }
056
057        public void stop() throws Exception {
058            if (method.getDeclaringClass().isAnnotationPresent(PerTestUnitTestData.class)) {
059            UnitTestData[] data = method.getDeclaringClass().getAnnotation(PerTestUnitTestData.class).tearDown();
060            UnitTestDataUtils.executeDataLoader(data);
061        }
062                started = false;
063        }
064}