Skip to content

Commit b856919

Browse files
committed
Extended JASPIC CDI forward test with check for servlet path
1 parent d25a239 commit b856919

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package org.javaee7.jaspic.dispatching.bean;
22

33
import javax.enterprise.context.RequestScoped;
4+
import javax.inject.Inject;
45
import javax.inject.Named;
6+
import javax.servlet.http.HttpServletRequest;
57

68
@Named
79
@RequestScoped
810
public class MyBean {
11+
12+
@Inject
13+
private HttpServletRequest request;
914

1015
public String getText() {
11-
return "Called from CDI";
16+
return "Called from CDI\n";
17+
}
18+
19+
public String getServletPath() {
20+
return request.getServletPath();
1221
}
1322

1423
}

jaspic/dispatching-jsf-cdi/src/main/java/org/javaee7/jaspic/dispatching/servlet/ForwardedServlet.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.javaee7.jaspic.dispatching.servlet;
22

3+
import static java.util.logging.Level.SEVERE;
4+
35
import java.io.IOException;
6+
import java.util.logging.Logger;
47

58
import javax.inject.Inject;
69
import javax.servlet.ServletException;
@@ -21,12 +24,20 @@ public class ForwardedServlet extends HttpServlet {
2124

2225
private static final long serialVersionUID = 1L;
2326

27+
private final static Logger logger = Logger.getLogger(ForwardedServlet.class.getName());
28+
2429
@Inject
2530
private MyBean myBean;
2631

2732
@Override
2833
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
2934
response.getWriter().write("response from forwardedServlet - " + myBean.getText());
35+
response.getWriter().write("servletPath via Servlet - " + request.getServletPath() + "\n");
36+
try {
37+
response.getWriter().write("servletPath via CDI - " + myBean.getServletPath());
38+
} catch (Exception e) {
39+
logger.log(SEVERE, "", e);
40+
}
3041
}
3142

3243
}

jaspic/dispatching-jsf-cdi/src/test/java/org/javaee7/jaspictest/dispatching/CDIForwardTest.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public static Archive<?> createDeployment() {
2929
);
3030
}
3131

32+
/**
33+
* Tests that the forwarded resource can utilize a CDI bean
34+
*
35+
* @throws IOException
36+
* @throws SAXException
37+
*/
3238
@Test
3339
public void testCDIForwardViaPublicResource() throws IOException, SAXException {
3440

@@ -39,6 +45,12 @@ public void testCDIForwardViaPublicResource() throws IOException, SAXException {
3945
);
4046
}
4147

48+
/**
49+
* Tests that the forwarded resource can utilize a CDI bean
50+
*
51+
* @throws IOException
52+
* @throws SAXException
53+
*/
4254
@Test
4355
public void testCDIForwardViaProtectedResource() throws IOException, SAXException {
4456

@@ -48,5 +60,97 @@ public void testCDIForwardViaProtectedResource() throws IOException, SAXExceptio
4860
response.contains("response from forwardedServlet - Called from CDI")
4961
);
5062
}
63+
64+
/**
65+
* Tests that the forwarded resource has the correct servlet path
66+
*
67+
* @throws IOException
68+
* @throws SAXException
69+
*/
70+
@Test
71+
public void testCDIForwardWithRequestPublic() throws IOException, SAXException {
72+
73+
String response = getFromServerPath("public/servlet");
74+
75+
assertTrue(
76+
"Servletpath reported by servlet request after forward from SAM not as expected.",
77+
response.contains("servletPath via Servlet - /forwardedServlet")
78+
);
79+
}
80+
81+
/**
82+
* Tests that the forwarded resource has the correct servlet path
83+
*
84+
* @throws IOException
85+
* @throws SAXException
86+
*/
87+
@Test
88+
public void testCDIForwardWithRequestProtected() throws IOException, SAXException {
89+
90+
String response = getFromServerPath("protected/servlet");
91+
92+
assertTrue(
93+
"Servletpath reported by servlet request after forward from SAM not as expected.",
94+
response.contains("servletPath via Servlet - /forwardedServlet")
95+
);
96+
}
97+
98+
/**
99+
* Tests that the forwarded resource can utilize an injected HttpServletRequest and that
100+
* the value is correct.
101+
*
102+
* @throws IOException
103+
* @throws SAXException
104+
*/
105+
@Test
106+
public void testCDIForwardWithRequestInjectPublic() throws IOException, SAXException {
107+
108+
String response = getFromServerPath("public/servlet");
109+
110+
assertTrue(
111+
"Servletpath reported by servlet request after forward from SAM not as expected.",
112+
response.contains("servletPath via Servlet - /forwardedServlet")
113+
);
114+
115+
assertTrue(
116+
"Response did not contain output from forwarded Servlet using CDI injected request. " +
117+
"Request appears not to be usable.",
118+
response.contains("servletPath via CDI")
119+
);
120+
121+
assertTrue(
122+
"Servletpath reported by injected request after forward from SAM not as expected.",
123+
response.contains("servletPath via CDI - /forwardedServlet")
124+
);
125+
}
126+
127+
/**
128+
* Tests that the forwarded resource can utilize an injected HttpServletRequest and that
129+
* the value is correct.
130+
*
131+
* @throws IOException
132+
* @throws SAXException
133+
*/
134+
@Test
135+
public void testCDIForwardWithRequestInjectProtected() throws IOException, SAXException {
136+
137+
String response = getFromServerPath("protected/servlet");
138+
139+
assertTrue(
140+
"Servletpath reported by servlet request after forward from SAM not as expected.",
141+
response.contains("servletPath via Servlet - /forwardedServlet")
142+
);
143+
144+
assertTrue(
145+
"Response did not contain output from forwarded Servlet using CDI injected request. " +
146+
"Request appears not to be usable.",
147+
response.contains("servletPath via CDI")
148+
);
149+
150+
assertTrue(
151+
"Servletpath reported by injected request after forward from SAM not as expected.",
152+
response.contains("servletPath via CDI - /forwardedServlet")
153+
);
154+
}
51155

52156
}

0 commit comments

Comments
 (0)