/** * @return {@link DataSource} corresponding to this {@link DataSourceFixture}'s attributes. */ public DataSource buildDataSource() { return new DataSourceConverter().fromProperties(buildDataSourceProperties()); }
/** * Test the driver properties to string getter. */ public void testGetDriverPropertiesAsString() { String propertyString = "user=APP;CreateDatabase=create"; String driverPropertyString = DatasourcePropertySet.CONNECTION_PROPERTIES + "=" + propertyString; DataSource ds = dataSourceConverter.fromPropertyString(driverPropertyString); try { assertEquals(propertyString, dataSourceConverter .getConnectionPropertiesAsASemicolonDelimitedString(ds)); } catch (ComparisonFailure e) { assertEquals("CreateDatabase=create;user=APP", dataSourceConverter .getConnectionPropertiesAsASemicolonDelimitedString(ds)); } }
/** * Get a string representation of this datasource. * * @param data DataSource to serialize into a string. * @return a string representation */ public String toPropertyString(DataSource data) { Properties properties = toProperties(data); return PropertyUtils.joinOnPipe(PropertyUtils.toMap(properties)); }
/** * @return String corresponding to this {@link DataSourceFixture}'s attributes. */ public String buildDataSourcePropertyString() { DataSourceConverter converter = new DataSourceConverter(); return converter.toPropertyString(buildDataSource()); }
/** * Parse properties and add any DataSources to pending configuration. DataSources will be found * if their property name starts with: {@link DatasourcePropertySet#DATASOURCE} */ protected void addDataSourcesFromProperties() { getLogger().debug("Searching properties for DataSource definitions", this.getClass().getName()); for (Map.Entry<String, String> property : getProperties().entrySet()) { String propertyName = property.getKey(); if (propertyName.startsWith(DatasourcePropertySet.DATASOURCE)) { String dataSourceProperty = property.getValue(); getLogger().debug( "Found DataSource definition: value [" + dataSourceProperty + "]", this.getClass().getName()); DataSource dataSource = new DataSourceConverter().fromPropertyString(dataSourceProperty); getDataSources().add(dataSource); } } }
connectionPool.setAttribute("Properties", new DataSourceConverter() .getConnectionPropertiesAsASemicolonDelimitedString(ds)); Element dataSource = null; if (ds.getTransactionSupport().equals(TransactionSupport.NO_TRANSACTION))
/** * Test the setting of multiple properties delimited by a semicolon. */ public void testMultipleDriverPropertiesDelimitedBySemiColon() { Properties driverProperties = new Properties(); driverProperties.setProperty("user", "APP"); driverProperties.setProperty("CreateDatabase", "create"); String driverPropertyString = "user=APP;CreateDatabase=create"; String propertyString = DatasourcePropertySet.CONNECTION_PROPERTIES + "=" + driverPropertyString; DataSource ds = dataSourceConverter.fromPropertyString(propertyString); assertEquals(driverProperties, ds.getConnectionProperties()); }
/** * Construct a DataSource from a single String. Note that database driver properties can be * nested as long as they are semicolon delimited Example: * <code>CreateDatabase=create;DatabaseName=TEST</code>. * * @param datasourceInformation A string, really a list of properties, representing a datasource * @return DataSource representing the string * @see org.codehaus.cargo.container.internal.util.PropertyUtils#splitPropertiesOnPipe(String) */ public DataSource fromPropertyString(String datasourceInformation) { return fromProperties(PropertyUtils.splitPropertiesOnPipe(PropertyUtils .escapeBackSlashesIfNotNull(datasourceInformation))); }
/** * Test the {@link Properties} constructor. */ public void testPropertiesConstructor() { Properties props = new Properties(); props.setProperty(DatasourcePropertySet.ID, "rudolf"); props.setProperty(DatasourcePropertySet.TRANSACTION_SUPPORT, TransactionSupport.NO_TRANSACTION.toString()); props.setProperty(DatasourcePropertySet.JNDI_LOCATION, "jdbc/JiraDS"); props .setProperty(DatasourcePropertySet.CONNECTION_TYPE, ConfigurationEntryType.JDBC_DRIVER); props.setProperty(DatasourcePropertySet.DRIVER_CLASS, "org.hsqldb.jdbcDriver"); props.setProperty(DatasourcePropertySet.URL, "postresql:localhost:jirads"); props.setProperty(DatasourcePropertySet.USERNAME, "sa"); props.setProperty(DatasourcePropertySet.PASSWORD, ""); DataSource ds = dataSourceConverter.fromProperties(props); assertEquals(0, ds.getConnectionProperties().size()); assertEquals("", ds.getPassword()); assertEquals(props, dataSourceConverter.toProperties(ds)); }
/** * Creates the test datasource converter. {@inheritDoc} * @throws Exception If anything goes wrong. */ @Override public void setUp() throws Exception { super.setUp(); dataSourceConverter = new DataSourceConverter(); }
/** * Get a properties object containing all of the members of this datasource object. Note that * driver properties will be nested and delimited by a semicolon. * * @param data DataSource to serialize into properties. * @return a properties object corresponding to this datasource */ public Properties toProperties(DataSource data) { Properties properties = new Properties(); PropertyUtils.setPropertyIfNotNull(properties, DatasourcePropertySet.JNDI_LOCATION, data .getJndiLocation()); PropertyUtils.setPropertyIfNotNull(properties, DatasourcePropertySet.CONNECTION_TYPE, data.getConnectionType()); PropertyUtils.setPropertyIfNotNull(properties, DatasourcePropertySet.TRANSACTION_SUPPORT, data.getTransactionSupport()); PropertyUtils.setPropertyIfNotNull(properties, DatasourcePropertySet.DRIVER_CLASS, data .getDriverClass()); PropertyUtils.setPropertyIfNotNull(properties, DatasourcePropertySet.URL, data.getUrl()); PropertyUtils.setPropertyIfNotNull(properties, DatasourcePropertySet.USERNAME, data .getUsername()); PropertyUtils.setPropertyIfNotNull(properties, DatasourcePropertySet.PASSWORD, data .getPassword()); PropertyUtils.setPropertyIfNotNull(properties, DatasourcePropertySet.ID, data.getId()); PropertyUtils.setPropertyIfNotNull(properties, DatasourcePropertySet.CONNECTION_PROPERTIES, getConnectionPropertiesAsASemicolonDelimitedString(data)); return properties; }
/** * In Resin 2.x DataSources are Resources * * @param ds datasource to configure * @return String representing the Resource representing it. */ protected String toResinConfigurationEntry(DataSource ds) { Resource resource = null; if (ConfigurationEntryType.XA_DATASOURCE.equals(ds.getConnectionType())) { resource = converter.convertToResource(ds, ConfigurationEntryType.XA_DATASOURCE, "driver-name"); } else { resource = converter.convertToResource(ds, ConfigurationEntryType.DATASOURCE, "driver-name"); } return toConfigurationEntry(resource); }
/** * Construct a DataSource from a list of properties. * * @param properties A list of properties representing this datasource * @return DataSource representing the properties * @see PropertyUtils#splitPropertiesOnPipe(String) */ public DataSource fromProperties(Properties properties) { String jndiLocation = properties.getProperty(DatasourcePropertySet.JNDI_LOCATION); String connectionType = properties.getProperty(DatasourcePropertySet.CONNECTION_TYPE); TransactionSupport transactionSupport = TransactionSupport.valueOf( properties.getProperty(DatasourcePropertySet.TRANSACTION_SUPPORT)); String driverClass = properties.getProperty(DatasourcePropertySet.DRIVER_CLASS); String url = properties.getProperty(DatasourcePropertySet.URL); String username = properties.getProperty(DatasourcePropertySet.USERNAME); String password = properties.getProperty(DatasourcePropertySet.PASSWORD); String id = properties.getProperty(DatasourcePropertySet.ID); String driverPropertiesAsASemicolonDelimitedString = properties.getProperty(DatasourcePropertySet.CONNECTION_PROPERTIES); Properties connectionProperties = getDriverPropertiesFromString( driverPropertiesAsASemicolonDelimitedString); DataSource data = new DataSource(jndiLocation, connectionType, transactionSupport, driverClass, url, username, password, id, connectionProperties); return data; }
connectionPool.setAttribute("Properties", new DataSourceConverter() .getConnectionPropertiesAsASemicolonDelimitedString(ds)); Element dataSource = null; if (ds.getTransactionSupport().equals(TransactionSupport.NO_TRANSACTION))
/** * Test the empty driver properties getter. */ public void testGetEmptyDriverProperties() { String propertyString = ""; String driverPropertyString = DatasourcePropertySet.CONNECTION_PROPERTIES + "=" + propertyString; DataSource ds = dataSourceConverter.fromPropertyString(driverPropertyString); assertEquals(0, ds.getConnectionProperties().size()); }
/** * Test that the {@link ConfigurationEntryType#JDBC_DRIVER} property is the driver. */ public void testDriverIsDriver() { Properties props = new Properties(); props .setProperty(DatasourcePropertySet.CONNECTION_TYPE, ConfigurationEntryType.JDBC_DRIVER); DataSource ds = dataSourceConverter.fromProperties(props); assertEquals("java.sql.Driver", ds.getConnectionType()); }
/** * In Resin 2.x DataSources are Resources * * @param ds datasource to configure * @return String representing the Resource representing it. */ protected String toResinConfigurationEntry(DataSource ds) { Resource resource = null; if (ConfigurationEntryType.XA_DATASOURCE.equals(ds.getConnectionType())) { resource = converter.convertToResource(ds, ConfigurationEntryType.XA_DATASOURCE, "driver-name"); } else { resource = converter.convertToResource(ds, ConfigurationEntryType.DATASOURCE, "driver-name"); } return toConfigurationEntry(resource); }
/** * Test the driver properties when the username is set on datasource. */ public void testDatabaseDriverPropertiesUsernamePropertySetsUserOnDataSource() { String driverPropertyString = "user=APP;CreateDatabase=create"; String propertyString = DatasourcePropertySet.CONNECTION_PROPERTIES + "=" + driverPropertyString; DataSource ds = dataSourceConverter.fromPropertyString(propertyString); assertEquals("APP", ds.getUsername()); }
/** * Test that the default mode is {@link TransactionSupport#NO_TRANSACTION}. */ public void testDefaultIsNoTransaction() { Properties props = new Properties(); DataSource ds = dataSourceConverter.fromProperties(props); assertEquals(TransactionSupport.NO_TRANSACTION, ds.getTransactionSupport()); }
/** * Test the driver properties when the password is set on datasource. */ public void testDatabaseDriverPropertiesPasswordPropertySetsPasswordOnDataSource() { String driverPropertyString = "password=egg;CreateDatabase=create"; String propertyString = DatasourcePropertySet.CONNECTION_PROPERTIES + "=" + driverPropertyString; DataSource ds = dataSourceConverter.fromPropertyString(propertyString); assertEquals("egg", ds.getPassword()); }