diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/AbstractBuildTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/AbstractBuildTests.java index ff3bc8b2..4acee38b 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/AbstractBuildTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/AbstractBuildTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2017 the original author or authors. + * Copyright 2016-2019 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. @@ -15,20 +15,20 @@ */ package org.springframework.statemachine.buildtests; -import org.junit.After; -import org.junit.Before; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public abstract class AbstractBuildTests { protected AnnotationConfigApplicationContext context; - @Before + @BeforeEach public void setup() { context = buildContext(); } - @After + @AfterEach public void clean() { if (context != null) { context.close(); diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ChoiceExitTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ChoiceExitTests.java index f265a8c0..ec6b4167 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ChoiceExitTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ChoiceExitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -15,7 +15,7 @@ */ package org.springframework.statemachine.buildtests; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/EndSmokeTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/EndSmokeTests.java index cf7b0513..fe2d6561 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/EndSmokeTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/EndSmokeTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -19,7 +19,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ForkJoinEntryExitTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ForkJoinEntryExitTests.java index 794e2049..a7d2aa11 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ForkJoinEntryExitTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/ForkJoinEntryExitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -15,7 +15,7 @@ */ package org.springframework.statemachine.buildtests; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/Gh737Tests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/Gh737Tests.java index 7b9ab7ca..4d2c036a 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/Gh737Tests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/Gh737Tests.java @@ -21,7 +21,7 @@ import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAl import static org.springframework.statemachine.TestUtils.doStartAndAssert; import static org.springframework.statemachine.TestUtils.resolveMachine; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.statemachine.StateMachine; diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/LinkedPseudoStatesTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/LinkedPseudoStatesTests.java index 9135fe0d..89e17295 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/LinkedPseudoStatesTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/LinkedPseudoStatesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2019 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. @@ -15,7 +15,7 @@ */ package org.springframework.statemachine.buildtests; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/LinkedRegionsTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/LinkedRegionsTests.java index 8a5efef6..1120fb3c 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/LinkedRegionsTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/LinkedRegionsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2018 the original author or authors. + * Copyright 2016-2019 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. @@ -22,7 +22,7 @@ import static org.junit.Assert.assertThat; import java.util.ArrayList; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/RedisPersistTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/RedisPersistTests.java index f6ed215e..5e0bf585 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/RedisPersistTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/RedisPersistTests.java @@ -25,8 +25,7 @@ import static org.springframework.statemachine.TestUtils.doStartAndAssert; import static org.springframework.statemachine.TestUtils.resolveFactory; import static org.springframework.statemachine.TestUtils.resolvePersister; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,7 +33,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.StateMachinePersist; -import org.springframework.statemachine.buildtests.tck.redis.RedisRule; +import org.springframework.statemachine.buildtests.tck.redis.EnabledOnRedis; import org.springframework.statemachine.config.EnableStateMachineFactory; import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; import org.springframework.statemachine.config.StateMachineFactory; @@ -45,11 +44,9 @@ import org.springframework.statemachine.data.redis.RedisStateMachinePersister; import org.springframework.statemachine.persist.RepositoryStateMachinePersist; import org.springframework.statemachine.persist.StateMachinePersister; +@EnabledOnRedis public class RedisPersistTests extends AbstractBuildTests { - @Rule - public RedisRule redisAvailableRule = new RedisRule(); - @Override protected AnnotationConfigApplicationContext buildContext() { return new AnnotationConfigApplicationContext(); diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/WaitErrorConceptTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/WaitErrorConceptTests.java index a1948fd7..43bca0b5 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/WaitErrorConceptTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/WaitErrorConceptTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -15,7 +15,7 @@ */ package org.springframework.statemachine.buildtests; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/AbstractTckTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/AbstractTckTests.java index 298d730a..7bc8ee50 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/AbstractTckTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/AbstractTckTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -17,9 +17,9 @@ package org.springframework.statemachine.buildtests.tck; import java.util.Map; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -43,13 +43,13 @@ public abstract class AbstractTckTests { protected AnnotationConfigApplicationContext context; - @Before + @BeforeEach public void setup() { cleanInternal(); context = buildContext(); } - @After + @AfterEach public void clean() { if (context != null) { context.close(); diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/EnabledOnMongoDb.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/EnabledOnMongoDb.java new file mode 100644 index 00000000..eefa248e --- /dev/null +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/EnabledOnMongoDb.java @@ -0,0 +1,31 @@ +/* + * Copyright 2019 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. + * 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 org.springframework.statemachine.buildtests.tck.mongodb; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@ExtendWith(EnabledOnMongoDbCondition.class) +public @interface EnabledOnMongoDb { +} diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/EnabledOnMongoDbCondition.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/EnabledOnMongoDbCondition.java new file mode 100644 index 00000000..1192b2d8 --- /dev/null +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/EnabledOnMongoDbCondition.java @@ -0,0 +1,41 @@ +/* + * Copyright 2019 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. + * 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 org.springframework.statemachine.buildtests.tck.mongodb; + +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled; +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled; + +import org.junit.jupiter.api.extension.ConditionEvaluationResult; +import org.junit.jupiter.api.extension.ExecutionCondition; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.springframework.util.SocketUtils; + +public class EnabledOnMongoDbCondition implements ExecutionCondition { + + static final ConditionEvaluationResult ENABLED_ON_MONGO = enabled("Mongo DB found"); + + static final ConditionEvaluationResult DISABLED_ON_MONGO = disabled("Mongo DB not found"); + + @Override + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { + try { + SocketUtils.findAvailableTcpPort(27017, 27017); + return DISABLED_ON_MONGO; + } catch (Exception e) { + } + return ENABLED_ON_MONGO; + } +} diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbJsonTckTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbJsonTckTests.java index 456000d3..f428ac05 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbJsonTckTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbJsonTckTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -15,7 +15,6 @@ */ package org.springframework.statemachine.buildtests.tck.mongodb; -import org.junit.Rule; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; @@ -49,11 +48,9 @@ import org.springframework.statemachine.data.support.StateMachineJackson2Reposit * @author Janne Valkealahti * */ +@EnabledOnMongoDb public class MongoDbJsonTckTests extends AbstractTckTests { - @Rule - public MongoDbRule MongoDbAvailableRule = new MongoDbRule(); - @Override protected void cleanInternal() { AnnotationConfigApplicationContext c = new AnnotationConfigApplicationContext(); diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbManualTckTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbManualTckTests.java index b7a04b9f..31844748 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbManualTckTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbManualTckTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -18,7 +18,6 @@ package org.springframework.statemachine.buildtests.tck.mongodb; import java.util.Arrays; import java.util.HashSet; -import org.junit.Rule; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; @@ -54,11 +53,9 @@ import org.springframework.statemachine.transition.TransitionKind; * @author Janne Valkealahti * */ +@EnabledOnMongoDb public class MongoDbManualTckTests extends AbstractTckTests { - @Rule - public MongoDbRule MongoDbAvailableRule = new MongoDbRule(); - @Override protected void cleanInternal() { AnnotationConfigApplicationContext c = new AnnotationConfigApplicationContext(); diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbRule.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbRule.java deleted file mode 100644 index 49ab50a3..00000000 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/mongodb/MongoDbRule.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-2019 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. - * 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 org.springframework.statemachine.buildtests.tck.mongodb; - -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; -import org.springframework.util.SocketUtils; - -/** - * Rule skipping tests if MongoDb is not available from localhost simply by - * checking if port can be bind. - * - * @author Janne Valkealahti - * - */ -public class MongoDbRule extends TestWatcher implements TestRule { - - @Override - public Statement apply(Statement base, Description description) { - - try { - SocketUtils.findAvailableTcpPort(27017, 27017); - return super.apply(new Statement() { - @Override - public void evaluate() throws Throwable { - } - }, Description.EMPTY); - } catch (Exception e) { - return super.apply(base, description); - } - } -} diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/EnabledOnRedis.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/EnabledOnRedis.java new file mode 100644 index 00000000..dfac6ca7 --- /dev/null +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/EnabledOnRedis.java @@ -0,0 +1,31 @@ +/* + * Copyright 2019 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. + * 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 org.springframework.statemachine.buildtests.tck.redis; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@ExtendWith(EnabledOnRedisCondition.class) +public @interface EnabledOnRedis { +} diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisRule.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/EnabledOnRedisCondition.java similarity index 57% rename from spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisRule.java rename to spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/EnabledOnRedisCondition.java index 3cf3b32b..3f9f2436 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisRule.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/EnabledOnRedisCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2019 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. @@ -15,38 +15,34 @@ */ package org.springframework.statemachine.buildtests.tck.redis; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled; +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled; + +import org.junit.jupiter.api.extension.ConditionEvaluationResult; +import org.junit.jupiter.api.extension.ExecutionCondition; +import org.junit.jupiter.api.extension.ExtensionContext; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; -/** - * Rule skipping tests if redis is not available from localhost with default settings. - * - * @author Janne Valkealahti - * - */ -public class RedisRule extends TestWatcher implements TestRule { +public class EnabledOnRedisCondition implements ExecutionCondition { + + static final ConditionEvaluationResult ENABLED_ON_REDIS = enabled("Redis found"); + + static final ConditionEvaluationResult DISABLED_ON_REDIS = disabled("Redis not found"); @Override - public Statement apply(Statement base, Description description) { + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { JedisConnectionFactory connectionFactory = null; try { connectionFactory = new JedisConnectionFactory(); connectionFactory.afterPropertiesSet(); connectionFactory.getConnection().close(); + return ENABLED_ON_REDIS; } catch (Exception e) { - return super.apply(new Statement() { - @Override - public void evaluate() throws Throwable { - } - }, Description.EMPTY); } finally { if (connectionFactory != null) { connectionFactory.destroy(); } } - return super.apply(base, description); + return DISABLED_ON_REDIS; } } diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisJsonTckTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisJsonTckTests.java index 3952cc7a..fb91fbb9 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisJsonTckTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisJsonTckTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -15,7 +15,6 @@ */ package org.springframework.statemachine.buildtests.tck.redis; -import org.junit.Rule; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; @@ -49,11 +48,9 @@ import org.springframework.statemachine.data.support.StateMachineJackson2Reposit * @author Janne Valkealahti * */ +@EnabledOnRedis public class RedisJsonTckTests extends AbstractTckTests { - @Rule - public RedisRule redisAvailableRule = new RedisRule(); - @Override protected AnnotationConfigApplicationContext buildContext() { return new AnnotationConfigApplicationContext(); diff --git a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisManualTckTests.java b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisManualTckTests.java index d8ba00fe..57ae1d18 100644 --- a/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisManualTckTests.java +++ b/spring-statemachine-build-tests/src/test/java/org/springframework/statemachine/buildtests/tck/redis/RedisManualTckTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2019 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. @@ -18,7 +18,6 @@ package org.springframework.statemachine.buildtests.tck.redis; import java.util.Arrays; import java.util.HashSet; -import org.junit.Rule; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; @@ -54,11 +53,9 @@ import org.springframework.statemachine.transition.TransitionKind; * @author Janne Valkealahti * */ +@EnabledOnRedis public class RedisManualTckTests extends AbstractTckTests { - @Rule - public RedisRule redisAvailableRule = new RedisRule(); - @Override protected void cleanInternal() { AnnotationConfigApplicationContext c = new AnnotationConfigApplicationContext(); diff --git a/spring-statemachine-data/jpa/src/test/java/org/springframework/statemachine/data/jpa/JpaRepositoryTests.java b/spring-statemachine-data/jpa/src/test/java/org/springframework/statemachine/data/jpa/JpaRepositoryTests.java index 48fda7ce..0f536ae7 100644 --- a/spring-statemachine-data/jpa/src/test/java/org/springframework/statemachine/data/jpa/JpaRepositoryTests.java +++ b/spring-statemachine-data/jpa/src/test/java/org/springframework/statemachine/data/jpa/JpaRepositoryTests.java @@ -29,7 +29,7 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; diff --git a/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/EnabledOnMongoDb.java b/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/EnabledOnMongoDb.java new file mode 100644 index 00000000..8b6f557d --- /dev/null +++ b/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/EnabledOnMongoDb.java @@ -0,0 +1,31 @@ +/* + * Copyright 2019 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. + * 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 org.springframework.statemachine.data.mongodb; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@ExtendWith(EnabledOnMongoDbCondition.class) +public @interface EnabledOnMongoDb { +} diff --git a/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/EnabledOnMongoDbCondition.java b/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/EnabledOnMongoDbCondition.java new file mode 100644 index 00000000..76512d2f --- /dev/null +++ b/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/EnabledOnMongoDbCondition.java @@ -0,0 +1,41 @@ +/* + * Copyright 2019 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. + * 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 org.springframework.statemachine.data.mongodb; + +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled; +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled; + +import org.junit.jupiter.api.extension.ConditionEvaluationResult; +import org.junit.jupiter.api.extension.ExecutionCondition; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.springframework.util.SocketUtils; + +public class EnabledOnMongoDbCondition implements ExecutionCondition { + + static final ConditionEvaluationResult ENABLED_ON_MONGO = enabled("Mongo DB found"); + + static final ConditionEvaluationResult DISABLED_ON_MONGO = disabled("Mongo DB not found"); + + @Override + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { + try { + SocketUtils.findAvailableTcpPort(27017, 27017); + return DISABLED_ON_MONGO; + } catch (Exception e) { + } + return ENABLED_ON_MONGO; + } +} diff --git a/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryTests.java b/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryTests.java index d488f6a9..5587e1e8 100644 --- a/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryTests.java +++ b/spring-statemachine-data/mongodb/src/test/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryTests.java @@ -25,8 +25,7 @@ import static org.springframework.statemachine.TestUtils.resolveMachine; import java.util.ArrayList; import java.util.List; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -50,11 +49,9 @@ import org.springframework.statemachine.transition.TransitionKind; * * @author Janne Valkealahti */ +@EnabledOnMongoDb public class MongoDbRepositoryTests extends AbstractRepositoryTests { - @Rule - public MongoDbRule MongoDbAvailableRule = new MongoDbRule(); - @Override protected void cleanInternal() { AnnotationConfigApplicationContext c = new AnnotationConfigApplicationContext(); diff --git a/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/EnabledOnRedis.java b/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/EnabledOnRedis.java new file mode 100644 index 00000000..3319aac0 --- /dev/null +++ b/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/EnabledOnRedis.java @@ -0,0 +1,31 @@ +/* + * Copyright 2019 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. + * 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 org.springframework.statemachine.data.redis; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@ExtendWith(EnabledOnRedisCondition.class) +public @interface EnabledOnRedis { +} diff --git a/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRule.java b/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/EnabledOnRedisCondition.java similarity index 56% rename from spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRule.java rename to spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/EnabledOnRedisCondition.java index 675b20c6..7fa1b4c1 100644 --- a/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRule.java +++ b/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/EnabledOnRedisCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2019 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. @@ -15,38 +15,34 @@ */ package org.springframework.statemachine.data.redis; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled; +import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled; + +import org.junit.jupiter.api.extension.ConditionEvaluationResult; +import org.junit.jupiter.api.extension.ExecutionCondition; +import org.junit.jupiter.api.extension.ExtensionContext; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; -/** - * Rule skipping tests if redis is not available from localhost with default settings. - * - * @author Janne Valkealahti - * - */ -public class RedisRule extends TestWatcher implements TestRule { +public class EnabledOnRedisCondition implements ExecutionCondition { + + static final ConditionEvaluationResult ENABLED_ON_REDIS = enabled("Redis found"); + + static final ConditionEvaluationResult DISABLED_ON_REDIS = disabled("Redis not found"); @Override - public Statement apply(Statement base, Description description) { + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { JedisConnectionFactory connectionFactory = null; try { connectionFactory = new JedisConnectionFactory(); connectionFactory.afterPropertiesSet(); connectionFactory.getConnection().close(); + return ENABLED_ON_REDIS; } catch (Exception e) { - return super.apply(new Statement() { - @Override - public void evaluate() throws Throwable { - } - }, Description.EMPTY); } finally { if (connectionFactory != null) { connectionFactory.destroy(); } } - return super.apply(base, description); + return DISABLED_ON_REDIS; } } diff --git a/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRepositoryTests.java b/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRepositoryTests.java index 5a655369..52ee867b 100644 --- a/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRepositoryTests.java +++ b/spring-statemachine-data/redis/src/test/java/org/springframework/statemachine/data/redis/RedisRepositoryTests.java @@ -25,8 +25,7 @@ import static org.springframework.statemachine.TestUtils.resolveMachine; import java.util.ArrayList; import java.util.List; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -48,11 +47,9 @@ import org.springframework.statemachine.transition.TransitionKind; * * @author Janne Valkealahti */ +@EnabledOnRedis public class RedisRepositoryTests extends AbstractRepositoryTests { - @Rule - public RedisRule redisAvailableRule = new RedisRule(); - @Override protected void cleanInternal() { AnnotationConfigApplicationContext c = new AnnotationConfigApplicationContext(); diff --git a/spring-statemachine-data/src/test/java/org/springframework/statemachine/data/AbstractRepositoryTests.java b/spring-statemachine-data/src/test/java/org/springframework/statemachine/data/AbstractRepositoryTests.java index 480ce21c..399ef552 100644 --- a/spring-statemachine-data/src/test/java/org/springframework/statemachine/data/AbstractRepositoryTests.java +++ b/spring-statemachine-data/src/test/java/org/springframework/statemachine/data/AbstractRepositoryTests.java @@ -21,14 +21,12 @@ import static org.junit.Assert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Iterator; -import java.util.List; import java.util.Map; -import java.util.function.Function; import java.util.stream.Collectors; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -60,13 +58,13 @@ public abstract class AbstractRepositoryTests { protected AnnotationConfigApplicationContext context; - @Before + @BeforeEach public void setup() { cleanInternal(); context = buildContext(); } - @After + @AfterEach public void clean() { if (context != null) { context.close(); @@ -443,18 +441,18 @@ public abstract class AbstractRepositoryTests { context.refresh(); StateMachineFactory stateMachineFactory = context.getBean(StateMachineFactory.class); StateMachine stateMachine = stateMachineFactory.getStateMachine(); - + Map> states = stateMachine.getStates().stream().collect(Collectors.toMap((State s1) -> s1.getId(), s2 -> s2)); assertEquals(2, states.size()); - + State S1 = (State) states.get("S1"); assertEquals(1, S1.getExitActions().size()); - + State S2 = (State)states.get("S2"); assertEquals(1, S2.getEntryActions().size()); assertEquals(1, S2.getStateActions().size()); } - + @Configuration public static class Config2 { diff --git a/spring-statemachine-samples/turnstilereactive/src/test/java/demo/turnstilereactive/TurnstileReactiveTests.java b/spring-statemachine-samples/turnstilereactive/src/test/java/demo/turnstilereactive/TurnstileReactiveTests.java index bf245244..b1153e95 100644 --- a/spring-statemachine-samples/turnstilereactive/src/test/java/demo/turnstilereactive/TurnstileReactiveTests.java +++ b/spring-statemachine-samples/turnstilereactive/src/test/java/demo/turnstilereactive/TurnstileReactiveTests.java @@ -17,20 +17,20 @@ package demo.turnstilereactive; import static org.hamcrest.CoreMatchers.containsString; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.http.MediaType; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.ClassMode; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.reactive.server.WebTestClient; import reactor.core.publisher.Mono; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class TurnstileReactiveTests {