1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.dbunit.database;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import java.sql.Connection;
26 import java.sql.DriverManager;
27
28 import org.dbunit.DatabaseEnvironment;
29 import org.dbunit.DatabaseProfile;
30 import org.dbunit.dataset.IDataSet;
31 import org.junit.jupiter.api.AfterEach;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 class CachingConnectionProviderIT
49 {
50 private DatabaseProfile profile;
51
52 private CachingConnectionProvider provider;
53
54 @BeforeEach
55 void setUp() throws Exception
56 {
57
58
59
60 provider = new CachingConnectionProvider();
61 profile = DatabaseEnvironment.getInstance().getProfile();
62 Class.forName(profile.getDriverClass());
63 }
64
65 @AfterEach
66 void tearDown() throws Exception
67 {
68 provider.close();
69 }
70
71 @Test
72 void testGetConnection_calledTwice_returnsSameConnectionAndPreservesMetadataCache()
73 throws Exception
74 {
75 final IDatabaseConnection first = provider.getConnection(this::createConnection);
76 final IDataSet firstDataSet = first.createDataSet();
77
78 final IDatabaseConnection second = provider.getConnection(this::createConnection);
79 final IDataSet secondDataSet = second.createDataSet();
80
81 assertThat(second).as("A second call while the cached connection is alive must reuse it.")
82 .isSameAs(first);
83 assertThat(secondDataSet)
84 .as("Reusing the connection must also reuse its accumulated table-metadata "
85 + "cache (DatabaseDataSet), not re-fetch it - that avoided re-fetch is the "
86 + "entire point of this feature.")
87 .isSameAs(firstDataSet);
88 }
89
90 @Test
91 void testGetConnection_afterUnderlyingConnectionDies_transparentlyReconnectsAndStaysUsable()
92 throws Exception
93 {
94 final IDatabaseConnection first = provider.getConnection(this::createConnection);
95
96 first.getConnection().close();
97
98 final IDatabaseConnection second = provider.getConnection(this::createConnection);
99
100 assertThat(second).as("A dead cached connection must be replaced, not returned as-is.")
101 .isNotSameAs(first);
102 assertThat(second.getRowCount("TEST_TABLE"))
103 .as("The replacement connection must be a real, working connection against the "
104 + "configured database.")
105 .isGreaterThanOrEqualTo(0);
106 }
107
108 @Test
109 void testClose_thenGetConnection_reconnectsSuccessfully() throws Exception
110 {
111 final IDatabaseConnection first = provider.getConnection(this::createConnection);
112
113 provider.close();
114
115 assertThat(first.getConnection().isClosed())
116 .as("close() must close the connection it hands back control of.").isTrue();
117
118 final IDatabaseConnection second = provider.getConnection(this::createConnection);
119
120 assertThat(second)
121 .as("Closing the provider must create a new connection.")
122 .isNotSameAs(first);
123 assertThat(second.getRowCount("TEST_TABLE"))
124 .as("The connection created after close() must be usable.")
125 .isGreaterThanOrEqualTo(0);
126 }
127
128 private IDatabaseConnection createConnection() throws Exception
129 {
130 final Connection jdbcConnection = DriverManager.getConnection(profile.getConnectionUrl(),
131 profile.getUser(), profile.getPassword());
132 return new DatabaseConnection(jdbcConnection, profile.getSchema());
133 }
134 }