001    /**
002     * Copyright 2004-2012 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.hr.time.util;
017    
018    import java.sql.SQLException;
019    import java.sql.Statement;
020    import java.util.List;
021    
022    import javax.sql.DataSource;
023    
024    import junit.framework.Assert;
025    
026    import org.apache.commons.lang.StringUtils;
027    import org.springframework.jdbc.core.JdbcTemplate;
028    import org.springframework.jdbc.core.StatementCallback;
029    import org.springframework.transaction.PlatformTransactionManager;
030    import org.springframework.transaction.TransactionStatus;
031    import org.springframework.transaction.support.TransactionCallback;
032    import org.springframework.transaction.support.TransactionTemplate;
033    
034    public class LoadDatabaseDataLifeCycle extends SQLDataLifeCycle {
035        public LoadDatabaseDataLifeCycle() {
036        }
037    
038        public LoadDatabaseDataLifeCycle(Class caller) {
039            super(caller);
040        }
041    
042            public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) {
043                    LOG.info("Clearing tables for schema " + schemaName);
044                    Assert.assertNotNull("DataSource could not be located.", dataSource);
045    
046                    if (schemaName == null || schemaName.equals("")) {
047                            Assert.fail("Empty schema name given");
048                    }
049                    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Object>() {
050                public Object doInTransaction(final TransactionStatus status) {
051                    verifyTestEnvironment(dataSource);
052                    return new JdbcTemplate(dataSource).execute(new StatementCallback<Object>() {
053                            public Object doInStatement(Statement statement) throws SQLException {
054                            if (callingTestClass != null) {
055                                    List<String> sqlStatements = getTestDataSQLStatements("src/test/config/sql/" + callingTestClass.getSimpleName() + ".sql");
056                                    
057                                    for(String sql : sqlStatements){
058                                    if (!sql.startsWith("#") && !sql.startsWith("//") && !StringUtils.isEmpty(sql.trim())) {
059                                        // ignore comment lines in our sql reader.
060                                                statement.addBatch(sql);
061                                    }
062                                    }
063                            }
064                                    statement.executeBatch();
065                                    return null;
066                            }
067                    });
068                }
069            });
070            }
071    }