Skip to content

Commit

Permalink
Merge branch '3.0.x' into 3.1.x
Browse files Browse the repository at this point in the history
Closes gh-37594
  • Loading branch information
wilkinsona committed Sep 27, 2023
2 parents a57b19b + 4e40ff8 commit 5015327
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 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.
Expand Down Expand Up @@ -33,6 +33,7 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.aot.Neo4jManagedTypes;
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.data.neo4j.core.Neo4jOperations;
Expand Down Expand Up @@ -71,12 +72,17 @@ public Neo4jConversions neo4jConversions() {

@Bean
@ConditionalOnMissingBean
public Neo4jMappingContext neo4jMappingContext(ApplicationContext applicationContext,
Neo4jConversions neo4jConversions) throws ClassNotFoundException {
Neo4jManagedTypes neo4jManagedTypes(ApplicationContext applicationContext) throws ClassNotFoundException {
Set<Class<?>> initialEntityClasses = new EntityScanner(applicationContext).scan(Node.class,
RelationshipProperties.class);
return Neo4jManagedTypes.fromIterable(initialEntityClasses);
}

@Bean
@ConditionalOnMissingBean
public Neo4jMappingContext neo4jMappingContext(Neo4jManagedTypes managedTypes, Neo4jConversions neo4jConversions) {
Neo4jMappingContext context = new Neo4jMappingContext(neo4jConversions);
context.setInitialEntitySet(initialEntityClasses);
context.setManagedTypes(managedTypes);
return context;
}

Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.aot.Neo4jManagedTypes;
import org.springframework.data.neo4j.core.DatabaseSelection;
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
import org.springframework.data.neo4j.core.Neo4jClient;
Expand All @@ -37,6 +38,7 @@
import org.springframework.data.neo4j.core.convert.Neo4jConversions;
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.TransactionManager;
Expand Down Expand Up @@ -162,6 +164,29 @@ void shouldFilterInitialEntityScanWithKnownAnnotations() {
});
}

@Test
void shouldProvideManagedTypes() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(Neo4jManagedTypes.class);
assertThat(context.getBean(Neo4jMappingContext.class))
.extracting((mappingContext) -> ReflectionTestUtils.getField(mappingContext, "managedTypes"))
.isEqualTo(context.getBean(Neo4jManagedTypes.class));
});
}

@Test
void shouldReuseExistingManagedTypes() {
Neo4jManagedTypes managedTypes = Neo4jManagedTypes.from();
this.contextRunner.withBean("customManagedTypes", Neo4jManagedTypes.class, () -> managedTypes)
.run((context) -> {
assertThat(context).hasSingleBean(Neo4jManagedTypes.class);
assertThat(context).doesNotHaveBean("neo4jManagedTypes");
assertThat(context.getBean(Neo4jMappingContext.class))
.extracting((mappingContext) -> ReflectionTestUtils.getField(mappingContext, "managedTypes"))
.isSameAs(managedTypes);
});
}

@Configuration(proxyBeanMethods = false)
static class CustomDatabaseSelectionProviderConfiguration {

Expand Down

0 comments on commit 5015327

Please sign in to comment.