Skip to content

Commit

Permalink
add pgarray equals override fix issue pgjdbc#3170
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawnliving committed Mar 29, 2024
1 parent a6f71a4 commit 6d35276
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 49 deletions.
49 changes: 0 additions & 49 deletions pgjdbc/src/test/java/org/postgresql/jdbc/ArraysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.postgresql.core.BaseConnection;
import org.postgresql.core.Oid;
import org.postgresql.util.PSQLException;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.Properties;

class ArraysTest {

Expand Down Expand Up @@ -51,45 +43,4 @@ void binaryNotSupported() throws Exception {
});
}

@Test
public void testArrayEquals() throws SQLException {
//because of install the postgresql at the VM, need to specify the url user and password for testing.
String url = "jdbc:postgresql://192.168.100.80/test";
String user = "postgres";
String password = "123456";
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
Connection connection = DriverManager.getConnection(url,props);
Statement statement = connection.createStatement();

statement.executeQuery("SELECT * FROM person");
Array pgArray1 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{'1','2','3'});
Array pgArray2 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{'1','2','3'});
Assertions.assertEquals(pgArray1,pgArray2);

Array pgArray3 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Array pgArray4 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertEquals(pgArray3,pgArray4);

Array pgArray5 = new PgArray((BaseConnection) connection, Oid.BIT_ARRAY, new byte[]{1,2,3});
Array pgArray6 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertNotEquals(pgArray5,pgArray6);

Array pgArray7 = new PgArray((BaseConnection) connection, Oid.JSON, new byte[]{1,2,3});
Array pgArray8 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertNotEquals(pgArray7,pgArray8);

Array pgArray9 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{}");
Array pgArray10 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{}");
Assertions.assertEquals(pgArray9,pgArray10);

Array pgArray11 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Array pgArray12 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Assertions.assertEquals(pgArray11,pgArray12);

Array pgArray13 = new PgArray((BaseConnection) connection, Oid.VARCHAR_ARRAY, "{\t \n 'testing1', \t \n 'testing2'}");
Array pgArray14 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Assertions.assertNotEquals(pgArray13,pgArray14);
}
}
35 changes: 35 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

Expand Down Expand Up @@ -904,4 +905,38 @@ public void testEmptyArray() throws SQLException {
}
}

@Test
public void testArrayEquals() throws SQLException {
Statement statement = conn.createStatement();

statement.executeQuery("SELECT * FROM person");
Array pgArray1 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{'1','2','3'});
Array pgArray2 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{'1','2','3'});
Assertions.assertEquals(pgArray1,pgArray2);

Array pgArray3 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Array pgArray4 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertEquals(pgArray3,pgArray4);

Array pgArray5 = new PgArray((BaseConnection) conn, Oid.BIT_ARRAY, new byte[]{1,2,3});
Array pgArray6 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertNotEquals(pgArray5,pgArray6);

Array pgArray7 = new PgArray((BaseConnection) conn, Oid.JSON, new byte[]{1,2,3});
Array pgArray8 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertNotEquals(pgArray7,pgArray8);

Array pgArray9 = new PgArray((BaseConnection) conn, Oid.VARCHAR, "{}");
Array pgArray10 = new PgArray((BaseConnection) conn, Oid.VARCHAR, "{}");
Assertions.assertEquals(pgArray9,pgArray10);

Array pgArray11 = new PgArray((BaseConnection) conn, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Array pgArray12 = new PgArray((BaseConnection) conn, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Assertions.assertEquals(pgArray11,pgArray12);

Array pgArray13 = new PgArray((BaseConnection) conn, Oid.VARCHAR_ARRAY, "{\t \n 'testing1', \t \n 'testing2'}");
Array pgArray14 = new PgArray((BaseConnection) conn, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Assertions.assertNotEquals(pgArray13,pgArray14);
}

}

0 comments on commit 6d35276

Please sign in to comment.