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 026e2fd65e4 Add more test cases on PostgreSQLErrorPacketFactoryTest 
(#37916)
026e2fd65e4 is described below

commit 026e2fd65e4deb31ac98b09a394a637987c0a086
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jan 31 23:52:01 2026 +0800

    Add more test cases on PostgreSQLErrorPacketFactoryTest (#37916)
---
 .../err/PostgreSQLErrorPacketFactoryTest.java      | 34 +++++++++++++++++++---
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git 
a/proxy/frontend/dialect/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactoryTest.java
 
b/proxy/frontend/dialect/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactoryTest.java
index 006c128e20f..a761c7aad6d 100644
--- 
a/proxy/frontend/dialect/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactoryTest.java
+++ 
b/proxy/frontend/dialect/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactoryTest.java
@@ -17,6 +17,8 @@
 
 package org.apache.shardingsphere.proxy.frontend.postgresql.err;
 
+import 
org.apache.shardingsphere.database.exception.postgresql.exception.PostgreSQLException;
+import 
org.apache.shardingsphere.database.exception.postgresql.vendor.PostgreSQLVendorError;
 import 
org.apache.shardingsphere.database.protocol.postgresql.constant.PostgreSQLMessageSeverityLevel;
 import 
org.apache.shardingsphere.database.protocol.postgresql.packet.generic.PostgreSQLErrorResponsePacket;
 import org.junit.jupiter.api.Test;
@@ -32,7 +34,6 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-@SuppressWarnings("unchecked")
 class PostgreSQLErrorPacketFactoryTest {
     
     @Test
@@ -43,7 +44,7 @@ class PostgreSQLErrorPacketFactoryTest {
         when(serverErrorMessage.getMessage()).thenReturn("message");
         when(serverErrorMessage.getPosition()).thenReturn(1);
         PostgreSQLErrorResponsePacket actual = 
PostgreSQLErrorPacketFactory.newInstance(new PSQLException(serverErrorMessage));
-        Map<Character, String> fields = (Map<Character, String>) 
Plugins.getMemberAccessor().get(PostgreSQLErrorResponsePacket.class.getDeclaredField("fields"),
 actual);
+        Map<Character, String> fields = getFields(actual);
         
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_SEVERITY), 
is(PostgreSQLMessageSeverityLevel.FATAL));
         assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_CODE), 
is("sqlState"));
         
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), 
is("message"));
@@ -53,7 +54,7 @@ class PostgreSQLErrorPacketFactoryTest {
     @Test
     void assertPSQLExceptionWithServerErrorMessageIsNull() throws 
ReflectiveOperationException {
         PostgreSQLErrorResponsePacket actual = 
PostgreSQLErrorPacketFactory.newInstance(new PSQLException("psqlEx", 
PSQLState.UNEXPECTED_ERROR, new Exception("test")));
-        Map<Character, String> fields = (Map<Character, String>) 
Plugins.getMemberAccessor().get(PostgreSQLErrorResponsePacket.class.getDeclaredField("fields"),
 actual);
+        Map<Character, String> fields = getFields(actual);
         assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_CODE), 
is(PSQLState.UNEXPECTED_ERROR.getState()));
         
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), 
is("psqlEx"));
     }
@@ -61,7 +62,32 @@ class PostgreSQLErrorPacketFactoryTest {
     @Test
     void assertRuntimeException() throws ReflectiveOperationException {
         PostgreSQLErrorResponsePacket actual = 
PostgreSQLErrorPacketFactory.newInstance(new RuntimeException("No reason"));
-        Map<Character, String> fields = (Map<Character, String>) 
Plugins.getMemberAccessor().get(PostgreSQLErrorResponsePacket.class.getDeclaredField("fields"),
 actual);
+        Map<Character, String> fields = getFields(actual);
         
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), 
is("Unknown exception." + System.lineSeparator() + "More details: 
java.lang.RuntimeException: No reason"));
     }
+    
+    @Test
+    void assertPostgreSQLExceptionWithServerErrorMessage() throws 
ReflectiveOperationException {
+        PostgreSQLException.ServerErrorMessage serverErrorMessage = new 
PostgreSQLException.ServerErrorMessage(
+                PostgreSQLMessageSeverityLevel.ERROR, 
PostgreSQLVendorError.INVALID_PARAMETER_VALUE, "param", "value");
+        PostgreSQLErrorResponsePacket actual = 
PostgreSQLErrorPacketFactory.newInstance(new 
PostgreSQLException(serverErrorMessage));
+        Map<Character, String> fields = getFields(actual);
+        
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_SEVERITY), 
is(PostgreSQLMessageSeverityLevel.ERROR));
+        assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_CODE), 
is(PostgreSQLVendorError.INVALID_PARAMETER_VALUE.getSqlState().getValue()));
+        
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), 
is("invalid value for parameter \"param\": \"value\""));
+    }
+    
+    @Test
+    void assertSQLExceptionWithoutMessageAndSQLState() throws 
ReflectiveOperationException {
+        PostgreSQLException cause = new PostgreSQLException(null, null);
+        PostgreSQLErrorResponsePacket actual = 
PostgreSQLErrorPacketFactory.newInstance(cause);
+        Map<Character, String> fields = getFields(actual);
+        assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_CODE), 
is(PostgreSQLVendorError.SYSTEM_ERROR.getSqlState().getValue()));
+        
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), 
is(cause.toString()));
+    }
+    
+    @SuppressWarnings("unchecked")
+    private Map<Character, String> getFields(final 
PostgreSQLErrorResponsePacket packet) throws ReflectiveOperationException {
+        return (Map<Character, String>) 
Plugins.getMemberAccessor().get(PostgreSQLErrorResponsePacket.class.getDeclaredField("fields"),
 packet);
+    }
 }

Reply via email to