⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formcontrollertest.xdt

📁 spring struts hibernate
💻 XDT
字号:
<XDtTagDef:tagDef namespace="Form" handler="org.example.antbook.xdoclet.FormTagsHandler"/>package <XDtForm:parentPackageName/>.webapp.action;

import <XDtPackage:packageName/>.<XDtForm:className/>;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;

public class <XDtForm:className/>FormControllerTest extends BaseControllerTestCase {
    private <XDtForm:className/>FormController c;
    private MockHttpServletRequest request;
    private ModelAndView mv;

    protected void setUp() throws Exception {
        // needed to initialize a user
        super.setUp();
        c = (<XDtForm:className/>FormController) ctx.getBean("<XDtForm:classNameLower/>FormController");
    }

    protected void tearDown() {
        c = null;
    }

    public void testEdit() throws Exception {
        log.debug("testing edit...");
        request = newGet("/edit<XDtForm:className/>.html");
        request.addParameter("<XDtMethodEx:idField getType="propertyName"/>", "1");

        mv = c.handleRequest(request, new MockHttpServletResponse());

        assertEquals("<XDtForm:classNameLower/>Form", mv.getViewName());
    }

    public void testSave() throws Exception {
        request = newGet("/edit<XDtForm:className/>.html");
        request.addParameter("<XDtMethodEx:idField getType="propertyName"/>", "1");

        mv = c.handleRequest(request, new MockHttpServletResponse());

        <XDtForm:className/> <XDtForm:classNameLower/> = (<XDtForm:className/>) mv.getModel().get(c.getCommandName());
        assertNotNull(<XDtForm:classNameLower/>);
        
        request = newPost("/edit<XDtForm:className/>.html");
        super.objectToRequestParameters(<XDtForm:classNameLower/>, request);

        // update the form's fields and add it back to the request
        <XDtMethodEx:forAllMethods>
        <XDtMethodEx:ifMethodTagValueEquals tagName="hibernate.property" paramName="not-null" value="true">
        <XDtMethodEx:setterWithValue/>
        </XDtMethodEx:ifMethodTagValueEquals>
        </XDtMethodEx:forAllMethods>
        
        mv = c.handleRequest(request, new MockHttpServletResponse());
        
        Errors errors = (Errors) mv.getModel().get(BindException.ERROR_KEY_PREFIX + "<XDtForm:classNameLower/>");

        if (errors != null) {
            log.debug(errors);
        }
        
        assertNull(errors);
        assertNotNull(request.getSession().getAttribute("messages"));        
    }

    public void testRemove() throws Exception {
        request = newPost("/edit<XDtForm:className/>.html");
        request.addParameter("delete", "");
        request.addParameter("<XDtMethodEx:idField getType="propertyName"/>", "2");
        mv = c.handleRequest(request, new MockHttpServletResponse());
        assertNotNull(request.getSession().getAttribute("messages"));
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -