View Javadoc
1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.test;
17  
18  import org.kuali.rice.test.data.PerSuiteUnitTestData;
19  import org.kuali.rice.test.data.UnitTestData;
20  import org.kuali.test.KRADTestCase;
21  import org.springframework.jdbc.core.ConnectionCallback;
22  import org.springframework.jdbc.core.JdbcTemplate;
23  
24  import javax.sql.DataSource;
25  import java.sql.Connection;
26  import java.sql.ResultSet;
27  import java.sql.SQLException;
28  import java.sql.Statement;
29  
30  import static org.junit.Assert.*;
31  
32  /**
33   * This class is used by the {@link DataLoaderAnnotationTest} and {@link DataLoaderAnnotationOverrideTest} classes to verify parent class annotation usage
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  @PerSuiteUnitTestData({
39          @UnitTestData("insert into " + AnnotationTestParent.TEST_TABLE_NAME + " (COL) values ('1')"),
40          @UnitTestData(filename = "classpath:org/kuali/rice/test/AnnotationTestParentData.sql")
41  })
42  public abstract class AnnotationTestParent extends KRADTestCase {
43  
44      protected static final String TEST_TABLE_NAME = "EN_UNITTEST_T";
45      
46      protected void verifyCount(String valueToVerify, int count) throws SQLException {
47          assertEquals(count + " value(s) should be found for id " + valueToVerify, count, countTableResults(valueToVerify));
48      }
49  
50      protected void verifyExistence(String valueToVerify) throws SQLException {
51          assertTrue("Value should be found for id " + valueToVerify, hasTableResults(valueToVerify));
52      }
53  
54      protected void verifyNonExistent(String valueToVerify) throws SQLException {
55          assertFalse("No value should be found for id " + valueToVerify, hasTableResults(valueToVerify));
56      }
57  
58      protected boolean hasTableResults(String valueToVerify) {
59          return countTableResults(valueToVerify) > 0;
60      }
61  
62      protected int countTableResults(String valueToVerify) {
63          final String valueToCheck = valueToVerify;
64          final DataSource dataSource = TestHarnessServiceLocator.getDataSource();
65          return (Integer) new JdbcTemplate(dataSource).execute(new ConnectionCallback() {
66              public Object doInConnection(final Connection connection) throws SQLException {
67                  Statement statement = null;
68                  try {
69                      statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
70                      final ResultSet resultSet = statement.executeQuery("Select * from " + TEST_TABLE_NAME + " where COL = '" + valueToCheck + "'");
71                      assertNotNull("ResultSet should not be null",resultSet);
72                      int count = 0;
73                      while (resultSet.next()) {
74                          count++;
75                      }
76                      return count;
77                  } finally {
78                      if (statement != null) {
79                          statement.close();
80                      }
81                  }
82              }
83          });
84      }
85  
86  }