From 989979bc8679233da069fad9901450ef7108c934 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Thu, 7 Mar 2024 17:15:20 +0100 Subject: [PATCH 1/3] HBX-2743: Create Interface for CriteriaWrapper - Create new initial test class 'org.hibernate.tool.orm.jbt.api.CriteriaWrapperTest' - Create new initial interface class 'org.hibernate.tool.orm.jbt.api.CriteriaWrapper' - Create new factory class 'org.hibernate.tool.orm.jbt.internal.factory.CriteriaWrapperFactory' Signed-off-by: Koen Aers --- .../tool/orm/jbt/api/CriteriaWrapper.java | 7 +++ .../factory/CriteriaWrapperFactory.java | 15 ++++++ .../tool/orm/jbt/api/CriteriaWrapperTest.java | 52 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java create mode 100644 jbt/src/main/java/org/hibernate/tool/orm/jbt/internal/factory/CriteriaWrapperFactory.java create mode 100644 jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java diff --git a/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java b/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java new file mode 100644 index 0000000000..fb7934290d --- /dev/null +++ b/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java @@ -0,0 +1,7 @@ +package org.hibernate.tool.orm.jbt.api; + +import org.hibernate.tool.orm.jbt.wrp.Wrapper; + +public interface CriteriaWrapper extends Wrapper { + +} diff --git a/jbt/src/main/java/org/hibernate/tool/orm/jbt/internal/factory/CriteriaWrapperFactory.java b/jbt/src/main/java/org/hibernate/tool/orm/jbt/internal/factory/CriteriaWrapperFactory.java new file mode 100644 index 0000000000..afd5fd6404 --- /dev/null +++ b/jbt/src/main/java/org/hibernate/tool/orm/jbt/internal/factory/CriteriaWrapperFactory.java @@ -0,0 +1,15 @@ +package org.hibernate.tool.orm.jbt.internal.factory; + +import org.hibernate.tool.orm.jbt.api.CriteriaWrapper; + +import jakarta.persistence.Query; + +public class CriteriaWrapperFactory { + + public static CriteriaWrapper createCriteriaWrapper(final Query wrappedCriteria) { + return new CriteriaWrapper() { + @Override public Query getWrappedObject() { return wrappedCriteria; } + }; + } + +} diff --git a/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java b/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java new file mode 100644 index 0000000000..da90e180b2 --- /dev/null +++ b/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java @@ -0,0 +1,52 @@ +package org.hibernate.tool.orm.jbt.api; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.Arrays; +import java.util.List; + +import org.hibernate.tool.orm.jbt.internal.factory.CriteriaWrapperFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Query; + +public class CriteriaWrapperTest { + + private static final List RESULT_LIST = Arrays.asList("foo", "bar"); + + private CriteriaWrapper criteriaWrapper = null; + private Query wrappedCriteria = null; + + @BeforeEach + public void beforeEach() throws Exception { + wrappedCriteria = createQuery(); + criteriaWrapper = CriteriaWrapperFactory.createCriteriaWrapper(wrappedCriteria); + } + + @Test + public void testConstruction() { + assertNotNull(wrappedCriteria); + assertNotNull(criteriaWrapper); + } + + private Query createQuery() { + return (Query)Proxy.newProxyInstance( + getClass().getClassLoader(), + new Class[] { Query.class }, + new InvocationHandler () { + private int maxResults = 0; + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if (method.getName().equals("getMaxResults")) { return maxResults; } + else if (method.getName().equals("setMaxResults")) { maxResults = (int)args[0]; } + else if (method.getName().equals("getResultList")) { return RESULT_LIST; } + return null; + } + }); + } + +} From 8875567b1c36e188a0041c9f9436f93667fe88e4 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Fri, 8 Mar 2024 13:04:02 +0100 Subject: [PATCH 2/3] HBX-2743: Create Interface for CriteriaWrapper - Add new test case 'org.hibernate.tool.orm.jbt.api.CriteriaWrapperTest#testSetMaxResults()' - Add new default interface method 'org.hibernate.tool.orm.jbt.api.CriteriaWrapper#setMaxResults(int)' Signed-off-by: Koen Aers --- .../org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java | 4 ++++ .../hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java b/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java index fb7934290d..e328a732ee 100644 --- a/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java +++ b/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java @@ -2,6 +2,10 @@ import org.hibernate.tool.orm.jbt.wrp.Wrapper; +import jakarta.persistence.Query; + public interface CriteriaWrapper extends Wrapper { + + default void setMaxResults(int intValue) { ((Query)getWrappedObject()).setMaxResults(intValue); } } diff --git a/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java b/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java index da90e180b2..d34b6a3c11 100644 --- a/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java +++ b/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java @@ -1,5 +1,7 @@ package org.hibernate.tool.orm.jbt.api; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.lang.reflect.InvocationHandler; @@ -33,6 +35,13 @@ public void testConstruction() { assertNotNull(criteriaWrapper); } + @Test + public void testSetMaxResults() { + assertNotEquals(wrappedCriteria.getMaxResults(), Integer.MAX_VALUE); + criteriaWrapper.setMaxResults(Integer.MAX_VALUE); + assertEquals(wrappedCriteria.getMaxResults(), Integer.MAX_VALUE); + } + private Query createQuery() { return (Query)Proxy.newProxyInstance( getClass().getClassLoader(), From 677b6fc05ca591633957b469ae71f621ae7ddde8 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Fri, 8 Mar 2024 13:20:55 +0100 Subject: [PATCH 3/3] HBX-2743: Create Interface for CriteriaWrapper - Add new test case 'org.hibernate.tool.orm.jbt.api.CriteriaWrapperTest#testList()' - Add new default interface method 'org.hibernate.tool.orm.jbt.api.CriteriaWrapper#list()' Signed-off-by: Koen Aers --- .../org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java | 3 +++ .../org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java b/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java index e328a732ee..34244aaba6 100644 --- a/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java +++ b/jbt/src/main/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapper.java @@ -1,5 +1,7 @@ package org.hibernate.tool.orm.jbt.api; +import java.util.List; + import org.hibernate.tool.orm.jbt.wrp.Wrapper; import jakarta.persistence.Query; @@ -7,5 +9,6 @@ public interface CriteriaWrapper extends Wrapper { default void setMaxResults(int intValue) { ((Query)getWrappedObject()).setMaxResults(intValue); } + default List list() { return ((Query)getWrappedObject()).getResultList(); } } diff --git a/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java b/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java index d34b6a3c11..1bfcb3a746 100644 --- a/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java +++ b/jbt/src/test/java/org/hibernate/tool/orm/jbt/api/CriteriaWrapperTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -42,6 +43,11 @@ public void testSetMaxResults() { assertEquals(wrappedCriteria.getMaxResults(), Integer.MAX_VALUE); } + @Test + public void testList() { + assertSame(criteriaWrapper.list(), RESULT_LIST); + } + private Query createQuery() { return (Query)Proxy.newProxyInstance( getClass().getClassLoader(),