import org.sonar.api.database.DatabaseProperties;
import org.sonar.api.utils.MessageException;
import org.sonar.core.persistence.DatabaseVersion;
+import org.sonar.core.properties.PropertiesDao;
/**
* Detects if database is not up-to-date with the version required by the batch.
private DatabaseVersion version;
private Settings settings;
+ private PropertiesDao propertiesDao;
private ServerMetadata server;
private AnalysisMode analysisMode;
- public DatabaseCompatibility(DatabaseVersion version, ServerMetadata server, Settings settings, AnalysisMode mode) {
+ public DatabaseCompatibility(DatabaseVersion version, ServerMetadata server, Settings settings, PropertiesDao propertiesDao, AnalysisMode mode) {
this.version = version;
this.server = server;
this.settings = settings;
+ this.propertiesDao = propertiesDao;
this.analysisMode = mode;
}
}
private void checkCorrectServerId() {
- if (!settings.getString(CoreProperties.SERVER_ID).equals(server.getServerId())) {
+ if (!propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID).getValue().equals(server.getServerId())) {
StringBuilder message = new StringBuilder("The current batch process and the configured remote server do not share the same DB configuration.\n");
message.append("\t- Batch side: ");
message.append(settings.getString(DatabaseProperties.PROP_URL));
import org.sonar.api.database.DatabaseProperties;
import org.sonar.api.utils.MessageException;
import org.sonar.core.persistence.DatabaseVersion;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
DatabaseVersion databaseVersion;
ServerMetadata server;
Settings settings;
+ PropertiesDao propertiesDao;
private AnalysisMode mode;
settings = new Settings();
settings.setProperty(DatabaseProperties.PROP_URL, "jdbc:postgresql://localhost/foo");
settings.setProperty(DatabaseProperties.PROP_USER, "bar");
- settings.setProperty(CoreProperties.SERVER_ID, "123456");
+
+ propertiesDao = mock(PropertiesDao.class);
+ when(propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID)).thenReturn(new PropertyDto().setValue("123456"));
mode = mock(AnalysisMode.class);
thrown.expect(MessageException.class);
thrown.expectMessage("Database relates to a more recent version of SonarQube. Please check your settings (JDBC settings, version of Maven plugin)");
- new DatabaseCompatibility(databaseVersion, server, settings, mode).start();
+ new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start();
}
@Test
thrown.expect(MessageException.class);
thrown.expectMessage("Database must be upgraded.");
- new DatabaseCompatibility(databaseVersion, server, settings, mode).start();
+ new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start();
}
@Test
public void shouldFailIfNotSameServerId() throws Exception {
- settings.setProperty(CoreProperties.SERVER_ID, "11111111");
+ when(propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID)).thenReturn(new PropertyDto().setValue("11111111"));
thrown.expect(MessageException.class);
thrown.expectMessage("The current batch process and the configured remote server do not share the same DB configuration.");
thrown.expectMessage("- Batch side: jdbc:postgresql://localhost/foo (bar / *****)");
thrown.expectMessage("- Server side: check the configuration at http://localhost:9000/system");
- new DatabaseCompatibility(databaseVersion, server, settings, mode).start();
+ new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start();
}
@Test
public void shouldUseDefaultUserNameWhenFaillingIfNotSameServerIdAndNoUserNameFound() throws Exception {
- settings.setProperty(CoreProperties.SERVER_ID, "11111111");
+ when(propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID)).thenReturn(new PropertyDto().setValue("11111111"));
settings.removeProperty(DatabaseProperties.PROP_USER);
thrown.expect(MessageException.class);
thrown.expectMessage("- Batch side: jdbc:postgresql://localhost/foo (sonar / *****)");
- new DatabaseCompatibility(databaseVersion, server, settings, mode).start();
+ new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start();
}
@Test
thrown.expect(IllegalStateException.class);
- new DatabaseCompatibility(mock(DatabaseVersion.class), server, settings, mode).start();
+ new DatabaseCompatibility(mock(DatabaseVersion.class), server, settings, propertiesDao, mode).start();
}
@Test
public void shouldDoNothingIfUpToDate() {
when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE);
- new DatabaseCompatibility(databaseVersion, server, settings, mode).start();
+ new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start();
// no error
}
settings.setProperty(CoreProperties.SERVER_ID, "11111111");
when(mode.isPreview()).thenReturn(true);
- new DatabaseCompatibility(databaseVersion, server, settings, mode).start();
+ new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start();
// no failure
}