1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.test;
17
18 import org.kuali.rice.test.TestHarnessServiceLocator;
19 import org.kuali.rice.test.data.PerSuiteUnitTestData;
20 import org.kuali.rice.test.data.UnitTestData;
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
34
35
36
37 @PerSuiteUnitTestData({
38 @UnitTestData("insert into " + AnnotationTestParent.TEST_TABLE_NAME + " (COL) values ('1')"),
39 @UnitTestData(filename = "classpath:org/kuali/rice/test/AnnotationTestParentData.sql")
40 })
41 public abstract class AnnotationTestParent extends CORETestCase {
42
43 protected static final String TEST_TABLE_NAME = "EN_UNITTEST_T";
44
45 protected void verifyCount(String valueToVerify, int count) throws SQLException {
46 verifyCount(valueToVerify, count, "");
47 }
48
49 protected void verifyCount(String valueToVerify, int count, String message) throws SQLException {
50 assertEquals(count + " value(s) should be found for id " + valueToVerify + " " + message, count, countTableResults(valueToVerify));
51 }
52
53 protected void verifyExistence(String valueToVerify) throws SQLException {
54 assertTrue("Value should be found for id " + valueToVerify, hasTableResults(valueToVerify));
55 }
56
57 protected void verifyNonExistent(String valueToVerify) throws SQLException {
58 assertFalse("No value should be found for id " + valueToVerify, hasTableResults(valueToVerify));
59 }
60
61 protected boolean hasTableResults(String valueToVerify) {
62 return countTableResults(valueToVerify) > 0;
63 }
64
65 protected int countTableResults(String valueToVerify) {
66 final String valueToCheck = valueToVerify;
67 final DataSource dataSource = TestHarnessServiceLocator.getDataSource();
68 return (Integer) new JdbcTemplate(dataSource).execute(new ConnectionCallback() {
69 public Object doInConnection(final Connection connection) throws SQLException {
70 Statement statement = null;
71 try {
72 statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
73 final ResultSet resultSet = statement.executeQuery("Select * from " + TEST_TABLE_NAME + " where COL = '" + valueToCheck + "'");
74 assertNotNull("ResultSet should not be null",resultSet);
75 int count = 0;
76 while (resultSet.next()) {
77 count++;
78 }
79 return count;
80 } finally {
81 if (statement != null) {
82 statement.close();
83 }
84 }
85 }
86 });
87 }
88
89 }