public void testInvalidMethod() throws Exception { Properties invalidProps = new Properties(); invalidProps.put("datasource.class", "org.postgresql.ds.PGPoolingDataSource"); invalidProps.put("datasource.someRubbish", "blahblahblah"); Database db = new Database(invalidProps); }
/** * Returns a connection to the named database * * @param instance the name of the database * @return a connection to that database * @throws SQLException if there is a problem with the underlying database * @throws ClassNotFoundException if the class that the instance uses cannot be found */ public static Database getDatabase(String instance) throws SQLException, ClassNotFoundException { Database database; // Only one thread to configure or test for a DataSource synchronized (databases) { // If we have this DataSource already configured if (databases.containsKey(instance)) { database = databases.get(instance); } else { Properties props = PropertiesUtil.getPropertiesStartingWith(instance); try { database = new Database(PropertiesUtil.stripStart(instance, props)); } catch (Exception e) { throw new RuntimeException("Failed to initialise " + instance, e); } } } databases.put(instance, database); return database; } }
/** * Returns a connection to the named database * * @param instance the name of the database * @return a connection to that database * @throws SQLException if there is a problem with the underlying database * @throws ClassNotFoundException if the class that the instance uses cannot be found */ public static Database getDatabase(String instance) throws SQLException, ClassNotFoundException { Database database; // Only one thread to configure or test for a DataSource synchronized (databases) { // If we have this DataSource already configured if (databases.containsKey(instance)) { database = databases.get(instance); } else { Properties props = PropertiesUtil.getPropertiesStartingWith(instance); try { database = new Database(PropertiesUtil.stripStart(instance, props)); } catch (Exception e) { throw new RuntimeException("Failed to initialise " + instance, e); } } } databases.put(instance, database); return database; } }
public void testInvalidDataSourceClass() throws Exception { Properties invalidProps = new Properties(); invalidProps.put("datasource.class", "org.class.that.cannot.be.Found"); try { Database db = new Database(invalidProps); fail("Expected: ClassNotFoundException"); } catch (ClassNotFoundException e) { } }
public void testNullProperties() throws Exception { try { Database db = new Database(null); fail("Expected: NullPointerException"); } catch (NullPointerException e) { } }
public void testURL() throws Exception { Database db = new Database(props); assertEquals("jdbc:postgresql://dbserver.mydomain.org/test", db.getURL()); }
public void testUser() throws Exception { Database db = new Database(props); assertEquals("auser", db.getUser()); }
public void testPassword() throws Exception { Database db = new Database(props); assertEquals("secret", db.getPassword()); }
public void testConfigure() throws Exception { Database db = new Database(); db.configure(props); assertTrue(db.getDataSource() != null); assertTrue(db.getDataSource() instanceof PGPoolingDataSource); assertEquals("PostgreSQL", db.getPlatform()); assertEquals("dbserver.mydomain.org", ((PGPoolingDataSource) db.getDataSource()).getServerName()); assertEquals("test", ((PGPoolingDataSource) db.getDataSource()).getDatabaseName()); assertEquals(10, ((PGPoolingDataSource) db.getDataSource()).getMaxConnections()); }
public void testVersionIsAtLeast() throws Exception { Database db = new Database(props); db.version = "9.3"; assertTrue(db.isVersionAtLeast("9.2")); assertTrue(db.isVersionAtLeast("9.1.7")); assertTrue(db.isVersionAtLeast("8")); assertTrue(db.isVersionAtLeast("9.3")); assertTrue(db.isVersionAtLeast("9.3.0")); assertFalse(db.isVersionAtLeast("10")); assertFalse(db.isVersionAtLeast("9.4")); assertFalse(db.isVersionAtLeast("9.3.1")); db.version = "9.2.1"; assertTrue(db.isVersionAtLeast("9.2")); assertTrue(db.isVersionAtLeast("9.2.0")); assertTrue(db.isVersionAtLeast("9.2.1.0")); assertFalse(db.isVersionAtLeast("9.2.1.1")); assertFalse(db.isVersionAtLeast("9.2.2")); db.version = "9.4beta3"; assertTrue(db.isVersionAtLeast("9.2")); assertTrue(db.isVersionAtLeast("9.4.0")); assertFalse(db.isVersionAtLeast("9.5")); }