1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.guice.datasource.dbcp;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19
20 import com.google.inject.AbstractModule;
21 import com.google.inject.Guice;
22 import com.google.inject.Injector;
23 import com.google.inject.name.Names;
24
25 import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS;
26 import org.junit.jupiter.api.Test;
27
28 class DriverAdapterCPDSProviderTest {
29 @Test
30 void get() throws Throwable {
31 final String driver = TestDriver.class.getName();
32 final String url = "jdbc:h2:mem:testdb";
33 final String username = "test_user";
34 final String password = "test_password";
35 final int loginTimeout = 10;
36 final String description = "test_description";
37 final int maxIdle = 30;
38 final int maxOpenPreparedStatements = 40;
39 final int minEvictableIdleTimeMillis = 50;
40 final int numTestsPerEvictionRun = 60;
41 final boolean poolPreparedStatements = true;
42 final int timeBetweenEvictionRunsMillis = 70;
43 Injector injector = Guice.createInjector(new AbstractModule() {
44 @Override
45 protected void configure() {
46 bindConstant().annotatedWith(Names.named("JDBC.driver")).to(driver);
47 bindConstant().annotatedWith(Names.named("JDBC.url")).to(url);
48 bindConstant().annotatedWith(Names.named("JDBC.username")).to(username);
49 bindConstant().annotatedWith(Names.named("JDBC.password")).to(password);
50 bindConstant().annotatedWith(Names.named("JDBC.loginTimeout")).to(loginTimeout);
51 bindConstant().annotatedWith(Names.named("DBCP.description")).to(description);
52 bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle);
53 bindConstant().annotatedWith(Names.named("DBCP.maxOpenPreparedStatements")).to(maxOpenPreparedStatements);
54 bindConstant().annotatedWith(Names.named("DBCP.minEvictableIdleTimeMillis")).to(minEvictableIdleTimeMillis);
55 bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun);
56 bindConstant().annotatedWith(Names.named("DBCP.poolPreparedStatements")).to(poolPreparedStatements);
57 bindConstant().annotatedWith(Names.named("DBCP.timeBetweenEvictionRunsMillis"))
58 .to(timeBetweenEvictionRunsMillis);
59 }
60 });
61 DriverAdapterCPDSProvider provider = injector.getInstance(DriverAdapterCPDSProvider.class);
62
63 DriverAdapterCPDS adapter = (DriverAdapterCPDS) provider.get();
64
65 assertEquals(driver, adapter.getDriver());
66 assertEquals(url, adapter.getUrl());
67 assertEquals(username, adapter.getUser());
68 assertEquals(password, adapter.getPassword());
69 assertEquals(loginTimeout, adapter.getLoginTimeout());
70 assertEquals(description, adapter.getDescription());
71 assertEquals(maxIdle, adapter.getMaxIdle());
72 assertEquals(maxOpenPreparedStatements, adapter.getMaxPreparedStatements());
73 assertEquals(minEvictableIdleTimeMillis, adapter.getMinEvictableIdleDuration().toMillis());
74 assertEquals(numTestsPerEvictionRun, adapter.getNumTestsPerEvictionRun());
75 assertEquals(poolPreparedStatements, adapter.isPoolPreparedStatements());
76 assertEquals(timeBetweenEvictionRunsMillis, adapter.getDurationBetweenEvictionRuns().toMillis());
77 }
78
79 @Test
80 void getOtherValues() throws Throwable {
81 final String driver = TestDriver2.class.getName();
82 final String url = "jdbc:h2:mem:testdb2";
83 final String username = "test_user2";
84 final String password = "test_password2";
85 final int loginTimeout = 11;
86 final String description = "test_description2";
87 final int maxIdle = 31;
88 final int maxOpenPreparedStatements = 41;
89 final int minEvictableIdleTimeMillis = 51;
90 final int numTestsPerEvictionRun = 61;
91 final boolean poolPreparedStatements = false;
92 final int timeBetweenEvictionRunsMillis = 71;
93 Injector injector = Guice.createInjector(new AbstractModule() {
94 @Override
95 protected void configure() {
96 bindConstant().annotatedWith(Names.named("JDBC.driver")).to(driver);
97 bindConstant().annotatedWith(Names.named("JDBC.url")).to(url);
98 bindConstant().annotatedWith(Names.named("JDBC.username")).to(username);
99 bindConstant().annotatedWith(Names.named("JDBC.password")).to(password);
100 bindConstant().annotatedWith(Names.named("JDBC.loginTimeout")).to(loginTimeout);
101 bindConstant().annotatedWith(Names.named("DBCP.description")).to(description);
102 bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle);
103 bindConstant().annotatedWith(Names.named("DBCP.maxOpenPreparedStatements")).to(maxOpenPreparedStatements);
104 bindConstant().annotatedWith(Names.named("DBCP.minEvictableIdleTimeMillis")).to(minEvictableIdleTimeMillis);
105 bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun);
106 bindConstant().annotatedWith(Names.named("DBCP.poolPreparedStatements")).to(poolPreparedStatements);
107 bindConstant().annotatedWith(Names.named("DBCP.timeBetweenEvictionRunsMillis"))
108 .to(timeBetweenEvictionRunsMillis);
109 }
110 });
111 DriverAdapterCPDSProvider provider = injector.getInstance(DriverAdapterCPDSProvider.class);
112
113 DriverAdapterCPDS adapter = (DriverAdapterCPDS) provider.get();
114
115 assertEquals(driver, adapter.getDriver());
116 assertEquals(url, adapter.getUrl());
117 assertEquals(username, adapter.getUser());
118 assertEquals(password, adapter.getPassword());
119 assertEquals(loginTimeout, adapter.getLoginTimeout());
120 assertEquals(description, adapter.getDescription());
121 assertEquals(maxIdle, adapter.getMaxIdle());
122 assertEquals(maxOpenPreparedStatements, adapter.getMaxPreparedStatements());
123 assertEquals(minEvictableIdleTimeMillis, adapter.getMinEvictableIdleDuration().toMillis());
124 assertEquals(numTestsPerEvictionRun, adapter.getNumTestsPerEvictionRun());
125 assertEquals(poolPreparedStatements, adapter.isPoolPreparedStatements());
126 assertEquals(timeBetweenEvictionRunsMillis, adapter.getDurationBetweenEvictionRuns().toMillis());
127 }
128
129 public static class TestDriver {
130 }
131
132 public static class TestDriver2 {
133 }
134 }