From 9f77e62faaeb78c7e30aea8665ec19d253b911d6 Mon Sep 17 00:00:00 2001 From: listening <893979653@qq.com> Date: Thu, 14 Dec 2017 10:06:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E4=B8=8D=E6=94=B9=E5=8F=98xxl-job?= =?UTF-8?q?=E6=BA=90=E7=A0=81=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9xxl-job=E7=9A=84=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AbstractSpringMvcTest.java | 25 +++++++++ .../admin/controller/IndexControllerTest.java | 22 ++++++++ .../controller/JobInfoControllerTest.java | 52 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 xxl-job-admin/src/test/java/com/xxl/job/admin/controller/AbstractSpringMvcTest.java create mode 100644 xxl-job-admin/src/test/java/com/xxl/job/admin/controller/IndexControllerTest.java create mode 100644 xxl-job-admin/src/test/java/com/xxl/job/admin/controller/JobInfoControllerTest.java diff --git a/xxl-job-admin/src/test/java/com/xxl/job/admin/controller/AbstractSpringMvcTest.java b/xxl-job-admin/src/test/java/com/xxl/job/admin/controller/AbstractSpringMvcTest.java new file mode 100644 index 00000000..3aab69dd --- /dev/null +++ b/xxl-job-admin/src/test/java/com/xxl/job/admin/controller/AbstractSpringMvcTest.java @@ -0,0 +1,25 @@ +package com.xxl.job.admin.controller; + +import org.junit.Before; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@WebAppConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath*:spring/*.xml"}) +public class AbstractSpringMvcTest { + @Autowired + private WebApplicationContext applicationContext; + protected MockMvc mockMvc; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.applicationContext).build(); + } +} diff --git a/xxl-job-admin/src/test/java/com/xxl/job/admin/controller/IndexControllerTest.java b/xxl-job-admin/src/test/java/com/xxl/job/admin/controller/IndexControllerTest.java new file mode 100644 index 00000000..1eb76743 --- /dev/null +++ b/xxl-job-admin/src/test/java/com/xxl/job/admin/controller/IndexControllerTest.java @@ -0,0 +1,22 @@ +package com.xxl.job.admin.controller; + +import org.junit.Test; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MvcResult; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; + +public class IndexControllerTest extends AbstractSpringMvcTest { + + @Test + public void testLogin() throws Exception { + MvcResult ret = mockMvc.perform( + post("/login") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .param("userName", "admin") + .param("password", "123456") + ).andReturn(); + + System.out.println(ret.getResponse().getContentAsString()); + } +} diff --git a/xxl-job-admin/src/test/java/com/xxl/job/admin/controller/JobInfoControllerTest.java b/xxl-job-admin/src/test/java/com/xxl/job/admin/controller/JobInfoControllerTest.java new file mode 100644 index 00000000..33df92e9 --- /dev/null +++ b/xxl-job-admin/src/test/java/com/xxl/job/admin/controller/JobInfoControllerTest.java @@ -0,0 +1,52 @@ +package com.xxl.job.admin.controller; + +import com.xxl.job.admin.core.model.XxlJobInfo; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Before; +import org.junit.Test; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MvcResult; + +import javax.servlet.http.Cookie; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; + +public class JobInfoControllerTest extends AbstractSpringMvcTest { + Cookie cookie; + + @Before + public void login() throws Exception { + MvcResult ret = mockMvc.perform( + post("/login") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .param("userName", "admin") + .param("password", "123456") + ).andReturn(); + cookie = ret.getResponse().getCookie("LOGIN_IDENTITY"); + } + + @Test + public void testAdd() throws Exception { + XxlJobInfo jobInfo = new XxlJobInfo(); + jobInfo.setJobGroup(1); + jobInfo.setJobDesc("autoEnquiryStatisPerWeek"); + jobInfo.setExecutorRouteStrategy("FIRST"); + jobInfo.setJobCron("0 0 1 ? * MON"); + jobInfo.setGlueType("BEAN"); + jobInfo.setExecutorHandler("AutoEnquriy"); + jobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION"); + jobInfo.setExecutorFailStrategy("FAIL_ALARM"); + jobInfo.setAuthor("listening"); + + ObjectMapper mapper = new ObjectMapper(); + String jobInfoStr = mapper.writeValueAsString(jobInfo); + MvcResult ret = mockMvc.perform( + post("/jobinfo/add") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .content(jobInfoStr) + .cookie(cookie) + ).andReturn(); + + System.out.println(ret.getResponse().getContentAsString()); + } +}