String dirName = configuration.getString(DatabaseProperties.PROP_EMBEDDED_DATA_DIR);
if (dirName == null) {
File sonarHome = new File(configuration.getString(CoreProperties.SONAR_HOME));
- if ( !sonarHome.isDirectory() || !sonarHome.exists()) {
+ if (!sonarHome.isDirectory() || !sonarHome.exists()) {
throw new ServerStartException("Sonar home directory does not exist");
}
return new File(sonarHome, "data");
if (dbHome.exists() && !dbHome.isDirectory()) {
throw new SonarException("Database home " + dbHome.getPath() + " is not a directory");
}
- if ( !dbHome.exists()) {
+ if (!dbHome.exists()) {
dbHome.mkdirs();
}
System.setProperty("derby.system.home", dbHome.getPath());
int port = Integer.parseInt(dbProps.getProperty("derby.drda.portNumber"));
String host = dbProps.getProperty("derby.drda.host");
serverControl = new NetworkServerControl(InetAddress.getByName(host), port, DEFAULT_USER, DEFAULT_PWD);
- Logs.INFO.info("Embedded database: " + serverControl);
+ Logs.INFO.info("Starting embedded database on port " + port);
serverControl.start(dbLog);
ensureServerIsUp();
} catch (Exception e) {
if (serverControl != null) {
try {
serverControl.shutdown();
+ ensureServerIsDown();
+ serverControl = null;
+ Logs.INFO.info("Embedded database stopped");
+
} catch (Exception e) {
throw new SonarException(e);
}
- ensureServerIsDown();
- serverControl = null;
- Logs.INFO.info("Embedded database stopped.");
}
}
private void ensureServerIsUp() {
- for (int retry = 0; retry < 16; retry++) {
+ for (int retry = 0; retry < 100; retry++) {
try {
serverControl.ping();
return;
+
} catch (Exception ex) {
- sleep(250);
+ sleep(300);
}
}
throw new SonarException("Embedded database does not respond to ping requests");
}
- private void sleep(long time) {
- try {
- Thread.sleep(time);
- } catch (InterruptedException e) {
- }
- }
-
private void ensureServerIsDown() {
- for (int retry = 0; retry < 16; retry++) {
+ for (int retry = 0; retry < 100; retry++) {
try {
serverControl.ping();
- sleep(250);
- } catch (Exception ex) {
+ sleep(300);
+
+ } catch (SonarException se) {
+ throw se;
+
+ } catch (Exception e) {
+ // normal case: the database does not respond to ping
return;
}
}
- throw new SonarException("Embedded database is not stopped");
+ throw new SonarException("Fail to stop embedded database");
+ }
+
+
+ private void sleep(long time) {
+ try {
+ Thread.sleep(time);
+ } catch (InterruptedException e) {
+ throw new SonarException("Fail to ping embedded database", e);
+ }
}
public static Properties getDefaultProperties(Configuration configuration) {
*/
package org.sonar.server.database;
-import junit.framework.TestCase;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.derby.jdbc.ClientDriver;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.sql.DriverManager;
import java.util.Properties;
-public class EmbeddedDatabaseTest extends TestCase {
+import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertTrue;
+
+public class EmbeddedDatabaseTest {
private final static String TEST_ROOT_DIR = "./target/";
private final static String TEST_DB_DIR_PREFIX = "testDB";
private Properties defaultProps;
private static String testPort;
- @Override
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
windowsCleanup();
if (testPort == null) {
testPort = Integer.toString(findFreeServerPort());
return port;
}
- public void testStart() throws Exception {
+ @Test
+ public void shouldStart() throws Exception {
database = new EmbeddedDatabase(new File(TEST_ROOT_DIR + TEST_DB_DIR_PREFIX + "Start" + testPort), defaultProps);
database.start();
ClientDriver.class.newInstance();
- Connection conn = null;
+ Connection conn;
try {
conn = DriverManager.getConnection(driverUrl);
conn.close();
database.stop();
}
- public void testStop() throws Exception {
+ @Test
+ public void shouldStop() throws Exception {
database = new EmbeddedDatabase(new File(TEST_ROOT_DIR + TEST_DB_DIR_PREFIX + "Stop" + testPort), defaultProps);
database.start();
ClientDriver.class.newInstance();
- Connection conn = null;
+ Connection conn;
try {
conn = DriverManager.getConnection(driverUrl);
conn.close();
}
}
- @Override
+ @After
public void tearDown() throws IOException {
if (database.getDataDir().exists()) {
String os = System.getProperty("os.name");