Spring-test + JSF 1.2 + Portlet

原创
2011/10/20 11:04
阅读数 147

jsf Bean

@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

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

@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");
  }
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部