32
32
import org .junit .Test ;
33
33
34
34
import java .sql .Connection ;
35
+ import java .sql .DatabaseMetaData ;
35
36
import java .sql .PreparedStatement ;
36
37
import java .sql .SQLException ;
37
38
import java .sql .Statement ;
48
49
49
50
public final class ExecuteEventListenerTest extends BaseEventListenerTest {
50
51
52
+ private static final String HOST_URL = "jdbc:mysql://127.0.0.1:3306/ds_0" ;
53
+
51
54
private ShardingExecuteEngine executeEngine = new ShardingExecuteEngine (5 );
52
55
53
56
private final SQLExecuteTemplate sqlExecuteTemplate = new SQLExecuteTemplate (executeEngine );
@@ -61,6 +64,8 @@ public void tearDown() {
61
64
public void assertSingleStatement () throws SQLException {
62
65
Statement statement = mock (Statement .class );
63
66
when (statement .getConnection ()).thenReturn (mock (Connection .class ));
67
+ when (statement .getConnection ().getMetaData ()).thenReturn (mock (DatabaseMetaData .class ));
68
+ when (statement .getConnection ().getMetaData ().getURL ()).thenReturn (HOST_URL );
64
69
final boolean isExceptionThrown = ExecutorExceptionHandler .isExceptionThrown ();
65
70
final Map <String , Object > dataMap = ExecutorDataMap .getDataMap ();
66
71
SQLExecuteCallback <Integer > executeCallback = new SQLExecuteCallback <Integer >(SQLType .DML , isExceptionThrown , dataMap ) {
@@ -80,9 +85,13 @@ public void assertMultiStatement() throws SQLException {
80
85
List <StatementExecuteUnit > statementExecuteUnits = new ArrayList <>(2 );
81
86
Statement stm1 = mock (Statement .class );
82
87
when (stm1 .getConnection ()).thenReturn (mock (Connection .class ));
88
+ when (stm1 .getConnection ().getMetaData ()).thenReturn (mock (DatabaseMetaData .class ));
89
+ when (stm1 .getConnection ().getMetaData ().getURL ()).thenReturn (HOST_URL );
83
90
statementExecuteUnits .add (new StatementExecuteUnit (new RouteUnit ("ds_0" , new SQLUnit ("insert into ..." , Collections .singletonList (Collections .<Object >singletonList (1 )))), stm1 ));
84
91
Statement stm2 = mock (Statement .class );
85
92
when (stm2 .getConnection ()).thenReturn (mock (Connection .class ));
93
+ when (stm2 .getConnection ().getMetaData ()).thenReturn (mock (DatabaseMetaData .class ));
94
+ when (stm2 .getConnection ().getMetaData ().getURL ()).thenReturn (HOST_URL );
86
95
statementExecuteUnits .add (new StatementExecuteUnit (new RouteUnit ("ds_0" , new SQLUnit ("insert into ..." , Collections .singletonList (Collections .<Object >singletonList (1 )))), stm2 ));
87
96
final boolean isExceptionThrown = ExecutorExceptionHandler .isExceptionThrown ();
88
97
final Map <String , Object > dataMap = ExecutorDataMap .getDataMap ();
@@ -103,9 +112,13 @@ public void assertBatchPreparedStatement() throws SQLException {
103
112
List <List <Object >> parameterSets = Arrays .asList (Arrays .<Object >asList (1 , 2 ), Arrays .<Object >asList (3 , 4 ));
104
113
PreparedStatement preparedStatement1 = mock (PreparedStatement .class );
105
114
when (preparedStatement1 .getConnection ()).thenReturn (mock (Connection .class ));
115
+ when (preparedStatement1 .getConnection ().getMetaData ()).thenReturn (mock (DatabaseMetaData .class ));
116
+ when (preparedStatement1 .getConnection ().getMetaData ().getURL ()).thenReturn (HOST_URL );
106
117
batchPreparedStatementExecuteUnits .add (new BatchPreparedStatementExecuteUnit (new RouteUnit ("ds_0" , new SQLUnit ("insert into ..." , parameterSets )), preparedStatement1 ));
107
118
PreparedStatement preparedStatement2 = mock (PreparedStatement .class );
108
119
when (preparedStatement2 .getConnection ()).thenReturn (mock (Connection .class ));
120
+ when (preparedStatement2 .getConnection ().getMetaData ()).thenReturn (mock (DatabaseMetaData .class ));
121
+ when (preparedStatement2 .getConnection ().getMetaData ().getURL ()).thenReturn (HOST_URL );
109
122
batchPreparedStatementExecuteUnits .add (new BatchPreparedStatementExecuteUnit (new RouteUnit ("ds_1" , new SQLUnit ("insert into ..." , parameterSets )), preparedStatement2 ));
110
123
final boolean isExceptionThrown = ExecutorExceptionHandler .isExceptionThrown ();
111
124
final Map <String , Object > dataMap = ExecutorDataMap .getDataMap ();
@@ -124,6 +137,8 @@ protected Integer executeSQL(final SQLExecuteUnit sqlExecuteUnit) {
124
137
public void assertSQLException () throws SQLException {
125
138
Statement statement = mock (Statement .class );
126
139
when (statement .getConnection ()).thenReturn (mock (Connection .class ));
140
+ when (statement .getConnection ().getMetaData ()).thenReturn (mock (DatabaseMetaData .class ));
141
+ when (statement .getConnection ().getMetaData ().getURL ()).thenReturn (HOST_URL );
127
142
final boolean isExceptionThrown = ExecutorExceptionHandler .isExceptionThrown ();
128
143
final Map <String , Object > dataMap = ExecutorDataMap .getDataMap ();
129
144
SQLExecuteCallback <Integer > executeCallback = new SQLExecuteCallback <Integer >(SQLType .DQL , isExceptionThrown , dataMap ) {
0 commit comments