Skip to content

Commit

Permalink
add modularized TestContainersSpringContextCustomizerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Aug 7, 2024
1 parent 710cdc4 commit c9722e8
Show file tree
Hide file tree
Showing 13 changed files with 630 additions and 301 deletions.
21 changes: 11 additions & 10 deletions generators/spring-boot-v2/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,23 @@ export default class extends BaseApplicationGenerator {
return file;
}

if (sourceFile.includes('spring.factories')) {
return undefined;
}

if (sourceFile.includes('.java')) {
// Kotlint User template does not implements Persistable api. Ignore for now.
if (application.user && destinationFile.endsWith('UserCallback.java')) {
return undefined;
}

// TestContainersSpringContextCustomizerFactory uses a single template for modularized (dbs) and non-modularized (kafka, etc) templates
if (sourceFile.endsWith('TestContainersSpringContextCustomizerFactory.java')) {
// Convert *TestContainersSpringContextCustomizerFactory to TestContainersSpringContextCustomizerFactory
const adjustTestContainersSpringContextCustomizerFactoryFile = filename =>
filename.replace(
/(\w*)TestContainersSpringContextCustomizerFactory.java/,
'TestContainersSpringContextCustomizerFactory.java',
);
sourceFile = adjustTestContainersSpringContextCustomizerFactoryFile(sourceFile);
destinationFile = adjustTestContainersSpringContextCustomizerFactoryFile(destinationFile);
if (
sourceFile.endsWith('/TestContainersSpringContextCustomizerFactory.java') &&
!application.databaseTypeMongodb &&
!application.searchEngineElasticsearch &&
!application.databaseTypeCouchbase
) {
return undefined;
}

for (const fileMap of [
Expand Down
4 changes: 2 additions & 2 deletions generators/spring-boot/__snapshots__/generator.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,10 @@ exports[`SubGenerator kotlin of kotlin JHipster blueprint > run > should succeed
"src/test/kotlin/com/mycompany/myapp/config/SqlTestContainer.kt": {
"stateCleared": "modified",
},
"src/test/kotlin/com/mycompany/myapp/config/StaticResourcesWebConfigurerTest.kt": {
"src/test/kotlin/com/mycompany/myapp/config/SqlTestContainersSpringContextCustomizerFactory.kt": {
"stateCleared": "modified",
},
"src/test/kotlin/com/mycompany/myapp/config/TestContainersSpringContextCustomizerFactory.kt": {
"src/test/kotlin/com/mycompany/myapp/config/StaticResourcesWebConfigurerTest.kt": {
"stateCleared": "modified",
},
"src/test/kotlin/com/mycompany/myapp/config/WebConfigurerTest.kt": {
Expand Down
242 changes: 166 additions & 76 deletions generators/spring-boot/__snapshots__/matrix.spec.js.snap

Large diffs are not rendered by default.

19 changes: 1 addition & 18 deletions generators/spring-boot/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,11 @@ export default class extends BaseApplicationGenerator {
return undefined;
}

// TestContainersSpringContextCustomizerFactory uses a single template for modularized (dbs) and non-modularized (kafka, etc) templates
if (sourceFile.endsWith('TestContainersSpringContextCustomizerFactory.java')) {
// Convert *TestContainersSpringContextCustomizerFactory to TestContainersSpringContextCustomizerFactory
const adjustTestContainersSpringContextCustomizerFactoryFile = filename =>
filename.replace(
/(\w*)TestContainersSpringContextCustomizerFactory.java/,
'TestContainersSpringContextCustomizerFactory.java',
);
sourceFile = adjustTestContainersSpringContextCustomizerFactoryFile(sourceFile);
destinationFile = adjustTestContainersSpringContextCustomizerFactoryFile(destinationFile);
}

sourceFile = convertToKotlinFile(sourceFile);
destinationFile = convertToKotlinFile(destinationFile);
}

const isCommonFile = filename => {
const sourceBasename = basename(filename);
return ['TestContainersSpringContextCustomizerFactory.kt'].includes(sourceBasename);
};

const prefix = ns === 'jhipster:spring-boot' || isCommonFile(sourceFile) ? '' : ns.split(':').pop();
const prefix = ns === 'jhipster:spring-boot' ? '' : ns.split(':').pop();
sourceFile = join(prefix, sourceFile);
let resolvedSourceFile = this.templatePath(sourceFile);
if (!resolvedSourceFile.includes('.kt')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<%#
Copyright 2013-2024 the original author or authors from the JHipster project.
This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
package <%= packageName %>.config

import java.util.*

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
import org.springframework.core.annotation.AnnotatedElementUtils

import org.springframework.test.context.ContextConfigurationAttributes
import org.springframework.test.context.ContextCustomizer
import org.springframework.test.context.ContextCustomizerFactory
import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.boot.test.util.TestPropertyValues
import org.testcontainers.containers.KafkaContainer

class KafkaTestContainersSpringContextCustomizerFactory: ContextCustomizerFactory {

private val log = LoggerFactory.getLogger(TestContainersSpringContextCustomizerFactory::class.java)

companion object {
private var kafkaBean: KafkaTestContainer? = null
}

override fun createContextCustomizer(
testClass: Class<*>,
configAttributes: MutableList<ContextConfigurationAttributes>
): ContextCustomizer {
return ContextCustomizer { context, _ ->
val beanFactory = context.beanFactory
var testValues = TestPropertyValues.empty()
val kafkaAnnotation = AnnotatedElementUtils.findMergedAnnotation(testClass, EmbeddedKafka::class.java)
if (null != kafkaAnnotation) {
log.debug("detected the EmbeddedKafka annotation on class {}", testClass.name)
log.info("Warming up the kafka broker")
if (null == kafkaBean) {
kafkaBean = beanFactory.createBean(KafkaTestContainer::class.java)
beanFactory.registerSingleton(KafkaTestContainer::class.java.name, kafkaBean)
// (beanFactory as (DefaultListableBeanFactory)).registerDisposableBean(KafkaTestContainer::class.java.name, kafkaBean)
}
kafkaBean?.let {
testValues = testValues.and("spring.cloud.stream.kafka.binder.brokers=" + it.getKafkaContainer().host + ':' + it.getKafkaContainer().getMappedPort(KafkaContainer.KAFKA_PORT))
}
}
testValues.applyTo(context)
}
}

override fun hashCode() = KafkaTestContainersSpringContextCustomizerFactory::class.java.name.hashCode()

override fun equals(other: Any?): Boolean {
return this.hashCode() == other.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<%#
Copyright 2013-2024 the original author or authors from the JHipster project.
This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
package <%= packageName %>.config

import java.util.*

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
import org.springframework.core.annotation.AnnotatedElementUtils

import org.springframework.test.context.ContextConfigurationAttributes
import org.springframework.test.context.ContextCustomizer
import org.springframework.test.context.ContextCustomizerFactory
import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.boot.test.util.TestPropertyValues

class RedisTestContainersSpringContextCustomizerFactory: ContextCustomizerFactory {

private val log = LoggerFactory.getLogger(TestContainersSpringContextCustomizerFactory::class.java)

companion object {
private var redisBean: RedisTestContainer? = null
}

override fun createContextCustomizer(
testClass: Class<*>,
configAttributes: MutableList<ContextConfigurationAttributes>
): ContextCustomizer {
return ContextCustomizer { context, _ ->
val beanFactory = context.beanFactory
var testValues = TestPropertyValues.empty()
val redisAnnotation = AnnotatedElementUtils.findMergedAnnotation(testClass, EmbeddedRedis::class.java)
if (null != redisAnnotation) {
log.debug("detected the EmbeddedRedis annotation on class {}", testClass.name)
log.info("Warming up the redis database")
if (null == redisBean) {
redisBean = beanFactory.createBean(RedisTestContainer::class.java)
beanFactory.registerSingleton(RedisTestContainer::class.java.name, redisBean)
// (beanFactory as (DefaultListableBeanFactory)).registerDisposableBean(RedisTestContainer::class.java.name, redisBean)
}
redisBean?.let {
testValues = testValues.and("jhipster.cache.redis.server=redis://" + it.getRedisContainer()?.containerIpAddress + ":" + it.getRedisContainer()?.getMappedPort(6379))
}
}

testValues.applyTo(context)
}
}

override fun hashCode() = RedisTestContainersSpringContextCustomizerFactory::class.java.name.hashCode()

override fun equals(other: Any?): Boolean {
return this.hashCode() == other.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<%#
Copyright 2013-2024 the original author or authors from the JHipster project.
This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
package <%= packageName %>.config

import java.util.*

import org.cassandraunit.CQLDataLoader

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
import org.springframework.core.annotation.AnnotatedElementUtils

import org.springframework.test.context.ContextConfigurationAttributes
import org.springframework.test.context.ContextCustomizer
import org.springframework.test.context.ContextCustomizerFactory
import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.boot.test.util.TestPropertyValues
import org.testcontainers.containers.CassandraContainer

class CassandraTestContainersSpringContextCustomizerFactory: ContextCustomizerFactory {

private val log = LoggerFactory.getLogger(TestContainersSpringContextCustomizerFactory::class.java)

companion object {
private var cassandraBean: CassandraTestContainer? = null
}

override fun createContextCustomizer(
testClass: Class<*>,
configAttributes: MutableList<ContextConfigurationAttributes>
): ContextCustomizer {
return ContextCustomizer { context, _ ->
val beanFactory = context.beanFactory
var testValues = TestPropertyValues.empty()

val cassandraAnnotation = AnnotatedElementUtils.findMergedAnnotation(testClass, EmbeddedCassandra::class.java)
if (null != cassandraAnnotation) {
log.debug("detected the EmbeddedCassandra annotation on class {}", testClass.name)
log.info("Warming up the cassandra database")
if (null == cassandraBean) {
cassandraBean = beanFactory.createBean(CassandraTestContainer::class.java)
beanFactory.registerSingleton(CassandraTestContainer::class.java.name, cassandraBean)
// (beanFactory as (DefaultListableBeanFactory)).registerDisposableBean(CassandraTestContainer::class.java.name, cassandraBean)
}
cassandraBean?.let {
testValues = testValues.and("spring.data.cassandra.port=" + it.getCassandraContainer().getMappedPort(CassandraContainer.CQL_PORT))
.and("spring.data.cassandra.contact-points=" + it.getCassandraContainer().host)
.and("spring.data.cassandra.keyspace-name=" + CQLDataLoader.DEFAULT_KEYSPACE_NAME)
.and("spring.data.cassandra.local-datacenter=" + it.getCassandraContainer().cluster.metadata.allHosts.iterator().next().datacenter)
.and("spring.data.cassandra.cluster-name=" + it.getCassandraContainer().cluster.metadata.clusterName)
}
}

testValues.applyTo(context)
}
}

override fun hashCode() = CassandraTestContainersSpringContextCustomizerFactory::class.java.name.hashCode()

override fun equals(other: Any?): Boolean {
return this.hashCode() == other.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<%#
Copyright 2013-2024 the original author or authors from the JHipster project.
This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
package <%= packageName %>.config

import java.util.*

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry
import org.springframework.core.annotation.AnnotatedElementUtils

import org.springframework.test.context.ContextConfigurationAttributes
import org.springframework.test.context.ContextCustomizer
import org.springframework.test.context.ContextCustomizerFactory
import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.boot.test.util.TestPropertyValues

class Neo4jTestContainersSpringContextCustomizerFactory: ContextCustomizerFactory {

private val log = LoggerFactory.getLogger(TestContainersSpringContextCustomizerFactory::class.java)

companion object {
private var neo4jBean: Neo4jTestContainer? = null
}

override fun createContextCustomizer(
testClass: Class<*>,
configAttributes: MutableList<ContextConfigurationAttributes>
): ContextCustomizer {
return ContextCustomizer { context, _ ->
val beanFactory = context.beanFactory
var testValues = TestPropertyValues.empty()
val neo4jAnnotation = AnnotatedElementUtils.findMergedAnnotation(testClass, EmbeddedNeo4j::class.java)
if (null != neo4jAnnotation) {
log.debug("detected the EmbeddedNeo4j annotation on class {}", testClass.name)
log.info("Warming up the neo4j database")
if (null == neo4jBean) {
neo4jBean = beanFactory.createBean(Neo4jTestContainer::class.java)
beanFactory.registerSingleton(Neo4jTestContainer::class.java.name, neo4jBean)
// (beanFactory as (DefaultListableBeanFactory)).registerDisposableBean(Neo4jTestContainer::class.java.name, neo4jBean)
}
neo4jBean?.let {
testValues =
testValues.and(
"spring.neo4j.uri=" + it.getNeo4jContainer()?.boltUrl
)
}
}
testValues.applyTo(context)
}
}

override fun hashCode() = Neo4jTestContainersSpringContextCustomizerFactory::class.java.name.hashCode()

override fun equals(other: Any?): Boolean {
return this.hashCode() == other.hashCode();
}
}
Loading

0 comments on commit c9722e8

Please sign in to comment.