Skip to content

Commit 3d993ca

Browse files
committed
Add unit tests.
1 parent 656c726 commit 3d993ca

File tree

3 files changed

+232
-0
lines changed

3 files changed

+232
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.logging.jdbc;
17+
18+
import org.apache.ibatis.logging.Log;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.Mock;
23+
import org.mockito.junit.MockitoJUnitRunner;
24+
25+
import java.sql.Connection;
26+
import java.sql.PreparedStatement;
27+
import java.sql.SQLException;
28+
29+
import static org.mockito.ArgumentMatchers.anyString;
30+
import static org.mockito.Mockito.*;
31+
32+
@RunWith(MockitoJUnitRunner.class)
33+
public class ConnectionLoggerTest {
34+
35+
@Mock
36+
Connection connection;
37+
38+
@Mock
39+
PreparedStatement preparedStatement;
40+
41+
@Mock
42+
Log log;
43+
44+
Connection conn;
45+
46+
@Before
47+
public void setUp() throws SQLException {
48+
when(log.isDebugEnabled()).thenReturn(true);
49+
conn = ConnectionLogger.newInstance(connection, log, 1);
50+
}
51+
52+
@Test
53+
public void shouldPrintPrepareStatement() throws SQLException {
54+
conn.prepareStatement("select 1");
55+
verify(log).debug(contains("Preparing: select 1"));
56+
}
57+
58+
@Test
59+
public void shouldPrintPrepareCall() throws SQLException {
60+
conn.prepareCall("{ call test() }");
61+
verify(log).debug(contains("Preparing: { call test() }"));
62+
}
63+
64+
@Test
65+
public void shouldNotPrintCreateStatement() throws SQLException {
66+
conn.createStatement();
67+
conn.close();
68+
verify(log, times(0)).debug(anyString());
69+
}
70+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.logging.jdbc;
17+
18+
import org.apache.ibatis.logging.Log;
19+
import org.apache.ibatis.type.JdbcType;
20+
import org.junit.Assert;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.Mock;
25+
import org.mockito.junit.MockitoJUnitRunner;
26+
27+
import java.sql.*;
28+
29+
import static org.mockito.Mockito.*;
30+
31+
@RunWith(MockitoJUnitRunner.class)
32+
public class PreparedStatementLoggerTest {
33+
34+
@Mock
35+
Log log;
36+
37+
@Mock
38+
PreparedStatement preparedStatement;
39+
40+
@Mock
41+
ResultSet resultSet;
42+
43+
PreparedStatement ps;
44+
@Before
45+
public void setUp() throws SQLException {
46+
when(log.isDebugEnabled()).thenReturn(true);
47+
48+
when(preparedStatement.executeQuery(anyString())).thenReturn(resultSet);
49+
when(preparedStatement.execute(anyString())).thenReturn(true);
50+
ps = PreparedStatementLogger.newInstance(this.preparedStatement, log, 1);
51+
}
52+
53+
@Test
54+
public void shouldPrintParameters() throws SQLException {
55+
ps.setInt(1, 10);
56+
ResultSet rs = ps.executeQuery("select 1 limit ?");
57+
58+
verify(log).debug(contains("Parameters: 10(Integer)"));
59+
Assert.assertNotNull(rs);
60+
Assert.assertNotSame(resultSet, rs);
61+
}
62+
63+
@Test
64+
public void shouldPrintNullParameters() throws SQLException {
65+
ps.setNull(1, JdbcType.VARCHAR.TYPE_CODE);
66+
boolean result = ps.execute("update name = ? from test");
67+
68+
verify(log).debug(contains("Parameters: null"));
69+
Assert.assertTrue(result);
70+
}
71+
72+
@Test
73+
public void shouldNotPrintLog() throws SQLException {
74+
ps.getResultSet();
75+
ps.getParameterMetaData();
76+
77+
verify(log, times(0)).debug(anyString());
78+
}
79+
80+
@Test
81+
public void shouldPrintUpdateCount() throws SQLException {
82+
when(preparedStatement.getUpdateCount()).thenReturn(1);
83+
ps.getUpdateCount();
84+
85+
verify(log).debug(contains("Updates: 1"));
86+
}
87+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.logging.jdbc;
17+
18+
import org.apache.ibatis.logging.Log;
19+
import org.junit.Assert;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.Mock;
24+
import org.mockito.junit.MockitoJUnitRunner;
25+
26+
import java.sql.SQLException;
27+
import java.sql.Statement;
28+
29+
import static org.mockito.ArgumentMatchers.anyString;
30+
import static org.mockito.ArgumentMatchers.contains;
31+
import static org.mockito.Mockito.times;
32+
import static org.mockito.Mockito.verify;
33+
import static org.mockito.Mockito.when;
34+
35+
@RunWith(MockitoJUnitRunner.class)
36+
public class StatementLoggerTest {
37+
38+
@Mock
39+
Statement statement;
40+
41+
@Mock
42+
Log log;
43+
44+
Statement st;
45+
46+
@Before
47+
public void setUp() throws SQLException {
48+
when(log.isDebugEnabled()).thenReturn(true);
49+
when(statement.execute(anyString())).thenReturn(true);
50+
st = StatementLogger.newInstance(statement, log, 1);
51+
}
52+
53+
@Test
54+
public void shouldPrintLog() throws SQLException {
55+
st.executeQuery("select 1");
56+
57+
verify(log).debug(contains("Executing: select 1"));
58+
}
59+
60+
@Test
61+
public void shouldPrintLogForUpdate() throws SQLException {
62+
String sql = "update name = '' from test";
63+
boolean execute = st.execute(sql);
64+
65+
verify(log).debug(contains(sql));
66+
Assert.assertTrue(execute);
67+
}
68+
69+
@Test
70+
public void shouldNotPrintLog() throws SQLException {
71+
st.getResultSet();
72+
st.close();
73+
verify(log, times(0)).debug(anyString());
74+
}
75+
}

0 commit comments

Comments
 (0)