@Test public void driver_file() throws Exception { File home = temp.newFolder(); File driverFile = new File(home, "extensions/jdbc-driver/oracle/ojdbc6.jar"); FileUtils.touch(driverFile); String path = settings.driverPath(home, JdbcSettings.Provider.ORACLE); assertThat(path).isEqualTo(driverFile.getAbsolutePath()); }
@Test public void driver_dir_does_not_exist() throws Exception { File home = temp.newFolder(); try { settings.driverPath(home, JdbcSettings.Provider.ORACLE); fail(); } catch (MessageException e) { assertThat(e).hasMessage("Directory does not exist: extensions/jdbc-driver/oracle"); } }
@Test public void check_h2() throws Exception { File home = temp.newFolder(); File driverFile = new File(home, "lib/jdbc/h2/h2.jar"); FileUtils.touch(driverFile); Props props = new Props(new Properties()); props.set("sonar.jdbc.url", "jdbc:h2:tcp://localhost:9092/sonar"); settings.checkAndComplete(home, props); assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile); }
@Test public void check_oracle() throws Exception { File home = temp.newFolder(); File driverFile = new File(home, "extensions/jdbc-driver/oracle/ojdbc6.jar"); FileUtils.touch(driverFile); Props props = new Props(new Properties()); props.set("sonar.jdbc.url", "jdbc:oracle:thin:@localhost/XE"); settings.checkAndComplete(home, props); assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile); }
@Test public void check_mssql() throws Exception { File home = temp.newFolder(); File driverFile = new File(home, "lib/jdbc/jtds/jtds.jar"); FileUtils.touch(driverFile); Props props = new Props(new Properties()); props.set("sonar.jdbc.url", "jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor"); settings.checkAndComplete(home, props); assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile); }
@Test public void check_mysql_parameters() throws Exception { // minimal -> ok settings.checkUrlParameters( JdbcSettings.Provider.MYSQL, "jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8"); // full -> ok settings.checkUrlParameters( JdbcSettings.Provider.MYSQL, "jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance"); // missing required -> ko try { settings.checkUrlParameters( JdbcSettings.Provider.MYSQL, "jdbc:mysql://localhost:3306/sonar?characterEncoding=utf8"); fail(); } catch (MessageException e) { assertThat(e).hasMessage("JDBC URL must have the property 'useUnicode=true'"); } }
@Test public void driver_provider() throws Exception { assertThat(settings.driverProvider("jdbc:oracle:thin:@localhost/XE")) .isEqualTo(JdbcSettings.Provider.ORACLE); assertThat( settings.driverProvider( "jdbc:mysql://*****:*****@localhost/XE"); fail(); } catch (MessageException e) { assertThat(e).hasMessage("Bad format of JDBC URL: oracle:thin:@localhost/XE"); } }
@Test public void no_files_in_driver_dir() throws Exception { File home = temp.newFolder(); FileUtils.forceMkdir(new File(home, "extensions/jdbc-driver/oracle")); try { settings.driverPath(home, JdbcSettings.Provider.ORACLE); fail(); } catch (MessageException e) { assertThat(e) .hasMessage("Directory does not contain JDBC driver: extensions/jdbc-driver/oracle"); } }
@Test public void too_many_files_in_driver_dir() throws Exception { File home = temp.newFolder(); FileUtils.touch(new File(home, "extensions/jdbc-driver/oracle/ojdbc5.jar")); FileUtils.touch(new File(home, "extensions/jdbc-driver/oracle/ojdbc6.jar")); try { settings.driverPath(home, JdbcSettings.Provider.ORACLE); fail(); } catch (MessageException e) { assertThat(e) .hasMessage("Directory must contain only one JAR file: extensions/jdbc-driver/oracle"); } }