Author: vyemialyanchyk
Date: 2011-01-05 08:56:57 -0500 (Wed, 05 Jan 2011)
New Revision: 27910
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLCompletionProcessor.java
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/hqleditor/HQLEditorTest.java
Log:
https://issues.jboss.org/browse/JBIDE-7991 - add JUnit test
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLCompletionProcessor.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLCompletionProcessor.java 2011-01-05
13:22:24 UTC (rev 27909)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLCompletionProcessor.java 2011-01-05
13:56:57 UTC (rev 27910)
@@ -63,18 +63,22 @@
completionComparator = DisplayStringProposalComparator.INSTANCE;
}
- public ICompletionProposal[] computeCompletionProposals( ITextViewer viewer, int
documentOffset ) {
+ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int
documentOffset) {
+ IDocument doc = viewer.getDocument();
+ return computeCompletionProposals(doc, documentOffset);
+ }
+
+ public ICompletionProposal[] computeCompletionProposals(IDocument doc, int
documentOffset) {
ICompletionProposal[] result = new ICompletionProposal[0];
try {
- IDocument doc = viewer.getDocument();
ITypedRegion partition = null;
if (documentOffset > 0) {
- partition = viewer.getDocument().getPartition( documentOffset - 1 );
+ partition = doc.getPartition( documentOffset - 1 );
}
else {
- partition = viewer.getDocument().getPartition( documentOffset );
+ partition = doc.getPartition( documentOffset );
}
if(partition!=null) {
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/hqleditor/HQLEditorTest.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/hqleditor/HQLEditorTest.java 2011-01-05
13:22:24 UTC (rev 27909)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/hqleditor/HQLEditorTest.java 2011-01-05
13:56:57 UTC (rev 27910)
@@ -10,19 +10,36 @@
******************************************************************************/
package org.hibernate.eclipse.hqleditor;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
import junit.framework.TestCase;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.IPage;
+import org.hibernate.console.ConsoleConfiguration;
+import org.hibernate.console.KnownConfigurations;
import org.hibernate.console.QueryInputModel;
import org.hibernate.eclipse.console.HibernateConsoleMessages;
import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.eclipse.console.test.project.SimpleTestProject;
+import org.hibernate.eclipse.console.test.project.SimpleTestProjectWithMapping;
+import org.hibernate.eclipse.console.test.project.TestProject;
+import org.hibernate.eclipse.console.test.utils.ConsoleConfigUtils;
import org.hibernate.eclipse.console.views.QueryParametersPage;
import org.hibernate.eclipse.console.views.QueryParametersView;
@@ -32,40 +49,59 @@
*/
public class HQLEditorTest extends TestCase {
+ private static final String PROJ_NAME = "HQLEditorTest"; //$NON-NLS-1$
+ private static final String CONSOLE_NAME = PROJ_NAME;
+
+ private SimpleTestProjectWithMapping project = null;
+
+ protected void setUp() throws Exception {
+ }
+
+ protected void tearDown() throws Exception {
+ cleanUpProject();
+ }
+
+ protected void cleanUpProject() {
+ if (project != null) {
+ project.deleteIProject();
+ project = null;
+ }
+ }
+
public void testHQLEditorOpen(){
IEditorPart editorPart = HibernateConsolePlugin.getDefault()
- .openScratchHQLEditor(null, "");
- assertNotNull("Editor was not opened", editorPart);
- assertTrue("Opened editor is not HQLEditor", editorPart instanceof
HQLEditor);
+ .openScratchHQLEditor(null, ""); //$NON-NLS-1$
+ assertNotNull("Editor was not opened", editorPart); //$NON-NLS-1$
+ assertTrue("Opened editor is not HQLEditor", editorPart instanceof
HQLEditor); //$NON-NLS-1$
HQLEditor editor = (HQLEditor)editorPart;
QueryInputModel model = editor.getQueryInputModel();
- assertNotNull("Model is NULL", model);
+ assertNotNull("Model is NULL", model); //$NON-NLS-1$
}
public void testSingleLineCommentsCutOff() throws PartInitException{
- String query = "from pack.Article a\n" +
- "where a.articleid in (:a, :b) --or a.articleid = :c";
+ String query = "from pack.Article a\n" + //$NON-NLS-1$
+ "where a.articleid in (:a, :b) --or a.articleid = :c"; //$NON-NLS-1$
IEditorPart editorPart = HibernateConsolePlugin.getDefault()
.openScratchHQLEditor(null, query);
- assertTrue("Opened editor is not HQLEditor", editorPart instanceof
HQLEditor);
+ assertTrue("Opened editor is not HQLEditor", editorPart instanceof
HQLEditor); //$NON-NLS-1$
HQLEditor editor = (HQLEditor)editorPart;
assertEquals(editor.getEditorText(), query);
- assertFalse("Comments were not cut off",
editor.getQueryString().contains("--"));
+ assertFalse("Comments were not cut off",
editor.getQueryString().contains("--")); //$NON-NLS-1$ //$NON-NLS-2$
QueryInputModel model = editor.getQueryInputModel();
assertTrue(model.getParameterCount() == 0);
IViewPart view = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
- .getActivePage().showView("org.hibernate.eclipse.console.views.QueryParametersView");
- assertNotNull("View was not opened", view);
- assertTrue("Opened view is not QueryParametersView", view instanceof
QueryParametersView);
+ .getActivePage().showView("org.hibernate.eclipse.console.views.QueryParametersView");
//$NON-NLS-1$
+ assertNotNull("View was not opened", view); //$NON-NLS-1$
+ assertTrue("Opened view is not QueryParametersView", view instanceof
QueryParametersView); //$NON-NLS-1$
QueryParametersView paramView = (QueryParametersView)view;
IPage ipage = paramView.getCurrentPage();
- assertNotNull("Current Page is NULL", ipage);
- assertTrue("Page is not Query Parameters Page", ipage instanceof
QueryParametersPage);
+ assertNotNull("Current Page is NULL", ipage); //$NON-NLS-1$
+ assertTrue("Page is not Query Parameters Page", ipage instanceof
QueryParametersPage); //$NON-NLS-1$
QueryParametersPage page = (QueryParametersPage)ipage;
IToolBarManager manager = page.getSite().getActionBars().getToolBarManager();
@@ -73,17 +109,59 @@
ActionContributionItem addParamItem = null;
for (int i = 0; i < items.length; i++) {
ActionContributionItem item = (ActionContributionItem) items[i];
- if (item.getAction().getClass().getName().endsWith("NewRowAction")){
+ if (item.getAction().getClass().getName().endsWith("NewRowAction")){
//$NON-NLS-1$
addParamItem = item;
break;
}
}
assertNotNull(HibernateConsoleMessages.QueryParametersPage_add_query_parameter_tooltip
- + " item not found", addParamItem);
+ + " item not found", addParamItem); //$NON-NLS-1$
addParamItem.getAction().run();//add query parameters automatically
assertTrue(model.getParameterCount() == 2);//a and b
}
+ public void testHQLEditorCodeCompletionWithTabs() throws CoreException,
NoSuchFieldException, IllegalAccessException{
+ cleanUpProject();
+ project = new SimpleTestProjectWithMapping(PROJ_NAME);
+
+ IPackageFragmentRoot sourceFolder = project.createSourceFolder();
+ IPackageFragment pf =
sourceFolder.createPackageFragment(SimpleTestProject.PACKAGE_NAME, false, null);
+ ConsoleConfigUtils.customizeCfgXmlForPack(pf);
+ List<IPath> libs = new ArrayList<IPath>();
+ project.generateClassPath(libs, sourceFolder);
+ project.fullBuild();
+
+ //setup console configuration
+ IPath cfgFilePath = new Path(project.getIProject().getName() + File.separator +
+ TestProject.SRC_FOLDER + File.separator + ConsoleConfigUtils.CFG_FILE_NAME);
+ ConsoleConfigUtils.createConsoleConfig(PROJ_NAME, cfgFilePath, CONSOLE_NAME);
+ ConsoleConfiguration cc = KnownConfigurations.getInstance().find(CONSOLE_NAME);
+ assertNotNull("Console Configuration not found", cc); //$NON-NLS-1$
+ cc.build();
+
+ final String codeCompletionPlaceMarker = " from "; //$NON-NLS-1$
+ final String query = "select\t \tt1." + codeCompletionPlaceMarker +
//$NON-NLS-1$
+ project.getFullyQualifiedTestClassName() + " t1"; //$NON-NLS-1$
+ IEditorPart editorPart = HibernateConsolePlugin.getDefault()
+ .openScratchHQLEditor(CONSOLE_NAME, query);
+ assertTrue("Opened editor is not HQLEditor", editorPart instanceof
HQLEditor); //$NON-NLS-1$
+
+ HQLEditor editor = (HQLEditor)editorPart;
+ assertEquals(editor.getEditorText(), query);
+
+ QueryInputModel model = editor.getQueryInputModel();
+ assertTrue(model.getParameterCount() == 0);
+
+ editor.setConsoleConfigurationName(CONSOLE_NAME);
+ IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
+
+ HQLCompletionProcessor processor = new HQLCompletionProcessor(editor);
+
+ int position = query.indexOf(codeCompletionPlaceMarker);
+ ICompletionProposal[] proposals = processor.computeCompletionProposals(doc, position);
+ assertTrue(proposals.length > 0);
+ cc.reset();
+ }
}