diff --git a/lifecycle/pom.xml b/lifecycle/pom.xml index e3166172b3..6432c4edf0 100644 --- a/lifecycle/pom.xml +++ b/lifecycle/pom.xml @@ -50,9 +50,5 @@ commons-codec commons-codec - - org.apache.curator - curator-test - diff --git a/spring/namespace/pom.xml b/spring/namespace/pom.xml index 53a038bb7d..48ba28d340 100644 --- a/spring/namespace/pom.xml +++ b/spring/namespace/pom.xml @@ -52,6 +52,13 @@ provided + + org.apache.shardingsphere.elasticjob + elasticjob-test-util + ${project.parent.version} + test + + org.springframework spring-context @@ -65,10 +72,6 @@ org.aspectj aspectjweaver - - org.apache.curator - curator-test - org.springframework spring-test diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/AbstractJobSpringIntegrateTest.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/AbstractJobSpringIntegrateTest.java index 0fe0e9f515..aa501154de 100644 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/AbstractJobSpringIntegrateTest.java +++ b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/AbstractJobSpringIntegrateTest.java @@ -19,15 +19,18 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry; +import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.DataflowElasticJob; import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.FooSimpleElasticJob; -import org.apache.shardingsphere.elasticjob.spring.namespace.test.AbstractZookeeperJUnitJupiterSpringContextTests; -import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.util.concurrent.TimeUnit; @@ -35,8 +38,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; +@ExtendWith(SpringExtension.class) @RequiredArgsConstructor -public abstract class AbstractJobSpringIntegrateTest extends AbstractZookeeperJUnitJupiterSpringContextTests { +public abstract class AbstractJobSpringIntegrateTest { + + private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(3181); private final String simpleJobName; @@ -45,6 +51,11 @@ public abstract class AbstractJobSpringIntegrateTest extends AbstractZookeeperJU @Autowired private CoordinatorRegistryCenter regCenter; + @BeforeAll + static void init() { + EMBED_TESTING_SERVER.start(); + } + @BeforeEach @AfterEach void reset() { diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/AbstractOneOffJobSpringIntegrateTest.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/AbstractOneOffJobSpringIntegrateTest.java index d690c6b49b..3682c0b2a2 100644 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/AbstractOneOffJobSpringIntegrateTest.java +++ b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/AbstractOneOffJobSpringIntegrateTest.java @@ -20,16 +20,19 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.impl.OneOffJobBootstrap; import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry; +import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.DataflowElasticJob; import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.FooSimpleElasticJob; -import org.apache.shardingsphere.elasticjob.spring.namespace.test.AbstractZookeeperJUnitJupiterSpringContextTests; -import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.util.concurrent.TimeUnit; @@ -37,8 +40,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; +@ExtendWith(SpringExtension.class) @RequiredArgsConstructor -public abstract class AbstractOneOffJobSpringIntegrateTest extends AbstractZookeeperJUnitJupiterSpringContextTests { +public abstract class AbstractOneOffJobSpringIntegrateTest { + + private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(3181); private final String simpleJobName; @@ -50,6 +56,11 @@ public abstract class AbstractOneOffJobSpringIntegrateTest extends AbstractZooke @Autowired private CoordinatorRegistryCenter regCenter; + @BeforeAll + static void init() { + EMBED_TESTING_SERVER.start(); + } + @BeforeEach @AfterEach void reset() { diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/JobSpringNamespaceWithRefTest.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/JobSpringNamespaceWithRefTest.java index 3443e123f2..e4cf732ee7 100644 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/JobSpringNamespaceWithRefTest.java +++ b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/JobSpringNamespaceWithRefTest.java @@ -18,15 +18,18 @@ package org.apache.shardingsphere.elasticjob.spring.namespace.job; import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry; -import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.ref.RefFooSimpleElasticJob; -import org.apache.shardingsphere.elasticjob.spring.namespace.test.AbstractZookeeperJUnitJupiterSpringContextTests; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.ref.RefFooSimpleElasticJob; +import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.util.concurrent.TimeUnit; @@ -34,14 +37,22 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; +@ExtendWith(SpringExtension.class) @ContextConfiguration(locations = "classpath:META-INF/job/withJobRef.xml") -class JobSpringNamespaceWithRefTest extends AbstractZookeeperJUnitJupiterSpringContextTests { +class JobSpringNamespaceWithRefTest { + + private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(3181); private final String simpleJobName = "simpleElasticJob_job_ref"; @Autowired private CoordinatorRegistryCenter regCenter; + @BeforeAll + static void init() { + EMBED_TESTING_SERVER.start(); + } + @BeforeEach @AfterEach void reset() { diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/JobSpringNamespaceWithTypeTest.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/JobSpringNamespaceWithTypeTest.java index e33cd687c7..7dbc902389 100644 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/JobSpringNamespaceWithTypeTest.java +++ b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/JobSpringNamespaceWithTypeTest.java @@ -18,15 +18,18 @@ package org.apache.shardingsphere.elasticjob.spring.namespace.job; import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry; -import org.apache.shardingsphere.elasticjob.spring.namespace.test.AbstractZookeeperJUnitJupiterSpringContextTests; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.util.ReflectionTestUtils; import java.util.concurrent.TimeUnit; @@ -35,8 +38,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; +@ExtendWith(SpringExtension.class) @ContextConfiguration(locations = "classpath:META-INF/job/withJobType.xml") -class JobSpringNamespaceWithTypeTest extends AbstractZookeeperJUnitJupiterSpringContextTests { +class JobSpringNamespaceWithTypeTest { + + private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(3181); private final String scriptJobName = "scriptElasticJob_job_type"; @@ -45,6 +51,11 @@ class JobSpringNamespaceWithTypeTest extends AbstractZookeeperJUnitJupiterSpring private Scheduler scheduler; + @BeforeAll + static void init() { + EMBED_TESTING_SERVER.start(); + } + @AfterEach void tearDown() { Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> assertThat(scheduler.getCurrentlyExecutingJobs().isEmpty(), is(true))); diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/OneOffJobSpringNamespaceWithRefTest.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/OneOffJobSpringNamespaceWithRefTest.java index c2e5c17d31..9d6003fd3a 100644 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/OneOffJobSpringNamespaceWithRefTest.java +++ b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/OneOffJobSpringNamespaceWithRefTest.java @@ -19,16 +19,19 @@ import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.impl.OneOffJobBootstrap; import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry; -import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.ref.RefFooSimpleElasticJob; -import org.apache.shardingsphere.elasticjob.spring.namespace.test.AbstractZookeeperJUnitJupiterSpringContextTests; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.ref.RefFooSimpleElasticJob; +import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.util.concurrent.TimeUnit; @@ -36,8 +39,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; +@ExtendWith(SpringExtension.class) @ContextConfiguration(locations = "classpath:META-INF/job/oneOffWithJobRef.xml") -class OneOffJobSpringNamespaceWithRefTest extends AbstractZookeeperJUnitJupiterSpringContextTests { +class OneOffJobSpringNamespaceWithRefTest { + + private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(3181); private final String oneOffSimpleJobName = "oneOffSimpleElasticJobRef"; @@ -47,6 +53,11 @@ class OneOffJobSpringNamespaceWithRefTest extends AbstractZookeeperJUnitJupiterS @Autowired private CoordinatorRegistryCenter regCenter; + @BeforeAll + static void init() { + EMBED_TESTING_SERVER.start(); + } + @BeforeEach @AfterEach void reset() { diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/OneOffJobSpringNamespaceWithTypeTest.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/OneOffJobSpringNamespaceWithTypeTest.java index 784cbada79..6bffa1ae08 100644 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/OneOffJobSpringNamespaceWithTypeTest.java +++ b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/job/OneOffJobSpringNamespaceWithTypeTest.java @@ -19,21 +19,27 @@ import org.apache.shardingsphere.elasticjob.kernel.api.bootstrap.impl.OneOffJobBootstrap; import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry; -import org.apache.shardingsphere.elasticjob.spring.namespace.test.AbstractZookeeperJUnitJupiterSpringContextTests; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.assertTrue; +@ExtendWith(SpringExtension.class) @ContextConfiguration(locations = "classpath:META-INF/job/oneOffWithJobType.xml") -class OneOffJobSpringNamespaceWithTypeTest extends AbstractZookeeperJUnitJupiterSpringContextTests { +class OneOffJobSpringNamespaceWithTypeTest { + + private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(3181); private final String scriptJobName = "oneOffScriptElasticJob_job_type"; @@ -43,6 +49,11 @@ class OneOffJobSpringNamespaceWithTypeTest extends AbstractZookeeperJUnitJupiter @Autowired private CoordinatorRegistryCenter regCenter; + @BeforeAll + static void init() { + EMBED_TESTING_SERVER.start(); + } + @AfterEach void tearDown() { JobRegistry.getInstance().shutdown(scriptJobName); diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/scanner/AbstractJobSpringIntegrateTest.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/scanner/AbstractJobSpringIntegrateTest.java index a28f60b4cc..8a84cb0fda 100644 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/scanner/AbstractJobSpringIntegrateTest.java +++ b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/scanner/AbstractJobSpringIntegrateTest.java @@ -19,14 +19,17 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry; -import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.annotation.AnnotationSimpleJob; -import org.apache.shardingsphere.elasticjob.spring.namespace.test.AbstractZookeeperJUnitJupiterSpringContextTests; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.apache.shardingsphere.elasticjob.spring.namespace.fixture.job.annotation.AnnotationSimpleJob; +import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.util.concurrent.TimeUnit; @@ -34,14 +37,22 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; +@ExtendWith(SpringExtension.class) @RequiredArgsConstructor -public abstract class AbstractJobSpringIntegrateTest extends AbstractZookeeperJUnitJupiterSpringContextTests { +public abstract class AbstractJobSpringIntegrateTest { + + private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(3181); private final String simpleJobName; @Autowired private CoordinatorRegistryCenter regCenter; + @BeforeAll + static void init() { + EMBED_TESTING_SERVER.start(); + } + @BeforeEach @AfterEach void reset() { diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/snapshot/SnapshotSpringNamespaceDisableTest.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/snapshot/SnapshotSpringNamespaceDisableTest.java index 126a792343..96ad3b721a 100644 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/snapshot/SnapshotSpringNamespaceDisableTest.java +++ b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/snapshot/SnapshotSpringNamespaceDisableTest.java @@ -18,16 +18,27 @@ package org.apache.shardingsphere.elasticjob.spring.namespace.snapshot; import org.apache.shardingsphere.elasticjob.kernel.internal.snapshot.SnapshotService; -import org.apache.shardingsphere.elasticjob.spring.namespace.test.AbstractZookeeperJUnitJupiterSpringContextTests; +import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertThrows; +@ExtendWith(SpringExtension.class) @ContextConfiguration(locations = "classpath:META-INF/snapshot/snapshotDisabled.xml") -class SnapshotSpringNamespaceDisableTest extends AbstractZookeeperJUnitJupiterSpringContextTests { +class SnapshotSpringNamespaceDisableTest { + + private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(3181); + + @BeforeAll + static void init() { + EMBED_TESTING_SERVER.start(); + } @Test void assertSnapshotDisable() { diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/snapshot/SnapshotSpringNamespaceEnableTest.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/snapshot/SnapshotSpringNamespaceEnableTest.java index a52e334832..d7d9a33d7c 100644 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/snapshot/SnapshotSpringNamespaceEnableTest.java +++ b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/snapshot/SnapshotSpringNamespaceEnableTest.java @@ -17,16 +17,27 @@ package org.apache.shardingsphere.elasticjob.spring.namespace.snapshot; -import org.apache.shardingsphere.elasticjob.spring.namespace.test.AbstractZookeeperJUnitJupiterSpringContextTests; +import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertNull; +@ExtendWith(SpringExtension.class) @ContextConfiguration(locations = "classpath:META-INF/snapshot/snapshotEnabled.xml") -class SnapshotSpringNamespaceEnableTest extends AbstractZookeeperJUnitJupiterSpringContextTests { +class SnapshotSpringNamespaceEnableTest { + + private static final EmbedTestingServer EMBED_TESTING_SERVER = new EmbedTestingServer(3181); + + @BeforeAll + static void init() { + EMBED_TESTING_SERVER.start(); + } @Test void assertSnapshotEnable() throws IOException { diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/test/AbstractZookeeperJUnitJupiterSpringContextTests.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/test/AbstractZookeeperJUnitJupiterSpringContextTests.java deleted file mode 100644 index bde8ca1248..0000000000 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/test/AbstractZookeeperJUnitJupiterSpringContextTests.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 - * - * http://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.apache.shardingsphere.elasticjob.spring.namespace.test; - -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.test.context.TestExecutionListeners; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -/** - * Background reference AbstractJUnit4SpringContextTests - * and spring-projects/spring-framework#29149. - * - * @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests - */ -@ExtendWith(SpringExtension.class) -@TestExecutionListeners(listeners = EmbedZookeeperTestExecutionListener.class, inheritListeners = false, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS) -public abstract class AbstractZookeeperJUnitJupiterSpringContextTests { -} diff --git a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/test/EmbedZookeeperTestExecutionListener.java b/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/test/EmbedZookeeperTestExecutionListener.java deleted file mode 100644 index f79ac9100b..0000000000 --- a/spring/namespace/src/test/java/org/apache/shardingsphere/elasticjob/spring/namespace/test/EmbedZookeeperTestExecutionListener.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 - * - * http://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.apache.shardingsphere.elasticjob.spring.namespace.test; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.imps.CuratorFrameworkState; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.test.TestingServer; -import org.apache.zookeeper.KeeperException; -import org.springframework.test.context.TestContext; -import org.springframework.test.context.support.AbstractTestExecutionListener; - -import java.io.IOException; -import java.util.Collection; -import java.util.concurrent.TimeUnit; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -@Slf4j -public final class EmbedZookeeperTestExecutionListener extends AbstractTestExecutionListener { - - private static final int PORT = 3181; - - private static volatile TestingServer testingServer; - - private static final Object INIT_LOCK = new Object(); - - @Override - public void beforeTestClass(final TestContext testContext) { - startEmbedTestingServer(); - } - - /** - * Start embed zookeeper server. - */ - private static void startEmbedTestingServer() { - if (null != testingServer) { - log.info("Embed zookeeper server already exists 1, on {}", testingServer.getConnectString()); - return; - } - log.info("Starting embed zookeeper server..."); - synchronized (INIT_LOCK) { - if (null != testingServer) { - log.info("Embed zookeeper server already exists 2, on {}", testingServer.getConnectString()); - return; - } - start0(); - waitTestingServerReady(); - } - } - - private static void start0() { - try { - testingServer = new TestingServer(PORT, true); - // CHECKSTYLE:OFF - } catch (final Exception ex) { - // CHECKSTYLE:ON - if (!isIgnoredException(ex)) { - throw new RuntimeException(ex); - } else { - log.warn("Start embed zookeeper server got exception: {}", ex.getMessage()); - } - } finally { - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - try { - testingServer.close(); - } catch (final IOException ignored) { - } - log.info("Close embed zookeeper server done"); - })); - } - } - - private static void waitTestingServerReady() { - int maxRetries = 60; - try (CuratorFramework client = buildCuratorClient()) { - client.start(); - int round = 0; - while (round < maxRetries) { - try { - if (client.getZookeeperClient().isConnected()) { - log.info("client is connected"); - break; - } - if (client.blockUntilConnected(500, TimeUnit.MILLISECONDS)) { - CuratorFrameworkState state = client.getState(); - Collection childrenKeys = client.getChildren().forPath("/"); - log.info("TestingServer connected, state={}, childrenKeys={}", state, childrenKeys); - break; - } - // CHECKSTYLE:OFF - } catch (final Exception ignored) { - // CHECKSTYLE:ON - } - ++round; - } - } - } - - private static CuratorFramework buildCuratorClient() { - CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); - int retryIntervalMilliseconds = 500; - int maxRetries = 3; - builder.connectString(getConnectionString()) - .retryPolicy(new ExponentialBackoffRetry(retryIntervalMilliseconds, maxRetries, retryIntervalMilliseconds * maxRetries)) - .namespace("test"); - builder.sessionTimeoutMs(60 * 1000); - builder.connectionTimeoutMs(500); - return builder.build(); - } - - private static boolean isIgnoredException(final Throwable cause) { - return cause instanceof KeeperException.ConnectionLossException || cause instanceof KeeperException.NoNodeException || cause instanceof KeeperException.NodeExistsException; - } - - /** - * Get the connection string. - * - * @return connection string - */ - public static String getConnectionString() { - return "localhost:" + PORT; - } -}