From e5bf2151c745a1fb697cb9e1c8f444b47fcb4b93 Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 12 Nov 2009 20:28:45 +0000 Subject: [PATCH] setterconfiguration now by default does not add all the setters. --- .../hibernate/AuthorizationComponent.java | 5 ++++- .../UserAdministrationComponent.java | 5 ++++- .../PersistentAuthorizationServiceTest.java | 11 ++++++++-- .../HibernateUserAdministrationTest.java | 5 ++++- .../UserMgtRepositoryTestContainer.java | 13 ++++++++---- .../system/adapters/DefaultContainer.java | 10 --------- .../system/adapters/SetterConfiguration.java | 21 +++++++++++-------- .../system/adapters/ClassAdapterTest.java | 7 ++++++- .../adapters/ClassConfigurationTest.java | 3 ++- .../system/adapters/DefaultContainerTest.java | 15 +++++++++++-- .../system/adapters/ObjectAdapterTest.java | 3 +++ .../adapters/SetterConfigurationTest.java | 13 +++++++----- 12 files changed, 74 insertions(+), 37 deletions(-) diff --git a/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java b/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java index b0d77e78..150a8447 100644 --- a/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java +++ b/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java @@ -25,6 +25,7 @@ import org.wamblee.cache.EhCache; import org.wamblee.security.authorization.AuthorizationService; import org.wamblee.security.authorization.hibernate.AuthorizationMappingFiles; import org.wamblee.system.adapters.DefaultContainer; +import org.wamblee.system.adapters.ObjectConfiguration; import org.wamblee.system.components.ORMappingConfig; import org.wamblee.system.core.Component; import org.wamblee.system.core.DefaultProvidedInterface; @@ -49,7 +50,9 @@ public class AuthorizationComponent extends DefaultContainer { throws IOException { super(aName); - addComponent("mappingFiles", new AuthorizationMappingFiles()); + ObjectConfiguration authConfig = new ObjectConfiguration(AuthorizationMappingFiles.class); + authConfig.getSetterConfig().initAllSetters(); + addComponent("mappingFiles", new AuthorizationMappingFiles(), authConfig); Component hibernate = new HibernateComponent("hibernate"); addComponent(hibernate); diff --git a/security/src/main/java/org/wamblee/usermgt/hibernate/UserAdministrationComponent.java b/security/src/main/java/org/wamblee/usermgt/hibernate/UserAdministrationComponent.java index 8904c797..66e3850f 100644 --- a/security/src/main/java/org/wamblee/usermgt/hibernate/UserAdministrationComponent.java +++ b/security/src/main/java/org/wamblee/usermgt/hibernate/UserAdministrationComponent.java @@ -23,6 +23,7 @@ import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.transaction.PlatformTransactionManager; import org.wamblee.cache.EhCache; import org.wamblee.system.adapters.DefaultContainer; +import org.wamblee.system.adapters.ObjectConfiguration; import org.wamblee.system.components.ORMappingConfig; import org.wamblee.system.core.Component; import org.wamblee.system.core.DefaultProvidedInterface; @@ -48,7 +49,9 @@ public class UserAdministrationComponent extends DefaultContainer { throws IOException { super(aName); - addComponent("mappingFiles", new UsermgtHibernateMappingFiles()); + ObjectConfiguration mappingFilesConfig = new ObjectConfiguration(UsermgtHibernateMappingFiles.class); + mappingFilesConfig.getSetterConfig().initAllSetters(); + addComponent("mappingFiles", new UsermgtHibernateMappingFiles(), mappingFilesConfig); Component _hibernate = new HibernateComponent("hibernate"); addComponent(_hibernate); diff --git a/security/src/test/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationServiceTest.java b/security/src/test/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationServiceTest.java index ce415cab..9afece00 100644 --- a/security/src/test/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationServiceTest.java +++ b/security/src/test/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationServiceTest.java @@ -24,6 +24,8 @@ import org.wamblee.general.BeanKernel; import org.wamblee.security.authorization.AuthorizationService; import org.wamblee.security.authorization.AuthorizationServiceTest; import org.wamblee.security.authorization.TestUserAccessor; +import org.wamblee.system.adapters.ClassConfiguration; +import org.wamblee.system.adapters.ClassConfigurationTest; import org.wamblee.system.adapters.DefaultContainer; import org.wamblee.system.adapters.ObjectConfiguration; import org.wamblee.system.components.DatabaseComponentFactory; @@ -67,12 +69,17 @@ public class PersistentAuthorizationServiceTest extends _container = new DefaultContainer("top"); DatabaseComponentFactory.addDatabaseConfig(_container); _container.addComponent(new DatasourceComponent("datasource")); - _container.addComponent("userAccessor", TestUserAccessor.class); + ClassConfiguration useraccessorConfig = new ClassConfiguration(TestUserAccessor.class); + useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters(); + _container.addComponent("userAccessor", useraccessorConfig); _container.addComponent(new AuthorizationComponent("authorization", true)); + + ClassConfiguration dbtesterConfig = new ClassConfiguration(DatabaseTesterComponent.class); + dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters(); _container - .addComponent("databaseTester", DatabaseTesterComponent.class); + .addComponent("databaseTester", dbtesterConfig); ObjectConfiguration config = new ObjectConfiguration( PersistentAuthorizationServiceTest.class); diff --git a/security/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserAdministrationTest.java b/security/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserAdministrationTest.java index bef27d2c..5d604fc0 100644 --- a/security/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserAdministrationTest.java +++ b/security/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserAdministrationTest.java @@ -23,6 +23,7 @@ import java.sql.SQLException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wamblee.cache.EhCache; +import org.wamblee.system.adapters.ClassConfiguration; import org.wamblee.system.adapters.DefaultContainer; import org.wamblee.system.adapters.ObjectConfiguration; import org.wamblee.system.components.DatabaseComponentFactory; @@ -64,7 +65,9 @@ public class HibernateUserAdministrationTest extends UserAdministrationImplTest _container.addComponent(new DatasourceComponent("datasource")); _container.addComponent(new UserAdministrationComponent("admin", true)); - _container.addComponent("databaseTester", DatabaseTesterComponent.class); + ClassConfiguration dbtesterConfig = new ClassConfiguration(DatabaseTesterComponent.class); + dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters(); + _container.addComponent("databaseTester", dbtesterConfig); ObjectConfiguration config = new ObjectConfiguration( HibernateUserAdministrationTest.class); diff --git a/security/src/test/java/org/wamblee/usermgt/hibernate/UserMgtRepositoryTestContainer.java b/security/src/test/java/org/wamblee/usermgt/hibernate/UserMgtRepositoryTestContainer.java index 22ae86cb..dd443916 100644 --- a/security/src/test/java/org/wamblee/usermgt/hibernate/UserMgtRepositoryTestContainer.java +++ b/security/src/test/java/org/wamblee/usermgt/hibernate/UserMgtRepositoryTestContainer.java @@ -17,10 +17,10 @@ package org.wamblee.usermgt.hibernate; import java.io.IOException; -import org.wamblee.io.ClassPathResource; +import org.wamblee.system.adapters.ClassConfiguration; import org.wamblee.system.adapters.DefaultContainer; +import org.wamblee.system.adapters.ObjectConfiguration; import org.wamblee.system.components.DatabaseComponentFactory; -import org.wamblee.system.components.PropertyComponent; import org.wamblee.system.spring.component.DatabaseTesterComponent; import org.wamblee.system.spring.component.DatasourceComponent; import org.wamblee.system.spring.component.HibernateComponent; @@ -38,10 +38,15 @@ public class UserMgtRepositoryTestContainer extends DefaultContainer { super(aName); DatabaseComponentFactory.addDatabaseConfig(this); addComponent(new DatasourceComponent("datasource")); - addComponent("mappingFiles", new UsermgtHibernateMappingFiles()); + + ObjectConfiguration mappingFilesConfig = new ObjectConfiguration(UsermgtHibernateMappingFiles.class); + mappingFilesConfig.getSetterConfig().initAllSetters(); + addComponent("mappingFiles", new UsermgtHibernateMappingFiles(), mappingFilesConfig); addComponent(new HibernateComponent("hibernate")); addComponent(new UserGroupRepositoryComponent("usersgroups")); - addComponent("databaseTester", DatabaseTesterComponent.class); + ClassConfiguration dbtesterConfig = new ClassConfiguration(DatabaseTesterComponent.class); + dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters(); + addComponent("databaseTester", dbtesterConfig); } diff --git a/system/general/src/main/java/org/wamblee/system/adapters/DefaultContainer.java b/system/general/src/main/java/org/wamblee/system/adapters/DefaultContainer.java index 7f630105..823b7c0d 100644 --- a/system/general/src/main/java/org/wamblee/system/adapters/DefaultContainer.java +++ b/system/general/src/main/java/org/wamblee/system/adapters/DefaultContainer.java @@ -31,21 +31,11 @@ public class DefaultContainer extends Container { super.addComponent(aComponent); return this; } - - public DefaultContainer addComponent(String aName, Class aClass) { - return addComponent(new ClassAdapter(aName, new ClassConfiguration( - aClass))); - } public DefaultContainer addComponent(String aName, ClassConfiguration aConfiguration) { return addComponent(new ClassAdapter(aName, aConfiguration)); } - public DefaultContainer addComponent(String aName, Object aObject) { - return addComponent(new ObjectAdapter(aName, aObject, new ObjectConfiguration( - aObject.getClass()))); - } - public DefaultContainer addComponent(String aName, Object aObject, ObjectConfiguration aConfiguration) { if ( !aConfiguration.appliesTo(aObject) ) { throw new IllegalArgumentException("Configuration '" + aConfiguration + "' does nto applu to '" + diff --git a/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java b/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java index b2ab5f6d..649e008f 100644 --- a/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java +++ b/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java @@ -52,7 +52,7 @@ public class SetterConfiguration { private Map _setters; /** - * Constructs the setter configuration. By default all setters are added. + * Constructs the setter configuration. By default no setters are added. * * @param aClass * Class which is being configured. @@ -60,27 +60,30 @@ public class SetterConfiguration { public SetterConfiguration(Class aClass) { _class = aClass; _publicOnly = true; - initSetters(); + _setters = new HashMap(); } - private void initSetters() { - _setters = new HashMap(); + /** + * Makes sure that all available setters are used. + */ + public SetterConfiguration initAllSetters() { + _setters.clear(); for (Method method: getAllSetters(_class, _publicOnly) ) { _setters.put(method, createParameterValues(method)); } + return this; } /** * Called to set whether non-public setters are also used. By default only - * public setters are used. This resets all changes made and selects all - * public setters if non-public is false and all setters if it is true. + * public setters are used. The currently selected setters remain chosen. * * @param aIsNonPublic * Non public flag. */ - public void setNonPublic(boolean aIsNonPublic) { + public SetterConfiguration setNonPublic(boolean aIsNonPublic) { _publicOnly = !aIsNonPublic; - initSetters(); + return this; } /** @@ -201,7 +204,7 @@ public class SetterConfiguration { * * @return List of all setters. */ - private static List getAllSetters(Class aClass, + public static List getAllSetters(Class aClass, boolean aPublicOnly) { List result = new ArrayList(); for (Method method : getAllMethods(aClass)) { diff --git a/system/general/src/test/java/org/wamblee/system/adapters/ClassAdapterTest.java b/system/general/src/test/java/org/wamblee/system/adapters/ClassAdapterTest.java index 113fe74e..e08190e9 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/ClassAdapterTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/ClassAdapterTest.java @@ -32,8 +32,10 @@ public class ClassAdapterTest extends AdapterTestCase { public void testSimpleConstructorInjection() { ClassConfiguration x1Config = new ClassConfiguration(X1.class); + x1Config.getObjectConfig().getSetterConfig().initAllSetters(); x1Config.getConstructorConfig().getParameters().setValue(0, "hello"); - ClassConfiguration x4Config = new ClassConfiguration(X4.class); + ClassConfiguration x4Config = new ClassConfiguration(X4.class); + x4Config.getObjectConfig().getSetterConfig().initAllSetters(); ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config); ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config); @@ -59,9 +61,12 @@ public class ClassAdapterTest extends AdapterTestCase { public void testConstructorAndSetterInjection() { ClassConfiguration x1Config = new ClassConfiguration(X1.class); + x1Config.getObjectConfig().getSetterConfig().initAllSetters(); x1Config.getConstructorConfig().getParameters().setValue(0, "hello"); ClassConfiguration x4Config = new ClassConfiguration(X4.class); + x4Config.getObjectConfig().getSetterConfig().initAllSetters(); ClassConfiguration x8Config = new ClassConfiguration(X8.class); + x8Config.getObjectConfig().getSetterConfig().initAllSetters(); ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config); ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config); diff --git a/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java b/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java index c0fc0398..aee32166 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java @@ -31,6 +31,7 @@ public class ClassConfigurationTest extends AdapterTestCase { ConstructorConfiguration config = classConfig.getConstructorConfig() .greedy(); + ProvidedInterface provided = new DefaultProvidedInterface("arg", String.class); List required = classConfig.getRequiredInterfaces(); @@ -52,7 +53,7 @@ public class ClassConfigurationTest extends AdapterTestCase { ClassConfiguration classConfig = new ClassConfiguration(X7.class); classConfig.getConstructorConfig().select(Boolean.class); - classConfig.getObjectConfig().getSetterConfig().values("port").setValue(0, 10); + classConfig.getObjectConfig().getSetterConfig().initAllSetters().values("port").setValue(0, 10); ProvidedInterface providedBoolean = new DefaultProvidedInterface("boolean", Boolean.class); diff --git a/system/general/src/test/java/org/wamblee/system/adapters/DefaultContainerTest.java b/system/general/src/test/java/org/wamblee/system/adapters/DefaultContainerTest.java index 07143e3e..938a1222 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/DefaultContainerTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/DefaultContainerTest.java @@ -23,9 +23,13 @@ public class DefaultContainerTest extends AdapterTestCase { public void testConstructorInjection() { ClassConfiguration x1Config = new ClassConfiguration(X1.class); x1Config.getConstructorConfig().getParameters().setValue(0, "hello"); + x1Config.getObjectConfig().getSetterConfig().initAllSetters(); + + ClassConfiguration x4Config = new ClassConfiguration(X4.class); + x4Config.getObjectConfig().getSetterConfig().initAllSetters(); DefaultContainer container = new DefaultContainer("top").addComponent( - "x1", x1Config).addComponent("x4", X4.class); + "x1", x1Config).addComponent("x4", x4Config); Scope scope = container.start(); AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)" }, @@ -40,12 +44,19 @@ public class DefaultContainerTest extends AdapterTestCase { public void testConstructorInjectionAndSetterInjection() { ClassConfiguration x1Config = new ClassConfiguration(X1.class); x1Config.getConstructorConfig().getParameters().setValue(0, "hello"); + x1Config.getObjectConfig().getSetterConfig().initAllSetters(); X8 x8 = new X8(null); EVENT_TRACKER.clear(); + ClassConfiguration x4Config = new ClassConfiguration(X4.class); + x4Config.getObjectConfig().getSetterConfig().initAllSetters(); + + ObjectConfiguration x8Config = new ObjectConfiguration(X8.class); + x8Config.getSetterConfig().initAllSetters(); + DefaultContainer container = new DefaultContainer("top").addComponent( - "x1", x1Config).addComponent("x4", X4.class).addComponent("x8", x8); + "x1", x1Config).addComponent("x4", x4Config).addComponent("x8", x8, x8Config); Scope scope = container.start(); AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)", diff --git a/system/general/src/test/java/org/wamblee/system/adapters/ObjectAdapterTest.java b/system/general/src/test/java/org/wamblee/system/adapters/ObjectAdapterTest.java index 6d5a3359..0c6825e7 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/ObjectAdapterTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/ObjectAdapterTest.java @@ -32,10 +32,13 @@ public class ObjectAdapterTest extends AdapterTestCase { public void testSetterInjection() { ClassConfiguration x1Config = new ClassConfiguration(X1.class); + x1Config.getObjectConfig().getSetterConfig().initAllSetters(); x1Config.getConstructorConfig().getParameters().setValue(0, "hello"); ClassConfiguration x4Config = new ClassConfiguration(X4.class); + x4Config.getObjectConfig().getSetterConfig().initAllSetters(); ObjectConfiguration x8Config = new ObjectConfiguration(X8.class); + x8Config.getSetterConfig().initAllSetters(); X1 x1 = new X1(); X8 x8 = new X8(x1); diff --git a/system/general/src/test/java/org/wamblee/system/adapters/SetterConfigurationTest.java b/system/general/src/test/java/org/wamblee/system/adapters/SetterConfigurationTest.java index 730dfe6d..e13c9fc0 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/SetterConfigurationTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/SetterConfigurationTest.java @@ -33,6 +33,7 @@ public class SetterConfigurationTest extends AdapterTestCase { public void testOneSetter() { SetterConfiguration config = new SetterConfiguration(X5.class); + config.initAllSetters(); List required = config.getRequiredInterfaces(); assertEquals(1, required.size()); assertEquals("value", required.get(0).getName()); @@ -51,6 +52,7 @@ public class SetterConfigurationTest extends AdapterTestCase { public void testPrivateSetter() { SetterConfiguration config = new SetterConfiguration(X5.class); config.setNonPublic(true); + config.initAllSetters(); List required = new ArrayList( config.getRequiredInterfaces()); Collections.sort(required, new Comparator() { @@ -86,6 +88,7 @@ public class SetterConfigurationTest extends AdapterTestCase { public void testInheritance() { SetterConfiguration config = new SetterConfiguration(X9.class); config.setNonPublic(true); + config.initAllSetters(); List required = new ArrayList( config.getRequiredInterfaces()); Collections.sort(required, new Comparator() { @@ -128,7 +131,7 @@ public class SetterConfigurationTest extends AdapterTestCase { } public void testMultipleSetters() { - SetterConfiguration config = new SetterConfiguration(X6.class); + SetterConfiguration config = new SetterConfiguration(X6.class).initAllSetters(); List required = config.getRequiredInterfaces(); Collections.sort(required, new RequiredInterfaceComparator()); assertEquals(2, required.size()); @@ -155,7 +158,7 @@ public class SetterConfigurationTest extends AdapterTestCase { } public void testInvokeWrongType() { - final SetterConfiguration config = new SetterConfiguration(X5.class); + final SetterConfiguration config = new SetterConfiguration(X5.class).initAllSetters(); List required = config.getRequiredInterfaces(); assertEquals(1, required.size()); assertEquals("value", required.get(0).getName()); @@ -175,7 +178,7 @@ public class SetterConfigurationTest extends AdapterTestCase { } public void testSetExplicitValue() { - SetterConfiguration config = new SetterConfiguration(X5.class); + SetterConfiguration config = new SetterConfiguration(X5.class).initAllSetters(); config.values("value").setValue(0, "bladibla"); List required = config.getRequiredInterfaces(); @@ -294,7 +297,7 @@ public class SetterConfigurationTest extends AdapterTestCase { } public void testRemove() { - SetterConfiguration config = new SetterConfiguration(X6.class); + SetterConfiguration config = new SetterConfiguration(X6.class).initAllSetters(); config.remove("port"); List required = config.getRequiredInterfaces(); Collections.sort(required, new RequiredInterfaceComparator()); @@ -327,7 +330,7 @@ public class SetterConfigurationTest extends AdapterTestCase { } public void testOverridingSetters() { - SetterConfiguration config = new SetterConfiguration(X10.class); + SetterConfiguration config = new SetterConfiguration(X10.class).initAllSetters(); assertEquals(2, config.getRequiredInterfaces().size()); List methods = config.getSetters(); assertEquals(2, methods.size()); -- 2.31.1