From 25cfb6ecb94d1256d81fbe7a3618bde531f48913 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Fri, 25 Sep 2020 09:18:50 +0200 Subject: [PATCH] DATAREST-1567 - Polishing. Tweaked test setups to work without the explicit component scanning. Original pull request: #382. --- .../halexplorer/HalExplorerConfiguration.java | 5 ++-- .../rest/webmvc/jpa/CorsIntegrationTests.java | 19 ++++++++++++++- .../jpa/JpaDefaultPageableWebTests.java | 23 ++++++++++--------- .../data/rest/webmvc/jpa/JpaWebTests.java | 22 +++++++++++++++--- .../webmvc/RestControllerConfiguration.java | 14 +++++------ .../RepositoryRestMvcConfiguration.java | 3 ++- .../config/RestControllerImportSelector.java | 16 +++++++++---- 7 files changed, 71 insertions(+), 31 deletions(-) diff --git a/spring-data-rest-hal-explorer/src/main/java/org/springframework/data/rest/webmvc/halexplorer/HalExplorerConfiguration.java b/spring-data-rest-hal-explorer/src/main/java/org/springframework/data/rest/webmvc/halexplorer/HalExplorerConfiguration.java index c99e0ad4d..ce92dc606 100644 --- a/spring-data-rest-hal-explorer/src/main/java/org/springframework/data/rest/webmvc/halexplorer/HalExplorerConfiguration.java +++ b/spring-data-rest-hal-explorer/src/main/java/org/springframework/data/rest/webmvc/halexplorer/HalExplorerConfiguration.java @@ -45,15 +45,14 @@ class HalExplorerConfiguration implements StaticResourceProvider { } /** - * The controller that exposes the {@link HalExplorer}. + * The controller that exposes the {@link HalExplorer}. TODO: Maybe register this one directly on mvc via + * WebMvcConfigurer.addViewCntroller(…)? * * @return never {@literal null}. * @since 3.4 */ @Bean HalExplorer halExplorer() { - - // TODO: maybe register this one directly on mvc via WebMvcConfigurer.addRedirectVieController() return new HalExplorer(); } } diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java index 046213930..24cdd5455 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2018 original author or authors. + * Copyright 2016-2020 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,6 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import org.junit.Test; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.data.rest.tests.AbstractWebIntegrationTests; import org.springframework.data.rest.webmvc.BasePathAwareController; import org.springframework.data.rest.webmvc.RepositoryRestController; @@ -46,8 +47,24 @@ import org.springframework.web.bind.annotation.RequestMethod; @ContextConfiguration public class CorsIntegrationTests extends AbstractWebIntegrationTests { + @Configuration static class CorsConfig extends JpaRepositoryConfig { + @Bean + AuthorsPdfController authorsPdfController() { + return new AuthorsPdfController(); + } + + @Bean + BooksPdfController booksPdfController() { + return new BooksPdfController(); + } + + @Bean + BooksXmlController booksXmlController() { + return new BooksXmlController(); + } + @Bean RepositoryRestConfigurer repositoryRestConfigurer() { diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaDefaultPageableWebTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaDefaultPageableWebTests.java index 3c2dd05a8..52ccf38ea 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaDefaultPageableWebTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaDefaultPageableWebTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2018 original author or authors. + * Copyright 2016-2020 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. @@ -23,16 +23,15 @@ import java.util.Collections; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.tests.AbstractWebIntegrationTests; import org.springframework.data.rest.webmvc.RepositoryRestController; import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; @@ -41,8 +40,6 @@ import org.springframework.hateoas.IanaLinkRelations; import org.springframework.hateoas.Link; import org.springframework.hateoas.LinkRelation; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @@ -52,18 +49,22 @@ import org.springframework.web.bind.annotation.ResponseBody; * * @author Mark Paluch */ -@RunWith(SpringRunner.class) -@WebAppConfiguration -@ContextConfiguration(classes = JpaDefaultPageableWebTests.Config.class) +@ContextConfiguration public class JpaDefaultPageableWebTests extends AbstractWebIntegrationTests { @Configuration @Import({ RepositoryRestMvcConfiguration.class, JpaRepositoryConfig.class }) @EnableJpaRepositories(considerNestedRepositories = true) - static class Config implements RepositoryRestConfigurer { + static class Config { - public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { - config.setDefaultPageSize(1); + @Bean + MyRestController myRestController() { + return new MyRestController(); + } + + @Bean + RepositoryRestConfigurer repositoryRestConfigurer() { + return RepositoryRestConfigurer.withConfig(config -> config.setDefaultPageSize(1)); } } diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java index f8bd24e6b..baf7345dc 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java @@ -17,25 +17,30 @@ package org.springframework.data.rest.webmvc.jpa; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; import static org.junit.Assert.*; +import static org.junit.Assert.assertThat; import static org.springframework.data.rest.webmvc.util.TestUtils.*; import static org.springframework.http.HttpHeaders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import net.minidev.json.JSONArray; + import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; -import net.minidev.json.JSONArray; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.support.GenericApplicationContext; import org.springframework.data.rest.core.mapping.ResourceMappings; import org.springframework.data.rest.tests.CommonWebTests; +import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig.BooksHtmlController; +import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig.OrdersJsonController; import org.springframework.hateoas.IanaLinkRelations; import org.springframework.hateoas.Link; import org.springframework.hateoas.LinkRelation; @@ -63,7 +68,7 @@ import com.jayway.jsonpath.JsonPath; * @author Ľubomír Varga */ @Transactional -@ContextConfiguration(classes = JpaRepositoryConfig.class) +@ContextConfiguration(classes = JpaRepositoryConfig.class, initializers = JpaWebTests.JpaContextInitializer.class) public class JpaWebTests extends CommonWebTests { private static final MediaType TEXT_URI_LIST = MediaType.valueOf("text/uri-list"); @@ -75,6 +80,17 @@ public class JpaWebTests extends CommonWebTests { ObjectMapper mapper = new ObjectMapper(); + static class JpaContextInitializer implements ApplicationContextInitializer { + + @Override + public void initialize(GenericApplicationContext ctx) { + + ctx.registerBean(AuthorsController.class); + ctx.registerBean(BooksHtmlController.class); + ctx.registerBean(OrdersJsonController.class); + } + } + /* * (non-Javadoc) * @see org.springframework.data.rest.webmvc.AbstractWebIntegrationTests#setUp() diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RestControllerConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RestControllerConfiguration.java index d9c636e5f..2eef0461f 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RestControllerConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RestControllerConfiguration.java @@ -61,7 +61,7 @@ public class RestControllerConfiguration { * @return never {@literal null}. */ @Bean - protected RepositoryController repositoryController(EntityLinks entityLinks) { + RepositoryController repositoryController(EntityLinks entityLinks) { return new RepositoryController(resourcesAssembler, repositories, entityLinks, resourceMappings); } @@ -74,7 +74,7 @@ public class RestControllerConfiguration { * @return never {@literal null}. */ @Bean - protected RepositoryEntityController repositoryEntityController(RepositoryEntityLinks entityLinks, + RepositoryEntityController repositoryEntityController(RepositoryEntityLinks entityLinks, HttpHeadersPreparer headersPreparer) { return new RepositoryEntityController(repositories, restConfiguration, entityLinks, resourcesAssembler, headersPreparer); @@ -87,7 +87,7 @@ public class RestControllerConfiguration { * @return never {@literal null}. */ @Bean - protected RepositoryPropertyReferenceController repositoryPropertyReferenceController( + RepositoryPropertyReferenceController repositoryPropertyReferenceController( RepositoryInvokerFactory repositoryInvokerFactory) { return new RepositoryPropertyReferenceController(repositories, repositoryInvokerFactory, resourcesAssembler); } @@ -101,7 +101,7 @@ public class RestControllerConfiguration { * @return never {@literal null}. */ @Bean - protected RepositorySearchController repositorySearchController(RepositoryEntityLinks entityLinks, + RepositorySearchController repositorySearchController(RepositoryEntityLinks entityLinks, HttpHeadersPreparer headersPreparer) { return new RepositorySearchController(resourcesAssembler, entityLinks, resourceMappings, headersPreparer); } @@ -113,7 +113,7 @@ public class RestControllerConfiguration { * @return never {@literal null}. */ @Bean - protected RepositorySchemaController repositorySchemaController( + RepositorySchemaController repositorySchemaController( PersistentEntityToJsonSchemaConverter jsonSchemaConverter) { return new RepositorySchemaController(jsonSchemaConverter); } @@ -125,7 +125,7 @@ public class RestControllerConfiguration { * @return never {@literal null}. */ @Bean - protected AlpsController alpsController() { + AlpsController alpsController() { return new AlpsController(restConfiguration); } @@ -135,7 +135,7 @@ public class RestControllerConfiguration { * @return never {@literal null}. */ @Bean - protected ProfileController profileController() { + ProfileController profileController() { return new ProfileController(restConfiguration, resourceMappings, repositories); } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java index 40f44d2a4..ae48d066f 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java @@ -147,7 +147,8 @@ import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; @Configuration(proxyBeanMethods = false) @EnableHypermediaSupport(type = HypermediaType.HAL) @ImportResource("classpath*:META-INF/spring-data-rest/**/*.xml") -@Import({ RestControllerImportSelector.class, SpringDataJacksonConfiguration.class, +@Import({ RestControllerImportSelector.class, // + SpringDataJacksonConfiguration.class, // EnableSpringDataWebSupport.QuerydslActivator.class }) public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebConfiguration implements BeanClassLoaderAware { diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RestControllerImportSelector.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RestControllerImportSelector.java index a289b9af7..98197cfb0 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RestControllerImportSelector.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RestControllerImportSelector.java @@ -15,6 +15,9 @@ */ package org.springframework.data.rest.webmvc.config; +import java.util.ArrayList; +import java.util.List; + import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; import org.springframework.data.rest.webmvc.RestControllerConfiguration; @@ -25,10 +28,13 @@ import org.springframework.util.ClassUtils; * present. * * @author Christoph Strobl + * @author Oliver Drotbohm * @since 3.4 */ class RestControllerImportSelector implements ImportSelector { + private static final String HAL_EXPLORER_CONFIGURATION = "org.springframework.data.rest.webmvc.halexplorer.HalExplorerConfiguration"; + /* * (non-Javadoc) * @see org.springframework.context.annotation.ImportSelector#selectImports(org.springframework.core.type.AnnotationMetadata) @@ -36,13 +42,13 @@ class RestControllerImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { - if (ClassUtils.isPresent("org.springframework.data.rest.webmvc.halexplorer.HalExplorerConfiguration", - importingClassMetadata.getClass().getClassLoader())) { + List configurations = new ArrayList<>(); + configurations.add(RestControllerConfiguration.class.getName()); - return new String[] { RestControllerConfiguration.class.getName(), - "org.springframework.data.rest.webmvc.halexplorer.HalExplorerConfiguration" }; + if (ClassUtils.isPresent(HAL_EXPLORER_CONFIGURATION, importingClassMetadata.getClass().getClassLoader())) { + configurations.add(HAL_EXPLORER_CONFIGURATION); } - return new String[] { RestControllerConfiguration.class.getName() }; + return configurations.toArray(new String[configurations.size()]); } }