Skip to content

Commit c4cb2af

Browse files
committed
GRAILS-5409 - Added setting of webRequest.controllerName back into integration tests.
1 parent c403d96 commit c4cb2af

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

src/java/org/codehaus/groovy/grails/test/junit3/JUnit3GrailsEnvironmentTestSuite.groovy

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import java.util.regex.Pattern
2828
import org.codehaus.groovy.grails.test.support.GrailsTestAutowirer
2929
import org.codehaus.groovy.grails.test.support.GrailsTestRequestEnvironmentInterceptor
3030
import org.codehaus.groovy.grails.test.support.GrailsTestTransactionInterceptor
31+
import org.codehaus.groovy.grails.test.support.ControllerNameExtractor
3132

3233
/**
3334
* A Grails specific test suite that runs tests in a “Grails” environment. That is,
@@ -71,8 +72,25 @@ class JUnit3GrailsEnvironmentTestSuite extends TestSuite {
7172
}
7273

7374
def rawRunner = { test.run(result) }
74-
def inTransactionRunner = mode.wrapInTransaction && transactionInterceptor.isTransactional(test) ? { transactionInterceptor.doInTransaction(rawRunner) } : rawRunner
75-
def inRequestRunner = mode.wrapInRequestEnvironment ? { requestEnvironmentInterceptor.doInRequestEnvironment(inTransactionRunner) } : rawRunner
75+
76+
def inTransactionRunner
77+
if (mode.wrapInTransaction && transactionInterceptor.isTransactional(test)) {
78+
inTransactionRunner = { transactionInterceptor.doInTransaction(rawRunner) }
79+
} else {
80+
inTransactionRunner = rawRunner
81+
}
82+
83+
def inRequestRunner
84+
if (mode.wrapInRequestEnvironment) {
85+
def controllerName = ControllerNameExtractor.extractControllerNameFromTestClassName(test.class.name, JUnit3GrailsTestType.TESTS_SUFFIX)
86+
if (controllerName) {
87+
inRequestRunner = { requestEnvironmentInterceptor.doInRequestEnvironment(controllerName, inTransactionRunner) }
88+
} else {
89+
inRequestRunner = { requestEnvironmentInterceptor.doInRequestEnvironment(inTransactionRunner) }
90+
}
91+
} else {
92+
inRequestRunner = inTransactionRunner
93+
}
7694

7795
inRequestRunner()
7896
}

src/java/org/codehaus/groovy/grails/test/junit3/JUnit3GrailsTestType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
*/
4444
public class JUnit3GrailsTestType extends GrailsTestTypeSupport {
4545

46+
public static final String TESTS_SUFFIX = "Tests";
47+
4648
protected TestSuite wholeTestSuite;
4749
protected JUnit3GrailsTestTypeMode mode;
4850

@@ -57,7 +59,7 @@ public JUnit3GrailsTestType(String name, String sourceDirectory, JUnit3GrailsTes
5759

5860
protected List<String> getTestSuffixes() {
5961
List<String> testSuffixes = new LinkedList<String>();
60-
testSuffixes.add("Tests");
62+
testSuffixes.add(TESTS_SUFFIX);
6163
return testSuffixes;
6264
}
6365

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.codehaus.groovy.grails.test.support
18+
19+
import grails.util.GrailsNameUtils
20+
21+
class ControllerNameExtractor {
22+
23+
static String extractControllerNameFromTestClassName(String testClassName, String[] testClassSuffixes) {
24+
if (!testClassSuffixes) testClassSuffixes = [''] as String[]
25+
26+
def matchingTail
27+
testClassSuffixes.find {
28+
def tail = "Controller$it"
29+
if (testClassName.endsWith(tail)) matchingTail = tail }
30+
31+
if (matchingTail) {
32+
testClassName[0..(testClassName.size() - matchingTail.size() - 1)]
33+
} else {
34+
null
35+
}
36+
}
37+
38+
}

src/java/org/codehaus/groovy/grails/test/support/GrailsTestRequestEnvironmentInterceptor.groovy

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import grails.util.GrailsWebUtil
3030
*/
3131
class GrailsTestRequestEnvironmentInterceptor {
3232

33+
static final String DEFAULT_CONTROLLER_NAME = 'test'
34+
3335
ApplicationContext applicationContext
3436

3537
GrailsTestRequestEnvironmentInterceptor(ApplicationContext applicationContext) {
@@ -39,10 +41,11 @@ class GrailsTestRequestEnvironmentInterceptor {
3941
/**
4042
* Establishes a mock request environment
4143
*/
42-
void init() {
44+
void init(String controllerName = DEFAULT_CONTROLLER_NAME) {
4345
GrailsWebRequest webRequest = GrailsWebUtil.bindMockWebRequest(applicationContext)
4446
ServletContextHolder.servletContext = webRequest.servletContext
4547
webRequest.servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, applicationContext)
48+
webRequest.controllerName = controllerName
4649
}
4750

4851
/**
@@ -54,15 +57,22 @@ class GrailsTestRequestEnvironmentInterceptor {
5457
}
5558

5659
/**
57-
* Calls init() before and destroy() after invoking {@code body}.
60+
* Passes {@code body} to {@code doInRequestEnvironment(String,Closure)} with the {@code DEFAULT_CONTROLLER_NAME}.
5861
*/
5962
void doInRequestEnvironment(Closure body) {
60-
init()
63+
doInRequestEnvironment(DEFAULT_CONTROLLER_NAME, body)
64+
}
65+
66+
/**
67+
* Calls {@code init()} before and {@code destroy()} after invoking {@code body}.
68+
*/
69+
void doInRequestEnvironment(String controllerName, Closure body) {
70+
init(controllerName)
6171
try {
6272
body()
6373
} finally {
6474
destroy()
6575
}
6676
}
67-
77+
6878
}

0 commit comments

Comments
 (0)