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;
22 import java.util.Date;
23 import org.junit.Test;
24 import org.mockito.InOrder;
25 import org.sonar.server.platform.Platform;
26 import org.sonar.server.platform.db.migration.engine.MigrationEngine;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.mockito.Mockito.doNothing;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.inOrder;
32 import static org.mockito.Mockito.mock;
35 * Unit test for DatabaseMigrationImpl which does not test any of its concurrency management and asynchronous execution code.
37 public class DatabaseMigrationImplTest {
38 private static final Throwable AN_ERROR = new RuntimeException("runtime exception created on purpose");
41 * Implementation of execute runs Runnable synchronously.
43 private DatabaseMigrationExecutorService executorService = new DatabaseMigrationExecutorServiceAdaptor() {
45 public void execute(Runnable command) {
49 private MutableDatabaseMigrationState migrationState = new DatabaseMigrationStateImpl();
50 private Platform platform = mock(Platform.class);
51 private MigrationEngine migrationEngine = mock(MigrationEngine.class);
52 private InOrder inOrder = inOrder(platform, migrationEngine);
54 private DatabaseMigrationImpl underTest = new DatabaseMigrationImpl(executorService, migrationState, migrationEngine, platform);
57 public void startit_calls_MigrationEngine_execute() {
60 inOrder.verify(migrationEngine).execute();
61 inOrder.verify(platform).doStart();
62 inOrder.verifyNoMoreInteractions();
66 public void status_is_SUCCEEDED_and_failure_is_null_when_trigger_runs_without_an_exception() {
69 assertThat(migrationState.getStatus()).isEqualTo(DatabaseMigrationState.Status.SUCCEEDED);
70 assertThat(migrationState.getError()).isNull();
71 assertThat(migrationState.getStartedAt()).isNotNull();
75 public void status_is_FAILED_and_failure_stores_the_exception_when_trigger_throws_an_exception() {
76 mockMigrationThrowsError();
80 assertThat(migrationState.getStatus()).isEqualTo(DatabaseMigrationState.Status.FAILED);
81 assertThat(migrationState.getError()).isSameAs(AN_ERROR);
82 assertThat(migrationState.getStartedAt()).isNotNull();
86 public void successive_calls_to_startIt_reset_status_startedAt_and_failureError() {
87 mockMigrationThrowsError();
91 assertThat(migrationState.getStatus()).isEqualTo(DatabaseMigrationState.Status.FAILED);
92 assertThat(migrationState.getError()).isSameAs(AN_ERROR);
93 Date firstStartDate = migrationState.getStartedAt();
94 assertThat(firstStartDate).isNotNull();
96 mockMigrationDoesNothing();
100 assertThat(migrationState.getStatus()).isEqualTo(DatabaseMigrationState.Status.SUCCEEDED);
101 assertThat(migrationState.getError()).isNull();
102 assertThat(migrationState.getStartedAt()).isNotSameAs(firstStartDate);
105 private void mockMigrationThrowsError() {
106 doThrow(AN_ERROR).when(migrationEngine).execute();
109 private void mockMigrationDoesNothing() {
110 doNothing().when(migrationEngine).execute();