Skip to content

Commit 23cfdf7

Browse files
authored
test: update StorageITRunner to search parent classes for annotations rather than only the current class (googleapis#3001)
1 parent 78fc076 commit 23cfdf7

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/StorageITRunner.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
import com.google.cloud.storage.it.runner.registry.Registry;
2828
import com.google.common.collect.ImmutableList;
2929
import com.google.common.collect.ImmutableSet;
30+
import java.lang.annotation.Annotation;
3031
import java.util.List;
3132
import java.util.Locale;
3233
import java.util.concurrent.locks.Lock;
3334
import java.util.concurrent.locks.ReentrantLock;
3435
import java.util.function.Function;
3536
import java.util.stream.Stream;
37+
import org.checkerframework.checker.nullness.qual.Nullable;
3638
import org.junit.ClassRule;
3739
import org.junit.runner.Description;
3840
import org.junit.runner.Runner;
@@ -125,8 +127,8 @@ private static List<Runner> computeRunners(Class<?> klass, Registry registry)
125127

126128
Parameterized parameterized = testClass.getAnnotation(Parameterized.class);
127129

128-
CrossRun crossRun = testClass.getAnnotation(CrossRun.class);
129-
SingleBackend singleBackend = testClass.getAnnotation(SingleBackend.class);
130+
CrossRun crossRun = getClassAnnotation(testClass, CrossRun.class);
131+
SingleBackend singleBackend = getClassAnnotation(testClass, SingleBackend.class);
130132
StorageITRunner.validateBackendAnnotations(crossRun, singleBackend);
131133

132134
final ImmutableList<?> parameters;
@@ -202,6 +204,19 @@ private static List<Runner> computeRunners(Class<?> klass, Registry registry)
202204
}
203205
}
204206

207+
private static <A extends Annotation> @Nullable A getClassAnnotation(
208+
TestClass testClass, Class<A> annotation) {
209+
A a = testClass.getAnnotation(annotation);
210+
if (a != null) {
211+
return a;
212+
}
213+
Class<?> parent = testClass.getJavaClass().getSuperclass();
214+
if (parent == null) {
215+
return null;
216+
}
217+
return getClassAnnotation(new TestClass(parent), annotation);
218+
}
219+
205220
private static String fmtParam(Object param) {
206221
return String.format(Locale.US, "[%s]", param.toString());
207222
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2025 Google LLC
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+
* http://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 com.google.cloud.storage.it.runner;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.storage.TransportCompatibility.Transport;
22+
import com.google.cloud.storage.it.runner.annotations.Backend;
23+
import com.google.cloud.storage.it.runner.annotations.CrossRun;
24+
import com.google.cloud.storage.it.runner.annotations.Inject;
25+
import org.junit.Test;
26+
import org.junit.experimental.runners.Enclosed;
27+
import org.junit.runner.RunWith;
28+
29+
@RunWith(Enclosed.class)
30+
public final class StorageITRunnerTest {
31+
32+
@RunWith(StorageITRunner.class)
33+
@CrossRun(
34+
backends = {Backend.PROD},
35+
transports = {Transport.HTTP, Transport.GRPC})
36+
public abstract static class Parent {
37+
@Inject public Transport transport;
38+
}
39+
40+
public static final class Child extends Parent {
41+
@Test
42+
public void transport() {
43+
assertThat(transport).isNotNull();
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)