From fe0bd12eec0ef48e11f79aacb0de3439663f16a6 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 29 Oct 2012 09:44:06 +0100 Subject: [PATCH] SONAR-3895 Tests --- .../org/sonar/batch/local/DryRunDatabase.java | 25 +++- .../sonar/batch/local/DryRunDatabaseTest.java | 130 ++++++++++++++++++ .../sonar/batch/local/DryRunExporterTest.java | 47 +++++++ .../persistence/DryRunDatabaseFactory.java | 8 +- 4 files changed, 202 insertions(+), 8 deletions(-) create mode 100644 sonar-batch/src/test/java/org/sonar/batch/local/DryRunDatabaseTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunDatabase.java b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunDatabase.java index 172d6b1196e..1b3f0d5547c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunDatabase.java +++ b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunDatabase.java @@ -19,15 +19,20 @@ */ package org.sonar.batch.local; +import com.google.common.base.Throwables; +import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.Settings; import org.sonar.api.database.DatabaseProperties; +import org.sonar.api.utils.SonarException; import org.sonar.batch.bootstrap.DryRun; import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.bootstrap.TempDirectories; import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; /** * @since 3.4 @@ -59,15 +64,25 @@ public class DryRunDatabase implements BatchComponent { return; } - File file = tempDirectories.getFile("dry_run", "db.h2.db"); - String h2DatabasePath = file.getAbsolutePath().replaceAll(".h2.db", ""); + File databaseFile = tempDirectories.getFile("dry_run", "db.h2.db"); + downloadDatabase(reactor.getRoot().getKey(), databaseFile); - downloadDatabase(reactor.getRoot().getKey(), file); - replaceSettings(h2DatabasePath); + String databasePath = StringUtils.removeEnd(databaseFile.getAbsolutePath(), ".h2.db"); + replaceSettings(databasePath); } private void downloadDatabase(String projectKey, File toFile) { - server.download(API_SYNCHRO + "?resource=" + projectKey, toFile); + try { + server.download(API_SYNCHRO + "?resource=" + projectKey, toFile); + } catch (SonarException e) { + Throwable rootCause = Throwables.getRootCause(e); + if (rootCause instanceof FileNotFoundException) { + throw new SonarException(String.format("Project [%s] doesn't exist on server", projectKey)); + } else if ((rootCause instanceof IOException) && (StringUtils.contains(rootCause.getMessage(), "401"))) { + throw new SonarException(String.format("You don't have access rights to project [%s]", projectKey)); + } + throw e; + } } private void replaceSettings(String h2DatabasePath) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/local/DryRunDatabaseTest.java b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunDatabaseTest.java new file mode 100644 index 00000000000..319da8d467f --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunDatabaseTest.java @@ -0,0 +1,130 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.local; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.config.Settings; +import org.sonar.api.database.DatabaseProperties; +import org.sonar.api.utils.SonarException; +import org.sonar.batch.bootstrap.DryRun; +import org.sonar.batch.bootstrap.ServerClient; +import org.sonar.batch.bootstrap.TempDirectories; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +public class DryRunDatabaseTest { + DryRunDatabase dryRunDatabase; + + DryRun dryRun = mock(DryRun.class); + Settings settings = new Settings(); + ServerClient server = mock(ServerClient.class); + TempDirectories tempDirectories = mock(TempDirectories.class); + ProjectReactor projectReactor = new ProjectReactor(ProjectDefinition.create().setKey("group:project")); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + dryRunDatabase = new DryRunDatabase(dryRun, settings, server, tempDirectories, projectReactor); + } + + @Test + public void should_disable_if_no_dry_run() { + dryRunDatabase.start(); + + verifyZeroInteractions(tempDirectories, server); + } + + @Test + public void should_download_database() { + File databaseFile = new File("/tmp/dry_run/db.h2.db"); + when(dryRun.isEnabled()).thenReturn(true); + when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(databaseFile); + + dryRunDatabase.start(); + + verify(server).download("/api/synchro?resource=group:project", databaseFile); + } + + @Test + public void should_replace_database_settings() { + when(dryRun.isEnabled()).thenReturn(true); + when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(new File("/tmp/dry_run/db.h2.db")); + + dryRunDatabase.start(); + + assertThat(settings.getString(DatabaseProperties.PROP_DIALECT)).isEqualTo("h2"); + assertThat(settings.getString(DatabaseProperties.PROP_DRIVER)).isEqualTo("org.h2.Driver"); + assertThat(settings.getString(DatabaseProperties.PROP_USER)).isEqualTo("sonar"); + assertThat(settings.getString(DatabaseProperties.PROP_PASSWORD)).isEqualTo("sonar"); + assertThat(settings.getString(DatabaseProperties.PROP_URL)).isEqualTo("jdbc:h2:/tmp/dry_run/db"); + } + + @Test + public void should_fail_on_unknown_project() { + when(dryRun.isEnabled()).thenReturn(true); + when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(new File("/tmp/dry_run/db.h2.db")); + doThrow(new SonarException(new FileNotFoundException())).when(server).download("/api/synchro?resource=group:project", new File("/tmp/dry_run/db.h2.db")); + + thrown.expect(SonarException.class); + thrown.expectMessage("Project [group:project] doesn't exist on server"); + + dryRunDatabase.start(); + } + + @Test + public void should_fail_on_invalid_role() { + when(dryRun.isEnabled()).thenReturn(true); + when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(new File("/tmp/dry_run/db.h2.db")); + doThrow(new SonarException(new IOException("HTTP 401"))).when(server).download("/api/synchro?resource=group:project", new File("/tmp/dry_run/db.h2.db")); + + thrown.expect(SonarException.class); + thrown.expectMessage("You don't have access rights to project [group:project]"); + + dryRunDatabase.start(); + } + + @Test + public void should_fail() { + when(dryRun.isEnabled()).thenReturn(true); + when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(new File("/tmp/dry_run/db.h2.db")); + doThrow(new SonarException("BUG")).when(server).download("/api/synchro?resource=group:project", new File("/tmp/dry_run/db.h2.db")); + + thrown.expect(SonarException.class); + thrown.expectMessage("BUG"); + + dryRunDatabase.start(); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java new file mode 100644 index 00000000000..0e1b25ef47b --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java @@ -0,0 +1,47 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.local; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.batch.SensorContext; +import org.sonar.batch.bootstrap.DryRun; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyZeroInteractions; + +public class DryRunExporterTest { + DryRunExporter dryRunExporter; + + DryRun dryRun = mock(DryRun.class); + SensorContext sensorContext = mock(SensorContext.class); + + @Before + public void setUp() { + dryRunExporter = new DryRunExporter(dryRun); + } + + @Test + public void should_disable_if_no_dry_run() { + dryRunExporter.execute(sensorContext); + + verifyZeroInteractions(sensorContext); + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java b/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java index 156db8ab830..b226869cdf7 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java @@ -50,8 +50,10 @@ public class DryRunDatabaseFactory implements ServerComponent { String name = serverFileSystem.getTempDir().getAbsolutePath() + "db-" + System.nanoTime(); try { + DataSource source = database.getDataSource(); BasicDataSource destination = create(DIALECT, DRIVER, USER, PASSWORD, URL + name); - copy(database.getDataSource(), destination, resourceId); + + copy(source, destination, resourceId); close(destination); return dbFileContent(name); @@ -83,8 +85,8 @@ public class DryRunDatabaseFactory implements ServerComponent { return dataSource; } - private void close(BasicDataSource dest) throws SQLException { - dest.close(); + private void close(BasicDataSource destination) throws SQLException { + destination.close(); } private byte[] dbFileContent(String name) { -- 2.39.5