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.

DbPrimaryKeyConstraintFinderTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.server.platform.db.migration.sql;
  21. import java.sql.SQLException;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.db.CoreDbTester;
  25. import org.sonar.db.Database;
  26. import org.sonar.db.dialect.Dialect;
  27. import org.sonar.db.dialect.H2;
  28. import org.sonar.db.dialect.MsSql;
  29. import org.sonar.db.dialect.Oracle;
  30. import org.sonar.db.dialect.PostgreSql;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.when;
  35. public class DbPrimaryKeyConstraintFinderTest {
  36. @Rule
  37. public CoreDbTester db = CoreDbTester.createForSchema(DbPrimaryKeyConstraintFinderTest.class, "schema.sql");
  38. private final Database dbMock = mock(Database.class);
  39. private final DbPrimaryKeyConstraintFinder underTest = new DbPrimaryKeyConstraintFinder(dbMock);
  40. private static final PostgreSql POSTGRESQL = new PostgreSql();
  41. private static final MsSql MS_SQL = new MsSql();
  42. private static final Oracle ORACLE = new Oracle();
  43. private static final org.sonar.db.dialect.H2 H2 = new H2();
  44. @Test
  45. public void findConstraintName_constraint_exists() throws SQLException {
  46. DbPrimaryKeyConstraintFinder underTest = new DbPrimaryKeyConstraintFinder(db.database());
  47. String constraintName = underTest.findConstraintName("TEST_PRIMARY_KEY");
  48. assertThat(constraintName).isEqualTo("PK_TEST_PRIMARY_KEY");
  49. }
  50. @Test
  51. public void findConstraintName_constraint_not_exist() {
  52. DbPrimaryKeyConstraintFinder underTest = new DbPrimaryKeyConstraintFinder(db.database());
  53. assertThatThrownBy(() -> underTest.findConstraintName("NOT_EXISTING_TABLE"))
  54. .hasMessage("Cannot find constraint for table 'NOT_EXISTING_TABLE'")
  55. .isInstanceOf(IllegalStateException.class);
  56. }
  57. @Test
  58. public void getDbVendorSpecificQuery_mssql() {
  59. when(dbMock.getDialect()).thenReturn(MS_SQL);
  60. assertThat(underTest.getDbVendorSpecificQuery("my_table"))
  61. .isEqualTo("SELECT name FROM sys.key_constraints WHERE type = 'PK' AND OBJECT_NAME(parent_object_id) = 'my_table'");
  62. }
  63. @Test
  64. public void getDbVendorSpecificQuery_postgresql() {
  65. when(dbMock.getDialect()).thenReturn(POSTGRESQL);
  66. assertThat(underTest.getDbVendorSpecificQuery("my_table"))
  67. .isEqualTo("SELECT conname FROM pg_constraint WHERE conrelid = (SELECT oid FROM pg_class WHERE relname LIKE 'my_table')");
  68. }
  69. @Test
  70. public void getDbVendorSpecificQuery_oracle() {
  71. when(dbMock.getDialect()).thenReturn(ORACLE);
  72. assertThat(underTest.getDbVendorSpecificQuery("my_table"))
  73. .isEqualTo("SELECT constraint_name FROM user_constraints WHERE table_name = UPPER('my_table') AND constraint_type='P'");
  74. }
  75. @Test
  76. public void getDbVendorSpecificQuery_h2() {
  77. when(dbMock.getDialect()).thenReturn(H2);
  78. assertThat(underTest.getDbVendorSpecificQuery("my_table"))
  79. .isEqualTo("SELECT constraint_name FROM information_schema.constraints WHERE table_name = 'MY_TABLE' and constraint_type = 'PRIMARY KEY'");
  80. }
  81. @Test
  82. public void getDbVendorSpecificQuery_unknown() {
  83. Dialect mockDialect = mock(Dialect.class);
  84. when(mockDialect.getId()).thenReturn("unknown");
  85. when(dbMock.getDialect()).thenReturn(mockDialect);
  86. assertThatThrownBy(() -> underTest.getDbVendorSpecificQuery("my_table"))
  87. .isInstanceOf(IllegalStateException.class)
  88. .hasMessage("Unsupported database 'unknown'");
  89. }
  90. }