From 56379d8b689fcfef70c8ad81e9a487115eb2c9ca Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 25 Jul 2017 22:49:39 +0800 Subject: [PATCH] add test --- .../web/service/file/FileUploadTests.java | 70 +++++++++++++++++++ .../src/test/resources/application.yml | 18 +++++ 2 files changed, 88 insertions(+) create mode 100644 hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/java/org/hswebframework/web/service/file/FileUploadTests.java create mode 100644 hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/resources/application.yml diff --git a/hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/java/org/hswebframework/web/service/file/FileUploadTests.java b/hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/java/org/hswebframework/web/service/file/FileUploadTests.java new file mode 100644 index 000000000..80ad660d8 --- /dev/null +++ b/hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/java/org/hswebframework/web/service/file/FileUploadTests.java @@ -0,0 +1,70 @@ +package org.hswebframework.web.service.file; + +import com.alibaba.fastjson.JSON; +import org.hswebframework.web.tests.SimpleWebApplicationTests; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.junit.Assert.*; + +/** + * Created by zhouhao on 2017/7/25. + */ +public class FileUploadTests extends SimpleWebApplicationTests { + + @Test + public void testUploadFile() throws Exception { + + //test multi file upload + String result = mvc.perform( + MockMvcRequestBuilders + .fileUpload("/file/upload-multi") + .file(new MockMultipartFile("files", "test.txt", + MediaType.TEXT_PLAIN_VALUE, "test".getBytes())) + .file(new MockMultipartFile("files", "test2.txt", + MediaType.TEXT_PLAIN_VALUE, "test2".getBytes())) + ).andReturn() + .getResponse() + .getContentAsString(); + + + Assert.assertEquals(JSON.parseObject(result).getJSONArray("result").size(),2); + System.out.println(result); + String fileId=JSON.parseObject(result) + .getJSONArray("result") + .getJSONObject(0) + .getString("id"); + String fileMd5 =JSON.parseObject(result) + .getJSONArray("result") + .getJSONObject(0) + .getString("md5"); + + result=testGet("/file/md5/"+fileMd5).exec().resultAsString(); + System.out.println(result); + + result= mvc.perform(MockMvcRequestBuilders.get("/file/download/"+fileId)) + .andReturn() + .getResponse() + .getContentAsString(); + + System.out.println(result); + + result = mvc.perform(MockMvcRequestBuilders + .fileUpload("/file/upload-static") + .file(new MockMultipartFile("file", "test.txt", + MediaType.TEXT_PLAIN_VALUE, "test".getBytes()))) + .andReturn() + .getResponse() + .getContentAsString(); + + System.out.println(result); + Assert.assertNotNull(JSON.parseObject(result).getString("result")); + + + + } + +} \ No newline at end of file diff --git a/hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/resources/application.yml b/hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/resources/application.yml new file mode 100644 index 000000000..ee206937f --- /dev/null +++ b/hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/resources/application.yml @@ -0,0 +1,18 @@ +spring: + aop: + auto: true + datasource: + url : jdbc:h2:mem:file_test_mem + username : sa + password : + type: com.alibaba.druid.pool.DruidDataSource + driver-class-name : org.h2.Driver +hsweb: + app: + name: 数据字典测试 + version: 3.0.0 + web: + upload: + static-file-path: target/static + static-location: http://localhost:8080/ + file-path: target/file