3 * Copyright (C) 2009-2024 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.charset;
22 import java.sql.Connection;
23 import java.util.List;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.db.CoreDbTester;
29 import static org.assertj.core.api.Assertions.assertThat;
31 public class SqlExecutorIT {
33 private static final String LOGIN_DB_COLUMN = "login";
34 private static final String NAME_DB_COLUMN = "name";
35 private static final String USERS_DB_TABLE = "users";
36 private static final String IS_ROOT_DB_COLUMN = "is_root";
38 private SqlExecutor underTest = new SqlExecutor();
41 public CoreDbTester dbTester = CoreDbTester.createForSchema(SqlExecutorIT.class, "users_table.sql");
44 public void executeSelect_executes_PreparedStatement() throws Exception {
45 dbTester.executeInsert(USERS_DB_TABLE, LOGIN_DB_COLUMN, "login1", NAME_DB_COLUMN, "name one", IS_ROOT_DB_COLUMN, false);
46 dbTester.executeInsert(USERS_DB_TABLE, LOGIN_DB_COLUMN, "login2", NAME_DB_COLUMN, "name two", IS_ROOT_DB_COLUMN, false);
48 try (Connection connection = dbTester.openConnection()) {
49 List<String[]> users = underTest.select(connection, "select " + LOGIN_DB_COLUMN + ", " + NAME_DB_COLUMN + " from users order by login", new SqlExecutor.StringsConverter(
51 assertThat(users).hasSize(2);
52 assertThat(users.get(0)[0]).isEqualTo("login1");
53 assertThat(users.get(0)[1]).isEqualTo("name one");
54 assertThat(users.get(1)[0]).isEqualTo("login2");
55 assertThat(users.get(1)[1]).isEqualTo("name two");
60 public void executeUpdate_executes_PreparedStatement() throws Exception {
61 dbTester.executeInsert(USERS_DB_TABLE, LOGIN_DB_COLUMN, "the_login", NAME_DB_COLUMN, "the name", IS_ROOT_DB_COLUMN, false);
63 try (Connection connection = dbTester.openConnection()) {
64 underTest.executeDdl(connection, "update users set " + NAME_DB_COLUMN + "='new name' where " + LOGIN_DB_COLUMN + "='the_login'");
66 Map<String, Object> row = dbTester.selectFirst("select " + NAME_DB_COLUMN + " from users where " + LOGIN_DB_COLUMN + "='the_login'");
69 .containsEntry("NAME", "new name");