java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
branchv_0
changeset 73 d32fd50d3c2c
parent 72 fc9fc1f26b88
child 74 a8444f6a54f3
--- 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<Object[]> 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();
 			}
 		};