Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBX-2743: Create Interface for CriteriaWrapper #4688

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.hibernate.tool.orm.jbt.api;

import java.util.List;

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); }
default List<?> list() { return ((Query)getWrappedObject()).getResultList(); }

}
Original file line number Diff line number Diff line change
@@ -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; }
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 static org.junit.jupiter.api.Assertions.assertSame;

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<String> 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);
}

@Test
public void testSetMaxResults() {
assertNotEquals(wrappedCriteria.getMaxResults(), Integer.MAX_VALUE);
criteriaWrapper.setMaxResults(Integer.MAX_VALUE);
assertEquals(wrappedCriteria.getMaxResults(), Integer.MAX_VALUE);
}

@Test
public void testList() {
assertSame(criteriaWrapper.list(), RESULT_LIST);
}

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;
}
});
}

}
Loading