Skip to content

Commit 46cf5db

Browse files
committed
GH-3213: Expose LeaderInitiatorFB.candidate
Fixes #3213 * Expose a `LeaderInitiatorFactoryBean.setCandidate()` for better control over candidate options * Expose a `candidate` attribute for the `<int-zk:leader-listener>` tag * Fix `dokka` task to obtain `package-list` URL from the local dir * Migrate ZK tests to JUnit 5
1 parent e9a3580 commit 46cf5db

File tree

13 files changed

+115
-93
lines changed

13 files changed

+115
-93
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ dokka {
10081008
moduleName = 'spring-integration'
10091009
externalDocumentationLink {
10101010
url = new URL("https://docs.spring.io/spring-integration/docs/$version/api/")
1011+
packageListUrl = new File(buildDir, "api/package-list").toURI().toURL()
10111012
}
10121013
externalDocumentationLink {
10131014
url = new URL('https://docs.spring.io/spring-framework/docs/current/javadoc-api/')

spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/config/LeaderInitiatorFactoryBean.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.integration.leader.event.DefaultLeaderEventPublisher;
3131
import org.springframework.integration.leader.event.LeaderEventPublisher;
3232
import org.springframework.integration.zookeeper.leader.LeaderInitiator;
33+
import org.springframework.util.Assert;
3334

3435
/**
3536
* Creates a {@link LeaderInitiator}.
@@ -71,8 +72,27 @@ public LeaderInitiatorFactoryBean setPath(String path) {
7172
return this;
7273
}
7374

75+
/**
76+
* Configure a role for {@link DefaultCandidate}.
77+
* Or this or {@link #setCandidate(Candidate)} can be configured, but not both.
78+
* @param role the role for candidate
79+
* @return this instance
80+
*/
7481
public LeaderInitiatorFactoryBean setRole(String role) {
75-
this.candidate = new DefaultCandidate(UUID.randomUUID().toString(), role);
82+
Assert.isNull(this.candidate,
83+
"Or 'role' for an internal 'DefaultCandidate' or 'candidate' option must be provided, but not both.");
84+
return setCandidate(new DefaultCandidate(UUID.randomUUID().toString(), role));
85+
}
86+
87+
/**
88+
* Configure a {@link Candidate} for leader election.
89+
* Or this or {@link #setRole(String)} can be configured, but not both.
90+
* @param candidate the {@link Candidate} to use
91+
* @return this instance
92+
* @since 5.3
93+
*/
94+
public LeaderInitiatorFactoryBean setCandidate(Candidate candidate) {
95+
this.candidate = candidate;
7696
return this;
7797
}
7898

spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/config/xml/LeaderListenerParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
4444

4545
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.AUTO_STARTUP);
4646
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.PHASE);
47+
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "candidate");
4748

4849
return builder.getBeanDefinition();
4950
}

spring-integration-zookeeper/src/main/resources/org/springframework/integration/zookeeper/config/spring-integration-zookeeper.xsd

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</xsd:annotation>
3232
<xsd:complexType>
3333
<xsd:attributeGroup ref="integration:smartLifeCycleAttributeGroup"/>
34-
<xsd:attribute name="id" type="xsd:string" use="optional"/>
34+
<xsd:attribute name="id" type="xsd:string"/>
3535
<xsd:attribute name="client" type="xsd:string">
3636
<xsd:annotation>
3737
<xsd:documentation>
@@ -44,6 +44,19 @@
4444
</xsd:appinfo>
4545
</xsd:annotation>
4646
</xsd:attribute>
47+
<xsd:attribute name="candidate" type="xsd:string">
48+
<xsd:annotation>
49+
<xsd:documentation>
50+
A reference to a Candidate bean.
51+
Or this or 'role' option can be provided, but not both.
52+
</xsd:documentation>
53+
<xsd:appinfo>
54+
<tool:annotation kind="ref">
55+
<tool:expected-type type="org.springframework.integration.leader.Candidate"/>
56+
</tool:annotation>
57+
</xsd:appinfo>
58+
</xsd:annotation>
59+
</xsd:attribute>
4760
<xsd:attribute name="path">
4861
<xsd:annotation>
4962
<xsd:documentation>

spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/ZookeeperTestSupport.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,10 +25,10 @@
2525
import org.apache.curator.retry.BoundedExponentialBackoffRetry;
2626
import org.apache.curator.test.TestingServer;
2727
import org.apache.curator.utils.CloseableUtils;
28-
import org.junit.After;
29-
import org.junit.AfterClass;
30-
import org.junit.Before;
31-
import org.junit.BeforeClass;
28+
import org.junit.jupiter.api.AfterAll;
29+
import org.junit.jupiter.api.AfterEach;
30+
import org.junit.jupiter.api.BeforeAll;
31+
import org.junit.jupiter.api.BeforeEach;
3232

3333
/**
3434
* @author Marius Bogoevici
@@ -48,12 +48,12 @@ public class ZookeeperTestSupport {
4848

4949
protected CuratorFramework client;
5050

51-
@BeforeClass
51+
@BeforeAll
5252
public static void setUpClass() throws Exception {
5353
testingServer = new TestingServer(true);
5454
}
5555

56-
@AfterClass
56+
@AfterAll
5757
public static void tearDownClass() {
5858
try {
5959
testingServer.stop();
@@ -64,12 +64,12 @@ public static void tearDownClass() {
6464
testingServer.getTempDirectory().delete();
6565
}
6666

67-
@Before
67+
@BeforeEach
6868
public void setUp() {
6969
client = createNewClient();
7070
}
7171

72-
@After
72+
@AfterEach
7373
public void tearDown() throws Exception {
7474
CloseableUtils.closeQuietly(this.client);
7575
}

spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/config/CuratorFrameworkFactoryBeanTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
2121
import org.apache.curator.framework.CuratorFramework;
2222
import org.apache.curator.framework.imps.CuratorFrameworkState;
2323
import org.apache.curator.test.TestingServer;
24-
import org.junit.Test;
24+
import org.junit.jupiter.api.Test;
2525

2626
/**
2727
* @author Gary Russell

spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/config/LeaderInitiatorFactoryBeanTests.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,14 +20,14 @@
2020

2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import java.util.UUID;
2324
import java.util.concurrent.CountDownLatch;
2425
import java.util.concurrent.TimeUnit;
2526

2627
import org.apache.curator.framework.CuratorFramework;
27-
import org.junit.AfterClass;
28-
import org.junit.BeforeClass;
29-
import org.junit.Test;
30-
import org.junit.runner.RunWith;
28+
import org.junit.jupiter.api.AfterAll;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.Test;
3131

3232
import org.springframework.beans.factory.annotation.Autowired;
3333
import org.springframework.context.ApplicationListener;
@@ -42,17 +42,16 @@
4242
import org.springframework.integration.zookeeper.ZookeeperTestSupport;
4343
import org.springframework.integration.zookeeper.leader.LeaderInitiator;
4444
import org.springframework.test.annotation.DirtiesContext;
45-
import org.springframework.test.context.ContextConfiguration;
46-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
45+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4746

4847
/**
4948
* @author Gary Russell
5049
* @author Artem Bilan
50+
*
5151
* @since 4.2
5252
*
5353
*/
54-
@ContextConfiguration
55-
@RunWith(SpringJUnit4ClassRunner.class)
54+
@SpringJUnitConfig
5655
@DirtiesContext
5756
public class LeaderInitiatorFactoryBeanTests extends ZookeeperTestSupport {
5857

@@ -64,12 +63,12 @@ public class LeaderInitiatorFactoryBeanTests extends ZookeeperTestSupport {
6463
@Autowired
6564
private Config config;
6665

67-
@BeforeClass
68-
public static void getClient() throws Exception {
66+
@BeforeAll
67+
public static void getClient() {
6968
client = createNewClient();
7069
}
7170

72-
@AfterClass
71+
@AfterAll
7372
public static void closeClient() {
7473
if (client != null) {
7574
client.close();
@@ -118,7 +117,7 @@ public void publishOnGranted(Object source, Context context, String role) {
118117
@Configuration
119118
public static class Config {
120119

121-
private final List<AbstractLeaderEvent> events = new ArrayList<AbstractLeaderEvent>();
120+
private final List<AbstractLeaderEvent> events = new ArrayList<>();
122121

123122
private final CountDownLatch latch1 = new CountDownLatch(1);
124123

@@ -129,7 +128,7 @@ public LeaderInitiatorFactoryBean leaderInitiator(CuratorFramework client) {
129128
return new LeaderInitiatorFactoryBean()
130129
.setClient(client)
131130
.setPath("/siTest/")
132-
.setRole("foo");
131+
.setCandidate(new DefaultCandidate(UUID.randomUUID().toString(), "foo"));
133132
}
134133

135134
@Bean

spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/config/xml/ZookeeperParserTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,7 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020

2121
import org.apache.curator.framework.CuratorFramework;
22-
import org.junit.Test;
23-
import org.junit.runner.RunWith;
22+
import org.junit.jupiter.api.Test;
2423

2524
import org.springframework.beans.factory.annotation.Autowired;
2625
import org.springframework.context.annotation.Bean;
@@ -29,16 +28,16 @@
2928
import org.springframework.integration.zookeeper.ZookeeperTestSupport;
3029
import org.springframework.integration.zookeeper.leader.LeaderInitiator;
3130
import org.springframework.test.annotation.DirtiesContext;
32-
import org.springframework.test.context.ContextConfiguration;
33-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3432

3533
/**
3634
* @author Gary Russell
35+
* @author Artem Bilan
36+
*
3737
* @since 4.2
3838
*
3939
*/
40-
@ContextConfiguration
41-
@RunWith(SpringJUnit4ClassRunner.class)
40+
@SpringJUnitConfig
4241
@DirtiesContext
4342
public class ZookeeperParserTests extends ZookeeperTestSupport {
4443

@@ -76,9 +75,10 @@ public void test() throws Exception {
7675
public static class Config {
7776

7877
@Bean
79-
public CuratorFramework client() throws Exception {
78+
public CuratorFramework client() {
8079
return createNewClient();
8180
}
81+
8282
}
8383

8484
}

spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/event/ZookeeperLeaderTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,12 +25,11 @@
2525
import java.util.concurrent.LinkedBlockingQueue;
2626
import java.util.concurrent.TimeUnit;
2727

28-
import org.junit.Test;
28+
import org.junit.jupiter.api.Test;
2929

3030
import org.springframework.beans.factory.BeanFactory;
3131
import org.springframework.context.ApplicationEvent;
3232
import org.springframework.context.ApplicationEventPublisher;
33-
import org.springframework.context.SmartLifecycle;
3433
import org.springframework.integration.channel.QueueChannel;
3534
import org.springframework.integration.core.MessageSource;
3635
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
@@ -48,17 +47,19 @@
4847

4948
/**
5049
* @author Gary Russell
50+
* @author Artem Bilan
51+
*
5152
* @since 4.2
5253
*
5354
*/
5455
public class ZookeeperLeaderTests extends ZookeeperTestSupport {
5556

56-
private final BlockingQueue<AbstractLeaderEvent> events = new LinkedBlockingQueue<AbstractLeaderEvent>();
57+
private final BlockingQueue<AbstractLeaderEvent> events = new LinkedBlockingQueue<>();
5758

5859
private final SourcePollingChannelAdapter adapter = buildChannelAdapter();
5960

6061
private final SmartLifecycleRoleController controller = new SmartLifecycleRoleController(
61-
Collections.singletonList("sitest"), Collections.<SmartLifecycle>singletonList(this.adapter));
62+
Collections.singletonList("sitest"), Collections.singletonList(this.adapter));
6263

6364
private final CountDownLatch yieldBarrier = new CountDownLatch(1);
6465

spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/lock/ZkLockRegistryTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,15 +27,16 @@
2727
import java.util.concurrent.atomic.AtomicBoolean;
2828
import java.util.concurrent.locks.Lock;
2929

30-
import org.junit.Test;
30+
import org.junit.jupiter.api.Test;
3131

3232
import org.springframework.integration.test.util.TestUtils;
3333
import org.springframework.integration.zookeeper.ZookeeperTestSupport;
3434
import org.springframework.messaging.MessagingException;
3535

3636
/**
3737
* @author Gary Russell
38-
* @author Artem Bilan
38+
* @author Artem Bilan\
39+
*
3940
* @since 4.2
4041
*
4142
*/
@@ -78,7 +79,7 @@ public void testLockInterruptibly() throws Exception {
7879
}
7980

8081
@Test
81-
public void testReentrantLock() throws Exception {
82+
public void testReentrantLock() {
8283
ZookeeperLockRegistry registry = new ZookeeperLockRegistry(this.client);
8384
for (int i = 0; i < 10; i++) {
8485
Lock lock1 = registry.obtain("foo");

0 commit comments

Comments
 (0)