0
public class DgiQtyAction extends DispatchAction {
    private final Logger mLog = Logger.getLogger(this.getClass());

    public ActionForward fnDgiQty(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        mLog.debug(request.getParameter(EcoConstants.ecopidid));
        ActionErrors errorMessage = new ActionErrors();
        if(request.getSession().getAttribute(EcoConstants.userBean)==null)
        {
            request.setAttribute(EcoConstants.ERROR_MESSAGE,EcoConstants.SESSION_TIMEOUT);
            errorMessage.add(Globals.MESSAGE_KEY, new ActionMessage(EcoConstants.error_message,EcoConstants.SESSION_TIMEOUT));
            saveMessages(request, errorMessage); 
            request.setAttribute(EcoConstants.errorMessageType,EcoConstants.errorMessageType);
            return mapping.findForward(EcoConstants.SESSION_FORWARD);
        }
        String ecoPidID = (String) request.getParameter(EcoConstants.ecopidid);
        String pidId = ESAPI.encoder().encodeForHTML((String) request.getParameter(EcoConstants.pidid));

        mLog.debug("pidid  --------" + pidId);
        mLog.debug("ecopidpid  --------" + ecoPidID);
        ArrayList dgiQty = new ArrayList();
        NeedDgiForm niForm = new NeedDgiForm();
        try {
            NeedDgiBD niBD = new NeedDgiBD();
            if (ecoPidID != null) {
                dgiQty = niBD.getDgiQty(ecoPidID);
                if (dgiQty.size() != 0) {
                    mLog.debug(dgiQty.get(0).toString());
                    if (dgiQty.get(0).toString().equals(EcoConstants.hundred)) {
                        niForm.setGlqtyList(new ArrayList());
                        request.setAttribute(EcoConstants.pidId, pidId);
                        return mapping.findForward(EcoConstants.SuccessInfo);
                    } else {
                        mLog.debug("fnBug 1----------------> " + dgiQty.get(0));
                        mLog.info("dgiQty         sizeeeee: :" + dgiQty.size());
                        niForm.setGlqtyList(dgiQty);
                    }
                }
            }
        } catch (Exception e) {
            //log.error("General Exception in DgiQtyAction.fnDgiQty: "
            //        + e.getMessage(), e);
            request.setAttribute(EcoConstants.ERROR_MESSAGE, e.getMessage());
            return mapping.findForward(EcoConstants.ERROR_PAGE);
        }
        mLog.debug("pidid  after wards--------" + pidId);
        request.setAttribute(EcoConstants.pidId, pidId);
        request.setAttribute(mapping.getAttribute(), niForm);
        return mapping.findForward(EcoConstants.SuccessInfo);
    }
}
public class DgiQtyActionTest {
    ActionMapping am;
    ActionForm af;
    DgiQtyAction dat;
    private MockHttpSession mocksession;
    private MockHttpServletRequest mockrequest;
    private MockHttpServletResponse mockresponse;
    
    @Test
    public void fnDgiQty() throws Exception
    {
        mocksession = new MockHttpSession();
        mockrequest = new MockHttpServletRequest();
        mockresponse = new MockHttpServletResponse();
        ((MockHttpServletRequest) mockrequest).setSession(mocksession);
        mocksession.setAttribute("userBean","userBean");
         RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockrequest));
         mockrequest.addParameter("ecopid","something");
         mockrequest.addParameter("pid","<script>");
         Encoder instance = ESAPI.encoder();
         assertEquals("something",mockrequest.getParameter("ecopid"));
         assertEquals("&lt;script&gt;",instance.encodeForHTML(mockrequest.getParameter("pid")));
         dat=mock(DgiQtyAction.class);
         am=mock(ActionMapping.class);
         af=mock(ActionForm.class);
         dat.fnDgiQty(am,af,mockrequest,  mockresponse);
    }
}

我为上述课程编写了单元测试用例。我通过 jenkins 运行此代码并使用 sonarqube 作为代码覆盖率。我需要覆盖参数的 ESAPI 编码器,它构建成功但覆盖率没有增加。我找不到它的错误。请帮帮我。提前致谢

4

0 回答 0