Skip to content

Commit 2f5a6d5

Browse files
fl0rian-pVanRoy
authored andcommitted
[FEATURE] Support array comparison
1 parent 3e8ace9 commit 2f5a6d5

File tree

7 files changed

+137
-0
lines changed

7 files changed

+137
-0
lines changed

src/main/java/org/assertj/db/type/AbstractDbData.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ protected void collectRowsFromResultSet(ResultSet resultSet) throws SQLException
191191
case Types.CLOB:
192192
object = resultSet.getString(columnName);
193193
break;
194+
case Types.ARRAY:
195+
object = resultSet.getArray(columnName);
196+
break;
194197

195198
default:
196199
object = resultSet.getObject(columnName);

src/main/java/org/assertj/db/type/Value.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.math.BigDecimal;
1616
import java.math.BigInteger;
17+
import java.sql.Array;
1718
import java.sql.Date;
1819
import java.sql.Time;
1920
import java.sql.Timestamp;
@@ -108,6 +109,9 @@ static ValueType getType(Object object) {
108109
if (object instanceof java.util.UUID) {
109110
return ValueType.UUID;
110111
}
112+
if (object instanceof java.sql.Array) {
113+
return ValueType.ARRAY;
114+
}
111115
if (object instanceof Byte
112116
|| object instanceof Short
113117
|| object instanceof Integer
@@ -190,6 +194,8 @@ public boolean isComparisonPossible(Object object) {
190194
return (object instanceof Number || object instanceof String);
191195
} else if (valueType == ValueType.UUID) {
192196
return (object instanceof UUID || object instanceof String);
197+
} else if (valueType == ValueType.ARRAY) {
198+
return (object instanceof Array);
193199
} else if (valueType == ValueType.NOT_IDENTIFIED) {
194200
if (value == null) {
195201
return object == null;

src/main/java/org/assertj/db/type/ValueType.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public enum ValueType {
5252
* UUID type.
5353
*/
5454
UUID,
55+
/**
56+
* Array type
57+
*/
58+
ARRAY,
5559
/**
5660
* Not identified type : null value for example.
5761
*/
@@ -89,6 +93,9 @@ public static ValueType[] getPossibleTypesForComparison(Object expected) {
8993
if (expected instanceof java.util.UUID) {
9094
return new ValueType[]{UUID};
9195
}
96+
if (expected instanceof java.sql.Array) {
97+
return new ValueType[]{ARRAY};
98+
}
9299
return new ValueType[]{NOT_IDENTIFIED};
93100
}
94101
}

src/main/java/org/assertj/db/util/Values.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
import java.math.BigDecimal;
1616
import java.math.BigInteger;
17+
import java.sql.Array;
1718
import java.sql.Date;
19+
import java.sql.SQLException;
1820
import java.sql.Time;
1921
import java.sql.Timestamp;
2022
import java.text.ParseException;
2123
import java.time.LocalDate;
2224
import java.time.LocalDateTime;
2325
import java.time.LocalTime;
26+
import java.util.Arrays;
2427
import java.util.UUID;
2528

2629
import org.assertj.db.exception.AssertJDBException;
@@ -114,6 +117,10 @@ public static boolean areEqual(Value value, Object expected) {
114117
} else if (expected instanceof LocalDate) {
115118
return areEqual(value, DateValue.from((LocalDate) expected));
116119
}
120+
} else if (valueType == ValueType.ARRAY) {
121+
if (expected instanceof Array) {
122+
return areEqual(value, (Array) expected);
123+
}
117124
} else {
118125
Object object = value.getValue();
119126
if (expected == null && object == null) {
@@ -1192,4 +1199,25 @@ public static boolean areClose(Value value, DateTimeValue expected, DateTimeValu
11921199
}
11931200
return false;
11941201
}
1202+
1203+
1204+
/**
1205+
* Returns if the value's underlying array equals the expected's underlying array
1206+
* The equality check is done using {{@link Arrays#equals(Object[], Object[])}
1207+
*
1208+
* @param value The value.
1209+
* @param expected The {@code Array} to compare.
1210+
* @return {@code true} if the value's underlying array equals the {@code Array}'s underlying array, {@code false} otherwise.
1211+
*/
1212+
private static boolean areEqual(Value value, Array expected) {
1213+
try {
1214+
Array object = (Array) value.getValue();
1215+
if (expected == null) {
1216+
return object == null;
1217+
}
1218+
return Arrays.equals((Object[]) object.getArray(), (Object[]) expected.getArray());
1219+
} catch (SQLException e) {
1220+
throw new AssertJDBException(e);
1221+
}
1222+
}
11951223
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.assertj.db.common;
2+
3+
4+
import java.sql.Array;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.util.Map;
8+
9+
/**
10+
* Helper class to build a java.sql.Array without using a DB connection
11+
*/
12+
public class SimpleArray implements Array {
13+
private final Object[] value;
14+
15+
public SimpleArray(Object[] value) {
16+
this.value = value;
17+
}
18+
19+
public Object getArray() throws SQLException {
20+
return this.value;
21+
}
22+
23+
public Object getArray(Map<String, Class<?>> var1) throws SQLException {
24+
throw new SQLException("Unsupported operation");
25+
}
26+
27+
public Object getArray(long var1, int var3) throws SQLException {
28+
throw new SQLException("Unsupported operation");
29+
}
30+
31+
public Object getArray(long var1, int var3, Map<String, Class<?>> var4) throws SQLException {
32+
throw new SQLException("Unsupported operation");
33+
}
34+
35+
public int getBaseType() {
36+
return 0;
37+
}
38+
39+
public String getBaseTypeName() {
40+
return "ATYPE";
41+
}
42+
43+
public ResultSet getResultSet() throws SQLException {
44+
throw new SQLException("Unsupported operation");
45+
}
46+
47+
public ResultSet getResultSet(Map<String, Class<?>> var1) throws SQLException {
48+
throw new SQLException("Unsupported operation");
49+
}
50+
51+
public ResultSet getResultSet(long var1, int var3) throws SQLException {
52+
throw new SQLException("Unsupported operation");
53+
}
54+
55+
public ResultSet getResultSet(long var1, int var3, Map<String, Class<?>> var4) throws SQLException {
56+
throw new SQLException("Unsupported operation");
57+
}
58+
59+
public void free() {
60+
}
61+
}

src/test/java/org/assertj/db/type/Value_IsComparisonPossible_Test.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.UUID;
2424

2525
import org.assertj.db.common.AbstractTest;
26+
import org.assertj.db.common.SimpleArray;
2627
import org.junit.Test;
2728

2829
/**
@@ -60,6 +61,7 @@ public void test_is_comparison_possible() throws Exception {
6061
assertThat(getValue("", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435")).isComparisonPossible("")).isTrue();
6162
assertThat(getValue("", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435")).isComparisonPossible(UUID.randomUUID())).isTrue();
6263
assertThat(getValue("", null).isComparisonPossible(null)).isTrue();
64+
assertThat(getValue("", new SimpleArray(new Object[]{})).isComparisonPossible(new SimpleArray(new Object[]{}))).isTrue();
6365
assertThat(getValue("", new URL("http://github.com")).isComparisonPossible(null)).isFalse();
6466
assertThat(getValue("", new byte[]{1}).isComparisonPossible("")).isFalse();
6567
assertThat(getValue("", true).isComparisonPossible("")).isFalse();
@@ -70,5 +72,6 @@ public void test_is_comparison_possible() throws Exception {
7072
assertThat(getValue("", Timestamp.valueOf("2007-12-23 09:01:00")).isComparisonPossible(new byte[]{1})).isFalse();
7173
assertThat(getValue("", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435")).isComparisonPossible(new byte[]{1})).isFalse();
7274
assertThat(getValue("", null).isComparisonPossible(new byte[]{1})).isFalse();
75+
assertThat(getValue("", new SimpleArray(new Object[]{})).isComparisonPossible(new byte[]{1})).isFalse();
7376
}
7477
}

src/test/java/org/assertj/db/util/Values_AreEqual_Value_And_Object_Test.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import java.math.BigDecimal;
1919
import java.math.BigInteger;
20+
import java.sql.Array;
2021
import java.sql.Date;
22+
import java.sql.SQLException;
2123
import java.sql.Time;
2224
import java.sql.Timestamp;
2325
import java.time.LocalDate;
@@ -27,6 +29,7 @@
2729
import java.util.concurrent.atomic.AtomicLong;
2830

2931
import org.assertj.db.common.AbstractTest;
32+
import org.assertj.db.common.SimpleArray;
3033
import org.assertj.db.exception.AssertJDBException;
3134
import org.assertj.db.type.DateTimeValue;
3235
import org.assertj.db.type.DateValue;
@@ -545,4 +548,30 @@ public void test_are_equal_for_datestimes() throws Exception {
545548
public void should_fail_because_string_is_not_parseable_in_datetime() throws Exception {
546549
Values.areEqual(getValue(null, Timestamp.valueOf("2007-12-23 09:01:06.000000003")), (Object) "***");
547550
}
551+
552+
/**
553+
* This method tests the {@code areEqual} method for {@code java.sql.Array}s.
554+
*/
555+
@Test
556+
public void test_are_equal_for_arrays() throws Exception {
557+
assertThat(Values.areEqual(getValue(null, (Array) null), (Array) null)).isTrue();
558+
assertThat(Values.areEqual(getValue(null, new SimpleArray(new String[]{"value1"})), (Array) null)).isFalse();
559+
assertThat(Values.areEqual(getValue(null, new SimpleArray(new String[]{"value1"})), new SimpleArray(new String[]{"value2"}))).isFalse();
560+
assertThat(Values.areEqual(getValue(null, new SimpleArray(new String[]{"value1"})), new SimpleArray(new String[]{"value1"}))).isTrue();
561+
assertThat(Values.areEqual(getValue(null, new SimpleArray(new String[]{"value1"})), new SimpleArray(new String[]{"value1", "value2"}))).isFalse();
562+
}
563+
564+
/**
565+
* This method should fail because the object is already closed
566+
*/
567+
@Test(expected = AssertJDBException.class)
568+
public void should_fail_because_array_not_readable() throws Exception {
569+
Array array = new SimpleArray(null) {
570+
@Override
571+
public Object getArray() throws SQLException {
572+
throw new SQLException();
573+
}
574+
};
575+
Values.areEqual(getValue(null, (Array) array), array);
576+
}
548577
}

0 commit comments

Comments
 (0)