-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ConnectionServiceFactoryTest.java
96 lines (74 loc) · 3.11 KB
/
ConnectionServiceFactoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package liquibase.database;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import liquibase.database.jvm.JdbcConnection;
public class ConnectionServiceFactoryTest {
private String mockUrl = "mock://url";
private ConnectionServiceFactory connectionServiceFactory;
@Before
public void setUp() {
ConnectionServiceFactory.reset();
connectionServiceFactory = ConnectionServiceFactory.getInstance();
}
@Test
public void testGetDatabaseConnection() {
// By Default only JdbcConnection registered that supports any Driver
assertThat(connectionServiceFactory.getDatabaseConnection(mockUrl))
.isInstanceOf(JdbcConnection.class);
// Register Medium Priority Connection that supports the Driver
final DatabaseConnection jdbcConnection100SupportsDriver = new JdbcConnection100SupportsDriver();
connectionServiceFactory.register(jdbcConnection100SupportsDriver);
assertThat(connectionServiceFactory.getDatabaseConnection(mockUrl))
.isInstanceOf(JdbcConnection100SupportsDriver.class);
// Register Higher Priority Connection that does not support the Driver
final DatabaseConnection jdbcConnection200DoesNotSupportDriver = new JdbcConnection200DoesNotSupportDriver();
connectionServiceFactory.register(jdbcConnection200DoesNotSupportDriver);
assertThat(connectionServiceFactory.getDatabaseConnection(mockUrl))
.isInstanceOf(JdbcConnection100SupportsDriver.class);
// Register Lower Priority Connection that supports the Driver
final DatabaseConnection jdbcConnection50SupportsDriver = new JdbcConnection50SupportsDriver();
connectionServiceFactory.register(jdbcConnection50SupportsDriver);
assertThat(connectionServiceFactory.getDatabaseConnection(mockUrl))
.isInstanceOf(JdbcConnection100SupportsDriver.class);
}
/**
* Test JdbcConnection Implementation Example Class with Low Priority and Supporting the Driver
*/
private static class JdbcConnection50SupportsDriver extends JdbcConnection {
@Override
public int getPriority() {
return 50;
}
@Override
public boolean supports(String url) {
return true;
}
}
/**
* Test JdbcConnection Implementation Example Class with Medium Priority and Supporting the Driver
*/
private static class JdbcConnection100SupportsDriver extends JdbcConnection {
@Override
public int getPriority() {
return 100;
}
@Override
public boolean supports(String url) {
return true;
}
}
/**
* Test JdbcConnection Implementation Example Class with High Priority and Not Supporting the Driver
*/
private static class JdbcConnection200DoesNotSupportDriver extends JdbcConnection {
@Override
public int getPriority() {
return 200;
}
@Override
public boolean supports(String url) {
return false;
}
}
}