"com.mysema.query.QueryException: Compilation of ... failed." occurs when passing mockito objects through QueryDSL collections #252

Closed
mcuelenaere opened this Issue Oct 20, 2012 · 4 comments

Comments

Projects
None yet
2 participants
@mcuelenaere

When the QueryDSL collections component generates code, it seems to use the class type of the objects passed to it, instead of relying on the class type of the used expression.

This causes problems when giving it dynamically generated objects, which is done for example when using Mockito.

See following exception:

test(example.domain.UserPolicyImplTest)  Time elapsed: 1.541 sec  <<< ERROR!
com.mysema.query.QueryException: Compilation of java.util.List<example.persistence.objects.CalendarItem> rv = new java.util.ArrayList<example.persistence.objects.CalendarItem>();
for (example.persistence.objects.CalendarItem calendarItem : calendarItem_){
    if (calendarItem.getOwner().equals(a1)){
        rv.add(calendarItem);
    }
}
return rv; failed.
/Q_2045977532_1275614662_1275614662.java:3: error: cannot find symbol
    public example.persistence.objects.Employee$$EnhancerByMockitoWithCGLIB$$45c738ca a1;
                                                     ^
  symbol:   class Employee$$EnhancerByMockitoWithCGLIB$$45c738ca
  location: package example.persistence.objects
1 error

    at com.mysema.query.collections.AbstractColQuery.count(AbstractColQuery.java:67)
    at example.domain.UserPolicyImpl.evaluate(UserPolicyImpl.java:26)
    at example.domain.UserPolicyImpl.canUserSee(UserPolicyImpl.java:197)
    at example.domain.UserPolicyImplTest.test(UserPolicyImplTest.java:81)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: com.mysema.codegen.CodegenException: Compilation of java.util.List<example.persistence.objects.CalendarItem> rv = new java.util.ArrayList<example.persistence.objects.CalendarItem>();
for (example.persistence.objects.CalendarItem calendarItem : calendarItem_){
    if (calendarItem.getOwner().equals(a1)){
        rv.add(calendarItem);
    }
}
return rv; failed.
/Q_2045977532_1275614662_1275614662.java:3: error: cannot find symbol
    public example.persistence.objects.Employee$$EnhancerByMockitoWithCGLIB$$45c738ca a1;
                                                     ^
  symbol:   class Employee$$EnhancerByMockitoWithCGLIB$$45c738ca
  location: package example.persistence.objects
1 error

    at com.mysema.codegen.JDKEvaluatorFactory.compile(JDKEvaluatorFactory.java:91)
    at com.mysema.codegen.AbstractEvaluatorFactory.createEvaluator(AbstractEvaluatorFactory.java:76)
    at com.mysema.query.collections.DefaultEvaluatorFactory.createEvaluator(DefaultEvaluatorFactory.java:150)
    at com.mysema.query.collections.DefaultQueryEngine.evaluateSingleSource(DefaultQueryEngine.java:167)
    at com.mysema.query.collections.DefaultQueryEngine.count(DefaultQueryEngine.java:57)
    at com.mysema.query.collections.AbstractColQuery.count(AbstractColQuery.java:65)
    ... 33 more

UserPolicyImpl is something like:

import static com.mysema.query.collections.MiniApi.*;
import static com.mysema.query.types.ExpressionUtils.*;

public class UserPolicyImpl implements UserPolicy {
    // helper method
    private static <T> boolean evaluate(Predicate expr, EntityPath<T> path, T obj) {
        return from(path, obj).where(expr).count() == 1;
    }

    @Override
    public Predicate visibleCalendarItemsFor(User user) {
        QCalendarItem root = QCalendarItem.calendarItem;

        return root.owner.eq(user);
    }

    @Override
    public boolean canUserSee(User user, CalendarItem calendarItem) {
        return evaluate(visibleCalendarItemsFor(user), QCalendarItem.calendarItem, calendarItem);
    }
}

and UserPolicyImplTest:

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class UserPolicyImplTest {
    @Test
    public void test() {
        Employee user = mock(Employee.class);
        CalendarItem item = mock(CalendarItem.class);

        assertTrue(userPolicy.canUserSee(user, item));
    }
}
@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 22, 2012

Member

The cglib issues come from constants that are injected into your query, not from path expressions.

Member

timowest commented Oct 22, 2012

The cglib issues come from constants that are injected into your query, not from path expressions.

timowest added a commit that referenced this issue Oct 22, 2012

@mcuelenaere

This comment has been minimized.

Show comment
Hide comment
@mcuelenaere

mcuelenaere Oct 22, 2012

You're right, however querydsl/codegen#8 fixed this, so this issue can be closed.

You're right, however querydsl/codegen#8 fixed this, so this issue can be closed.

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 22, 2012

Member

I'll close it, when it's released.

Member

timowest commented Oct 22, 2012

I'll close it, when it's released.

@timowest timowest reopened this Oct 22, 2012

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 30, 2012

Member

Released in 2.8.2

Member

timowest commented Oct 30, 2012

Released in 2.8.2

@timowest timowest closed this Oct 30, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment