Skip to content

Commit bcf435d

Browse files
committed
Add simple servlet content tests
1 parent 8b2d2fa commit bcf435d

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.felix.http.itest.servletapi3;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
24+
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.PrintWriter;
28+
import java.net.HttpURLConnection;
29+
import java.util.ArrayList;
30+
import java.util.Dictionary;
31+
import java.util.Hashtable;
32+
import java.util.List;
33+
import java.util.concurrent.CountDownLatch;
34+
import java.util.concurrent.TimeUnit;
35+
36+
import javax.servlet.Servlet;
37+
import javax.servlet.http.HttpServletRequest;
38+
import javax.servlet.http.HttpServletResponse;
39+
40+
import org.junit.After;
41+
import org.junit.Test;
42+
import org.junit.runner.RunWith;
43+
import org.ops4j.pax.exam.junit.PaxExam;
44+
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
45+
import org.ops4j.pax.exam.spi.reactors.PerMethod;
46+
import org.osgi.framework.ServiceRegistration;
47+
48+
@RunWith(PaxExam.class)
49+
@ExamReactorStrategy(PerMethod.class)
50+
public class ServletContentTest extends Servlet3BaseIntegrationTest {
51+
52+
private static final String CONTENT = "myservletcontent";
53+
54+
private List<ServiceRegistration<?>> registrations = new ArrayList<>();
55+
56+
private CountDownLatch initLatch;
57+
private CountDownLatch destroyLatch;
58+
59+
public void setupLatches(int count) {
60+
initLatch = new CountDownLatch(count);
61+
destroyLatch = new CountDownLatch(count);
62+
}
63+
64+
public void setupServlet(final String path) throws Exception {
65+
Dictionary<String, Object> servletProps = new Hashtable<>();
66+
servletProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, path);
67+
68+
TestServlet servletWithErrorCode = new TestServlet(initLatch, destroyLatch) {
69+
private static final long serialVersionUID = 1L;
70+
71+
@Override
72+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
73+
throws IOException {
74+
resp.setContentType("text/plain");
75+
resp.setCharacterEncoding("utf-8");
76+
PrintWriter writer = resp.getWriter();
77+
resp.setCharacterEncoding("utf-16");
78+
writer.print(CONTENT);
79+
}
80+
};
81+
82+
registrations.add(m_context.registerService(Servlet.class.getName(), servletWithErrorCode, servletProps));
83+
}
84+
85+
@After
86+
public void unregisterServices() throws InterruptedException {
87+
for (ServiceRegistration<?> serviceRegistration : registrations) {
88+
serviceRegistration.unregister();
89+
}
90+
91+
assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
92+
93+
Thread.sleep(500);
94+
}
95+
96+
protected void assertContent(final String path) throws IOException {
97+
HttpURLConnection conn = (HttpURLConnection) createURL(path).openConnection();
98+
99+
int rc = conn.getResponseCode();
100+
assertEquals("Unexpected response code,", 200, rc);
101+
102+
assertEquals("text/plain;charset=utf-8", conn.getHeaderField("content-type"));
103+
try (InputStream is = conn.getInputStream()) {
104+
assertEquals(CONTENT, slurpAsString(is));
105+
} finally {
106+
conn.disconnect();
107+
}
108+
}
109+
110+
@Test
111+
public void testContentAndHeaders() throws Exception {
112+
setupLatches(1);
113+
114+
setupServlet("/myservlet");
115+
116+
assertTrue(initLatch.await(5, TimeUnit.SECONDS));
117+
118+
assertContent("/myservlet");
119+
}
120+
}
121+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.felix.http.itest.servletapi5;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
23+
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.io.PrintWriter;
27+
import java.net.HttpURLConnection;
28+
import java.util.Dictionary;
29+
import java.util.Hashtable;
30+
import jakarta.servlet.Servlet;
31+
import jakarta.servlet.http.HttpServletRequest;
32+
import jakarta.servlet.http.HttpServletResponse;
33+
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.ops4j.pax.exam.junit.PaxExam;
37+
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
38+
import org.ops4j.pax.exam.spi.reactors.PerMethod;
39+
40+
@RunWith(PaxExam.class)
41+
@ExamReactorStrategy(PerMethod.class)
42+
public class ServletContentTest extends Servlet5BaseIntegrationTest {
43+
44+
private static final String CONTENT = "myservletcontent";
45+
46+
public void setupServlet(final String path) throws Exception {
47+
Dictionary<String, Object> servletProps = new Hashtable<>();
48+
servletProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, path);
49+
50+
TestServlet servletWithErrorCode = new TestServlet() {
51+
private static final long serialVersionUID = 1L;
52+
53+
@Override
54+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
55+
throws IOException {
56+
resp.setContentType("text/plain");
57+
resp.setCharacterEncoding("utf-8");
58+
PrintWriter writer = resp.getWriter();
59+
resp.setCharacterEncoding("utf-16");
60+
writer.print(CONTENT);
61+
}
62+
};
63+
64+
registrations.add(m_context.registerService(Servlet.class.getName(), servletWithErrorCode, servletProps));
65+
}
66+
67+
protected void assertContent(final String path) throws IOException {
68+
HttpURLConnection conn = (HttpURLConnection) createURL(path).openConnection();
69+
70+
int rc = conn.getResponseCode();
71+
assertEquals("Unexpected response code,", 200, rc);
72+
73+
assertEquals("text/plain;charset=utf-8", conn.getHeaderField("content-type"));
74+
try (InputStream is = conn.getInputStream()) {
75+
assertEquals(CONTENT, slurpAsString(is));
76+
} finally {
77+
conn.disconnect();
78+
}
79+
}
80+
81+
@Test
82+
public void testContentAndHeaders() throws Exception {
83+
setupLatches(1);
84+
85+
setupServlet("/myservlet");
86+
87+
this.waitForInit();
88+
89+
assertContent("/myservlet");
90+
}
91+
}
92+

0 commit comments

Comments
 (0)