package dev.db.biz.configuration.property;
import org.junit.Test;
import dev.db.biz.configuration.property.annotations.PropertyConfigurationListener;
import dev.db.biz.configuration.property.logic.MasterFileLoadingLogic;
import dev.db.biz.configuration.property.logic.MultiFileLoadingLogic;
import static org.junit.Assert.*;
public class PropertyConfigurationTest {
private DatabaseConfiguration databaseConfiguration;
private AuthenticationModule authenticationModule;
@Test
public void testMultiFileConfigurationListener() throws Exception {
PropertyConfigurator configurator = new PropertyConfigurator( new MultiFileLoadingLogic( "etc", true ) );
configurator.registerConfigurationListener( DatabaseConfiguration.class, this );
configurator.registerConfigurationListener( AuthenticationModule.class, this );
// database configuration
assertEquals( "javax.sql", databaseConfiguration.getDriverName() );
assertEquals( "mysql://localhost/database", databaseConfiguration.getDatabaseURL() );
assertEquals( new Integer( 2 ), new Integer( databaseConfiguration.getConnectionCount() ) );
assertEquals( new Integer( 1 ), databaseConfiguration.getInitConnectionCount() );
assertEquals( new Long( 10000 ), new Long( databaseConfiguration.getConnectionTimeout() ) );
assertEquals( new Long( 60000 ), databaseConfiguration.getCloseConnectionAfter() );
assertEquals( new Float( 10.0 ), new Float( databaseConfiguration.getQueryMaxLatency() ) );
assertEquals( new Float( 20.0 ), databaseConfiguration.getQueryMaxDelay() );
assertEquals( new Double( 30.0 ), databaseConfiguration.getQueryMaxDuration() );
assertEquals( new Double( 50.5 ), new Double( databaseConfiguration.getQueryMaxWait() ) );
assertTrue( databaseConfiguration.isStartOnStartup() );
// authentication module
assertEquals( "application", authenticationModule.getModuleName() );
assertFalse( authenticationModule.isActive() );
assertEquals( "kresimir", authenticationModule.getUser().getUsername() );
assertEquals( "ROLE_ADMIN", authenticationModule.getUser().getRole().getName() );
assertEquals( "Can do anything", authenticationModule.getUser().getRole().getDescription() );
}
@Test
public void testLoadMultiFileConfiguration() throws Exception {
PropertyConfigurator configurator = new PropertyConfigurator( new MultiFileLoadingLogic( "etc", true ) );
DatabaseConfiguration configuration = new DatabaseConfiguration();
configurator.loadPropertyConfigurationObject( configuration );
AuthenticationModule authenticationModule = new AuthenticationModule();
configurator.loadPropertyConfigurationObject( authenticationModule );
assertEquals( "javax.sql", configuration.getDriverName() );
assertEquals( "mysql://localhost/database", configuration.getDatabaseURL() );
assertEquals( new Integer( 2 ), new Integer( configuration.getConnectionCount() ) );
assertEquals( new Integer( 1 ), configuration.getInitConnectionCount() );
assertEquals( new Long( 10000 ), new Long( configuration.getConnectionTimeout() ) );
assertEquals( new Long( 60000 ), configuration.getCloseConnectionAfter() );
assertEquals( new Float( 10.0 ), new Float( configuration.getQueryMaxLatency() ) );
assertEquals( new Float( 20.0 ), configuration.getQueryMaxDelay() );
assertEquals( new Double( 30.0 ), configuration.getQueryMaxDuration() );
assertEquals( new Double( 50.5 ), new Double( configuration.getQueryMaxWait() ) );
assertTrue( configuration.isStartOnStartup() );
// authentication module
assertEquals( "application", authenticationModule.getModuleName() );
assertFalse( authenticationModule.isActive() );
assertEquals( "kresimir", authenticationModule.getUser().getUsername() );
assertEquals( "password", authenticationModule.getUser().getPassword() );
assertEquals( "ROLE_ADMIN", authenticationModule.getUser().getRole().getName() );
assertEquals( "Can do anything", authenticationModule.getUser().getRole().getDescription() );
}
@Test
public void testMasterFileConfigurationListener() throws Exception {
PropertyConfigurator configurator = new PropertyConfigurator( new MasterFileLoadingLogic( "etc/master-configuration.properties", true ) );
configurator.registerConfigurationListener( DatabaseConfiguration.class, this );
configurator.registerConfigurationListener( AuthenticationModule.class, this );
assertEquals( "javax.sql", databaseConfiguration.getDriverName() );
assertEquals( "mysql://localhost/database", databaseConfiguration.getDatabaseURL() );
assertEquals( new Integer( 2 ), new Integer( databaseConfiguration.getConnectionCount() ) );
assertEquals( new Integer( 1 ), databaseConfiguration.getInitConnectionCount() );
assertEquals( new Long( 10000 ), new Long( databaseConfiguration.getConnectionTimeout() ) );
assertEquals( new Long( 60000 ), databaseConfiguration.getCloseConnectionAfter() );
assertEquals( new Float( 10.0 ), new Float( databaseConfiguration.getQueryMaxLatency() ) );
assertEquals( new Float( 20.0 ), databaseConfiguration.getQueryMaxDelay() );
assertEquals( new Double( 30.0 ), databaseConfiguration.getQueryMaxDuration() );
assertEquals( new Double( 40.0 ), new Double( databaseConfiguration.getQueryMaxWait() ) );
assertTrue( databaseConfiguration.isStartOnStartup() );
// authentication module
assertEquals( "application", authenticationModule.getModuleName() );
assertFalse( authenticationModule.isActive() );
assertEquals( "kresimir", authenticationModule.getUser().getUsername() );
assertEquals( "password", authenticationModule.getUser().getPassword() );
assertEquals( "ROLE_ADMIN", authenticationModule.getUser().getRole().getName() );
assertEquals( "Can do anything", authenticationModule.getUser().getRole().getDescription() );
}
@Test
public void testLoadMasterFileConfiguration() throws Exception {
PropertyConfigurator configurator = new PropertyConfigurator( new MasterFileLoadingLogic( "etc/master-configuration.properties", true ) );
DatabaseConfiguration configuration = new DatabaseConfiguration();
configurator.loadPropertyConfigurationObject( configuration );
AuthenticationModule authenticationModule = new AuthenticationModule();
configurator.loadPropertyConfigurationObject( authenticationModule );
assertEquals( "javax.sql", configuration.getDriverName() );
assertEquals( "mysql://localhost/database", configuration.getDatabaseURL() );
assertEquals( new Integer( 2 ), new Integer( configuration.getConnectionCount() ) );
assertEquals( new Integer( 1 ), configuration.getInitConnectionCount() );
assertEquals( new Long( 10000 ), new Long( configuration.getConnectionTimeout() ) );
assertEquals( new Long( 60000 ), configuration.getCloseConnectionAfter() );
assertEquals( new Float( 10.0 ), new Float( configuration.getQueryMaxLatency() ) );
assertEquals( new Float( 20.0 ), configuration.getQueryMaxDelay() );
assertEquals( new Double( 30.0 ), configuration.getQueryMaxDuration() );
assertEquals( new Double( 40.0 ), new Double( configuration.getQueryMaxWait() ) );
assertTrue( configuration.isStartOnStartup() );
// authentication module
assertEquals( "application", authenticationModule.getModuleName() );
assertFalse( authenticationModule.isActive() );
assertEquals( "kresimir", authenticationModule.getUser().getUsername() );
assertEquals( "password", authenticationModule.getUser().getPassword() );
assertEquals( "ROLE_ADMIN", authenticationModule.getUser().getRole().getName() );
assertEquals( "Can do anything", authenticationModule.getUser().getRole().getDescription() );
}
@PropertyConfigurationListener
public void onDatabaseConfigurationChange( DatabaseConfiguration databaseConfiguration ) {
this.databaseConfiguration = databaseConfiguration;
System.out.println( this.databaseConfiguration );
}
@PropertyConfigurationListener
public void onDatabaseConfigurationChange( AuthenticationModule authenticationModule ) {
this.authenticationModule = authenticationModule;
System.out.println( this.authenticationModule );
}
public static void main(String[] args) throws Exception {
PropertyConfigurator configurator = new PropertyConfigurator( new MasterFileLoadingLogic( "etc/master-configuration.properties", true ) );
configurator.registerConfigurationListener( DatabaseConfiguration.class, configurator );
configurator.registerConfigurationListener( AuthenticationModule.class, configurator );
while( true ) {
Thread.sleep(10000);
configurator.refresh();
}
}
}