This is an automated email from the ASF dual-hosted git repository.
chia7712 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/trunk by this push:
new 6d436a8f989 KAFKA-16627 Remove ClusterConfig parameter in BeforeEach
and AfterEach (#15824)
6d436a8f989 is described below
commit 6d436a8f989fde5458d030acb3d529964388f0ff
Author: Kuan-Po (Cooper) Tseng <[email protected]>
AuthorDate: Tue Apr 30 08:40:28 2024 +0800
KAFKA-16627 Remove ClusterConfig parameter in BeforeEach and AfterEach
(#15824)
Reviewers: Chia-Ping Tsai <[email protected]>
---
core/src/test/java/kafka/test/ClusterInstance.java | 3 +-
.../java/kafka/test/ClusterTestExtensionsTest.java | 25 ++---------
.../kafka/test/junit/GenericParameterResolver.java | 51 ----------------------
core/src/test/java/kafka/test/junit/README.md | 9 ++--
.../test/junit/RaftClusterInvocationContext.java | 3 +-
.../test/junit/ZkClusterInvocationContext.java | 3 +-
6 files changed, 10 insertions(+), 84 deletions(-)
diff --git a/core/src/test/java/kafka/test/ClusterInstance.java
b/core/src/test/java/kafka/test/ClusterInstance.java
index 0c518a99921..c04f9ec08db 100644
--- a/core/src/test/java/kafka/test/ClusterInstance.java
+++ b/core/src/test/java/kafka/test/ClusterInstance.java
@@ -46,8 +46,7 @@ public interface ClusterInstance {
}
/**
- * The cluster configuration used to create this cluster. Changing data in
this instance through this accessor will
- * have no effect on the cluster since it is already provisioned.
+ * The immutable cluster configuration used to create this cluster.
*/
ClusterConfig config();
diff --git a/core/src/test/java/kafka/test/ClusterTestExtensionsTest.java
b/core/src/test/java/kafka/test/ClusterTestExtensionsTest.java
index 77877eabaf2..ada1d1b21b4 100644
--- a/core/src/test/java/kafka/test/ClusterTestExtensionsTest.java
+++ b/core/src/test/java/kafka/test/ClusterTestExtensionsTest.java
@@ -26,9 +26,7 @@ import kafka.test.annotation.ClusterTests;
import kafka.test.annotation.Type;
import kafka.test.junit.ClusterTestExtensions;
import org.apache.kafka.server.common.MetadataVersion;
-import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.HashMap;
@@ -42,11 +40,9 @@ import java.util.Map;
public class ClusterTestExtensionsTest {
private final ClusterInstance clusterInstance;
- private final ClusterConfig config;
- ClusterTestExtensionsTest(ClusterInstance clusterInstance, ClusterConfig
config) { // Constructor injections
+ ClusterTestExtensionsTest(ClusterInstance clusterInstance) { //
Constructor injections
this.clusterInstance = clusterInstance;
- this.config = config;
}
// Static methods can generate cluster configurations
@@ -59,22 +55,9 @@ public class ClusterTestExtensionsTest {
.build());
}
- // BeforeEach run after class construction, but before cluster
initialization and test invocation
- @BeforeEach
- public void beforeEach(ClusterConfig config) {
- Assertions.assertSame(this.config, config, "Injected objects should be
the same");
- }
-
- // AfterEach runs after test invocation and cluster teardown
- @AfterEach
- public void afterEach(ClusterConfig config) {
- Assertions.assertSame(this.config, config, "Injected objects should be
the same");
- }
-
// With no params, configuration comes from the annotation defaults as
well as @ClusterTestDefaults (if present)
@ClusterTest
- public void testClusterTest(ClusterConfig config, ClusterInstance
clusterInstance) {
- Assertions.assertSame(this.config, config, "Injected objects should be
the same");
+ public void testClusterTest(ClusterInstance clusterInstance) {
Assertions.assertSame(this.clusterInstance, clusterInstance, "Injected
objects should be the same");
Assertions.assertEquals(ClusterInstance.ClusterType.ZK,
clusterInstance.clusterType()); // From the class level default
Assertions.assertEquals("default.value",
clusterInstance.config().serverProperties().get("default.key"));
@@ -129,7 +112,7 @@ public class ClusterTestExtensionsTest {
}
@ClusterTest
- public void testDefaults(ClusterConfig config) {
- Assertions.assertEquals(MetadataVersion.IBP_3_8_IV0,
config.metadataVersion());
+ public void testDefaults(ClusterInstance clusterInstance) {
+ Assertions.assertEquals(MetadataVersion.IBP_3_8_IV0,
clusterInstance.config().metadataVersion());
}
}
diff --git a/core/src/test/java/kafka/test/junit/GenericParameterResolver.java
b/core/src/test/java/kafka/test/junit/GenericParameterResolver.java
deleted file mode 100644
index 70387e1680d..00000000000
--- a/core/src/test/java/kafka/test/junit/GenericParameterResolver.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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
- *
- * http://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 kafka.test.junit;
-
-import org.junit.jupiter.api.extension.ExtensionContext;
-import org.junit.jupiter.api.extension.ParameterContext;
-import org.junit.jupiter.api.extension.ParameterResolver;
-
-/**
- * This resolver is used for supplying any type of object to the test
invocation. It does not restrict where the given
- * type can be injected, it simply checks if the requested injection type
matches the type given in the constructor. If
- * it matches, the given object is returned.
- *
- * This is useful for injecting helper objects and objects which can be fully
initialized before the test lifecycle
- * begins.
- */
-public class GenericParameterResolver<T> implements ParameterResolver {
-
- private final T instance;
- private final Class<T> clazz;
-
- GenericParameterResolver(T instance, Class<T> clazz) {
- this.instance = instance;
- this.clazz = clazz;
- }
-
- @Override
- public boolean supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) {
- return parameterContext.getParameter().getType().equals(clazz);
- }
-
- @Override
- public Object resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) {
- return instance;
- }
-}
diff --git a/core/src/test/java/kafka/test/junit/README.md
b/core/src/test/java/kafka/test/junit/README.md
index 6973ca78ed3..523c585f47d 100644
--- a/core/src/test/java/kafka/test/junit/README.md
+++ b/core/src/test/java/kafka/test/junit/README.md
@@ -102,21 +102,18 @@ For each generated invocation:
# Dependency Injection
-A few classes are introduced to provide context to the underlying cluster and
to provide reusable functionality that was
+The class is introduced to provide context to the underlying cluster and to
provide reusable functionality that was
previously garnered from the test hierarchy.
-* ClusterConfig: a mutable cluster configuration, includes cluster type,
number of brokers, properties, etc
* ClusterInstance: a shim to the underlying class that actually runs the
cluster, provides access to things like SocketServers
-In order to have one of these objects injected, simply add it as a parameter
to your test class, `@BeforeEach` method, or test method.
+In order to inject the object, simply add it as a parameter to your test
class, `@BeforeEach` method, or test method.
| Injection | Class | BeforeEach | Test | Notes
| --- | --- | --- | --- | --- |
-| ClusterConfig | yes | yes | yes* | Once in the test, changing config has no
effect |
| ClusterInstance | yes* | no | yes | Injectable at class level for
convenience, can only be accessed inside test |
-
# Gotchas
* Test methods annotated with JUnit's `@Test` will still be run, but no
cluster will be started and no dependency
injection will happen. This is generally not what you want.
-* Even though ClusterConfig is accessible and mutable inside the test method,
changing it will have no effect on the cluster.
\ No newline at end of file
+* Even though ClusterConfig is accessible, it is immutable inside the test
method.
diff --git
a/core/src/test/java/kafka/test/junit/RaftClusterInvocationContext.java
b/core/src/test/java/kafka/test/junit/RaftClusterInvocationContext.java
index 294863232dd..b2f040a4907 100644
--- a/core/src/test/java/kafka/test/junit/RaftClusterInvocationContext.java
+++ b/core/src/test/java/kafka/test/junit/RaftClusterInvocationContext.java
@@ -118,8 +118,7 @@ public class RaftClusterInvocationContext implements
TestTemplateInvocationConte
}
},
(AfterTestExecutionCallback) context -> clusterInstance.stop(),
- new ClusterInstanceParameterResolver(clusterInstance),
- new GenericParameterResolver<>(clusterConfig, ClusterConfig.class)
+ new ClusterInstanceParameterResolver(clusterInstance)
);
}
diff --git
a/core/src/test/java/kafka/test/junit/ZkClusterInvocationContext.java
b/core/src/test/java/kafka/test/junit/ZkClusterInvocationContext.java
index b986f5e36d2..eb88f95a885 100644
--- a/core/src/test/java/kafka/test/junit/ZkClusterInvocationContext.java
+++ b/core/src/test/java/kafka/test/junit/ZkClusterInvocationContext.java
@@ -105,8 +105,7 @@ public class ZkClusterInvocationContext implements
TestTemplateInvocationContext
}
},
(AfterTestExecutionCallback) context -> clusterShim.stop(),
- new ClusterInstanceParameterResolver(clusterShim),
- new GenericParameterResolver<>(clusterConfig, ClusterConfig.class)
+ new ClusterInstanceParameterResolver(clusterShim)
);
}