diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java index 277f7701309..83f3e8092de 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package org.springframework.messaging.simp.annotation.support; import java.lang.reflect.Method; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.security.Principal; import java.util.LinkedHashMap; import java.util.Map; @@ -42,6 +41,7 @@ import org.springframework.messaging.converter.MappingJackson2MessageConverter; import org.springframework.messaging.converter.StringMessageConverter; import org.springframework.messaging.handler.DestinationPatternsMessageCondition; import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver; import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.messaging.simp.SimpMessageSendingOperations; import org.springframework.messaging.simp.SimpMessagingTemplate; @@ -53,8 +53,6 @@ import org.springframework.util.MimeType; import static org.junit.Assert.*; import static org.mockito.BDDMockito.*; -import static org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver.*; -import static org.springframework.messaging.support.MessageHeaderAccessor.*; /** * Test fixture for {@link SendToMethodReturnValueHandlerTests}. @@ -103,31 +101,31 @@ public class SendToMethodReturnValueHandlerTests { jsonMessagingTemplate.setMessageConverter(new MappingJackson2MessageConverter()); this.jsonHandler = new SendToMethodReturnValueHandler(jsonMessagingTemplate, true); - Method method = this.getClass().getDeclaredMethod("handleNoAnnotations"); + Method method = getClass().getDeclaredMethod("handleNoAnnotations"); this.noAnnotationsReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToDefaultDestination"); + method = getClass().getDeclaredMethod("handleAndSendToDefaultDestination"); this.sendToDefaultDestReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendTo"); + method = getClass().getDeclaredMethod("handleAndSendTo"); this.sendToReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToWithPlaceholders"); + method = getClass().getDeclaredMethod("handleAndSendToWithPlaceholders"); this.sendToWithPlaceholdersReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToUser"); + method = getClass().getDeclaredMethod("handleAndSendToUser"); this.sendToUserReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToUserSingleSession"); + method = getClass().getDeclaredMethod("handleAndSendToUserSingleSession"); this.sendToUserSingleSessionReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination"); + method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination"); this.sendToUserDefaultDestReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToUserDefaultDestinationSingleSession"); + method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestinationSingleSession"); this.sendToUserSingleSessionDefaultDestReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToJsonView"); + method = getClass().getDeclaredMethod("handleAndSendToJsonView"); this.jsonViewReturnType = new SynthesizingMethodParameter(method, -1); } @@ -226,7 +224,8 @@ public class SendToMethodReturnValueHandlerTests { verify(messagingTemplate).convertAndSend(eq("/topic/dest"), eq(PAYLOAD), captor.capture()); MessageHeaders messageHeaders = captor.getValue(); - SimpMessageHeaderAccessor accessor = getAccessor(messageHeaders, SimpMessageHeaderAccessor.class); + SimpMessageHeaderAccessor accessor = + MessageHeaderAccessor.getAccessor(messageHeaders, SimpMessageHeaderAccessor.class); assertNotNull(accessor); assertTrue(accessor.isMutable()); assertEquals("sess1", accessor.getSessionId()); @@ -267,7 +266,7 @@ public class SendToMethodReturnValueHandlerTests { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(); accessor.setSessionId(sessionId); accessor.setSubscriptionId("sub1"); - accessor.setHeader(DESTINATION_TEMPLATE_VARIABLES_HEADER, vars); + accessor.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars); Message message = MessageBuilder.createMessage(PAYLOAD, accessor.getMessageHeaders()); this.handler.handleReturnValue(PAYLOAD, this.sendToWithPlaceholdersReturnType, message); @@ -403,7 +402,7 @@ public class SendToMethodReturnValueHandlerTests { Message message = this.messageCaptor.getValue(); assertNotNull(message); - assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), StandardCharsets.UTF_8)); + assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), Charset.forName("UTF-8"))); } @@ -429,25 +428,6 @@ public class SendToMethodReturnValueHandlerTests { } - private static class TestUser implements Principal { - - public String getName() { - return "joe"; - } - - public boolean implies(Subject subject) { - return false; - } - } - - private static class UniqueUser extends TestUser implements DestinationUserNameProvider { - - @Override - public String getDestinationUserName() { - return "Me myself and I"; - } - } - public String handleNoAnnotations() { return PAYLOAD; } @@ -498,9 +478,32 @@ public class SendToMethodReturnValueHandlerTests { } + private static class TestUser implements Principal { + + public String getName() { + return "joe"; + } + + public boolean implies(Subject subject) { + return false; + } + } + + + private static class UniqueUser extends TestUser implements DestinationUserNameProvider { + + @Override + public String getDestinationUserName() { + return "Me myself and I"; + } + } + + private interface MyJacksonView1 {} + private interface MyJacksonView2 {} + @SuppressWarnings("unused") private static class JacksonViewBean { @@ -516,23 +519,23 @@ public class SendToMethodReturnValueHandlerTests { return withView1; } - public void setWithView1(String withView1) { + void setWithView1(String withView1) { this.withView1 = withView1; } - public String getWithView2() { + String getWithView2() { return withView2; } - public void setWithView2(String withView2) { + void setWithView2(String withView2) { this.withView2 = withView2; } - public String getWithoutView() { + String getWithoutView() { return withoutView; } - public void setWithoutView(String withoutView) { + void setWithoutView(String withoutView) { this.withoutView = withoutView; } } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandlerTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandlerTests.java index ceaa4f0f720..823887f075b 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandlerTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package org.springframework.messaging.simp.annotation.support; import java.lang.reflect.Method; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.security.Principal; import com.fasterxml.jackson.annotation.JsonView; @@ -179,7 +178,7 @@ public class SubscriptionMethodReturnValueHandlerTests { Message message = this.messageCaptor.getValue(); assertNotNull(message); - assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), StandardCharsets.UTF_8)); + assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), Charset.forName("UTF-8"))); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java index 48038763210..f5b5b4fcd1c 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -98,10 +98,7 @@ public class AsyncTests { .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); } - /** - * SPR-13079 - */ - @Test + @Test // SPR-13079 public void deferredResultWithDelayedError() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true")) .andExpect(request().asyncStarted()) @@ -126,10 +123,7 @@ public class AsyncTests { .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); } - /** - * SPR-12597 - */ - @Test + @Test // SPR-12597 public void completableFutureWithImmediateValue() throws Exception { MvcResult mvcResult = this.mockMvc.perform(get("/1").param("completableFutureWithImmediateValue", "true")) .andExpect(request().asyncStarted()) @@ -141,10 +135,7 @@ public class AsyncTests { .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); } - /** - * SPR-12735 - */ - @Test + @Test // SPR-12735 public void printAsyncResult() throws Exception { StringWriter writer = new StringWriter(); @@ -172,12 +163,9 @@ public class AsyncTests { @RequestMapping(path = "/{id}", produces = "application/json") private static class AsyncController { - private final Collection> deferredResults = - new CopyOnWriteArrayList>(); - - private final Collection> futureTasks = - new CopyOnWriteArrayList>(); + private final Collection> deferredResults = new CopyOnWriteArrayList<>(); + private final Collection> futureTasks = new CopyOnWriteArrayList<>(); @RequestMapping(params = "callable") public Callable getCallable() { @@ -186,21 +174,21 @@ public class AsyncTests { @RequestMapping(params = "deferredResult") public DeferredResult getDeferredResult() { - DeferredResult deferredResult = new DeferredResult(); + DeferredResult deferredResult = new DeferredResult<>(); this.deferredResults.add(deferredResult); return deferredResult; } @RequestMapping(params = "deferredResultWithImmediateValue") public DeferredResult getDeferredResultWithImmediateValue() { - DeferredResult deferredResult = new DeferredResult(); + DeferredResult deferredResult = new DeferredResult<>(); deferredResult.setResult(new Person("Joe")); return deferredResult; } @RequestMapping(params = "deferredResultWithDelayedError") public DeferredResult getDeferredResultWithDelayedError() { - final DeferredResult deferredResult = new DeferredResult(); + final DeferredResult deferredResult = new DeferredResult<>(); new Thread() { public void run() { try { @@ -217,14 +205,14 @@ public class AsyncTests { @RequestMapping(params = "listenableFuture") public ListenableFuture getListenableFuture() { - ListenableFutureTask futureTask = new ListenableFutureTask(() -> new Person("Joe")); + ListenableFutureTask futureTask = new ListenableFutureTask<>(() -> new Person("Joe")); this.futureTasks.add(futureTask); return futureTask; } @RequestMapping(params = "completableFutureWithImmediateValue") public CompletableFuture getCompletableFutureWithImmediateValue() { - CompletableFuture future = new CompletableFuture(); + CompletableFuture future = new CompletableFuture<>(); future.complete(new Person("Joe")); return future; } @@ -235,7 +223,7 @@ public class AsyncTests { return e.getMessage(); } - public void onMessage(String name) { + void onMessage(String name) { for (DeferredResult deferredResult : this.deferredResults) { deferredResult.setResult(new Person(name)); this.deferredResults.remove(deferredResult); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java index 8e3c347d79b..e8a7b787aaa 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Method; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -371,6 +370,7 @@ public class MvcNamespaceTests { request.setMethod("GET"); HandlerExecutionChain chain = mapping.getHandler(request); + assertNotNull(chain); assertTrue(chain.getHandler() instanceof ResourceHttpRequestHandler); MockHttpServletResponse response = new MockHttpServletResponse(); @@ -813,7 +813,7 @@ public class MvcNamespaceTests { assertNotNull(scriptTemplateConfigurer); assertEquals("render", scriptTemplateConfigurer.getRenderFunction()); assertEquals(MediaType.TEXT_PLAIN_VALUE, scriptTemplateConfigurer.getContentType()); - assertEquals(StandardCharsets.ISO_8859_1, scriptTemplateConfigurer.getCharset()); + assertEquals("ISO-8859-1", scriptTemplateConfigurer.getCharset().name()); assertEquals("classpath:", scriptTemplateConfigurer.getResourceLoaderPath()); assertFalse(scriptTemplateConfigurer.isSharedEngine()); String[] scripts = { "org/springframework/web/servlet/view/script/nashorn/render.js" }; @@ -945,18 +945,21 @@ public class MvcNamespaceTests { public @interface IsoDate { } + @NumberFormat(style = NumberFormat.Style.PERCENT) @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface PercentNumber { } + @Validated(MyGroup.class) @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface MyValid { } + @Controller public static class TestController { @@ -967,13 +970,16 @@ public class MvcNamespaceTests { private boolean recordedValidationError; @RequestMapping - public void testBind(@RequestParam @IsoDate Date date, @RequestParam(required = false) @PercentNumber Double percent, @MyValid TestBean bean, BindingResult result) { + public void testBind(@RequestParam @IsoDate Date date, + @RequestParam(required = false) @PercentNumber Double percent, + @MyValid TestBean bean, BindingResult result) { this.date = date; this.percent = percent; this.recordedValidationError = (result.getErrorCount() == 1); } } + public static class TestValidator implements Validator { boolean validatorInvoked; @@ -989,10 +995,12 @@ public class MvcNamespaceTests { } } + @Retention(RetentionPolicy.RUNTIME) public @interface MyGroup { } + private static class TestBean { @NotNull(groups = MyGroup.class) @@ -1009,6 +1017,7 @@ public class MvcNamespaceTests { } } + private static class TestMockServletContext extends MockServletContext { @Override @@ -1022,12 +1031,15 @@ public class MvcNamespaceTests { } } + public static class TestCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter { } + public static class TestDeferredResultProcessingInterceptor extends DeferredResultProcessingInterceptorAdapter { } + public static class TestPathMatcher implements PathMatcher { @Override @@ -1066,9 +1078,11 @@ public class MvcNamespaceTests { } } + public static class TestPathHelper extends UrlPathHelper { } + public static class TestCacheManager implements CacheManager { @Override diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java index e08605b99b4..21ec8e58a9f 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/script/ScriptTemplateViewTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ package org.springframework.web.servlet.view.script; import java.net.URLClassLoader; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -47,7 +47,6 @@ import org.springframework.web.servlet.DispatcherServlet; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.mockito.BDDMockito.*; -import static org.mockito.Mockito.mock; /** * Unit tests for {@link ScriptTemplateView}. @@ -56,14 +55,15 @@ import static org.mockito.Mockito.mock; */ public class ScriptTemplateViewTests { + private static final String RESOURCE_LOADER_PATH = "classpath:org/springframework/web/servlet/view/script/"; + + private ScriptTemplateView view; private ScriptTemplateConfigurer configurer; private StaticWebApplicationContext wac; - private static final String RESOURCE_LOADER_PATH = "classpath:org/springframework/web/servlet/view/script/"; - @Before public void setup() { @@ -74,16 +74,16 @@ public class ScriptTemplateViewTests { this.view.setUrl(RESOURCE_LOADER_PATH + "empty.txt"); } + @Test public void missingScriptTemplateConfig() throws Exception { try { this.view.setApplicationContext(new StaticApplicationContext()); + fail("Should have thrown ApplicationContextException"); } catch (ApplicationContextException ex) { assertTrue(ex.getMessage().contains("ScriptTemplateConfig")); - return; } - fail(); } @Test @@ -93,7 +93,7 @@ public class ScriptTemplateViewTests { this.configurer.setRenderObject("Template"); this.configurer.setRenderFunction("render"); this.configurer.setContentType(MediaType.TEXT_PLAIN_VALUE); - this.configurer.setCharset(StandardCharsets.ISO_8859_1); + this.configurer.setCharset(Charset.forName("ISO-8859-1")); this.configurer.setSharedEngine(true); DirectFieldAccessor accessor = new DirectFieldAccessor(this.view); @@ -102,7 +102,7 @@ public class ScriptTemplateViewTests { assertEquals("Template", accessor.getPropertyValue("renderObject")); assertEquals("render", accessor.getPropertyValue("renderFunction")); assertEquals(MediaType.TEXT_PLAIN_VALUE, accessor.getPropertyValue("contentType")); - assertEquals(StandardCharsets.ISO_8859_1, accessor.getPropertyValue("charset")); + assertEquals(Charset.forName("ISO-8859-1"), accessor.getPropertyValue("charset")); assertEquals(true, accessor.getPropertyValue("sharedEngine")); } @@ -119,7 +119,7 @@ public class ScriptTemplateViewTests { assertEquals("Template", accessor.getPropertyValue("renderObject")); assertEquals("render", accessor.getPropertyValue("renderFunction")); assertEquals(MediaType.TEXT_HTML_VALUE, accessor.getPropertyValue("contentType")); - assertEquals(StandardCharsets.UTF_8, accessor.getPropertyValue("charset")); + assertEquals(Charset.forName("UTF-8"), accessor.getPropertyValue("charset")); } @Test @@ -135,7 +135,7 @@ public class ScriptTemplateViewTests { DirectFieldAccessor accessor = new DirectFieldAccessor(this.view); assertNull(accessor.getPropertyValue("renderObject")); assertEquals("render", accessor.getPropertyValue("renderFunction")); - assertEquals(StandardCharsets.UTF_8, accessor.getPropertyValue("charset")); + assertEquals(Charset.forName("UTF-8"), accessor.getPropertyValue("charset")); } @Test @@ -147,11 +147,11 @@ public class ScriptTemplateViewTests { this.view.setApplicationContext(this.wac); ExecutorService executor = Executors.newFixedThreadPool(4); List> results = new ArrayList<>(); - for(int i = 0; i < iterations; i++) { + for (int i = 0; i < iterations; i++) { results.add(executor.submit(() -> view.getEngine() != null)); } assertEquals(iterations, results.size()); - for(int i = 0; i < iterations; i++) { + for (int i = 0; i < iterations; i++) { assertTrue(results.get(i).get()); } executor.shutdown(); @@ -165,7 +165,6 @@ public class ScriptTemplateViewTests { } catch (IllegalArgumentException ex) { assertThat(ex.getMessage(), containsString("instance")); - return; } } @@ -206,9 +205,7 @@ public class ScriptTemplateViewTests { } catch (IllegalArgumentException ex) { assertThat(ex.getMessage(), containsString("sharedEngine")); - return; } - fail(); } @Test @@ -223,6 +220,7 @@ public class ScriptTemplateViewTests { assertThat(Arrays.asList(urlClassLoader.getURLs()), Matchers.hasSize(1)); assertThat(Arrays.asList(urlClassLoader.getURLs()).get(0).toString(), Matchers.endsWith("org/springframework/web/servlet/view/script/")); + this.view.setResourceLoaderPath(RESOURCE_LOADER_PATH + ",classpath:org/springframework/web/servlet/view/"); classLoader = this.view.createClassLoader(); assertNotNull(classLoader); @@ -248,19 +246,19 @@ public class ScriptTemplateViewTests { this.view.render(model, request, response); assertEquals(MediaType.TEXT_HTML_VALUE + ";charset=" + - StandardCharsets.UTF_8, response.getHeader(HttpHeaders.CONTENT_TYPE)); + Charset.forName("UTF-8"), response.getHeader(HttpHeaders.CONTENT_TYPE)); response = new MockHttpServletResponse(); this.view.setContentType(MediaType.TEXT_PLAIN_VALUE); this.view.render(model, request, response); assertEquals(MediaType.TEXT_PLAIN_VALUE + ";charset=" + - StandardCharsets.UTF_8, response.getHeader(HttpHeaders.CONTENT_TYPE)); + Charset.forName("UTF-8"), response.getHeader(HttpHeaders.CONTENT_TYPE)); response = new MockHttpServletResponse(); - this.view.setCharset(StandardCharsets.ISO_8859_1); + this.view.setCharset(Charset.forName("ISO-8859-1")); this.view.render(model, request, response); assertEquals(MediaType.TEXT_PLAIN_VALUE + ";charset=" + - StandardCharsets.ISO_8859_1, response.getHeader(HttpHeaders.CONTENT_TYPE)); + Charset.forName("ISO-8859-1"), response.getHeader(HttpHeaders.CONTENT_TYPE)); }