2121import static org .junit .Assert .assertEquals ;
2222import static org .junit .Assert .assertTrue ;
2323import static org .osgi .service .http .whiteboard .HttpWhiteboardConstants .HTTP_WHITEBOARD_SERVLET_PATTERN ;
24-
24+
2525import java .io .IOException ;
2626import java .io .InputStream ;
2727import java .io .PrintWriter ;
3232import java .util .List ;
3333import java .util .concurrent .CountDownLatch ;
3434import java .util .concurrent .TimeUnit ;
35-
35+
3636import javax .servlet .Servlet ;
37+ import javax .servlet .ServletRequest ;
38+ import javax .servlet .ServletResponse ;
3739import javax .servlet .http .HttpServletRequest ;
3840import javax .servlet .http .HttpServletResponse ;
3941
42+ import org .apache .felix .http .javaxwrappers .ServletWrapper ;
4043import org .junit .After ;
4144import org .junit .Test ;
4245import org .junit .runner .RunWith ;
4346import org .ops4j .pax .exam .junit .PaxExam ;
4447import org .ops4j .pax .exam .spi .reactors .ExamReactorStrategy ;
4548import org .ops4j .pax .exam .spi .reactors .PerMethod ;
4649import org .osgi .framework .ServiceRegistration ;
47-
50+
51+ import jakarta .servlet .http .HttpServlet ;
52+
4853@ RunWith (PaxExam .class )
4954@ ExamReactorStrategy (PerMethod .class )
5055public class ServletContentTest extends Servlet3BaseIntegrationTest {
@@ -81,7 +86,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
8186
8287 registrations .add (m_context .registerService (Servlet .class .getName (), servletWithErrorCode , servletProps ));
8388 }
84-
89+
8590 @ After
8691 public void unregisterServices () throws InterruptedException {
8792 for (ServiceRegistration <?> serviceRegistration : registrations ) {
@@ -117,5 +122,67 @@ public void testContentAndHeaders() throws Exception {
117122
118123 assertContent ("/myservlet" );
119124 }
125+
126+ private class JakartaServlet extends HttpServlet {
127+ private static final long serialVersionUID = 1L ;
128+
129+ @ Override
130+ public void init () throws jakarta .servlet .ServletException {
131+ super .init ();
132+ initLatch .countDown ();
133+ }
134+
135+ @ Override
136+ protected void doGet (jakarta .servlet .http .HttpServletRequest req , jakarta .servlet .http .HttpServletResponse resp )
137+ throws IOException {
138+ resp .getWriter ().print ("helloworld" );
139+ resp .flushBuffer ();
140+ }
141+
142+ @ Override
143+ public void destroy () {
144+ destroyLatch .countDown ();
145+ }
146+ }
147+
148+ @ Test
149+ public void testRegisteringWrapperAsServlet () throws Exception {
150+ this .setupLatches (1 );
151+
152+ final Dictionary <String , Object > servletProps = new Hashtable <>();
153+ servletProps .put (HTTP_WHITEBOARD_SERVLET_PATTERN , "/testjakarta" );
154+
155+ final ServiceRegistration <Servlet > reg = m_context .registerService (Servlet .class , new ServletWrapper (new JakartaServlet ()), servletProps );
156+
157+ assertTrue (initLatch .await (DEFAULT_TIMEOUT , TimeUnit .MILLISECONDS ));
158+
159+ assertContent ("helloworld" , createURL ("/testjakarta" ));
160+
161+ reg .unregister ();
162+ assertTrue (destroyLatch .await (DEFAULT_TIMEOUT , TimeUnit .MILLISECONDS ));
163+ }
164+
165+ @ Test
166+ public void testRegisteringCustomWrapperAsServlet () throws Exception {
167+ this .setupLatches (1 );
168+
169+ final Dictionary <String , Object > servletProps = new Hashtable <>();
170+ servletProps .put (HTTP_WHITEBOARD_SERVLET_PATTERN , "/testjakarta" );
171+
172+ final ServiceRegistration <Servlet > reg = m_context .registerService (Servlet .class , new ServletWrapper (new JakartaServlet ()) {
173+ @ Override
174+ public void service (ServletRequest req , ServletResponse resp )
175+ throws IOException {
176+ resp .getWriter ().print ("helloworldwrapped" );
177+ resp .flushBuffer ();
178+ }
179+ }, servletProps );
180+
181+ assertTrue (initLatch .await (DEFAULT_TIMEOUT , TimeUnit .MILLISECONDS ));
182+
183+ assertContent ("helloworldwrapped" , createURL ("/testjakarta" ));
184+
185+ reg .unregister ();
186+ assertTrue (destroyLatch .await (DEFAULT_TIMEOUT , TimeUnit .MILLISECONDS ));
187+ }
120188}
121-
0 commit comments