Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,8 @@ public GrpcClientRegistrationSpec packages(String... packages) {

public GrpcClientRegistrationSpec packageClasses(Class<?>... packageClasses) {
String[] packages = new String[packageClasses.length];
for (Class<?> basePackageClass : packageClasses) {
for (int i = 0; i < packageClasses.length; i++) {
String basePackage = ClassUtils.getPackageName(basePackageClass);
packages[i] = basePackage;
}
for (int i = 0; i < packageClasses.length; i++) {
packages[i] = ClassUtils.getPackageName(packageClasses[i]);
}
return packages(packages);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.util.function.Supplier;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

Expand All @@ -30,14 +32,17 @@
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.grpc.client.GrpcClientFactory.GrpcClientRegistrationSpec;
import org.springframework.grpc.client.GrpcClientFactoryTests.MyProto.MyStub;
import org.springframework.grpc.client.bar.Bar;
import org.springframework.grpc.client.foo.ExtraFoo;
import org.springframework.grpc.client.foo.Foo;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.kotlin.AbstractCoroutineStub;
import io.grpc.stub.AbstractStub;

public class GrpcClientFactoryTests {
class GrpcClientFactoryTests {

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

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

@Nested // GH-361
class GrpcClientRegistrationSpecPackageClassesShould {

private GrpcClientRegistrationSpec spec = GrpcClientRegistrationSpec.of("local");

@Test
void returnSinglePackageGivenSingleClass() {
assertThat(spec.packageClasses(Foo.class)).extracting(GrpcClientRegistrationSpec::packages)
.asInstanceOf(InstanceOfAssertFactories.array(String[].class))
.containsExactly("org.springframework.grpc.client.foo");
}

@Test
void returnSinglePackageGivenMultipleClassesInSamePackage() {
assertThat(spec.packageClasses(Foo.class, ExtraFoo.class)).extracting(GrpcClientRegistrationSpec::packages)
.asInstanceOf(InstanceOfAssertFactories.array(String[].class))
.containsExactly("org.springframework.grpc.client.foo");
}

@Test
void returnMultiPackagesGivenMultipleClassesInDifferentPackage() {
assertThat(spec.packageClasses(Foo.class, Bar.class)).extracting(GrpcClientRegistrationSpec::packages)
.asInstanceOf(InstanceOfAssertFactories.array(String[].class))
.containsExactly("org.springframework.grpc.client.foo", "org.springframework.grpc.client.bar");
}

@Test
void returnMultiPackagesGivenMultipleClassesInSameAndDifferentPackage() {
assertThat(spec.packageClasses(Foo.class, Bar.class, ExtraFoo.class))
.extracting(GrpcClientRegistrationSpec::packages)
.asInstanceOf(InstanceOfAssertFactories.array(String[].class))
.containsExactly("org.springframework.grpc.client.foo", "org.springframework.grpc.client.bar");
}

}

static class OtherStubFactory implements StubFactory<OtherStub> {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2026-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.grpc.client.bar;

/**
* Simple test record used to help test {@code GrpcClientRegistrationSpec} in
* {@code GrpcClientFactoryTests}.
*
* @param msg some context that can be used to differentiate instances
*/
public record Bar(String msg) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2026-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.grpc.client.foo;

/**
* Simple test record used to help test {@code GrpcClientRegistrationSpec} in
* {@code GrpcClientFactoryTests}.
*
* @param msg some context that can be used to differentiate instances
*/
public record ExtraFoo(String msg) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2026-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.grpc.client.foo;

/**
* Simple test record used to help test {@code GrpcClientRegistrationSpec} in
* {@code GrpcClientFactoryTests}.
*
* @param msg some context that can be used to differentiate instances
*/
public record Foo(String msg) {
}
3 changes: 2 additions & 1 deletion src/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<suppress files="Proto" checks=".*"/>
<suppress files=".*Tests" checks="HideUtilityClassConstructor" />
<suppress files=".*Tests" checks="RegexpSinglelineJava" id="toLowerCaseWithoutLocale"/>
<suppress files=".*Tests" checks="RegexpSinglelineJava" id="toUpperCaseWithoutLocale"/>
<suppress files="GrpcClientFactoryTests" checks="RegexpSinglelineJava" id="toUpperCaseWithoutLocale"/>
<suppress files="" checks="RedundantModifier" />
<suppress files="[\\/]spring-grpc-docs[\\/]" checks="JavadocPackage|JavadocType|JavadocVariable|SpringDeprecatedCheck" />
<suppress files="[\\/]spring-grpc-docs[\\/]" checks="SpringJavadoc" message="\@since" />
<suppress files="[\\/]spring-grpc-docs[\\/].*jooq" checks="AvoidStaticImport" />
Expand Down
Loading