This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 b41e3acb948 Add more test cases for ReflectionUtil (#23598)
b41e3acb948 is described below

commit b41e3acb948de25f80d073a7ac8a54c2c2ab4228
Author: xuup <[email protected]>
AuthorDate: Tue Jan 17 15:38:32 2023 +0800

    Add more test cases for ReflectionUtil (#23598)
---
 .../infra/util/reflection/ReflectionUtilTest.java  | 18 ++++++++++++
 .../util/reflection/fixture/ReflectionFixture.java | 34 ++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git 
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflection/ReflectionUtilTest.java
 
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflection/ReflectionUtilTest.java
index 55e08522d30..597c058a217 100644
--- 
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflection/ReflectionUtilTest.java
+++ 
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflection/ReflectionUtilTest.java
@@ -36,4 +36,22 @@ public final class ReflectionUtilTest {
         assertThat(ReflectionFixture.getStaticValue(), is("bar"));
         ReflectionUtil.setStaticFieldValue(ReflectionFixture.class, 
"staticValue", "foo");
     }
+    
+    @Test
+    public void assertGetFieldValue() {
+        ReflectionFixture reflectionFixture = new ReflectionFixture();
+        assertThat(ReflectionUtil.getFieldValue(reflectionFixture, 
"fooField").get(), is("foo_value"));
+        assertThat(ReflectionUtil.getFieldValue(reflectionFixture, 
"barField").get(), is("bar_value"));
+        assertThat(ReflectionUtil.getFieldValue(new ReflectionFixture(), 
"foo_field").isPresent(), is(false));
+    }
+    
+    @Test
+    public void assertInvokeMethod() throws NoSuchMethodException {
+        ReflectionFixture reflectionFixture = new ReflectionFixture();
+        
assertThat(ReflectionUtil.invokeMethod(reflectionFixture.getClass().getDeclaredMethod("getFooField"),
 reflectionFixture), is("foo_value"));
+        
assertThat(ReflectionUtil.invokeMethod(reflectionFixture.getClass().getDeclaredMethod("getBarField"),
 reflectionFixture), is("bar_value"));
+        assertThat(ReflectionUtil.invokeMethod(
+                
reflectionFixture.getClass().getDeclaredMethod("getContactValue", String.class, 
String.class),
+                reflectionFixture, "foo", "bar"), is("foo_bar"));
+    }
 }
diff --git 
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflection/fixture/ReflectionFixture.java
 
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflection/fixture/ReflectionFixture.java
index f283439c5d0..bf00ae6db60 100644
--- 
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflection/fixture/ReflectionFixture.java
+++ 
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflection/fixture/ReflectionFixture.java
@@ -22,6 +22,10 @@ public final class ReflectionFixture {
     @SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
     private static String staticValue = "foo";
     
+    private String fooField = "foo_value";
+    
+    private String barField = "bar_value";
+    
     /**
      * Get static value.
      * 
@@ -30,4 +34,34 @@ public final class ReflectionFixture {
     public static String getStaticValue() {
         return staticValue;
     }
+    
+    /**
+     * Get fooField value.
+     *
+     * @return fooField value
+     */
+    public String getFooField() {
+        return fooField;
+    }
+    
+    /**
+     * Get barField value.
+     *
+     * @return barField value
+     */
+    public String getBarField() {
+        return barField;
+    }
+    
+    /**
+     * Get contact value.
+     *
+     * @param val1 not null
+     * @param val2 not null
+     * @return contact value
+     */
+    public String getContactValue(final String val1, final String val2) {
+        return String.join("_", val1, val2);
+    }
+    
 }

Reply via email to