Search in sources :

Example 1 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class UploadingSpeedTest method main.

public static void main(String[] args) {
    if (0 == args.length) {
        System.err.println("Lack files directory!");
        System.exit(0);
    }
    File dir = Files.findFile(args[0]);
    if (null == dir) {
        System.err.println("Fail to found directory: " + args[0]);
        System.exit(0);
    }
    final Uploading up = UploadUnit.TYPE.born();
    final UploadingContext uc = UploadingContext.create("~/nutz/unit/uploadtmp");
    File[] files = dir.listFiles();
    final MockHttpServletRequest req = request().setInputStream(insmulti("UTF-8", files));
    req.setSession(session(context()));
    req.init();
    Object monLock = new Object();
    int monInterval = 2000;
    UploadMonitor mon = new UploadMonitor(monLock, req.getSession(), out, monInterval);
    Thread monThread = new Thread(mon, "UploadingMonitor");
    monThread.start();
    out.println("Begin...");
    Stopwatch sw = null;
    try {
        sw = Stopwatch.run(new Atom() {

            public void run() {
                try {
                    up.parse(req, uc);
                } catch (UploadException e) {
                    throw Lang.wrapThrow(e);
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        mon.stop();
        out.println("\n...Done!");
        if (null != sw)
            out.println(sw);
    }
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Stopwatch(org.nutz.lang.Stopwatch) Atom(org.nutz.trans.Atom) File(java.io.File)

Example 2 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class NameInjectorTest method test_blank_param_to_number.

/**
     * 根据 Issue 272,如果为空串,原生类型的外覆类应该返回 null
     */
@Test
public void test_blank_param_to_number() {
    // 准备数据
    MockHttpServletRequest req = Mock.servlet.request();
    req.setParameter("a", "  ");
    req.setParameter("b", "  ");
    // 执行 & 检测
    assertNull(inj("a", Long.class, req));
    assertEquals(0, (int) inj("b", int.class, req));
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 3 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class NameInjectorTest method test_normal_int.

@Test
public void test_normal_int() {
    // 准备数据
    MockHttpServletRequest req = Mock.servlet.request();
    req.setParameter("abc", "1");
    // 执行 & 检测
    assertEquals(Integer.valueOf(1), inj("abc", int.class, req));
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 4 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class ObjectNavlPairInjectorTest method test_array_to_string.

@Test
public void test_array_to_string() {
    // 准备数据
    MockHttpServletRequest req = Mock.servlet.request();
    req.setParameterValues("pojo.str", Lang.array("A", "B", "C"));
    // 执行
    MvcTestPojo pojo = (MvcTestPojo) inj().get(null, req, null, null);
    // 检测
    assertEquals("A,B,C", pojo.str);
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 5 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class ObjectNavlPairInjectorTest method test_duplicated_name_params.

/**
     * 这个测试将检验在 HTTP 请求中,如果存在多个参数同名的情况,本注入器能否正确处理
     */
@Test
public void test_duplicated_name_params() {
    // 准备数据
    MockHttpServletRequest req = Mock.servlet.request();
    req.setParameter("pojo.num", 23);
    req.setParameterValues("pojo.names", Lang.array("A", "B", "C"));
    // 执行
    MvcTestPojo pojo = (MvcTestPojo) inj().get(null, req, null, null);
    // 检测
    assertNull(pojo.longValue);
    assertEquals(23, pojo.num);
    assertEquals(3, pojo.names.length);
    assertEquals("A", pojo.names[0]);
    assertEquals("B", pojo.names[1]);
    assertEquals("C", pojo.names[2]);
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (org.nutz.mock.servlet.MockHttpServletRequest)43 Test (org.junit.Test)42 Uploading (org.nutz.mvc.upload.Uploading)14 MultipartInputStream (org.nutz.mock.servlet.multipart.MultipartInputStream)11 File (java.io.File)9 TempFile (org.nutz.mvc.upload.TempFile)8 ParamInjector (org.nutz.mvc.adaptor.ParamInjector)3 SimpleDateFormat (java.text.SimpleDateFormat)2 List (java.util.List)1 Map (java.util.Map)1 ServletInputStream (javax.servlet.ServletInputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Stopwatch (org.nutz.lang.Stopwatch)1 NutMap (org.nutz.lang.util.NutMap)1 ParamExtractor (org.nutz.mvc.adaptor.ParamExtractor)1 ObjectNaviNode (org.nutz.mvc.adaptor.injector.ObjectNaviNode)1 Atom (org.nutz.trans.Atom)1