Spring-test + JSF 1.2 + Portlet

jsf Bean

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
@Controller
@Scope("request")
public class FooBean extends AbstractRequestBean {
private static final Logger log = Logger
.getLogger(FooBean.class);

@Autowired
private FooService service;

private String user;

public void setFooService(FooService service){
this.service = service;
}

public String hi(){
String str = getLoginUser() + " say hi to " + getUser();
getPortletRequest().setAttribute("message", str);
return "bar";
}

public void setUser(String user){
this.user = user;
}

public String getUser(){
return user;
}
}

AbstractRequestBean

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
import javax.faces.context.FacesContext;
import javax.portlet.PortletRequest;
import javax.servlet.http.HttpServletRequest;

public abstract class AbstractRequestBean {
private String loginUser;
private PortletRequest portletRequest;
private HttpServletRequest servletRequest;

public String getLoginUser() {
if (loginUser == null)
return User.getCurrentUser(getPortletRequest()).getUsername();

return loginUser;
}

public void setLoginUser(String loginUser) {
this.loginUser = loginUser;
}

public PortletRequest getPortletRequest() {
if (portletRequest == null)
return (PortletRequest) FacesContext.getCurrentInstance()
.getExternalContext().getRequest();

return portletRequest;
}

// for jsp/servlet call directly, not jsf bean
public HttpServletRequest getServletRequest() {
if (servletRequest == null)
return (HttpServletRequest) FacesContext.getCurrentInstance()
.getExternalContext().getRequest();

return servletRequest;
}

public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}

public void setPortletRequest(PortletRequest portletRequest) {
this.portletRequest = portletRequest;
}

}

FooTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@ContextConfiguration(locations = "/applicationContext.xml")
public class FooTest extends AbstractJUnit38SpringContextTests {

@Autowired
private FooService service;;

public void testHi(){
FooBean bean = new FooBean();
MockPortletRequest request = new MockPortletRequest();
MockPortletSession session = new MockPortletSession();
session.setAttribute(FooBean.class.getName()
+ "/programme", "06721", PortletSession.APPLICATION_SCOPE);
request.setSession(session);
bean.setPortletRequest(request);
bean.setLoginUser("Jay");

bean.setService(service);
bean.hi("Forest");
}
}