import java.io.File;
import java.io.IOException;
import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
/**
* Implementation of configuration holder that retrieves it from the registry.
*/
private String userConfigFilename;
+ /**
+ * Listeners we've registered.
+ */
+ private List listeners = new LinkedList();
+
public String getFilteredUserConfigFilename()
{
- return StringUtils.replace( userConfigFilename, "${user.home}", System.getProperty( "user.home" + "" ) );
+ return StringUtils.replace( userConfigFilename, "${user.home}", System.getProperty( "user.home" ) );
}
public synchronized Configuration getConfiguration()
try
{
( (Initializable) registry ).initialize();
+
+ for ( Iterator i = listeners.iterator(); i.hasNext(); )
+ {
+ RegistryListener l = (RegistryListener) i.next();
+
+ addRegistryChangeListener( l );
+ }
}
catch ( InitializationException e )
{
}
public void addChangeListener( RegistryListener listener )
+ {
+ addRegistryChangeListener( listener );
+
+ // keep track for later
+ listeners.add( listener );
+ }
+
+ private void addRegistryChangeListener( RegistryListener listener )
{
Registry section = registry.getSection( KEY + ".user" );
if ( section != null )
*/
import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.registry.Registry;
+import org.codehaus.plexus.registry.RegistryListener;
import org.codehaus.plexus.util.FileUtils;
+import org.easymock.AbstractMatcher;
+import org.easymock.MockControl;
import java.io.File;
import java.util.List;
public class ArchivaConfigurationTest
extends PlexusTestCase
{
+ private Registry registry;
+
+ protected void setUp()
+ throws Exception
+ {
+ super.setUp();
+
+ registry = (Registry) lookup( Registry.ROLE, "commons-configuration" );
+ }
+
public void testGetConfigurationFromRegistryWithASingleNamedConfigurationResource()
throws Exception
{
file.getParentFile().mkdirs();
FileUtils.fileWrite( file.getAbsolutePath(), "<configuration/>" );
- ArchivaConfiguration archivaConfiguration =
- (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save" );
+ DefaultArchivaConfiguration archivaConfiguration =
+ (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save" );
Configuration configuration = new Configuration();
configuration.setVersion( "1" );
configuration.getWebapp().setUi( new UserInterfaceOptions() );
configuration.getWebapp().getUi().setAppletFindEnabled( false );
+ // add a change listener
+ MockControl control = createRegistryListenerMockControl();
+ RegistryListener listener = (RegistryListener) control.getMock();
+ archivaConfiguration.addChangeListener( listener );
+
+ listener.beforeConfigurationChange( registry, "version", "1" );
+ listener.beforeConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE );
+
+ listener.afterConfigurationChange( registry, "version", "1" );
+ listener.afterConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE );
+
+ control.replay();
+
archivaConfiguration.save( configuration );
+ control.verify();
+
assertTrue( "Check file exists", file.exists() );
// check it
assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
// read it back
- archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-read-saved" );
+ archivaConfiguration =
+ (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-read-saved" );
configuration = archivaConfiguration.getConfiguration();
assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
}
+ private static MockControl createRegistryListenerMockControl()
+ {
+ MockControl control = MockControl.createControl( RegistryListener.class );
+ control.setDefaultMatcher( new AbstractMatcher()
+ {
+ protected boolean argumentMatches( Object object, Object object1 )
+ {
+ return object instanceof Registry || super.argumentMatches( object, object1 );
+ }
+
+ protected String argumentToString( Object object )
+ {
+ return object instanceof Registry ? "<any>" : super.argumentToString( object );
+ }
+ } );
+ return control;
+ }
+
public void testStoreConfigurationUser()
throws Exception
{
configuration.getWebapp().setUi( new UserInterfaceOptions() );
configuration.getWebapp().getUi().setAppletFindEnabled( false );
+ // add a change listener
+ MockControl control = createRegistryListenerMockControl();
+ RegistryListener listener = (RegistryListener) control.getMock();
+ archivaConfiguration.addChangeListener( listener );
+
+ listener.beforeConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE );
+ listener.afterConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE );
+
+ control.replay();
+
archivaConfiguration.save( configuration );
+ control.verify();
+
assertTrue( "Check file exists", userFile.exists() );
assertFalse( "Check file not created", baseFile.exists() );