Skip to content

Commit aabdb71

Browse files
committed
Revert "GRAILS-8976 enable autowiring of Filters during unit test"
This reverts commit c14cc70.
1 parent e76f08a commit aabdb71

File tree

4 files changed

+8
-106
lines changed

4 files changed

+8
-106
lines changed

grails-plugin-filters/src/main/groovy/org/codehaus/groovy/grails/plugins/web/filters/FilterConfig.groovy

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,48 +74,43 @@ class FilterConfig extends ControllersApi {
7474
*/
7575
def propertyMissing(String propertyName) {
7676
// Delegate to the parent definition if it has this property.
77-
if (wiredFiltersDefinition.metaClass.hasProperty(wiredFiltersDefinition, propertyName)) {
77+
if (filtersDefinition.metaClass.hasProperty(filtersDefinition, propertyName)) {
7878
def getterName = GrailsClassUtils.getGetterName(propertyName)
79-
metaClass."$getterName" = {-> delegate.wiredFiltersDefinition.getProperty(propertyName) }
80-
return wiredFiltersDefinition."$propertyName"
79+
metaClass."$getterName" = {-> delegate.filtersDefinition.getProperty(propertyName) }
80+
return filtersDefinition."$propertyName"
8181
}
8282

8383
throw new MissingPropertyException(propertyName, filtersDefinition.getClass())
8484
}
8585

86-
def getWiredFiltersDefinition() {
87-
final grailsFilter = grailsApplication.getArtefact(FiltersConfigArtefactHandler.TYPE, filtersDefinition.class.name)
88-
applicationContext."${grailsFilter.fullName}"
89-
}
90-
9186
/**
9287
* When the filter does not have a particular method, it passes
9388
* the call on to the filter definition class.
9489
*/
9590
def methodMissing(String methodName, args) {
9691
// Delegate to the parent definition if it has this method.
97-
List<MetaMethod> respondsTo = wiredFiltersDefinition.metaClass.respondsTo(wiredFiltersDefinition, methodName, args)
92+
List<MetaMethod> respondsTo = filtersDefinition.metaClass.respondsTo(filtersDefinition, methodName, args)
9893
if (respondsTo) {
9994
// Use DelegateMetaMethod to proxy calls to actual MetaMethod for subsequent calls to this method
10095
DelegateMetaMethod dmm=new DelegateMetaMethod(respondsTo[0], FilterConfigDelegateMetaMethodTargetStrategy.instance)
10196
// register the metamethod to EMC
10297
metaClass.registerInstanceMethod(dmm)
10398

10499
// for this invocation we still have to make the call
105-
return respondsTo[0].invoke(wiredFiltersDefinition, args)
100+
return respondsTo[0].invoke(filtersDefinition, args)
106101
}
107102

108103
// Ideally, we would throw a MissingMethodException here
109104
// whether the filter config is intialised or not. However,
110105
// if it's in the initialisation phase, the MME gets swallowed somewhere.
111106
if (!initialised) {
112107
throw new IllegalStateException(
113-
"Invalid filter definition in ${wiredFiltersDefinition.getClass().name} - trying "
108+
"Invalid filter definition in ${filtersDefinition.getClass().name} - trying "
114109
+ "to call method '${methodName}' outside of an interceptor.")
115110
}
116111

117112
// The required method was not found on the parent filter definition either.
118-
throw new MissingMethodException(methodName, wiredFiltersDefinition.getClass(), args)
113+
throw new MissingMethodException(methodName, filtersDefinition.getClass(), args)
119114
}
120115

121116
String toString() {"FilterConfig[$name, scope=$scope]"}

grails-plugin-testing/src/main/groovy/grails/test/mixin/web/FiltersUnitTestMixin.groovy

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ class FiltersUnitTestMixin extends ControllerUnitTestMixin {
8686
arguments = [FiltersConfigArtefactHandler.TYPE, grailsFilter.fullName]
8787

8888
}
89-
"$grailsFilter.fullName"(grailsFilter.clazz) { bean ->
90-
bean.scope = 'prototype'
91-
bean.autowire = true
92-
}
89+
"$grailsFilter.fullName"(grailsFilter.clazz)
9390
}
9491

9592

grails-plugin-testing/src/test/groovy/grails/test/mixin/FiltersUnitTestMixinTests.groovy

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package grails.test.mixin
22

3-
import grails.test.GrailsMock
43
import grails.test.mixin.web.FiltersUnitTestMixin
54
import org.junit.Before
65
import org.junit.Test
7-
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
86

97
@TestMixin(FiltersUnitTestMixin)
108
class FiltersUnitTestMixinTests {
119

1210
AuthorController controller
13-
GrailsMock autowiredServiceMock
1411

1512
@Before
1613
void setUp() {
@@ -75,46 +72,6 @@ class FiltersUnitTestMixinTests {
7572
assert request.filterView == null
7673
assert request.exception != null
7774
}
78-
79-
@Test
80-
void testFilterIsAutoWired() {
81-
defineBeans {
82-
autowiredService(MethodInvokingFactoryBean) {
83-
targetObject = this
84-
targetMethod = 'mockAutowiredService'
85-
}
86-
}
87-
mockFilters(AutowiredFilters)
88-
89-
withFilters(action:"list") {
90-
controller.list()
91-
}
92-
93-
autowiredServiceMock.verify()
94-
}
95-
96-
@Test
97-
void testFilterIsAutoWiredWithBeansDefinedAfterMocking() {
98-
mockFilters(AutowiredFilters)
99-
defineBeans {
100-
autowiredService(MethodInvokingFactoryBean) {
101-
targetObject = this
102-
targetMethod = 'mockAutowiredService'
103-
}
104-
}
105-
106-
withFilters(action:"list") {
107-
controller.list()
108-
}
109-
110-
autowiredServiceMock.verify()
111-
}
112-
113-
AutowiredService mockAutowiredService() {
114-
this.autowiredServiceMock = mockFor(AutowiredService)
115-
this.autowiredServiceMock.demand.setupSession(1) {}
116-
return this.autowiredServiceMock.createMock()
117-
}
11875
}
11976

12077
class AuthorController {
@@ -167,22 +124,4 @@ class ExceptionThrowingFilters {
167124
}
168125
}
169126
}
170-
}
171-
172-
class AutowiredFilters {
173-
174-
def autowiredService
175-
176-
def filters = {
177-
all(controller:"author", action:"list") {
178-
before = {
179-
autowiredService.setupSession()
180-
}
181-
}
182-
}
183-
}
184-
185-
class AutowiredService {
186-
187-
void setupSession() {}
188127
}

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/filters/FiltersUnitTestSpec.groovy

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.codehaus.groovy.grails.web.filters
22

