From 047414d9a92559550fddf9e25490097c9ac5777c Mon Sep 17 00:00:00 2001 From: p3t <3204560+p3t@users.noreply.github.com> Date: Fri, 15 Nov 2024 19:24:39 +0100 Subject: [PATCH 1/5] Upgrade to java 21 --- .github/workflows/maven_build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/maven_build.yml b/.github/workflows/maven_build.yml index 943d2ab..063b908 100644 --- a/.github/workflows/maven_build.yml +++ b/.github/workflows/maven_build.yml @@ -50,7 +50,7 @@ jobs: uses: actions/setup-java@v4 with: distribution: temurin - java-version: 17 + java-version: 21 cache: 'maven' cache-dependency-path: '**/pom.xml' server-id: 'github' From a8914992c220052bb65cf96bca33e124df1b4c66 Mon Sep 17 00:00:00 2001 From: p3t <3204560+p3t@users.noreply.github.com> Date: Fri, 15 Nov 2024 19:26:50 +0100 Subject: [PATCH 2/5] Upgrade to java 21 --- .github/workflows/maven_publish.yml | 4 +- .../vigier/cursorpaging/jpa/FilterTest.java | 33 ++++++++++++++ .../cursorpaging/jpa/filter/FilterTest.java | 45 ------------------- 3 files changed, 35 insertions(+), 47 deletions(-) delete mode 100644 cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/filter/FilterTest.java diff --git a/.github/workflows/maven_publish.yml b/.github/workflows/maven_publish.yml index 16706be..c3150a6 100644 --- a/.github/workflows/maven_publish.yml +++ b/.github/workflows/maven_publish.yml @@ -27,7 +27,7 @@ jobs: uses: actions/setup-java@v4 with: distribution: temurin - java-version: 17 + java-version: 21 cache: 'maven' cache-dependency-path: '**/pom.xml' server-id: 'central' @@ -70,7 +70,7 @@ jobs: uses: actions/setup-java@v4 with: distribution: temurin - java-version: 17 + java-version: 21 cache: 'maven' cache-dependency-path: '**/pom.xml' server-id: 'github' diff --git a/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/FilterTest.java b/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/FilterTest.java index 5de308b..8bef81a 100644 --- a/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/FilterTest.java +++ b/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/FilterTest.java @@ -1,10 +1,13 @@ package io.vigier.cursorpaging.jpa; +import io.vigier.cursorpaging.jpa.Filters.FilterCreator; import io.vigier.cursorpaging.jpa.itest.model.DataRecord; import io.vigier.cursorpaging.jpa.itest.model.DataRecord_; import io.vigier.cursorpaging.jpa.itest.model.Tag_; import jakarta.persistence.metamodel.SingularAttribute; import java.util.Set; +import java.util.function.Function; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -65,4 +68,34 @@ private static > void verifyNameAndValue( final assertThat( f.values( type ) ).containsExactly( values ); } ); } + + @Test + void shouldBeEqual() { + shouldBeEqual( f1 -> f1.like( "John" ), f2 -> f2.like( "John" ) ); + shouldBeEqual( f1 -> f1.equalTo( "John" ), f2 -> f2.equalTo( "John" ) ); + shouldBeEqual( f1 -> f1.greaterThan( "1L" ), f2 -> f2.greaterThan( "1L" ) ); + shouldBeEqual( f1 -> f1.lessThan( "1L" ), f2 -> f2.lessThan( "1L" ) ); + } + + private void shouldBeEqual( Function f1, Function f2 ) { + var name1 = Attribute.of( "name", String.class ); + var name2 = Attribute.of( "name", String.class ); + + Assertions.assertThat( f1.apply( Filters.attribute( name1 ) ) ) + .isEqualTo( f2.apply( Filters.attribute( name2 ) ) ); + } + + @Test + void filterListsShouldBeEqual() { + var nameAndAge1 = Filters.and( Filters.attribute( Attribute.of( "name", String.class ) ).equalTo( "John" ), + Filters.attribute( Attribute.of( "age", Long.class ) ).equalTo( 18 ) ); + var nameAndAge2 = Filters.and( Filters.attribute( Attribute.of( "name", String.class ) ).equalTo( "John" ), + Filters.attribute( Attribute.of( "age", Long.class ) ).equalTo( 18 ) ); + + Assertions.assertThat( nameAndAge1 ).isEqualTo( nameAndAge2 ); + Assertions.assertThat( Filters.and( nameAndAge1 ) ).isEqualTo( Filters.and( nameAndAge2 ) ); + Assertions.assertThat( Filters.or( nameAndAge1 ) ).isEqualTo( Filters.or( nameAndAge2 ) ); + + Assertions.assertThat( Filters.or( nameAndAge1 ) ).isNotEqualTo( Filters.and( nameAndAge2 ) ); + } } \ No newline at end of file diff --git a/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/filter/FilterTest.java b/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/filter/FilterTest.java deleted file mode 100644 index 54fecc4..0000000 --- a/cursorpaging-jpa/src/test/java/io/vigier/cursorpaging/jpa/filter/FilterTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package io.vigier.cursorpaging.jpa.filter; - -import io.vigier.cursorpaging.jpa.Attribute; -import io.vigier.cursorpaging.jpa.Filter; -import io.vigier.cursorpaging.jpa.Filters; -import io.vigier.cursorpaging.jpa.Filters.FilterCreator; -import java.util.function.Function; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; - -/** - * Some test to make sure that comparing by equals is safe in other tests - */ -class FilterTest { - - @Test - void shouldBeEqual() { - shouldBeEqual( f1 -> f1.like( "John" ), f2 -> f2.like( "John" ) ); - shouldBeEqual( f1 -> f1.equalTo( "John" ), f2 -> f2.equalTo( "John" ) ); - shouldBeEqual( f1 -> f1.greaterThan( 1L ), f2 -> f2.greaterThan( 1L ) ); - shouldBeEqual( f1 -> f1.lessThan( 1L ), f2 -> f2.lessThan( 1L ) ); - } - - private void shouldBeEqual( Function f1, Function f2 ) { - var name1 = Attribute.of( "name", String.class ); - var name2 = Attribute.of( "name", String.class ); - - Assertions.assertThat( f1.apply( Filters.attribute( name1 ) ) ) - .isEqualTo( f2.apply( Filters.attribute( name2 ) ) ); - } - - @Test - void filterListsShouldBeEqual() { - var nameAndAge1 = Filters.and( Filters.attribute( Attribute.of( "name", String.class ) ).equalTo( "John" ), - Filters.attribute( Attribute.of( "age", Long.class ) ).equalTo( 18 ) ); - var nameAndAge2 = Filters.and( Filters.attribute( Attribute.of( "name", String.class ) ).equalTo( "John" ), - Filters.attribute( Attribute.of( "age", Long.class ) ).equalTo( 18 ) ); - - Assertions.assertThat( nameAndAge1 ).isEqualTo( nameAndAge2 ); - Assertions.assertThat( Filters.and( nameAndAge1 ) ).isEqualTo( Filters.and( nameAndAge2 ) ); - Assertions.assertThat( Filters.or( nameAndAge1 ) ).isEqualTo( Filters.or( nameAndAge2 ) ); - - Assertions.assertThat( Filters.or( nameAndAge1 ) ).isNotEqualTo( Filters.and( nameAndAge2 ) ); - } -} \ No newline at end of file From 3492e9998914a50796bc8c08c95ed8c30b825ad9 Mon Sep 17 00:00:00 2001 From: p3t <3204560+p3t@users.noreply.github.com> Date: Fri, 15 Nov 2024 19:49:39 +0100 Subject: [PATCH 3/5] Improved verification --- .../io/vigier/cursorpaging/jpa/Attribute.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java b/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java index 10a43fe..59d8525 100644 --- a/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java +++ b/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java @@ -180,9 +180,19 @@ public boolean ignoreCase() { public Comparable verify( Comparable v ) { if ( type().isAssignableFrom( v.getClass() ) ) { return type().cast( v ); - } else if ( v instanceof Integer && type().isAssignableFrom( Long.class ) ) { + } else if ( v instanceof Integer && typeIsInteger() ) { + return v; + } else if ( v instanceof Long && typeIsLong() ) { + return v; + } else if ( v instanceof Short && (type() == Short.class || type() == short.class) ) { + return v; + } else if ( v instanceof Character && (type() == Character.class || type() == char.class) ) { + return v; + } else if ( v instanceof Byte && (type() == Byte.class || type() == byte.class) ) { + return v; + } else if ( v instanceof Integer && typeIsLong() ) { return Long.valueOf( (Integer) v ); - } else if ( v instanceof Long && type().isAssignableFrom( Integer.class ) && (Long) v <= Integer.MAX_VALUE ) { + } else if ( v instanceof Long && typeIsInteger() && ((Long) v) <= Integer.MAX_VALUE ) { return Integer.valueOf( v.toString() ); } else { throw new IllegalArgumentException( @@ -190,6 +200,14 @@ public Comparable verify( Comparable v ) { } } + private boolean typeIsInteger() { + return type() == Integer.class || type() == int.class; + } + + private boolean typeIsLong() { + return type() == Long.class || type() == long.class; + } + public List> verify( List> values ) { return values.stream().map( this::verify ).toList(); } From 66d43c2fe669f07517480fd4d2e169e5e8563358 Mon Sep 17 00:00:00 2001 From: p3t <3204560+p3t@users.noreply.github.com> Date: Fri, 15 Nov 2024 20:01:01 +0100 Subject: [PATCH 4/5] Improved verification --- .../jpa/api/DtoPageRequestTest.java | 2 +- .../io/vigier/cursorpaging/jpa/Attribute.java | 46 +++++++++++-------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/cursorpaging-jpa-api/src/test/java/io/vigier/cursorpaging/jpa/api/DtoPageRequestTest.java b/cursorpaging-jpa-api/src/test/java/io/vigier/cursorpaging/jpa/api/DtoPageRequestTest.java index 6045b4c..6c8fc20 100644 --- a/cursorpaging-jpa-api/src/test/java/io/vigier/cursorpaging/jpa/api/DtoPageRequestTest.java +++ b/cursorpaging-jpa-api/src/test/java/io/vigier/cursorpaging/jpa/api/DtoPageRequestTest.java @@ -71,7 +71,7 @@ void shouldDeserializeWithOrRootList() throws Exception { }, "filterBy": { "OR": [ - { "GT": { "id": [ "666" ] } } + { "GT": { "id": [ 666 ] } } ] }, "pageSize": 10, diff --git a/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java b/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java index 59d8525..8208739 100644 --- a/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java +++ b/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java @@ -177,29 +177,39 @@ public boolean ignoreCase() { return ignoreCase; } - public Comparable verify( Comparable v ) { - if ( type().isAssignableFrom( v.getClass() ) ) { - return type().cast( v ); - } else if ( v instanceof Integer && typeIsInteger() ) { - return v; - } else if ( v instanceof Long && typeIsLong() ) { - return v; - } else if ( v instanceof Short && (type() == Short.class || type() == short.class) ) { - return v; - } else if ( v instanceof Character && (type() == Character.class || type() == char.class) ) { - return v; - } else if ( v instanceof Byte && (type() == Byte.class || type() == byte.class) ) { - return v; - } else if ( v instanceof Integer && typeIsLong() ) { - return Long.valueOf( (Integer) v ); - } else if ( v instanceof Long && typeIsInteger() && ((Long) v) <= Integer.MAX_VALUE ) { - return Integer.valueOf( v.toString() ); + public Comparable verify( final Comparable value ) { + if ( type().isAssignableFrom( value.getClass() ) ) { + return type().cast( value ); + } else if ( value instanceof Integer && typeIsInteger() ) { + return value; + } else if ( value instanceof Long && typeIsLong() ) { + return value; + } else if ( value instanceof Short && typeIsShort() ) { + return value; + } else if ( value instanceof Character && (type() == Character.class || type() == char.class) ) { + return value; + } else if ( value instanceof Byte && (type() == Byte.class || type() == byte.class) ) { + return value; + } else if ( value instanceof Integer && typeIsLong() ) { + return Long.valueOf( (Integer) value ); + } else if ( value instanceof Long && typeIsInteger() && ((Long) value) <= Integer.MAX_VALUE ) { + return Integer.valueOf( value.toString() ); + } else if ( value instanceof String && typeIsInteger() ) { + return Integer.valueOf( value.toString() ); + } else if ( value instanceof String && typeIsLong() ) { + return Long.valueOf( value.toString() ); + } else if ( value instanceof String && typeIsShort() ) { + return Short.valueOf( value.toString() ); } else { throw new IllegalArgumentException( - "Value %s (%s) is not of type %s".formatted( v, v.getClass().getName(), type() ) ); + "Value %s (%s) is not of type %s".formatted( value, value.getClass().getName(), type() ) ); } } + private boolean typeIsShort() { + return type() == Short.class || type() == short.class; + } + private boolean typeIsInteger() { return type() == Integer.class || type() == int.class; } From 08382a2c3600ba73518e0f08f509e2e1d6287291 Mon Sep 17 00:00:00 2001 From: p3t <3204560+p3t@users.noreply.github.com> Date: Fri, 15 Nov 2024 20:04:11 +0100 Subject: [PATCH 5/5] Improved verification --- .../main/java/io/vigier/cursorpaging/jpa/Attribute.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java b/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java index 8208739..8ed109b 100644 --- a/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java +++ b/cursorpaging-jpa/src/main/java/io/vigier/cursorpaging/jpa/Attribute.java @@ -188,6 +188,8 @@ public Comparable verify( final Comparable value ) { return value; } else if ( value instanceof Character && (type() == Character.class || type() == char.class) ) { return value; + } else if ( value instanceof Boolean && typeIsBoolean() ) { + return value; } else if ( value instanceof Byte && (type() == Byte.class || type() == byte.class) ) { return value; } else if ( value instanceof Integer && typeIsLong() ) { @@ -200,12 +202,18 @@ public Comparable verify( final Comparable value ) { return Long.valueOf( value.toString() ); } else if ( value instanceof String && typeIsShort() ) { return Short.valueOf( value.toString() ); + } else if ( value instanceof String && typeIsBoolean() ) { + return Boolean.valueOf( value.toString() ); } else { throw new IllegalArgumentException( "Value %s (%s) is not of type %s".formatted( value, value.getClass().getName(), type() ) ); } } + private boolean typeIsBoolean() { + return type() == Boolean.class || type() == boolean.class; + } + private boolean typeIsShort() { return type() == Short.class || type() == short.class; }