2424import static org .mockito .Mockito .inOrder ;
2525import static org .mockito .Mockito .mock ;
2626
27+ import java .lang .reflect .Method ;
2728import java .time .Duration ;
29+ import java .util .Arrays ;
2830import java .util .Collection ;
2931import java .util .Collections ;
32+ import java .util .List ;
3033import java .util .Map ;
3134import java .util .concurrent .CountDownLatch ;
3235import java .util .concurrent .TimeUnit ;
36+ import java .util .stream .Collectors ;
37+ import java .util .stream .Stream ;
3338
3439import org .apache .kafka .clients .consumer .Consumer ;
3540import org .apache .kafka .clients .consumer .ConsumerRecords ;
4247import org .springframework .context .annotation .Configuration ;
4348import org .springframework .kafka .annotation .EnableKafka ;
4449import org .springframework .kafka .annotation .KafkaListener ;
50+ import org .springframework .kafka .annotation .KafkaListenerAnnotationBeanPostProcessor ;
4551import org .springframework .kafka .annotation .PartitionOffset ;
4652import org .springframework .kafka .config .ConcurrentKafkaListenerContainerFactory ;
4753import org .springframework .kafka .config .KafkaListenerEndpointRegistry ;
4854import org .springframework .kafka .core .ConsumerFactory ;
4955import org .springframework .kafka .listener .ContainerProperties .AckMode ;
56+ import org .springframework .kafka .support .TopicPartitionOffset ;
5057import org .springframework .kafka .test .utils .KafkaTestUtils ;
5158import org .springframework .test .annotation .DirtiesContext ;
5259import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
@@ -77,7 +84,7 @@ public class ManualAssignmentInitialSeekTests {
7784 */
7885 @ SuppressWarnings ("unchecked" )
7986 @ Test
80- public void discardRemainingRecordsFromPollAndSeek () throws Exception {
87+ void discardRemainingRecordsFromPollAndSeek () throws Exception {
8188 assertThat (this .config .pollLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
8289 this .registry .stop ();
8390 assertThat (this .config .closeLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
@@ -90,6 +97,29 @@ public void discardRemainingRecordsFromPollAndSeek() throws Exception {
9097 assertThat (this .config .assignments ).hasSize (3 );
9198 }
9299
100+ @ Test
101+ void parsePartitions () {
102+ TopicPartitionOffset [] topicPartitions = registry .getListenerContainer ("pp" )
103+ .getContainerProperties ()
104+ .getTopicPartitions ();
105+ List <Integer > collected = Arrays .stream (topicPartitions ).map (tp -> tp .getPartition ())
106+ .collect (Collectors .toList ());
107+ assertThat (collected ).containsExactly (0 , 1 , 2 , 3 , 4 , 5 , 7 , 10 , 11 , 12 , 13 , 14 , 15 );
108+ }
109+
110+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
111+ @ Test
112+ void parseUnitTests () throws Exception {
113+ Method parser = KafkaListenerAnnotationBeanPostProcessor .class .getDeclaredMethod ("parsePartitions" ,
114+ String .class );
115+ parser .setAccessible (true );
116+ KafkaListenerAnnotationBeanPostProcessor bpp = new KafkaListenerAnnotationBeanPostProcessor ();
117+ assertThat ((Stream <Integer >) parser .invoke (bpp , "0-2" )).containsExactly (0 , 1 , 2 );
118+ assertThat ((Stream <Integer >) parser .invoke (bpp , " 0-2 , 5" )).containsExactly (0 , 1 , 2 , 5 );
119+ assertThat ((Stream <Integer >) parser .invoke (bpp , "0-2,5-6" )).containsExactly (0 , 1 , 2 , 5 , 6 );
120+ assertThat ((Stream <Integer >) parser .invoke (bpp , "5-6,0-2,0-2" )).containsExactly (0 , 1 , 2 , 5 , 6 );
121+ }
122+
93123 @ Configuration
94124 @ EnableKafka
95125 public static class Config extends AbstractConsumerSeekAware {
@@ -111,6 +141,12 @@ public static class Config extends AbstractConsumerSeekAware {
111141 public void foo (String in ) {
112142 }
113143
144+ @ KafkaListener (id = "pp" , autoStartup = "false" ,
145+ topicPartitions = @ org .springframework .kafka .annotation .TopicPartition (topic = "foo" ,
146+ partitions = "0-5, 7, 10-15" ))
147+ public void bar (String in ) {
148+ }
149+
114150 @ SuppressWarnings ({ "rawtypes" })
115151 @ Bean
116152 public ConsumerFactory consumerFactory () {
0 commit comments