Skip to content

Commit 6ed9314

Browse files
erdibartoszmajsak
authored andcommitted
fix: improves method name handling executors(#215)
* Use the logical and not the actual test method name and escape reserved characters in test method names when composing urls in ServletMethodExecutor. * Fix two incorrect implementations of of TestMethodExecutor.getMethodName() * Consistently use TestMethodExecutor.getMethodName() instead of TestMethodExecutor.getMethod().getName() Fixes #214
1 parent 8f74705 commit 6ed9314

File tree

10 files changed

+94
-9
lines changed

10 files changed

+94
-9
lines changed

container/test-impl-base/src/test/java/org/jboss/arquillian/container/test/impl/ContainerEventControllerTestCase.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ public void shouldInvokeTestInContainerDeploymentContext() throws Exception {
174174
public void invoke(Object... parameters) throws Throwable {
175175
}
176176

177+
@Override
178+
public String getMethodName() {
179+
return getMethod().getName();
180+
}
181+
177182
@Override
178183
public Method getMethod() {
179184
return testMethod();
@@ -198,6 +203,11 @@ public void shouldNotInvokeTestInContainerDeploymentContextIfNoDeploymentFound()
198203
public void invoke(Object... parameters) throws Throwable {
199204
}
200205

206+
@Override
207+
public String getMethodName() {
208+
return getMethod().getName();
209+
}
210+
201211
@Override
202212
public Method getMethod() {
203213
return testMethod();
@@ -220,6 +230,11 @@ public void shouldThrowExceptionIfTryingToOperateOnANonExistingContext() throws
220230
public void invoke(Object... parameters) throws Throwable {
221231
}
222232

233+
@Override
234+
public String getMethodName() {
235+
return getMethod().getName();
236+
}
237+
223238
@Override
224239
public Method getMethod() {
225240
return nonExistingOperatesOnDeploymentMethod();

junit/core/src/main/java/org/jboss/arquillian/junit/MethodInvoker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public void invoke(Object... parameters) throws Throwable {
1818
invokeMethod(parameters);
1919
}
2020

21+
@Override
22+
public String getMethodName() {
23+
return method.getName();
24+
}
25+
2126
public Method getMethod() {
2227
return method.getMethod();
2328
}

protocols/jmx/src/main/java/org/jboss/arquillian/protocol/jmx/JMXMethodExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public TestResult invoke(TestMethodExecutor testMethodExecutor) {
6464
}
6565

6666
String testClass = testMethodExecutor.getInstance().getClass().getName();
67-
String testMethod = testMethodExecutor.getMethod().getName();
67+
String testMethod = testMethodExecutor.getMethodName();
6868
String testCanonicalName = testClass + "." + testMethod;
6969

7070
NotificationListener commandListener = null;
@@ -125,4 +125,4 @@ public void handleNotification(Notification notification, Object handback) {
125125
}
126126
}
127127
}
128-
}
128+
}

protocols/jmx/src/test/java/org/jboss/arquillian/protocol/jmx/JMXTestRunnerTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ public void shouldBeAbleToSendReceiveCommands() throws Throwable {
151151
public void invoke(Object... parameters) throws Throwable {
152152
}
153153

154+
@Override
155+
public String getMethodName() {
156+
return getMethod().getName();
157+
}
158+
154159
@Override
155160
public Method getMethod() {
156161
return testMethod();

protocols/servlet/src/main/java/org/jboss/arquillian/protocol/servlet/ServletMethodExecutor.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.URI;
2424
import java.net.URL;
2525
import java.net.URLConnection;
26+
import java.net.URLEncoder;
2627
import java.util.Collection;
2728
import java.util.Timer;
2829
import java.util.TimerTask;
@@ -75,16 +76,18 @@ public TestResult invoke(final TestMethodExecutor testMethodExecutor) {
7576
URI targetBaseURI = uriHandler.locateTestServlet(testMethodExecutor.getMethod());
7677

7778
Class<?> testClass = testMethodExecutor.getInstance().getClass();
78-
final String url = targetBaseURI.toASCIIString() + ARQUILLIAN_SERVLET_MAPPING
79-
+ "?outputMode=serializedObject&className=" + testClass.getName() + "&methodName="
80-
+ testMethodExecutor.getMethod().getName();
81-
82-
final String eventUrl = targetBaseURI.toASCIIString() + ARQUILLIAN_SERVLET_MAPPING
83-
+ "?outputMode=serializedObject&className=" + testClass.getName() + "&methodName="
84-
+ testMethodExecutor.getMethod().getName() + "&cmd=event";
8579

8680
Timer eventTimer = null;
8781
try {
82+
String urlEncodedMethodName = URLEncoder.encode(testMethodExecutor.getMethodName(), "UTF-8");
83+
final String url = targetBaseURI.toASCIIString() + ARQUILLIAN_SERVLET_MAPPING
84+
+ "?outputMode=serializedObject&className=" + testClass.getName() + "&methodName="
85+
+ urlEncodedMethodName;
86+
87+
final String eventUrl = targetBaseURI.toASCIIString() + ARQUILLIAN_SERVLET_MAPPING
88+
+ "?outputMode=serializedObject&className=" + testClass.getName() + "&methodName="
89+
+ urlEncodedMethodName + "&cmd=event";
90+
8891
eventTimer = createCommandServicePullTimer(eventUrl);
8992
return executeWithRetry(url, TestResult.class);
9093
} catch (Exception e) {

protocols/servlet/src/test/java/org/jboss/arquillian/protocol/servlet/AbstractServerBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ public static class MockTestExecutor implements TestMethodExecutor, Serializable
136136
public void invoke(Object... parameters) throws Throwable {
137137
}
138138

139+
public String getMethodName() {
140+
return getMethod().getName();
141+
}
142+
139143
public Method getMethod() {
140144
try {
141145
return this.getClass().getMethod("getMethod");

protocols/servlet/src/test/java/org/jboss/arquillian/protocol/servlet/ProtocolTestCase.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ public void shouldReturnTestResult() throws Exception {
5050
result.getThrowable());
5151
}
5252

53+
@Test
54+
public void shouldUseLogicalMethodNameAndSupportUrlReservedCharactersInIt() throws Exception {
55+
final String testMethodNameWithReservedUrlCharacters = "non standard!test&name";
56+
57+
MockTestRunner.add(TestResult.passed());
58+
59+
ServletMethodExecutor executor = createExecutor();
60+
executor.invoke(new MockTestExecutor() {
61+
@Override
62+
public String getMethodName() {
63+
return testMethodNameWithReservedUrlCharacters;
64+
}
65+
});
66+
67+
Assert.assertEquals(
68+
"Should use the logical method name",
69+
testMethodNameWithReservedUrlCharacters,
70+
MockTestRunner.testRequests.get(0).getMethodName()
71+
);
72+
}
73+
5374
@Test
5475
public void shouldReturnThrownException() throws Exception {
5576
MockTestRunner.add(TestResult.failed(new Exception().fillInStackTrace()));

protocols/servlet/src/test/java/org/jboss/arquillian/protocol/servlet/test/MockTestRunner.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class MockTestRunner implements TestRunner {
3737
public static List<Command<?>> commands = new ArrayList<Command<?>>();
3838
public static List<Object> commandResults = new ArrayList<Object>();
3939

40+
public static List<TestRequest> testRequests = new ArrayList<TestRequest>();
41+
4042
public static void add(Command<?> command) {
4143
commands.add(command);
4244
}
@@ -49,13 +51,33 @@ public static void clear() {
4951
wantedResults = null;
5052
commands.clear();
5153
commandResults.clear();
54+
testRequests.clear();
5255
}
5356

5457
public TestResult execute(Class<?> testClass, String methodName) {
58+
testRequests.add(new TestRequest(testClass, methodName));
5559
for (Command<?> command : commands) {
5660
commandResults.add(new ServletCommandService().execute(command));
5761
}
5862

5963
return wantedResults;
6064
}
65+
66+
public static class TestRequest {
67+
private final Class<?> testClass;
68+
private final String methodName;
69+
70+
public TestRequest(Class<?> testClass, String methodName) {
71+
this.testClass = testClass;
72+
this.methodName = methodName;
73+
}
74+
75+
public Class<?> getTestClass() {
76+
return testClass;
77+
}
78+
79+
public String getMethodName() {
80+
return methodName;
81+
}
82+
}
6183
}

test/spi/src/main/java/org/jboss/arquillian/test/spi/TestMethodExecutor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
* @version $Revision: $
2626
*/
2727
public interface TestMethodExecutor {
28+
29+
/**
30+
* The method name to invoke
31+
*/
32+
String getMethodName();
33+
2834
/**
2935
* The method to invoke.
3036
*/

testng/core/src/main/java/org/jboss/arquillian/testng/Arquillian.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ private void swapWithClassNames(Object[] source) {
172172
}
173173
}
174174

175+
public String getMethodName() {
176+
return testResult.getMethod().getMethodName();
177+
}
178+
175179
public Method getMethod() {
176180
return testResult.getMethod().getMethod();
177181
}

0 commit comments

Comments
 (0)