# HG changeset patch # User František Kučera # Date 1388093526 -3600 # Node ID d32fd50d3c2cc0b5b28451a58ddffdde115d37fd # Parent fc9fc1f26b882d140cb5a38d4fbc699d51207b7f formatted output for: --test-connection diff -r fc9fc1f26b88 -r d32fd50d3c2c java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Thu Dec 26 22:18:24 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Thu Dec 26 22:32:06 2013 +0100 @@ -132,23 +132,44 @@ printTable(formatter, header, data); } - public void testConnection() { - boolean connectionTestResult = false; + public void testConnection() throws FormatterException, ConfigurationException { + ColumnsHeader header = constructHeader( + new HeaderField("database_name", SQLType.VARCHAR), + new HeaderField("configured", SQLType.VARCHAR), + new HeaderField("connected", SQLType.VARCHAR)); + List data = new ArrayList<>(); + + /** TODO: support multiple DB to test */ String dbName = options.getDatabaseNameToTest(); + + data.add(testConnection(dbName)); + + printTable(formatter, header, data); + } + + public Object[] testConnection(String dbName) { log.log(Level.FINE, "Testing connection to database: {0}", dbName); + + boolean succesfullyConnected = false; + boolean succesfullyConfigured = false; + try { DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName); if (dd == null) { - log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName); + log.log(Level.FINE, "No database with this name is configured: {0}", dbName); } else { log.log(Level.FINE, "Database definition was loaded from configuration"); - DatabaseConnection dc = dd.connect(); - connectionTestResult = dc.test(); + succesfullyConfigured = true; + try (DatabaseConnection dc = dd.connect()) { + succesfullyConnected = dc.test(); + } + log.log(Level.FINE, "Database connection test was successful"); } } catch (ConfigurationException | SQLException e) { log.log(Level.SEVERE, "Error during testing connection", e); } - log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure"); + + return new Object[]{dbName, succesfullyConfigured, succesfullyConnected}; } public void printResource(String fileName) { @@ -262,7 +283,7 @@ }, CONNECTION { @Override - public void showInfo(InfoLister infoLister) { + public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException { infoLister.testConnection(); } };