3-
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
4-
53
import javax.servlet.http.HttpServletResponse
64
import grails.artefact.Artefact
75
import grails.test.mixin.TestFor
@@ -12,17 +10,7 @@ import spock.lang.Specification
1210
@TestFor(UserController)
1311
@Mock(AuthenticationFilters)
1412
class FiltersUnitTestSpec extends Specification{
15-
SecurityService securityServiceMock
16-
1713
void "test filters are applied for a unit test"() {
18-
given:"A mock for the injected security service"
19-
securityServiceMock = Mock(SecurityService)
20-
defineBeans {
21-
securityService(MethodInvokingFactoryBean) {
22-
targetObject = this
23-
targetMethod = 'getSecurityServiceMock'
24-
}
25-
}
2614
when:"A filter is used around a controller"
2715
params.username = ''
2816
withFilters(action: "create") {
@@ -31,7 +19,6 @@ class FiltersUnitTestSpec extends Specification{
3119
}
3220
then:"Check that the filter logic is applied"
3321
400 == response.status
34-
1 * securityServiceMock.isAuthorized() >> true
3522
}
3623

3724
void "test filters relay exceptions"() {
@@ -99,17 +86,9 @@ class UserController {
9986

10087
@Artefact("Filters")
10188
class AuthenticationFilters {
102-
103-
def securityService
104-
10589
def filters = {
10690
create(controller: 'user', action: 'create') {
10791
before = {
108-
if (!securityService.isAuthorized()) {
109-
render(status: HttpServletResponse.SC_UNAUTHORIZED)
110-
return false
111-
}
112-
11392
if (params.username == '') {
11493
render(status: HttpServletResponse.SC_BAD_REQUEST)
11594
return false
@@ -131,12 +110,4 @@ class AuthenticationFilters {
131110
}
132111
}
133112
}
134-
}
135-
136-
@Artefact("Service")
137-
class SecurityService {
138-
139-
boolean isAuthorized() {
140-
return true
141-
}
142113
}

0 commit comments

Comments
 (0)