Search in sources :

Example 1 with WebAppConfiguration

use of org.springframework.test.context.web.WebAppConfiguration in project judge by zjnu-acm.

the class MockGenerator method test.

@Test
public void test() throws IOException {
    Gson gson = new Gson();
    Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
    Map<Class<?>, List<Info>> map = handlerMethods.entrySet().stream().map(entry -> new Info(entry.getValue(), entry.getKey())).collect(Collectors.groupingBy(info -> info.getHandlerMethod().getMethod().getDeclaringClass()));
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw, true);
    for (Map.Entry<Class<?>, List<Info>> entry : map.entrySet()) {
        Class<?> key = entry.getKey();
        List<Info> value = entry.getValue();
        if (!accept(key, value)) {
            continue;
        }
        value.sort(Comparator.comparing(info -> info.getHandlerMethod().getMethod(), Comparator.comparing(method -> method.getDeclaringClass().getName().replace(".", "/") + "." + method.getName() + ":" + org.springframework.asm.Type.getMethodDescriptor(method), toMethodComparator(key))));
        TestClass testClass = new TestClass(key, "@AutoConfigureMockMvc", "@RunWith(SpringRunner.class)", "@Slf4j", "@SpringBootTest(classes = " + mainClass.getSimpleName() + ".class)", "@Transactional", "@WebAppConfiguration");
        testClass.addImport(mainClass);
        testClass.addImport(AutoConfigureMockMvc.class);
        testClass.addImport(RunWith.class);
        testClass.addImport(SpringRunner.class);
        testClass.addImport(Slf4j.class);
        testClass.addImport(SpringBootTest.class);
        testClass.addImport(Transactional.class);
        testClass.addImport(WebAppConfiguration.class);
        testClass.addImport(Autowired.class);
        testClass.addField(MockMvc.class, "mvc", "@Autowired");
        for (Info info : value) {
            RequestMappingInfo requestMappingInfo = info.getRequestMappingInfo();
            Set<RequestMethod> requestMethods = requestMappingInfo.getMethodsCondition().getMethods();
            String requestMethod = requestMethods.isEmpty() ? "post" : requestMethods.iterator().next().toString().toLowerCase();
            HandlerMethod handlerMethod = info.getHandlerMethod();
            String url = gson.toJson(requestMappingInfo.getPatternsCondition().getPatterns().iterator().next());
            generate(key, requestMappingInfo, handlerMethod, url, testClass, requestMethod);
        }
        testClass.write(out);
        Path to = outputDir.resolve(key.getName().replace(".", "/") + "Test.java");
        Files.createDirectories(to.getParent());
        Files.write(to, sw.toString().replace("\t", "    ").getBytes(StandardCharsets.UTF_8));
        sw.getBuffer().setLength(0);
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) ClassPool(org.apache.ibatis.javassist.ClassPool) Spliterators(java.util.Spliterators) Autowired(org.springframework.beans.factory.annotation.Autowired) HandlerMethod(org.springframework.web.method.HandlerMethod) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Gson(com.google.gson.Gson) Locale(java.util.Locale) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) SpringRunner(org.springframework.test.context.junit4.SpringRunner) Method(java.lang.reflect.Method) Path(java.nio.file.Path) PrintWriter(java.io.PrintWriter) WebAppConfiguration(org.springframework.test.context.web.WebAppConfiguration) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) MediaType(org.springframework.http.MediaType) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Modifier(java.lang.reflect.Modifier) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) RequestHeader(org.springframework.web.bind.annotation.RequestHeader) Spliterator(java.util.Spliterator) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) MockMvc(org.springframework.test.web.servlet.MockMvc) RequestBody(org.springframework.web.bind.annotation.RequestBody) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) HttpServletRequest(javax.servlet.http.HttpServletRequest) Parameter(java.lang.reflect.Parameter) MvcResult(org.springframework.test.web.servlet.MvcResult) StreamSupport(java.util.stream.StreamSupport) Application(cn.edu.zjnu.acm.judge.Application) Files(java.nio.file.Files) StringWriter(java.io.StringWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) CtMethod(org.apache.ibatis.javassist.CtMethod) Consumer(java.util.function.Consumer) AutoConfigureMockMvc(org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc) Paths(java.nio.file.Paths) ReflectionUtils(org.springframework.util.ReflectionUtils) MultipartFile(org.springframework.web.multipart.MultipartFile) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Transactional(org.springframework.transaction.annotation.Transactional) Path(java.nio.file.Path) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Gson(com.google.gson.Gson) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) HandlerMethod(org.springframework.web.method.HandlerMethod) StringWriter(java.io.StringWriter) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) PrintWriter(java.io.PrintWriter) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

Application (cn.edu.zjnu.acm.judge.Application)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Gson (com.google.gson.Gson)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UncheckedIOException (java.io.UncheckedIOException)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 Parameter (java.lang.reflect.Parameter)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 LinkedHashMap (java.util.LinkedHashMap)1