diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-03-10 23:50:38 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-03-10 23:50:38 +0100 |
commit | d50137ff898bd372c14859b775628685e367a92a (patch) | |
tree | e2f2d818a6ede7997b6f72270500d2ddcaf24759 /sonar-db | |
parent | 26a602cad2459fdabe6c68cdbeba06f6efb07500 (diff) | |
download | sonarqube-d50137ff898bd372c14859b775628685e367a92a.tar.gz sonarqube-d50137ff898bd372c14859b775628685e367a92a.zip |
SONAR-7325 add missing test
Diffstat (limited to 'sonar-db')
-rw-r--r-- | sonar-db/src/test/java/org/sonar/db/profiling/ProfiledDataSourceTest.java | 70 |
1 files changed, 56 insertions, 14 deletions
diff --git a/sonar-db/src/test/java/org/sonar/db/profiling/ProfiledDataSourceTest.java b/sonar-db/src/test/java/org/sonar/db/profiling/ProfiledDataSourceTest.java index a98227877be..cb39cde1653 100644 --- a/sonar-db/src/test/java/org/sonar/db/profiling/ProfiledDataSourceTest.java +++ b/sonar-db/src/test/java/org/sonar/db/profiling/ProfiledDataSourceTest.java @@ -42,15 +42,40 @@ public class ProfiledDataSourceTest { @Rule public LogTester logTester = new LogTester(); + BasicDataSource originDataSource = mock(BasicDataSource.class); + + @Test + public void execute_and_log_statement() throws Exception { + logTester.setLevel(LoggerLevel.TRACE); + + Connection connection = mock(Connection.class); + when(originDataSource.getConnection()).thenReturn(connection); + + String sql = "select from dual"; + Statement stmt = mock(Statement.class); + when(connection.createStatement()).thenReturn(stmt); + when(stmt.execute(sql)).thenReturn(true); + + ProfiledDataSource underTest = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); + + assertThat(underTest.getUrl()).isNull(); + assertThat(underTest.getConnection().getClientInfo()).isNull(); + final Statement statementProxy = underTest.getConnection().createStatement(); + assertThat(statementProxy.getConnection()).isNull(); + assertThat(statementProxy.execute(sql)).isTrue(); + + assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1); + assertThat(logTester.logs(LoggerLevel.TRACE).get(0)) + .contains("sql=select from dual"); + } + @Test - public void execute_and_log_sql_requests() throws Exception { + public void execute_and_log_prepared_statement_with_parameters() throws Exception { logTester.setLevel(LoggerLevel.TRACE); - BasicDataSource originDataSource = mock(BasicDataSource.class); Connection connection = mock(Connection.class); when(originDataSource.getConnection()).thenReturn(connection); - String sql = "select 'polop' from dual"; String sqlWithParams = "insert into polop (col1, col2, col3, col4) values (?, ?, ?, ?, ?)"; int param1 = 42; String param2 = "plouf"; @@ -62,10 +87,6 @@ public class ProfiledDataSourceTest { when(connection.prepareStatement(sqlWithParams)).thenReturn(preparedStatement); when(preparedStatement.execute()).thenReturn(true); - Statement statement = mock(Statement.class); - when(connection.createStatement()).thenReturn(statement); - when(statement.execute(sql)).thenReturn(true); - ProfiledDataSource ds = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); assertThat(ds.getUrl()).isNull(); @@ -78,21 +99,42 @@ public class ProfiledDataSourceTest { preparedStatementProxy.setBlob(5, new ByteArrayInputStream(param5)); assertThat(preparedStatementProxy.getConnection()).isNull(); assertThat(preparedStatementProxy.execute()).isTrue(); - final Statement statementProxy = ds.getConnection().createStatement(); - assertThat(statementProxy.getConnection()).isNull(); - assertThat(statementProxy.execute(sql)).isTrue(); - assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(2); + assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1); assertThat(logTester.logs(LoggerLevel.TRACE).get(0)) .contains("sql=insert into polop (col1, col2, col3, col4) values (?, ?, ?, ?, ?)") .contains("params=42, plouf"); - assertThat(logTester.logs(LoggerLevel.TRACE).get(1)).contains("sql=select 'polop' from dual"); + } + + @Test + public void execute_and_log_prepared_statement_without_parameters() throws Exception { + logTester.setLevel(LoggerLevel.TRACE); + + Connection connection = mock(Connection.class); + when(originDataSource.getConnection()).thenReturn(connection); + + String sqlWithParams = "select from dual"; + PreparedStatement preparedStatement = mock(PreparedStatement.class); + when(connection.prepareStatement(sqlWithParams)).thenReturn(preparedStatement); + when(preparedStatement.execute()).thenReturn(true); + + ProfiledDataSource ds = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); + + assertThat(ds.getUrl()).isNull(); + assertThat(ds.getConnection().getClientInfo()).isNull(); + PreparedStatement preparedStatementProxy = ds.getConnection().prepareStatement(sqlWithParams); + assertThat(preparedStatementProxy.getConnection()).isNull(); + assertThat(preparedStatementProxy.execute()).isTrue(); + + assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1); + assertThat(logTester.logs(LoggerLevel.TRACE).get(0)) + .contains("sql=select from dual") + .doesNotContain("params="); } @Test public void delegate_to_underlying_data_source() throws Exception { - BasicDataSource delegate = mock(BasicDataSource.class); - ProfiledDataSource proxy = new ProfiledDataSource(delegate, ProfiledConnectionInterceptor.INSTANCE); + ProfiledDataSource proxy = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); // painful to call all methods // so using reflection to check that calls does not fail |