26
26
import java .util .Collection ;
27
27
import java .util .Collections ;
28
28
import java .util .HashMap ;
29
+ import java .util .HashSet ;
29
30
import java .util .List ;
30
31
import java .util .Map ;
31
32
import java .util .Properties ;
33
+ import java .util .Set ;
32
34
import java .util .concurrent .CountDownLatch ;
33
35
import java .util .concurrent .TimeUnit ;
34
36
import java .util .stream .Collectors ;
77
79
* @author Gary Russell
78
80
* @author Kamill Sokol
79
81
* @author Elliot Kennedy
82
+ * @author Nakul Mishra
80
83
*/
81
84
public class KafkaEmbedded extends ExternalResource implements KafkaRule , InitializingBean , DisposableBean {
82
85
@@ -119,7 +122,7 @@ public class KafkaEmbedded extends ExternalResource implements KafkaRule, Initia
119
122
120
123
private final boolean controlledShutdown ;
121
124
122
- private final String [] topics ;
125
+ private final Set < String > topics ;
123
126
124
127
private final int partitionsPerTopic ;
125
128
@@ -161,10 +164,10 @@ public KafkaEmbedded(int count, boolean controlledShutdown, int partitions, Stri
161
164
this .kafkaPorts = new int [this .count ]; // random ports by default.
162
165
this .controlledShutdown = controlledShutdown ;
163
166
if (topics != null ) {
164
- this .topics = topics ;
167
+ this .topics = new HashSet <>( Arrays . asList ( topics )) ;
165
168
}
166
169
else {
167
- this .topics = new String [ 0 ] ;
170
+ this .topics = new HashSet <>() ;
168
171
}
169
172
this .partitionsPerTopic = partitions ;
170
173
}
@@ -234,19 +237,18 @@ public void before() throws Exception { //NOSONAR
234
237
this .kafkaPorts [i ] = TestUtils .boundPort (server , SecurityProtocol .PLAINTEXT );
235
238
}
236
239
}
237
- addTopics (this .topics );
240
+ createKafkaTopics (this .topics );
238
241
System .setProperty (SPRING_EMBEDDED_KAFKA_BROKERS , getBrokersAsString ());
239
242
System .setProperty (SPRING_EMBEDDED_ZOOKEEPER_CONNECT , getZookeeperConnectionString ());
240
243
}
241
244
242
245
/**
243
- * Add topics to the existing broker(s) using the configured number of partitions.
246
+ * Create topics in the existing broker(s) using the configured number of partitions.
244
247
* @param topics the topics.
245
- * @since 2.1
246
248
*/
247
- public void addTopics ( String ... topics ) {
249
+ private void createKafkaTopics ( Set < String > topics ) {
248
250
doWithAdmin (admin -> {
249
- List <NewTopic > newTopics = Arrays .stream (topics )
251
+ List <NewTopic > newTopics = topics .stream ()
250
252
.map (t -> new NewTopic (t , this .partitionsPerTopic , (short ) this .count ))
251
253
.collect (Collectors .toList ());
252
254
CreateTopicsResult createTopics = admin .createTopics (newTopics );
@@ -259,6 +261,18 @@ public void addTopics(String... topics) {
259
261
});
260
262
}
261
263
264
+
265
+ /**
266
+ * Add topics to the existing broker(s) using the configured number of partitions.
267
+ * @param topics the topics.
268
+ * @since 2.1
269
+ */
270
+ public void addTopics (String ... topics ) {
271
+ HashSet <String > set = new HashSet <>(Arrays .asList (topics ));
272
+ createKafkaTopics (set );
273
+ this .topics .addAll (set );
274
+ }
275
+
262
276
/**
263
277
* Create an {@link AdminClient} invoke the callback and reliable close the
264
278
* admin.
@@ -339,6 +353,10 @@ public void after() {
339
353
}
340
354
}
341
355
356
+ public Set <String > getTopics () {
357
+ return new HashSet <>(this .topics );
358
+ }
359
+
342
360
@ Override
343
361
public List <KafkaServer > getKafkaServers () {
344
362
return this .kafkaServers ;
@@ -456,7 +474,7 @@ public boolean isEmbedded() {
456
474
* @throws Exception an exception.
457
475
*/
458
476
public void consumeFromAllEmbeddedTopics (Consumer <?, ?> consumer ) throws Exception {
459
- consumeFromEmbeddedTopics (consumer , this .topics );
477
+ consumeFromEmbeddedTopics (consumer , this .topics . toArray ( new String [ 0 ]) );
460
478
}
461
479
462
480
/**
@@ -476,9 +494,11 @@ public void consumeFromAnEmbeddedTopic(Consumer<?, ?> consumer, String topic) th
476
494
* @throws Exception an exception.
477
495
*/
478
496
public void consumeFromEmbeddedTopics (Consumer <?, ?> consumer , String ... topics ) throws Exception {
479
- for (String topic : topics ) {
480
- assertThat (this .topics ).as ("topic '" + topic + "' is not in embedded topic list" ).contains (topic );
481
- }
497
+ HashSet <String > diff = new HashSet <>(Arrays .asList (topics ));
498
+ diff .removeAll (new HashSet <>(this .topics ));
499
+ assertThat (this .topics )
500
+ .as ("topic(s):'" + diff + "' are not in embedded topic list" )
501
+ .containsAll (new HashSet <>(Arrays .asList (topics )));
482
502
final CountDownLatch consumerLatch = new CountDownLatch (1 );
483
503
consumer .subscribe (Arrays .asList (topics ), new ConsumerRebalanceListener () {
484
504
0 commit comments