DATAREST-1567 - Polishing.

Tweaked test setups to work without the explicit component scanning.

Original pull request: #382.
This commit is contained in:
Oliver Drotbohm
2020-09-25 09:18:50 +02:00
parent 8ff438678d
commit 25cfb6ecb9
7 changed files with 71 additions and 31 deletions

View File

@@ -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();
}
}

View File

@@ -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() {

View File

@@ -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));
}
}

View File

@@ -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<GenericApplicationContext> {
@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()

View File

@@ -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);
}
}

View File

@@ -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 {

View File

@@ -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<String> 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()]);
}
}