From: Julien Lancelot Date: Thu, 17 Apr 2014 15:07:35 +0000 (+0200) Subject: SONAR-5226 Remove extensions/jdbc-driver/h2 X-Git-Tag: 4.4-RC1~1445^2~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e06fbd40a4b52994f4c5a5f9918c09e2d6032c3c;p=sonarqube.git SONAR-5226 Remove extensions/jdbc-driver/h2 --- diff --git a/sonar-application/src/main/assembly/conf/wrapper.conf b/sonar-application/src/main/assembly/conf/wrapper.conf index 9353b41cd83..d53aa251978 100644 --- a/sonar-application/src/main/assembly/conf/wrapper.conf +++ b/sonar-application/src/main/assembly/conf/wrapper.conf @@ -33,11 +33,10 @@ wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp # needed starting from 1 wrapper.java.classpath.1=../../lib/*.jar wrapper.java.classpath.2=../../conf -wrapper.java.classpath.3=../../extensions/jdbc-driver/h2/*.jar -wrapper.java.classpath.4=../../extensions/jdbc-driver/mysql/*.jar -wrapper.java.classpath.5=../../extensions/jdbc-driver/oracle/*.jar -wrapper.java.classpath.6=../../extensions/jdbc-driver/postgresql/*.jar -wrapper.java.classpath.7=../../extensions/jdbc-driver/mssql/*.jar +wrapper.java.classpath.3=../../extensions/jdbc-driver/mysql/*.jar +wrapper.java.classpath.4=../../extensions/jdbc-driver/oracle/*.jar +wrapper.java.classpath.5=../../extensions/jdbc-driver/postgresql/*.jar +wrapper.java.classpath.6=../../extensions/jdbc-driver/mssql/*.jar # Java Library Path (location of Wrapper.DLL or libwrapper.so) wrapper.java.library.path.1=./lib diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java index 614841858ca..437fd62ff55 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java @@ -20,11 +20,14 @@ package org.sonar.batch.bootstrap; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.utils.SonarException; import org.sonar.home.cache.FileCache; +import javax.annotation.CheckForNull; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -58,22 +61,23 @@ public class JdbcDriverHolder { try { LOG.info("Install JDBC driver"); String[] nameAndHash = downloadJdbcDriverIndex(); - String filename = nameAndHash[0]; - String hash = nameAndHash[1]; - - File jdbcDriver = fileCache.get(filename, hash, new FileCache.Downloader() { - public void download(String filename, File toFile) throws IOException { - String url = "/deploy/" + filename; - if (LOG.isDebugEnabled()) { - LOG.debug("Download {} to {}", url, toFile.getAbsolutePath()); - } else { - LOG.info("Download {}", filename); + if (nameAndHash != null) { + String filename = nameAndHash[0]; + String hash = nameAndHash[1]; + + File jdbcDriver = fileCache.get(filename, hash, new FileCache.Downloader() { + public void download(String filename, File toFile) throws IOException { + String url = "/deploy/" + filename; + if (LOG.isDebugEnabled()) { + LOG.debug("Download {} to {}", url, toFile.getAbsolutePath()); + } else { + LOG.info("Download {}", filename); + } + serverClient.download(url, toFile); } - serverClient.download(url, toFile); - } - }); - - classLoader = initClassloader(jdbcDriver); + }); + classLoader = initClassloader(jdbcDriver); + } } catch (SonarException e) { throw e; } catch (Exception e) { @@ -134,11 +138,16 @@ public class JdbcDriverHolder { } } + @CheckForNull private String[] downloadJdbcDriverIndex() { String url = "/deploy/jdbc-driver.txt"; try { LOG.debug("Download index of jdbc-driver"); String indexContent = serverClient.request(url); + // File is empty when H2 is used + if (Strings.isNullOrEmpty(indexContent)) { + return null; + } return indexContent.split("\\|"); } catch (Exception e) { throw new SonarException("Fail to download jdbc-driver index: " + url, e); diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/JdbcDriverHolderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/JdbcDriverHolderTest.java index aece0a8f370..3d3dcad38db 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/JdbcDriverHolderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/JdbcDriverHolderTest.java @@ -19,6 +19,7 @@ */ package org.sonar.batch.bootstrap; +import com.google.common.io.Resources; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -32,9 +33,7 @@ import java.io.File; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class JdbcDriverHolderTest { @@ -59,10 +58,10 @@ public class JdbcDriverHolderTest { } @Test - public void should_extend_classloader_with_jdbc_driver() throws Exception { + public void extend_classloader_with_jdbc_driver() throws Exception { FileCache cache = mock(FileCache.class); - File fakeDriver = new File(getClass().getResource("/org/sonar/batch/bootstrap/JdbcDriverHolderTest/jdbc-driver.jar").toURI()); + File fakeDriver = new File(Resources.getResource(JdbcDriverHolderTest.class, "JdbcDriverHolderTest/jdbc-driver.jar").getFile()); when(cache.get(eq("ojdbc14.jar"), eq("fakemd5"), any(FileCache.Downloader.class))).thenReturn(fakeDriver); /* jdbc-driver.jar has just one file /foo/foo.txt */ @@ -85,7 +84,22 @@ public class JdbcDriverHolderTest { } @Test - public void should_be_disabled_if_preview() { + public void do_nothing_when_jdbc_driver_file_is_empty() throws Exception { + FileCache cache = mock(FileCache.class); + + ServerClient server = mock(ServerClient.class); + when(server.request("/deploy/jdbc-driver.txt")).thenReturn(""); + + JdbcDriverHolder holder = new JdbcDriverHolder(cache, mode, server); + holder.start(); + + verify(server, never()).download(anyString(), any(File.class)); + verifyZeroInteractions(cache); + assertThat(holder.getClassLoader()).isNull(); + } + + @Test + public void be_disabled_if_preview() { FileCache cache = mock(FileCache.class); when(mode.isPreview()).thenReturn(true); ServerClient server = mock(ServerClient.class); diff --git a/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerFileSystem.java b/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerFileSystem.java index cf7a2d6ac0f..9932eda1aad 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerFileSystem.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerFileSystem.java @@ -29,6 +29,9 @@ import org.sonar.api.config.Settings; import org.sonar.api.platform.Server; import org.sonar.api.platform.ServerFileSystem; import org.sonar.core.persistence.Database; +import org.sonar.core.persistence.dialect.H2; + +import javax.annotation.CheckForNull; import java.io.File; import java.io.FileFilter; @@ -132,8 +135,15 @@ public class DefaultServerFileSystem implements ServerFileSystem, Startable { return new File(getHomeDir(), "extensions/trash"); } + /** + * Return null when database is H2, as batch is already containing H2 jar + */ + @CheckForNull public File getJdbcDriver() { String dialect = database.getDialect().getId(); + if (H2.ID.equals(dialect)) { + return null; + } File dir = new File(getHomeDir(), "/extensions/jdbc-driver/" + dialect + "/"); List jars = getFiles(dir, "jar"); if (jars.isEmpty()) { diff --git a/sonar-server/src/main/java/org/sonar/server/startup/JdbcDriverDeployer.java b/sonar-server/src/main/java/org/sonar/server/startup/JdbcDriverDeployer.java index 141cc2e1261..bb8ee65f373 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/JdbcDriverDeployer.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/JdbcDriverDeployer.java @@ -23,6 +23,8 @@ import org.apache.commons.io.FileUtils; import org.sonar.home.cache.FileHashes; import org.sonar.server.platform.DefaultServerFileSystem; +import javax.annotation.Nullable; + import java.io.File; import java.io.IOException; @@ -35,22 +37,35 @@ public class JdbcDriverDeployer { } public void start() { + File deployedDriver = null; File driver = fileSystem.getJdbcDriver(); - File deployedDriver = new File(fileSystem.getDeployDir(), driver.getName()); - if (!deployedDriver.exists() || deployedDriver.length() != driver.length()) { - try { - FileUtils.copyFile(driver, deployedDriver); + // Driver can be null for H2, in that case we write an empty jdbc-driver.txt file + if (driver != null) { + deployedDriver = new File(fileSystem.getDeployDir(), driver.getName()); + if (!deployedDriver.exists() || deployedDriver.length() != driver.length()) { + try { + FileUtils.copyFile(driver, deployedDriver); - } catch (IOException e) { - throw new IllegalStateException("Can not copy the JDBC driver from " + driver + " to " + deployedDriver, e); + } catch (IOException e) { + throw new IllegalStateException("Can not copy the JDBC driver from " + driver + " to " + deployedDriver, e); + } } } + File deployedDriverIndex = fileSystem.getDeployedJdbcDriverIndex(); try { - String hash = new FileHashes().of(deployedDriver); - FileUtils.writeStringToFile(deployedDriverIndex, deployedDriver.getName() + "|" + hash); + FileUtils.writeStringToFile(deployedDriverIndex, driverIndexContent(deployedDriver)); } catch (IOException e) { throw new IllegalStateException("Can not generate index of JDBC driver", e); } } + + private String driverIndexContent(@Nullable File deployedDriver){ + if (deployedDriver != null) { + String hash = new FileHashes().of(deployedDriver); + return deployedDriver.getName() + "|" + hash; + } + return ""; + } + } diff --git a/sonar-server/src/test/java/org/sonar/server/platform/DefaultServerFileSystemTest.java b/sonar-server/src/test/java/org/sonar/server/platform/DefaultServerFileSystemTest.java index dd97bd9b638..8f22e23a815 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/DefaultServerFileSystemTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/DefaultServerFileSystemTest.java @@ -23,14 +23,14 @@ import org.junit.Test; import org.sonar.api.platform.ServerFileSystem; import org.sonar.core.persistence.Database; import org.sonar.core.persistence.dialect.Dialect; +import org.sonar.core.persistence.dialect.H2; import org.sonar.core.persistence.dialect.MySql; import org.sonar.test.TestUtils; import java.io.File; import java.util.List; -import static junit.framework.Assert.assertNotNull; -import static org.junit.Assert.assertEquals; +import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -39,15 +39,22 @@ public class DefaultServerFileSystemTest { private static final String PATH = "/org/sonar/server/platform/DefaultServerFileSystemTest/"; @Test - public void testGetJdbcDriver() { + public void get_jdbc_driver() { Database database = mock(Database.class); when(database.getDialect()).thenReturn(new MySql()); File driver = new DefaultServerFileSystem(database, TestUtils.getResource(PATH + "testGetJdbcDriver"), null).getJdbcDriver(); - assertNotNull(driver); + assertThat(driver).isNotNull(); + } + + @Test + public void get_jdbc_driver_return_null_when_h2() { + Database database = mock(Database.class); + when(database.getDialect()).thenReturn(new H2()); + assertThat(new DefaultServerFileSystem(database, (File) null, null).getJdbcDriver()).isNull(); } @Test(expected = IllegalStateException.class) - public void failIfJdbcDriverNotFound() { + public void fail_if_jdbc_driver_not_found() { Database database = mock(Database.class); Dialect fakeDialect = mock(Dialect.class); @@ -58,40 +65,40 @@ public class DefaultServerFileSystemTest { } @Test - public void shouldFindPlugins() { + public void find_plugins() { List plugins = new DefaultServerFileSystem(null, TestUtils.getResource(PATH + "shouldFindPlugins"), null).getUserPlugins(); - assertEquals(2, plugins.size()); + assertThat(plugins).hasSize(2); } @Test - public void shouldNotFailIfNoPlugins() { + public void not_fail_if_no_plugins() { List plugins = new DefaultServerFileSystem(null, TestUtils.getResource(PATH + "shouldNotFailIfNoPlugins"), null).getUserPlugins(); - assertEquals(0, plugins.size()); + assertThat(plugins).isEmpty(); } @Test - public void shouldFindCheckstyleExtensions() { + public void find_checkstyle_extensions() { ServerFileSystem fs = new DefaultServerFileSystem(null, TestUtils.getResource(PATH + "shouldFindCheckstyleExtensions"), null); List xmls = fs.getExtensions("checkstyle", "xml"); - assertEquals(1, xmls.size()); + assertThat(xmls).hasSize(1); List all = fs.getExtensions("checkstyle"); - assertEquals(3, all.size()); + assertThat(all).hasSize(3); } @Test - public void shouldNotFailIfNoCheckstyleExtensions() { + public void not_fail_if_no_checkstyle_extensions() { ServerFileSystem fs = new DefaultServerFileSystem(null, TestUtils.getResource(PATH + "shouldNotFailIfNoCheckstyleExtensions"), null); List xmls = fs.getExtensions("checkstyle", "xml"); - assertEquals(0, xmls.size()); + assertThat(xmls).isEmpty(); List jars = fs.getExtensions("checkstyle"); - assertEquals(0, jars.size()); + assertThat(jars).isEmpty(); } @Test(expected = IllegalStateException.class) - public void shouldFailIfHomeDirectoryNotExists() { + public void fail_if_home_directory_not_exists() { DefaultServerFileSystem fs = new DefaultServerFileSystem(null, new File("/notexists"), null); fs.start(); } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/JdbcDriverDeployerTest.java b/sonar-server/src/test/java/org/sonar/server/startup/JdbcDriverDeployerTest.java index 3f85cf0c1a4..f03d4f86f77 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/JdbcDriverDeployerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/JdbcDriverDeployerTest.java @@ -19,8 +19,11 @@ */ package org.sonar.server.startup; -import org.apache.commons.io.FileUtils; +import com.google.common.base.Charsets; +import com.google.common.io.Files; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.sonar.server.platform.DefaultServerFileSystem; import org.sonar.test.TestUtils; @@ -32,13 +35,16 @@ import static org.mockito.Mockito.when; public class JdbcDriverDeployerTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test public void test_deploy() throws Exception { DefaultServerFileSystem fs = mock(DefaultServerFileSystem.class); File initialDriver = TestUtils.getResource(getClass(), "deploy/my-driver.jar"); when(fs.getJdbcDriver()).thenReturn(initialDriver); - File deployDir = TestUtils.getTestTempDir(getClass(), "deploy", true); + File deployDir = temp.newFolder("deploy"); when(fs.getDeployDir()).thenReturn(deployDir); File deployedIndex = new File(deployDir, "jdbc-driver.txt"); File deployedFile = new File(deployDir, "my-driver.jar"); @@ -46,12 +52,29 @@ public class JdbcDriverDeployerTest { assertThat(deployedFile).doesNotExist(); when(fs.getDeployedJdbcDriverIndex()).thenReturn(deployedIndex); - JdbcDriverDeployer deployer = new JdbcDriverDeployer(fs); - deployer.start(); + new JdbcDriverDeployer(fs).start(); assertThat(deployedIndex).exists(); assertThat(deployedFile).exists(); assertThat(deployedFile).hasSize(initialDriver.length()); - assertThat(FileUtils.readFileToString(deployedIndex)).isEqualTo("my-driver.jar|02b97f7bc37b2b68fc847fcc3fc1c156"); + + assertThat(Files.toString(deployedIndex, Charsets.UTF_8)).isEqualTo("my-driver.jar|02b97f7bc37b2b68fc847fcc3fc1c156"); + } + + @Test + public void create_empty_file_when_no_jdbc_driver() throws Exception { + DefaultServerFileSystem fs = mock(DefaultServerFileSystem.class); + when(fs.getJdbcDriver()).thenReturn(null); + + File deployDir = temp.newFolder("deploy"); + when(fs.getDeployDir()).thenReturn(deployDir); + File deployedIndex = new File(deployDir, "jdbc-driver.txt"); + assertThat(deployedIndex).doesNotExist(); + when(fs.getDeployedJdbcDriverIndex()).thenReturn(deployedIndex); + + new JdbcDriverDeployer(fs).start(); + + assertThat(deployedIndex).exists(); + assertThat(Files.toString(deployedIndex, Charsets.UTF_8)).isEqualTo(""); } }