Skip to content

Commit 51c43b1

Browse files
committed
Fix GrpcClientRegistrationSpec#packageClasses
Fixes the `packageClasses` method so that it does not overwrite the other packages. If you pass in A.class and B.class (from separate packages) a string array w/ each class base package string is now returned. Fixes #361 Signed-off-by: onobc <chris.bono@gmail.com>
1 parent 0b766b0 commit 51c43b1

6 files changed

Lines changed: 124 additions & 7 deletions

File tree

spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientFactory.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,8 @@ public GrpcClientRegistrationSpec packages(String... packages) {
325325

326326
public GrpcClientRegistrationSpec packageClasses(Class<?>... packageClasses) {
327327
String[] packages = new String[packageClasses.length];
328-
for (Class<?> basePackageClass : packageClasses) {
329-
for (int i = 0; i < packageClasses.length; i++) {
330-
String basePackage = ClassUtils.getPackageName(basePackageClass);
331-
packages[i] = basePackage;
332-
}
328+
for (int i = 0; i < packageClasses.length; i++) {
329+
packages[i] = ClassUtils.getPackageName(packageClasses[i]);
333330
}
334331
return packages(packages);
335332
}

spring-grpc-core/src/test/java/org/springframework/grpc/client/GrpcClientFactoryTests.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import java.util.function.Supplier;
2323

24+
import org.assertj.core.api.InstanceOfAssertFactories;
25+
import org.junit.jupiter.api.Nested;
2426
import org.junit.jupiter.api.Test;
2527
import org.mockito.Mockito;
2628

@@ -30,14 +32,17 @@
3032
import org.springframework.context.support.StaticApplicationContext;
3133
import org.springframework.grpc.client.GrpcClientFactory.GrpcClientRegistrationSpec;
3234
import org.springframework.grpc.client.GrpcClientFactoryTests.MyProto.MyStub;
35+
import org.springframework.grpc.client.bar.Bar;
36+
import org.springframework.grpc.client.foo.ExtraFoo;
37+
import org.springframework.grpc.client.foo.Foo;
3338

3439
import io.grpc.CallOptions;
3540
import io.grpc.Channel;
3641
import io.grpc.ManagedChannel;
3742
import io.grpc.kotlin.AbstractCoroutineStub;
3843
import io.grpc.stub.AbstractStub;
3944

40-
public class GrpcClientFactoryTests {
45+
class GrpcClientFactoryTests {
4146

4247
private GrpcChannelFactory channelFactory = Mockito.mock(GrpcChannelFactory.class);
4348

@@ -114,6 +119,42 @@ void testCoroutineStubFactoryAfterDefault() {
114119
assertThat(factory.getClient("local", MyStub.class, null)).isNotNull();
115120
}
116121

122+
@Nested // GH-361
123+
class GrpcClientRegistrationSpecPackageClassesShould {
124+
125+
private GrpcClientRegistrationSpec spec = GrpcClientRegistrationSpec.of("local");
126+
127+
@Test
128+
void returnSinglePackageGivenSingleClass() {
129+
assertThat(spec.packageClasses(Foo.class)).extracting(GrpcClientRegistrationSpec::packages)
130+
.asInstanceOf(InstanceOfAssertFactories.array(String[].class))
131+
.containsExactly("org.springframework.grpc.client.foo");
132+
}
133+
134+
@Test
135+
void returnSinglePackageGivenMultipleClassesInSamePackage() {
136+
assertThat(spec.packageClasses(Foo.class, ExtraFoo.class)).extracting(GrpcClientRegistrationSpec::packages)
137+
.asInstanceOf(InstanceOfAssertFactories.array(String[].class))
138+
.containsExactly("org.springframework.grpc.client.foo");
139+
}
140+
141+
@Test
142+
void returnMultiPackagesGivenMultipleClassesInDifferentPackage() {
143+
assertThat(spec.packageClasses(Foo.class, Bar.class)).extracting(GrpcClientRegistrationSpec::packages)
144+
.asInstanceOf(InstanceOfAssertFactories.array(String[].class))
145+
.containsExactly("org.springframework.grpc.client.foo", "org.springframework.grpc.client.bar");
146+
}
147+
148+
@Test
149+
void returnMultiPackagesGivenMultipleClassesInSameAndDifferentPackage() {
150+
assertThat(spec.packageClasses(Foo.class, Bar.class, ExtraFoo.class))
151+
.extracting(GrpcClientRegistrationSpec::packages)
152+
.asInstanceOf(InstanceOfAssertFactories.array(String[].class))
153+
.containsExactly("org.springframework.grpc.client.foo", "org.springframework.grpc.client.bar");
154+
}
155+
156+
}
157+
117158
static class OtherStubFactory implements StubFactory<OtherStub> {
118159

119160
@Override
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.grpc.client.bar;
18+
19+
/**
20+
* Simple test record used to help test {@code GrpcClientRegistrationSpec} in
21+
* {@code GrpcClientFactoryTests}.
22+
*
23+
* @param msg some context that can be used to differentiate instances
24+
*/
25+
public record Bar(String msg) {
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.grpc.client.foo;
18+
19+
/**
20+
* Simple test record used to help test {@code GrpcClientRegistrationSpec} in
21+
* {@code GrpcClientFactoryTests}.
22+
*
23+
* @param msg some context that can be used to differentiate instances
24+
*/
25+
public record ExtraFoo(String msg) {
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.grpc.client.foo;
18+
19+
/**
20+
* Simple test record used to help test {@code GrpcClientRegistrationSpec} in
21+
* {@code GrpcClientFactoryTests}.
22+
*
23+
* @param msg some context that can be used to differentiate instances
24+
*/
25+
public record Foo(String msg) {
26+
}

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
<suppress files="Proto" checks=".*"/>
99
<suppress files=".*Tests" checks="HideUtilityClassConstructor" />
1010
<suppress files=".*Tests" checks="RegexpSinglelineJava" id="toLowerCaseWithoutLocale"/>
11-
<suppress files=".*Tests" checks="RegexpSinglelineJava" id="toUpperCaseWithoutLocale"/>
11+
<suppress files="GrpcClientFactoryTests" checks="RegexpSinglelineJava" id="toUpperCaseWithoutLocale"/>
12+
<suppress files="" checks="RedundantModifier" />
1213
<suppress files="[\\/]spring-grpc-docs[\\/]" checks="JavadocPackage|JavadocType|JavadocVariable|SpringDeprecatedCheck" />
1314
<suppress files="[\\/]spring-grpc-docs[\\/]" checks="SpringJavadoc" message="\@since" />
1415
<suppress files="[\\/]spring-grpc-docs[\\/].*jooq" checks="AvoidStaticImport" />

0 commit comments

Comments
 (0)