]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5226 Remove extensions/jdbc-driver/h2
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 17 Apr 2014 15:07:35 +0000 (17:07 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 17 Apr 2014 15:07:35 +0000 (17:07 +0200)
sonar-application/src/main/assembly/conf/wrapper.conf
sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/JdbcDriverHolderTest.java
sonar-server/src/main/java/org/sonar/server/platform/DefaultServerFileSystem.java
sonar-server/src/main/java/org/sonar/server/startup/JdbcDriverDeployer.java
sonar-server/src/test/java/org/sonar/server/platform/DefaultServerFileSystemTest.java
sonar-server/src/test/java/org/sonar/server/startup/JdbcDriverDeployerTest.java

index 9353b41cd831a56fbf7df6bd467a780f3057fdba..d53aa251978430a98c72c44bdfef9d34b768a541 100644 (file)
@@ -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
index 614841858ca32d9f2d49cab702be5788795c85af..437fd62ff55ad17ff9feb8e4a37c69097d1a73e5 100644 (file)
 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);
index aece0a8f37074d308bec52adbe5059c6260a1531..3d3dcad38dbb50b243cf5a4de719e8983918e456 100644 (file)
@@ -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);
index cf7a2d6ac0fb7c308706f5158643ffb914e0b959..9932eda1aad636d8e51f898f972b7ff836c3d6d7 100644 (file)
@@ -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<File> jars = getFiles(dir, "jar");
     if (jars.isEmpty()) {
index 141cc2e1261408dfa7fcb629309e8c9ab1bf39db..bb8ee65f373aea446f63510c8dfd2db1b91d5def 100644 (file)
@@ -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 "";
+  }
+
 }
index dd97bd9b638cf0a54b200f6acfd343d7ae8c08d2..8f22e23a815d47fe2be3fd4b7b095c89cc9dfeb9 100644 (file)
@@ -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<File> 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<File> 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<File> xmls = fs.getExtensions("checkstyle", "xml");
-    assertEquals(1, xmls.size());
+    assertThat(xmls).hasSize(1);
 
     List<File> 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<File> xmls = fs.getExtensions("checkstyle", "xml");
-    assertEquals(0, xmls.size());
+    assertThat(xmls).isEmpty();
 
     List<File> 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();
   }
index 3f85cf0c1a4f731f9fa03c394e86fed8014149e5..f03d4f86f772cfd1f0658be1033073d3140d04a3 100644 (file)
  */
 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("");
   }
 }