forked from jhipster/jhipster-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add modularized TestContainersSpringContextCustomizerFactory
- Loading branch information
Showing
13 changed files
with
630 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
242 changes: 166 additions & 76 deletions
242
generators/spring-boot/__snapshots__/matrix.spec.js.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...src/test/kotlin/_package_/config/KafkaTestContainersSpringContextCustomizerFactory.kt.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...src/test/kotlin/_package_/config/RedisTestContainersSpringContextCustomizerFactory.kt.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...test/kotlin/_package_/config/CassandraTestContainersSpringContextCustomizerFactory.kt.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...src/test/kotlin/_package_/config/Neo4jTestContainersSpringContextCustomizerFactory.kt.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.