This is an automated email from the ASF dual-hosted git repository.
zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new af07dccf87c Refactor ReflectionUtilTest (#19254)
af07dccf87c is described below
commit af07dccf87c2336dbb037a4c0fff768fe2bfe611
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jul 16 00:04:48 2022 +0800
Refactor ReflectionUtilTest (#19254)
---
.../data/pipeline/core/util/ReflectionUtil.java | 18 +-----
.../pipeline/core/util/ReflectionUtilTest.java | 64 +++++++++++-----------
2 files changed, 33 insertions(+), 49 deletions(-)
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/ReflectionUtil.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/ReflectionUtil.java
index beb9fff3f74..80b3f6b2a50 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/ReflectionUtil.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/ReflectionUtil.java
@@ -92,22 +92,8 @@ public final class ReflectionUtil {
throw new ClassCastException("field " + fieldName + " is " +
value.getClass().getName() + " can cast to " + valueClass.getName());
}
- /**
- * Get field from class.
- *
- * @param targetClass target class
- * @param fieldName field name
- * @param isDeclared is declared
- * @return {@link Field}
- * @throws NoSuchFieldException no such field exception
- */
- public static Field getField(final Class<?> targetClass, final String
fieldName, final boolean isDeclared) throws NoSuchFieldException {
- Field result;
- if (isDeclared) {
- result = targetClass.getDeclaredField(fieldName);
- } else {
- result = targetClass.getField(fieldName);
- }
+ private static Field getField(final Class<?> targetClass, final String
fieldName, final boolean isDeclared) throws NoSuchFieldException {
+ Field result = isDeclared ? targetClass.getDeclaredField(fieldName) :
targetClass.getField(fieldName);
result.setAccessible(true);
return result;
}
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/data/pipeline/core/util/ReflectionUtilTest.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/data/pipeline/core/util/ReflectionUtilTest.java
index d5461e44b02..7ecf3290cd7 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/data/pipeline/core/util/ReflectionUtilTest.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/test/java/org/apache/shardingsphere/data/pipeline/core/util/ReflectionUtilTest.java
@@ -17,54 +17,52 @@
package org.apache.shardingsphere.data.pipeline.core.util;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
import lombok.Getter;
+import lombok.NoArgsConstructor;
import lombok.Setter;
import org.junit.Test;
-public final class ReflectionUtilTest {
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+public final class ReflectionUtilTest {
+
@Test
- public void assertSetFieldValue() throws Exception {
- ReflectionSimple reflectionSimple = new ReflectionSimple();
- ReflectionUtil.setFieldValue(reflectionSimple, "name",
"sharding-sphere");
- assertThat(reflectionSimple.getName(), is("sharding-sphere"));
+ public void assertSetFieldValue() throws NoSuchFieldException,
IllegalAccessException {
+ ReflectionFixture reflectionFixture = new ReflectionFixture();
+ ReflectionUtil.setFieldValue(reflectionFixture, "value", "foo");
+ assertThat(reflectionFixture.getValue(), is("foo"));
}
-
+
@Test
- public void assertGetFieldValue() throws Exception {
- ReflectionSimple reflectionSimple = new ReflectionSimple();
- Integer age = ReflectionUtil.getFieldValue(reflectionSimple, "age",
Integer.class);
- assertThat(age, is(18));
+ public void assertGetFieldValue() throws NoSuchFieldException,
IllegalAccessException {
+ ReflectionFixture reflectionFixture = new ReflectionFixture("bar");
+ assertThat(ReflectionUtil.getFieldValue(reflectionFixture, "value",
String.class), is("bar"));
}
-
+
@Test
- public void assertGetStaticFieldValue() throws Exception {
- ReflectionSimple.setType("ReflectionSimple");
- String fieldValue =
ReflectionUtil.getStaticFieldValue(ReflectionSimple.class, "type",
String.class);
- assertThat(fieldValue, is("ReflectionSimple"));
+ public void assertGetStaticFieldValue() throws NoSuchFieldException,
IllegalAccessException {
+ assertThat(ReflectionUtil.getStaticFieldValue(ReflectionFixture.class,
"staticValue", String.class), is("static_value"));
}
-
+
@Test
- public void assertInvokeMethodAndGetFieldValue() throws Exception {
- ReflectionSimple reflectionSimple = new ReflectionSimple();
- ReflectionUtil.invokeMethod(reflectionSimple, "setName", new
Class[]{String.class}, new Object[]{"apache"});
- assertThat(reflectionSimple.getName(), is("apache"));
+ public void assertInvokeMethod() throws Exception {
+ ReflectionFixture reflectionFixture = new ReflectionFixture();
+ ReflectionUtil.invokeMethod(reflectionFixture, "setValue", new
Class[]{String.class}, new Object[]{"new_value"});
+ assertThat(reflectionFixture.getValue(), is("new_value"));
}
-
- private static class ReflectionSimple {
-
- @Setter
- private static String type;
-
+
+ @AllArgsConstructor
+ @NoArgsConstructor
+ private static final class ReflectionFixture {
+
+ @SuppressWarnings("unused")
+ private final static String staticValue = "static_value";
+
@Getter
@Setter(AccessLevel.PRIVATE)
- private String name;
-
- @Getter(AccessLevel.PRIVATE)
- private final int age = 18;
+ private String value;
}
}