forked from arquillian/arquillian-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseServletProtocolTestCase.java
156 lines (121 loc) · 5.99 KB
/
BaseServletProtocolTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.protocol.servlet;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext;
import org.jboss.arquillian.container.spi.client.protocol.metadata.Servlet;
import org.jboss.arquillian.container.test.api.TargetsContainer;
import org.junit.Assert;
import org.junit.Test;
/**
* ServletProtocolTestCase
*
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
public class BaseServletProtocolTestCase {
@Test
public void shouldFindTestServletInMetadata() throws Exception {
ServletProtocolConfiguration config = new ServletProtocolConfiguration();
HTTPContext testContext = new HTTPContext("127.0.0.1", 8080)
.add(new Servlet(ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME, "test"));
Method testMethod = getTestMethod("testNoAnnotations");
ServletURIHandler handler = new ServletURIHandler(config, to(testContext));
URI result = handler.locateTestServlet(testMethod);
Assert.assertEquals("http://127.0.0.1:8080/test", result.toString());
}
@Test
public void shouldOverrideMetadata() throws Exception {
ServletProtocolConfiguration config = new ServletProtocolConfiguration();
config.setScheme("https");
config.setHost("10.10.10.1");
config.setPort(90);
HTTPContext testContext = new HTTPContext("127.0.0.1", 8080)
.add(new Servlet(ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME, "test"));
Method testMethod = getTestMethod("testNoAnnotations");
ServletURIHandler handler = new ServletURIHandler(config, to(testContext));
URI result = handler.locateTestServlet(testMethod);
Assert.assertEquals("https://10.10.10.1:90/test", result.toString());
}
@Test
public void shouldMatchNamedTargetedContext() throws Exception {
ServletProtocolConfiguration config = new ServletProtocolConfiguration();
HTTPContext testContextOne = new HTTPContext("Y", "127.0.0.1", 8080)
.add(new Servlet(ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME, "test"));
HTTPContext testContextTwo = new HTTPContext("X", "127.0.0.1", 8081)
.add(new Servlet(ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME, "test"));
Method testMethod = getTestMethod("testTargeted");
ServletURIHandler handler = new ServletURIHandler(config, to(testContextOne, testContextTwo));
URI result = handler.locateTestServlet(testMethod);
Assert.assertEquals("http://127.0.0.1:8081/test", result.toString());
}
@Test
public void shouldOverrideMatchNamedTargetedContext() throws Exception {
ServletProtocolConfiguration config = new ServletProtocolConfiguration();
config.setScheme("https");
config.setHost("10.10.10.1");
config.setPort(90);
HTTPContext testContextOne = new HTTPContext("Y", "127.0.0.1", 8080)
.add(new Servlet(ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME, "testY"));
HTTPContext testContextTwo = new HTTPContext("X", "127.0.0.1", 8081)
.add(new Servlet(ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME, "testX"));
Method testMethod = getTestMethod("testTargeted");
ServletURIHandler handler = new ServletURIHandler(config, to(testContextOne, testContextTwo));
URI result = handler.locateTestServlet(testMethod);
Assert.assertEquals("https://10.10.10.1:90/testX", result.toString());
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionOnMissingNamedTargetedContext() throws Exception {
ServletProtocolConfiguration config = new ServletProtocolConfiguration();
HTTPContext testContextOne = new HTTPContext("Y", "127.0.0.1", 8080)
.add(new Servlet(ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME, "test"));
Method testMethod = getTestMethod("testTargeted");
ServletURIHandler handler = new ServletURIHandler(config, to(testContextOne));
handler.locateTestServlet(testMethod);
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenNoConfig() throws Exception {
new ServletURIHandler(null, to(new HTTPContext("127.0.0.1", 8080).add(new Servlet(ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME, "test"))));
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionOnNoContexts() throws Exception {
new ServletURIHandler(new ServletProtocolConfiguration(), null);
}
private Collection<HTTPContext> to(HTTPContext... inputs) {
List<HTTPContext> contexts = new ArrayList<HTTPContext>();
Collections.addAll(contexts, inputs);
return contexts;
}
private Method getTestMethod(String methodName) throws Exception {
return getClass().getDeclaredMethod(methodName);
}
/*
* Methods used for ServletURIHandler HTTPContext lookups.
*/
@SuppressWarnings("unused")
private void testNoAnnotations() {
}
@TargetsContainer("X")
private void testTargeted() {
}
}