kapua icon indicating copy to clipboard operation
kapua copied to clipboard

'static final' use should be avoided in class fields

Open gbarbon opened this issue 3 years ago • 0 comments

Describe the bug All the static final usages should be removed, since they are impacting with property overriding in tests. Factories, services and locators (which are often assigned to static final fields) are the same for every class, thus one will intuitively use the static final. However, since they are all initialised form the classloader, their initialisation might have an impact to other things, like configurations.

As an example, consider the class UserPassCredentialsMatcher. If one tries to override the property defined by the key SystemSettingKey.DB_CONNECTION_ADDITIONAL_OPTIONS (note that this property is not used in the UserPassCredentialsMatcher class), the property is not overridden because the UserPassCredentialsMatcher class (which is first initialised by Shiro) is first initialising all the services,. Thus the override of the property is coming too late, when the Db Connection has already been initialised by the services called in the class.

What is happening with test is that the static are referenced in a sort of chain, make the services being initialised before the test can set the properties that it wants to override.

To Reproduce Define a test that overrides a given property (e.g. the one defined above), then try to run the test and debug it. The property will maintain its original value.

Expected behavior static final should not be used. In such a way services are instantiated later w.r.t. properties, and properties can be correctly overridden.

Screenshots n/a

Version of Kapua 1.5.0-SNAPSHOT

Type of deployment [ ] Local Vagrant deployment [ ] Docker [ ] Openshift (in its variants) [x] Others (all)

Main component affected [ ] Console (in case of console please report info on which browser you encountered the problem) [ ] REST API [ ] Message Broker [x] - Others (all tests)

Additional context First noticed by @riccardomodanese

gbarbon avatar Mar 08 '21 14:03 gbarbon