1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
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 }