Skip to content

Commit

Permalink
feat: Change default controller scope from prototype to singleton (#1…
Browse files Browse the repository at this point in the history
…3332)

* Singleton has been the defualt scope (via setting) for quite a while, but the fallback defaultValue was prototype. Align unset defaults to actual defaults. Profiles should be updated too to remove this setting.

Fixed tests to have the same intent as before

* test: Add to controller scope testing

---------

Co-authored-by: Eric Helgeson <erichelgeson@gmail.com>
  • Loading branch information
matrei and erichelgeson authored Jan 9, 2024
1 parent 9094b2d commit 639a95c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void initialize() {
public void setGrailsApplication(GrailsApplication grailsApplication) {
this.grailsApplication = grailsApplication;
if (this.scope == null) {
this.scope = grailsApplication.getConfig().getProperty(Settings.CONTROLLERS_DEFAULT_SCOPE, "prototype");
this.scope = grailsApplication.getConfig().getProperty(Settings.CONTROLLERS_DEFAULT_SCOPE, SCOPE_SINGLETON);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,62 @@ class DefaultGrailsControllerClassSpec extends Specification {

static final String SINGLETON = "singleton"
static final String PROTOTYPE = "prototype"
static final String SESSION = "session"

void "test getScope when scope is not specified on the controller, but it specified in config"() {
void "test getScope when scope is not specified on the controller, but it specified in config"(final String configScopeValue) {
given:
def controllerClass = new DefaultGrailsControllerClass(NotSpecifiedController)
def grailsApplication = new DefaultGrailsApplication()
grailsApplication.getConfig().put(Settings.CONTROLLERS_DEFAULT_SCOPE, SINGLETON)
grailsApplication.getConfig().put(Settings.CONTROLLERS_DEFAULT_SCOPE, configScopeValue)
controllerClass.setGrailsApplication(grailsApplication)

expect: "the configuration value is used"
controllerClass.getScope() == SINGLETON
controllerClass.isSingleton()
controllerClass.getScope() == configScopeValue
(SINGLETON == configScopeValue) == controllerClass.isSingleton()
(SINGLETON != configScopeValue) != controllerClass.isSingleton()

where:
configScopeValue << [SINGLETON, PROTOTYPE, SESSION]
}

void "test getScope when scope is not specified on the controller, and not specified in config"() {
given:
def controllerClass = new DefaultGrailsControllerClass(NotSpecifiedController)
controllerClass.setGrailsApplication(new DefaultGrailsApplication())

expect: "the default scope is prototype"
controllerClass.getScope() == PROTOTYPE
!controllerClass.isSingleton()
expect: "the default scope is singleton"
controllerClass.getScope() == SINGLETON
controllerClass.isSingleton()
}

void "test getScope when scope is specified on the controller, and not specified in config"() {
given:
def controllerClass = new DefaultGrailsControllerClass(SingletonController)
def controllerClass = new DefaultGrailsControllerClass(PrototypeController)

expect:
controllerClass.getScope() == SINGLETON
controllerClass.isSingleton()
controllerClass.getScope() == PROTOTYPE
!controllerClass.isSingleton()
}

void "test getScope when scope is specified both in the controller and config"() {
void "test getScope when scope is specified both in the controller and config"(final String configScopeValue) {
given:
def controllerClass = new DefaultGrailsControllerClass(SingletonController)
def controllerClass = new DefaultGrailsControllerClass(PrototypeController)
def grailsApplication = new DefaultGrailsApplication()
grailsApplication.getConfig().put(Settings.CONTROLLERS_DEFAULT_SCOPE, PROTOTYPE)
grailsApplication.getConfig().put(Settings.CONTROLLERS_DEFAULT_SCOPE, configScopeValue)
controllerClass.setGrailsApplication(grailsApplication)

expect: "controller's setting to have priority"
controllerClass.getScope() == SINGLETON
controllerClass.isSingleton()
controllerClass.getScope() == PROTOTYPE
!controllerClass.isSingleton()

where:
configScopeValue << [SINGLETON, SESSION]
}

class NotSpecifiedController {
}

class SingletonController {
static scope = "singleton"
class PrototypeController {
static scope = "prototype"
}
}

0 comments on commit 639a95c

Please sign in to comment.