You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseUtilsTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db;
  21. import com.google.common.base.Function;
  22. import java.sql.Connection;
  23. import java.sql.DatabaseMetaData;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.sql.SQLException;
  27. import java.sql.Statement;
  28. import java.util.ArrayList;
  29. import java.util.Collections;
  30. import java.util.HashSet;
  31. import java.util.List;
  32. import java.util.Objects;
  33. import javax.annotation.Nullable;
  34. import org.junit.Rule;
  35. import org.junit.Test;
  36. import org.junit.rules.ExpectedException;
  37. import org.sonar.api.utils.log.LogTester;
  38. import org.sonar.api.utils.log.LoggerLevel;
  39. import org.sonar.api.utils.log.Loggers;
  40. import org.sonar.core.util.stream.MoreCollectors;
  41. import org.sonar.db.dialect.Oracle;
  42. import static com.google.common.collect.Lists.newArrayList;
  43. import static java.util.Arrays.asList;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.assertj.core.api.Assertions.fail;
  46. import static org.mockito.ArgumentMatchers.any;
  47. import static org.mockito.ArgumentMatchers.eq;
  48. import static org.mockito.Mockito.doReturn;
  49. import static org.mockito.Mockito.doThrow;
  50. import static org.mockito.Mockito.mock;
  51. import static org.mockito.Mockito.spy;
  52. import static org.mockito.Mockito.verify;
  53. import static org.sonar.db.DatabaseUtils.toUniqueAndSortedList;
  54. public class DatabaseUtilsTest {
  55. @Rule
  56. public CoreDbTester dbTester = CoreDbTester.createForSchema(DatabaseUtilsTest.class, "just_one_table.sql");
  57. @Rule
  58. public ExpectedException expectedException = ExpectedException.none();
  59. @Rule
  60. public LogTester logTester = new LogTester();
  61. @Test
  62. public void should_close_connection() throws Exception {
  63. Connection connection = dbTester.openConnection();
  64. assertThat(isClosed(connection)).isFalse();
  65. DatabaseUtils.closeQuietly(connection);
  66. assertThat(isClosed(connection)).isTrue();
  67. }
  68. @Test
  69. public void should_support_null_connection() {
  70. DatabaseUtils.closeQuietly((Connection) null);
  71. // no failure
  72. }
  73. @Test
  74. public void should_close_statement_and_resultset() throws Exception {
  75. Connection connection = dbTester.openConnection();
  76. try {
  77. PreparedStatement statement = connection.prepareStatement(selectDual());
  78. ResultSet rs = statement.executeQuery();
  79. DatabaseUtils.closeQuietly(rs);
  80. DatabaseUtils.closeQuietly(statement);
  81. assertThat(isClosed(statement)).isTrue();
  82. assertThat(isClosed(rs)).isTrue();
  83. } finally {
  84. DatabaseUtils.closeQuietly(connection);
  85. }
  86. }
  87. @Test
  88. public void should_not_fail_on_connection_errors() throws SQLException {
  89. Connection connection = mock(Connection.class);
  90. doThrow(new SQLException()).when(connection).close();
  91. DatabaseUtils.closeQuietly(connection);
  92. // no failure
  93. verify(connection).close(); // just to be sure
  94. }
  95. @Test
  96. public void should_not_fail_on_statement_errors() throws SQLException {
  97. Statement statement = mock(Statement.class);
  98. doThrow(new SQLException()).when(statement).close();
  99. DatabaseUtils.closeQuietly(statement);
  100. // no failure
  101. verify(statement).close(); // just to be sure
  102. }
  103. @Test
  104. public void should_not_fail_on_resulset_errors() throws SQLException {
  105. ResultSet rs = mock(ResultSet.class);
  106. doThrow(new SQLException()).when(rs).close();
  107. DatabaseUtils.closeQuietly(rs);
  108. // no failure
  109. verify(rs).close(); // just to be sure
  110. }
  111. @Test
  112. public void toUniqueAndSortedList_throws_NPE_if_arg_is_null() {
  113. expectedException.expect(NullPointerException.class);
  114. toUniqueAndSortedList(null);
  115. }
  116. @Test
  117. public void toUniqueAndSortedList_throws_NPE_if_arg_contains_a_null() {
  118. expectedException.expect(NullPointerException.class);
  119. toUniqueAndSortedList(asList("A", null, "C"));
  120. }
  121. @Test
  122. public void toUniqueAndSortedList_throws_NPE_if_arg_is_a_set_containing_a_null() {
  123. expectedException.expect(NullPointerException.class);
  124. toUniqueAndSortedList(new HashSet<>(asList("A", null, "C")));
  125. }
  126. @Test
  127. public void toUniqueAndSortedList_enforces_natural_order() {
  128. assertThat(toUniqueAndSortedList(asList("A", "B", "C"))).containsExactly("A", "B", "C");
  129. assertThat(toUniqueAndSortedList(asList("B", "A", "C"))).containsExactly("A", "B", "C");
  130. assertThat(toUniqueAndSortedList(asList("B", "C", "A"))).containsExactly("A", "B", "C");
  131. }
  132. @Test
  133. public void toUniqueAndSortedList_removes_duplicates() {
  134. assertThat(toUniqueAndSortedList(asList("A", "A", "A"))).containsExactly("A");
  135. assertThat(toUniqueAndSortedList(asList("A", "C", "A"))).containsExactly("A", "C");
  136. assertThat(toUniqueAndSortedList(asList("C", "C", "B", "B", "A", "N", "C", "A"))).containsExactly("A", "B", "C", "N");
  137. }
  138. @Test
  139. public void toUniqueAndSortedList_removes_duplicates_and_apply_natural_order_of_any_Comparable() {
  140. assertThat(
  141. toUniqueAndSortedList(asList(myComparable(2), myComparable(5), myComparable(2), myComparable(4), myComparable(-1), myComparable(10))))
  142. .containsExactly(
  143. myComparable(-1), myComparable(2), myComparable(4), myComparable(5), myComparable(10));
  144. }
  145. private static DatabaseUtilsTest.MyComparable myComparable(int ordinal) {
  146. return new DatabaseUtilsTest.MyComparable(ordinal);
  147. }
  148. private static final class MyComparable implements Comparable<MyComparable> {
  149. private final int ordinal;
  150. private MyComparable(int ordinal) {
  151. this.ordinal = ordinal;
  152. }
  153. @Override
  154. public int compareTo(MyComparable o) {
  155. return ordinal - o.ordinal;
  156. }
  157. @Override
  158. public boolean equals(@Nullable Object o) {
  159. if (this == o) {
  160. return true;
  161. }
  162. if (o == null || getClass() != o.getClass()) {
  163. return false;
  164. }
  165. MyComparable that = (MyComparable) o;
  166. return ordinal == that.ordinal;
  167. }
  168. @Override
  169. public int hashCode() {
  170. return Objects.hash(ordinal);
  171. }
  172. }
  173. /**
  174. * Connection.isClosed() has been introduced in java 1.6
  175. */
  176. private boolean isClosed(Connection c) {
  177. try {
  178. c.createStatement().execute(selectDual());
  179. return false;
  180. } catch (Exception e) {
  181. return true;
  182. }
  183. }
  184. /**
  185. * Statement.isClosed() has been introduced in java 1.6
  186. */
  187. private boolean isClosed(Statement s) {
  188. try {
  189. s.execute("SELECT 1");
  190. return false;
  191. } catch (Exception e) {
  192. return true;
  193. }
  194. }
  195. /**
  196. * ResultSet.isClosed() has been introduced in java 1.6
  197. */
  198. private boolean isClosed(ResultSet rs) {
  199. try {
  200. rs.next();
  201. return false;
  202. } catch (Exception e) {
  203. return true;
  204. }
  205. }
  206. private String selectDual() {
  207. String sql = "SELECT 1";
  208. if (Oracle.ID.equals(dbTester.database().getDialect().getId())) {
  209. sql = "SELECT 1 FROM DUAL";
  210. }
  211. return sql;
  212. }
  213. @Test
  214. public void executeLargeInputs() {
  215. List<Integer> inputs = newArrayList();
  216. List<String> expectedOutputs = newArrayList();
  217. for (int i = 0; i < 2010; i++) {
  218. inputs.add(i);
  219. expectedOutputs.add(Integer.toString(i));
  220. }
  221. List<String> outputs = DatabaseUtils.executeLargeInputs(inputs, input -> {
  222. // Check that each partition is only done on 1000 elements max
  223. assertThat(input.size()).isLessThanOrEqualTo(1000);
  224. return input.stream().map(String::valueOf).collect(MoreCollectors.toList());
  225. });
  226. assertThat(outputs).isEqualTo(expectedOutputs);
  227. }
  228. @Test
  229. public void executeLargeInputs_on_empty_list() {
  230. List<String> outputs = DatabaseUtils.executeLargeInputs(Collections.emptyList(), new Function<List<Integer>, List<String>>() {
  231. @Override
  232. public List<String> apply(List<Integer> input) {
  233. fail("No partition should be made on empty list");
  234. return Collections.emptyList();
  235. }
  236. });
  237. assertThat(outputs).isEmpty();
  238. }
  239. @Test
  240. public void executeLargeInputs_uses_specified_partition_size_manipulations() {
  241. List<List<Integer>> partitions = new ArrayList<>();
  242. List<Integer> outputs = DatabaseUtils.executeLargeInputs(
  243. asList(1, 2, 3),
  244. partition -> {
  245. partitions.add(partition);
  246. return partition;
  247. },
  248. i -> i / 500);
  249. assertThat(outputs).containsExactly(1,2,3);
  250. assertThat(partitions).containsExactly(asList(1,2), asList(3));
  251. }
  252. @Test
  253. public void executeLargeUpdates() {
  254. List<Integer> inputs = newArrayList();
  255. for (int i = 0; i < 2010; i++) {
  256. inputs.add(i);
  257. }
  258. List<Integer> processed = newArrayList();
  259. DatabaseUtils.executeLargeUpdates(inputs, input -> {
  260. assertThat(input.size()).isLessThanOrEqualTo(1000);
  261. processed.addAll(input);
  262. });
  263. assertThat(processed).containsExactlyElementsOf(inputs);
  264. }
  265. @Test
  266. public void executeLargeUpdates_on_empty_list() {
  267. DatabaseUtils.executeLargeUpdates(Collections.<Integer>emptyList(), input -> {
  268. fail("No partition should be made on empty list");
  269. });
  270. }
  271. @Test
  272. public void log_all_sql_exceptions() {
  273. SQLException root = new SQLException("this is root", "123");
  274. SQLException next = new SQLException("this is next", "456");
  275. root.setNextException(next);
  276. DatabaseUtils.log(Loggers.get(getClass()), root);
  277. assertThat(logTester.logs(LoggerLevel.ERROR)).contains("SQL error: 456. Message: this is next");
  278. }
  279. @Test
  280. public void tableExists_returns_true_if_table_is_referenced_in_db_metadata() throws Exception {
  281. try (Connection connection = dbTester.openConnection()) {
  282. assertThat(DatabaseUtils.tableExists("SCHEMA_MIGRATIONS", connection)).isTrue();
  283. assertThat(DatabaseUtils.tableExists("schema_migrations", connection)).isTrue();
  284. assertThat(DatabaseUtils.tableExists("schema_MIGRATIONS", connection)).isTrue();
  285. assertThat(DatabaseUtils.tableExists("foo", connection)).isFalse();
  286. }
  287. }
  288. @Test
  289. public void tableExists_is_resilient_on_getSchema() throws Exception {
  290. try (Connection connection = spy(dbTester.openConnection())) {
  291. doThrow(AbstractMethodError.class).when(connection).getSchema();
  292. assertThat(DatabaseUtils.tableExists("SCHEMA_MIGRATIONS", connection)).isTrue();
  293. assertThat(DatabaseUtils.tableExists("schema_migrations", connection)).isTrue();
  294. assertThat(DatabaseUtils.tableExists("schema_MIGRATIONS", connection)).isTrue();
  295. assertThat(DatabaseUtils.tableExists("foo", connection)).isFalse();
  296. }
  297. }
  298. @Test
  299. public void tableExists_is_using_getSchema_when_not_using_h2() throws Exception {
  300. try (Connection connection = spy(dbTester.openConnection())) {
  301. // DatabaseMetaData mock
  302. DatabaseMetaData metaData = mock(DatabaseMetaData.class);
  303. doReturn("xxx").when(metaData).getDriverName();
  304. // ResultSet mock
  305. ResultSet resultSet = mock(ResultSet.class);
  306. doReturn(true, false).when(resultSet).next();
  307. doReturn("SCHEMA_MIGRATIONS").when(resultSet).getString(eq("TABLE_NAME"));
  308. doReturn(resultSet).when(metaData).getTables(any(), eq("yyyy"), any(), any());
  309. // Connection mock
  310. doReturn("yyyy").when(connection).getSchema();
  311. doReturn(metaData).when(connection).getMetaData();
  312. assertThat(DatabaseUtils.tableExists("SCHEMA_MIGRATIONS", connection)).isTrue();
  313. }
  314. }
  315. @Test
  316. public void checkThatNotTooManyConditions_does_not_fail_if_less_than_1000_conditions() {
  317. DatabaseUtils.checkThatNotTooManyConditions(null, "unused");
  318. DatabaseUtils.checkThatNotTooManyConditions(Collections.emptySet(), "unused");
  319. DatabaseUtils.checkThatNotTooManyConditions(Collections.nCopies(10, "foo"), "unused");
  320. DatabaseUtils.checkThatNotTooManyConditions(Collections.nCopies(1_000, "foo"), "unused");
  321. }
  322. @Test
  323. public void checkThatNotTooManyConditions_throws_IAE_if_strictly_more_than_1000_conditions() {
  324. expectedException.expect(IllegalArgumentException.class);
  325. expectedException.expectMessage("the message");
  326. DatabaseUtils.checkThatNotTooManyConditions(Collections.nCopies(1_001, "foo"), "the message");
  327. }
  328. }