Search in sources :

Example 1 with HQLJavaCompletionProposalComputer

use of org.hibernate.eclipse.jdt.ui.internal.HQLJavaCompletionProposalComputer in project jbosstools-hibernate by jbosstools.

the class JavaHBMQueryTest method testJavaHQLQueryCodeCompletion.

@SuppressWarnings("unused")
public void testJavaHQLQueryCodeCompletion() throws JavaModelException, CoreException, NoSuchFieldException, IllegalAccessException {
    IPackageFragment pack = project.getTestClassType().getPackageFragment();
    String testCP = "TestCompletionProposals.java";
    ICompilationUnit cu = pack.createCompilationUnit(testCP, "", true, // $NON-NLS-1$
    null);
    cu.createPackageDeclaration(pack.getElementName(), null);
    IType type = cu.createType("public class " + testCP + " {}", null, false, // $NON-NLS-1$//$NON-NLS-2$
    null);
    type.createMethod("public static void main(String[] args){String query = \"from \";}", null, false, // $NON-NLS-1$
    null);
    IWorkbenchPage p = JavaPlugin.getActivePage();
    IEditorPart part = EditorUtility.openInEditor(type, true);
    if (part instanceof CompilationUnitEditor) {
        CompilationUnitEditor editor = (CompilationUnitEditor) part;
        IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
        HQLJavaCompletionProposalComputer proposalComputer = new HQLJavaCompletionProposalComputer();
        ContentAssistInvocationContext context = new JavaContentAssistInvocationContext(editor.getViewer(), 125, editor);
        List<ICompletionProposal> computeCompletionProposals = proposalComputer.computeCompletionProposals(context, null);
        assertTrue(computeCompletionProposals.size() > 0);
        for (ICompletionProposal iCompletionProposal : computeCompletionProposals) {
            Class<? extends ICompletionProposal> class1 = iCompletionProposal.getClass();
            if (class1.getPackage().getName().indexOf("org.jboss.tools.hibernate") == 0) {
                // this is our completion proposal
                Field declaredField = class1.getDeclaredField("documentOffset");
                declaredField.setAccessible(true);
                Integer offset = (Integer) declaredField.get(iCompletionProposal);
                Assert.assertTrue(offset > 0);
            }
        }
    } else {
        fail("Can't open CompilationUnitEditor");
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext) JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) CompilationUnitEditor(org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor) IEditorPart(org.eclipse.ui.IEditorPart) IType(org.eclipse.jdt.core.IType) Field(java.lang.reflect.Field) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) HQLJavaCompletionProposalComputer(org.hibernate.eclipse.jdt.ui.internal.HQLJavaCompletionProposalComputer) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

Field (java.lang.reflect.Field)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IType (org.eclipse.jdt.core.IType)1 CompilationUnitEditor (org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor)1 ContentAssistInvocationContext (org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext)1 JavaContentAssistInvocationContext (org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext)1 IDocument (org.eclipse.jface.text.IDocument)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 IEditorPart (org.eclipse.ui.IEditorPart)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 HQLJavaCompletionProposalComputer (org.hibernate.eclipse.jdt.ui.internal.HQLJavaCompletionProposalComputer)1