fernando88to commented on code in PR #17: URL: https://github.com/apache/grails-intellij-plugin/pull/17#discussion_r3775137811
########## plugin/src/test/java/org/apache/grails/intellij/plugin/domain/GrailsBuildableCriteriaTest.java: ########## @@ -0,0 +1,314 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.intellij.plugin.domain; + +import com.intellij.codeInsight.completion.CompletionType; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiReference; +import com.intellij.testFramework.UsefulTestCase; +import org.apache.grails.intellij.lib.testFramework.GrailsTestCase; +import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Since GORM 4, {@code createCriteria()} returns {@code BuildableCriteria} instead of + * {@code HibernateCriteriaBuilder}, so out of the box only the closure-terminal calls that interface + * declares itself ({@code get}, {@code list}, ...) resolve. The GORM libraries the other tests run against + * are older than that, hence the stubs below for the pieces of a GORM 5 classpath the plugin looks at. + */ +public class GrailsBuildableCriteriaTest extends GrailsTestCase { + /** + * The bundled GORM is older than 4, and so is its {@code grails.orm.HibernateCriteriaBuilder}: it does not + * implement {@code BuildableCriteria}. That hierarchy is what CriteriaBuilderUtil.isCriteriaBuilderMethod() + * keys off, so the builder is stubbed below instead of taken from the library. + */ + @Override + protected boolean needGormLibrary() { + return false; + } + + /** GormTraitContributor only picks GormEntity when {@code org.hibernate.Hibernate} is on the classpath. */ + @Override + protected boolean needHibernate() { + return true; + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + myFixture.addFileToProject("src/java/org/grails/datastore/mapping/query/api/Criteria.java", """ + package org.grails.datastore.mapping.query.api; + + import groovy.lang.Closure; + + public interface Criteria { + Criteria eq(String propertyName, Object value); + Criteria ge(String propertyName, Object value); + Criteria order(String propertyName, String direction); + Criteria and(Closure callable); + } + """); + + myFixture.addFileToProject("src/java/org/grails/datastore/mapping/query/api/BuildableCriteria.java", """ + package org.grails.datastore.mapping.query.api; + + import groovy.lang.Closure; + + public interface BuildableCriteria extends Criteria { + Object get(Closure callable); + Object list(Closure callable); + Object listDistinct(Closure callable); + Object scroll(Closure callable); + } + """); + + myFixture.addFileToProject("src/java/grails/orm/HibernateCriteriaBuilder.java", """ + package grails.orm; + + import groovy.lang.Closure; + import org.grails.datastore.mapping.query.api.BuildableCriteria; + import org.grails.datastore.mapping.query.api.Criteria; + + public class HibernateCriteriaBuilder implements BuildableCriteria { + public Criteria eq(String propertyName, Object value) { return this; } + public Criteria ge(String propertyName, Object value) { return this; } + public Criteria order(String propertyName, String direction) { return this; } + public Criteria and(Closure callable) { return this; } + public Object get(Closure callable) { return null; } + public Object list(Closure callable) { return null; } + public Object listDistinct(Closure callable) { return null; } + public Object scroll(Closure callable) { return null; } + } + """); + + // GormVersion.IS_5 is the lowest version GormTraitContributor injects the trait for. + myFixture.addFileToProject("src/java/grails/gorm/annotation/Entity.java", """ + package grails.gorm.annotation; + + public @interface Entity { + } + """); + + myFixture.addFileToProject("src/groovy/org/grails/datastore/gorm/GormEntity.groovy", """ + package org.grails.datastore.gorm + + import org.grails.datastore.mapping.query.api.BuildableCriteria + + trait GormEntity<D> { + static BuildableCriteria createCriteria() { null } + static Object withCriteria(Closure callable) { null } + } + """); + + addDomain(""" + + class Ddd { + String aaa + String bbb + } + """); + } + + /** + * {@code count} exists only in {@code AbstractHibernateCriteriaBuilder.invokeMethod(...)}, so it has to be + * contributed to {@code BuildableCriteria} explicitly. + */ + public void testResolveCountCall() { + PsiFile file = myFixture.addFileToProject("src/groovy/Ggg.groovy", """ + + class Ggg { + void someMethod() { + Ddd.createCriteria().cou<caret>nt { + ge('aaa', 'bbb') + } + } + } + """); + + myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); + + PsiElement elementAtCaret = myFixture.getElementAtCaret(); + UsefulTestCase.assertInstanceOf(elementAtCaret, PsiMethod.class); + assertEquals("count", ((PsiMethod)elementAtCaret).getName()); + } + + /** + * Resolving {@code count} is what lets CriteriaBuilderUtil.checkCriteriaClosure() find the domain class of + * the closure, which in turn is what makes the properties inside it navigable. + */ + public void testNavigateToPropertyInsideCountClosure() { + PsiFile file = myFixture.addFileToProject("src/groovy/Ggg.groovy", """ + + class Ggg { + void someMethod() { + Ddd.createCriteria().count { + ge('aa<caret>a', 'bbb') + } + } + } + """); + + myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); + + PsiElement elementAtCaret = myFixture.getElementAtCaret(); + UsefulTestCase.assertInstanceOf(elementAtCaret, GrField.class); + assertEquals("aaa", ((GrField)elementAtCaret).getName()); + } + + /** + * Every closure-terminal form has to end up with the same delegate, whether the method is contributed + * ({@code count}, {@code call}) or declared by BuildableCriteria itself ({@code get}, {@code list}, ...). + */ + public void testNavigateToPropertyInsideEveryTerminalClosure() { + PsiFile file = myFixture.addFileToProject("src/groovy/Ggg.groovy", """ + + class Ggg { + void someMethod() { + def c = Ddd.createCriteria() + + Ddd.createCriteria().count { ge('aaa', 'x') } + Ddd.createCriteria().get { ge('aaa', 'x') } + Ddd.createCriteria().list { ge('aaa', 'x') } + Ddd.createCriteria().listDistinct { ge('aaa', 'x') } + Ddd.createCriteria().scroll { ge('aaa', 'x') } + c { ge('aaa', 'x') } + c({ ge('aaa', 'x') }) + } + } + """); + + String text = file.getText(); + List<String> unresolvedForms = new ArrayList<>(); + int propertyCount = 0; + + for (int i = text.indexOf("'aaa'"); i >= 0; i = text.indexOf("'aaa'", i + 1)) { + PsiReference reference = file.findReferenceAt(i + 1); + PsiElement resolved = reference == null ? null : reference.resolve(); + + if (!(resolved instanceof GrField field) || !"aaa".equals(field.getName())) { + int lineStart = text.lastIndexOf('\n', i) + 1; + int lineEnd = text.indexOf('\n', i); + unresolvedForms.add(text.substring(lineStart, lineEnd < 0 ? text.length() : lineEnd).trim()); + } + + propertyCount++; + } + + assertEquals("Not every terminal form was checked", 7, propertyCount); + UsefulTestCase.assertEmpty("The property does not resolve in these forms", unresolvedForms); Review Comment: Added in ebd3f85 — `GrailsTestCase.checkResolve(file)` on that same file, which covers all the terminal calls for free as you said, so `listDistinct` and `scroll` are now asserted somewhere. I also folded `createCriteria().list(max: 10) { }` into the same file rather than giving it a test of its own, since it's just one more terminal form; the count assertion went from 7 to 8 accordingly. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
