3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.db.migration.sql;
22 import java.sql.Connection;
23 import java.sql.SQLException;
24 import javax.sql.DataSource;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.db.CoreDbTester;
28 import org.sonar.db.Database;
29 import org.sonar.db.dialect.H2;
30 import org.sonar.db.dialect.MsSql;
31 import org.sonar.db.dialect.Oracle;
32 import org.sonar.db.dialect.PostgreSql;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.assertThatThrownBy;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
39 public class DbPrimaryKeyConstraintFinderTest {
42 public CoreDbTester db = CoreDbTester.createForSchema(DbPrimaryKeyConstraintFinderTest.class, "schema.sql");
44 private final Database dbMock = mock(Database.class);
45 private final DbPrimaryKeyConstraintFinder underTest = new DbPrimaryKeyConstraintFinder(dbMock);
47 private static final PostgreSql POSTGRESQL = new PostgreSql();
48 private static final MsSql MS_SQL = new MsSql();
49 private static final Oracle ORACLE = new Oracle();
50 private static final org.sonar.db.dialect.H2 H2 = new H2();
53 public void findConstraintName_constraint_exists() throws SQLException {
54 DbPrimaryKeyConstraintFinder underTest = new DbPrimaryKeyConstraintFinder(db.database());
55 String constraintName = underTest.findConstraintName("TEST_PRIMARY_KEY");
56 assertThat(constraintName).isEqualTo("PK_TEST_PRIMARY_KEY");
60 public void findConstraintName_constraint_not_exist() {
61 DbPrimaryKeyConstraintFinder underTest = new DbPrimaryKeyConstraintFinder(db.database());
62 assertThatThrownBy(() -> underTest.findConstraintName("NOT_EXISTING_TABLE"))
63 .hasMessage("Cannot find constraint for table 'NOT_EXISTING_TABLE'")
64 .isInstanceOf(IllegalStateException.class);
68 public void getDbVendorSpecificQuery_mssql() {
69 when(dbMock.getDialect()).thenReturn(MS_SQL);
71 assertThat(underTest.getDbVendorSpecificQuery("my_table"))
72 .isEqualTo("SELECT name FROM sys.key_constraints WHERE type = 'PK' AND OBJECT_NAME(parent_object_id) = 'my_table'");
76 public void getDbVendorSpecificQuery_postgresql() throws SQLException {
77 DataSource dataSource = mock(DataSource.class);
78 Connection connection = mock(Connection.class);
79 when(dataSource.getConnection()).thenReturn(connection);
80 when(connection.getSchema()).thenReturn("SonarQube");
81 when(dbMock.getDialect()).thenReturn(POSTGRESQL);
82 when(dbMock.getDataSource()).thenReturn(dataSource);
84 assertThat(underTest.getDbVendorSpecificQuery("my_table"))
85 .isEqualTo("SELECT conname FROM pg_constraint c JOIN pg_namespace n on c.connamespace = n.oid JOIN pg_class cls on c.conrelid = cls.oid WHERE cls.relname = 'my_table' AND n.nspname = 'SonarQube'");
89 public void getDbVendorSpecificQuery_oracle() {
90 when(dbMock.getDialect()).thenReturn(ORACLE);
92 assertThat(underTest.getDbVendorSpecificQuery("my_table"))
93 .isEqualTo("SELECT constraint_name FROM user_constraints WHERE table_name = UPPER('my_table') AND constraint_type='P'");
97 public void getDbVendorSpecificQuery_h2() {
98 when(dbMock.getDialect()).thenReturn(H2);
100 assertThat(underTest.getDbVendorSpecificQuery("my_table"))
101 .isEqualTo("SELECT constraint_name FROM information_schema.constraints WHERE table_name = 'MY_TABLE' and constraint_type = 'PRIMARY KEY'");