1616
1717package org .springframework .boot .grpc .client .autoconfigure ;
1818
19+ import io .grpc .Codec ;
20+ import io .grpc .Compressor ;
1921import io .grpc .CompressorRegistry ;
22+ import io .grpc .Decompressor ;
2023import io .grpc .DecompressorRegistry ;
2124import org .junit .jupiter .api .Test ;
2225
2326import org .springframework .boot .autoconfigure .AutoConfigurations ;
27+ import org .springframework .boot .test .context .FilteredClassLoader ;
2428import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
2529
2630import static org .assertj .core .api .Assertions .assertThat ;
31+ import static org .mockito .BDDMockito .given ;
32+ import static org .mockito .Mockito .mock ;
2733
2834/**
2935 * Tests for {@link GrpcCodecConfiguration}.
@@ -36,13 +42,59 @@ class GrpcCodecConfigurationTests {
3642 .withConfiguration (AutoConfigurations .of (GrpcCodecConfiguration .class ));
3743
3844 @ Test
39- void testCompressorRegistryBean () {
40- this .contextRunner .run ((context ) -> assertThat (context ).hasSingleBean (CompressorRegistry .class ));
45+ void whenCodecNotOnClasspathThenAutoconfigurationSkipped () {
46+ this .contextRunner .withClassLoader (new FilteredClassLoader (Codec .class ))
47+ .run ((context ) -> assertThat (context ).doesNotHaveBean (GrpcCodecConfiguration .class ));
4148 }
4249
4350 @ Test
44- void testDecompressorRegistryBean () {
45- this .contextRunner .run ((context ) -> assertThat (context ).hasSingleBean (DecompressorRegistry .class ));
51+ void whenHasCustomCompressorRegistryDoesNotAutoConfigureBean () {
52+ CompressorRegistry customRegistry = mock ();
53+ this .contextRunner .withBean ("customCompressorRegistry" , CompressorRegistry .class , () -> customRegistry )
54+ .run ((context ) -> assertThat (context ).getBean (CompressorRegistry .class ).isSameAs (customRegistry ));
55+ }
56+
57+ @ Test
58+ void compressorRegistryAutoConfiguredAsExpected () {
59+ this .contextRunner .run ((context ) -> assertThat (context ).getBean (CompressorRegistry .class )
60+ .isSameAs (CompressorRegistry .getDefaultInstance ()));
61+ }
62+
63+ @ Test
64+ void whenCustomCompressorsThenCompressorRegistryIsNewInstance () {
65+ Compressor compressor = mock ();
66+ given (compressor .getMessageEncoding ()).willReturn ("foo" );
67+ this .contextRunner .withBean (Compressor .class , () -> compressor ).run ((context ) -> {
68+ assertThat (context ).hasSingleBean (CompressorRegistry .class );
69+ CompressorRegistry registry = context .getBean (CompressorRegistry .class );
70+ assertThat (registry ).isNotSameAs (CompressorRegistry .getDefaultInstance ());
71+ assertThat (registry .lookupCompressor ("foo" )).isSameAs (compressor );
72+ });
73+ }
74+
75+ @ Test
76+ void whenHasCustomDecompressorRegistryDoesNotAutoConfigureBean () {
77+ DecompressorRegistry customRegistry = mock ();
78+ this .contextRunner .withBean ("customDecompressorRegistry" , DecompressorRegistry .class , () -> customRegistry )
79+ .run ((context ) -> assertThat (context ).getBean (DecompressorRegistry .class ).isSameAs (customRegistry ));
80+ }
81+
82+ @ Test
83+ void decompressorRegistryAutoConfiguredAsExpected () {
84+ this .contextRunner .run ((context ) -> assertThat (context ).getBean (DecompressorRegistry .class )
85+ .isSameAs (DecompressorRegistry .getDefaultInstance ()));
86+ }
87+
88+ @ Test
89+ void whenCustomDecompressorsThenDecompressorRegistryIsNewInstance () {
90+ Decompressor decompressor = mock ();
91+ given (decompressor .getMessageEncoding ()).willReturn ("foo" );
92+ this .contextRunner .withBean (Decompressor .class , () -> decompressor ).run ((context ) -> {
93+ assertThat (context ).hasSingleBean (DecompressorRegistry .class );
94+ DecompressorRegistry registry = context .getBean (DecompressorRegistry .class );
95+ assertThat (registry ).isNotSameAs (DecompressorRegistry .getDefaultInstance ());
96+ assertThat (registry .lookupDecompressor ("foo" )).isSameAs (decompressor );
97+ });
4698 }
4799
48100}
0 commit comments