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.rice.test;
017    
018    import org.kuali.rice.test.data.PerSuiteUnitTestData;
019    import org.kuali.rice.test.data.UnitTestData;
020    import org.kuali.test.KRADTestCase;
021    import org.springframework.jdbc.core.ConnectionCallback;
022    import org.springframework.jdbc.core.JdbcTemplate;
023    
024    import javax.sql.DataSource;
025    import java.sql.Connection;
026    import java.sql.ResultSet;
027    import java.sql.SQLException;
028    import java.sql.Statement;
029    
030    import static org.junit.Assert.*;
031    
032    /**
033     * This class is used by the {@link DataLoaderAnnotationTest} and {@link DataLoaderAnnotationOverrideTest} classes to verify parent class annotation usage
034     * 
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     *
037     */
038    @PerSuiteUnitTestData({
039            @UnitTestData("insert into " + AnnotationTestParent.TEST_TABLE_NAME + " (COL) values ('1')"),
040            @UnitTestData(filename = "classpath:org/kuali/rice/test/AnnotationTestParentData.sql")
041    })
042    public abstract class AnnotationTestParent extends KRADTestCase {
043    
044        protected static final String TEST_TABLE_NAME = "EN_UNITTEST_T";
045        
046        protected void verifyCount(String valueToVerify, int count) throws SQLException {
047            assertEquals(count + " value(s) should be found for id " + valueToVerify, count, countTableResults(valueToVerify));
048        }
049    
050        protected void verifyExistence(String valueToVerify) throws SQLException {
051            assertTrue("Value should be found for id " + valueToVerify, hasTableResults(valueToVerify));
052        }
053    
054        protected void verifyNonExistent(String valueToVerify) throws SQLException {
055            assertFalse("No value should be found for id " + valueToVerify, hasTableResults(valueToVerify));
056        }
057    
058        protected boolean hasTableResults(String valueToVerify) {
059            return countTableResults(valueToVerify) > 0;
060        }
061    
062        protected int countTableResults(String valueToVerify) {
063            final String valueToCheck = valueToVerify;
064            final DataSource dataSource = TestHarnessServiceLocator.getDataSource();
065            return (Integer) new JdbcTemplate(dataSource).execute(new ConnectionCallback() {
066                public Object doInConnection(final Connection connection) throws SQLException {
067                    Statement statement = null;
068                    try {
069                        statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
070                        final ResultSet resultSet = statement.executeQuery("Select * from " + TEST_TABLE_NAME + " where COL = '" + valueToCheck + "'");
071                        assertNotNull("ResultSet should not be null",resultSet);
072                        int count = 0;
073                        while (resultSet.next()) {
074                            count++;
075                        }
076                        return count;
077                    } finally {
078                        if (statement != null) {
079                            statement.close();
080                        }
081                    }
082                }
083            });
084        }
085    
086    }