Skip to content

Commit

Permalink
JBIDE-23862 Enable CDI support when beans xml is created and cdi lib …
Browse files Browse the repository at this point in the history
…is in build path

Signed-off-by: Rastislav Wagner <rawagner@redhat.com>
  • Loading branch information
rawagner committed Aug 14, 2017
1 parent c2e2c4a commit 246bf84
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 4 deletions.
Expand Up @@ -26,9 +26,11 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
Expand All @@ -52,6 +54,7 @@
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
import org.jboss.tools.cdi.core.CDICorePlugin;
import org.jboss.tools.cdi.core.CDIImages;
import org.jboss.tools.cdi.core.CDIUtil;
import org.jboss.tools.cdi.core.CDIVersion;
import org.jboss.tools.cdi.ui.CDIUIMessages;
import org.jboss.tools.cdi.ui.CDIUIPlugin;
Expand Down Expand Up @@ -154,11 +157,20 @@ public boolean performFinish() {
DialogUtil.openError(dw.getShell(), ResourceMessages.FileResource_errorMessage, e.getMessage(), e);
}
}
IProject project = mainPage.getSelectedProject();
try {
if(CDIUtil.getCDIImplementationVersion(project) != null) {
CDIUtil.enableCDI(project, false, new NullProgressMonitor());
}
} catch (CoreException e) {
CDICorePlugin.getDefault().logError(e);
}
return true;
}

class WizardNewBeansXMLFileCreationPage extends WizardNewFileCreationPage {
IFieldEditor versionEditor = null;
IProject selectedProject = null;

public WizardNewBeansXMLFileCreationPage(String pageName, IStructuredSelection selection) {
super(pageName, selection);
Expand All @@ -182,19 +194,19 @@ protected void initialPopulateContainerNameField() {
if(r != null) {
boolean needMetaInf = false;
IPath current = getContainerFullPath();
IProject p = r.getProject();
selectedProject = r.getProject();
//Prefer location of existing beans.xml to any other location.
IPath path = getContainerWithExistingBeansXML(p);
IPath path = getContainerWithExistingBeansXML(selectedProject);

if(path == null) {
//If no beans.xml exist, prefer WEB-INF if it exists
path = ProjectHome.getWebInfPath(p);
path = ProjectHome.getWebInfPath(selectedProject);
}
if(current != null && current.equals(path)) {
return;
}
if(path == null) {
Set<IFolder> fs = EclipseResourceUtil.getSourceFolders(p);
Set<IFolder> fs = EclipseResourceUtil.getSourceFolders(selectedProject);
for (IFolder f: fs) {
IFolder fm = f.getFolder("META-INF");
if(!fm.exists()) {
Expand Down Expand Up @@ -323,6 +335,10 @@ String getInitialCDIVersion() {
}
return CDIVersion.getLatestDefaultVersion().toString();
}

IProject getSelectedProject() {
return selectedProject;
}

}

Expand Down
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/cdi-api-1.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CDITestCDISupportNotEnabled</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,12 @@
#Thu Sep 02 19:40:42 MSD 2010
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
Binary file not shown.
Expand Up @@ -31,6 +31,7 @@
import org.jboss.tools.cdi.ui.test.validation.java.CDIAsYouTypeInJavaSupressWarningsTest;
import org.jboss.tools.cdi.ui.test.validation.java.CDIAsYouTypeInJavaValidationTest;
import org.jboss.tools.cdi.ui.test.wizard.AddQualifiersToBeanWizardTest;
import org.jboss.tools.cdi.ui.test.wizard.NewBeansXMLCreationWizardTest;
import org.jboss.tools.cdi.ui.test.wizard.NewCDIClassWizardFactoryTest;
import org.jboss.tools.cdi.ui.test.wizard.NewCDIWebProjectWizardTest;
import org.jboss.tools.cdi.ui.test.wizard.NewCDIWizardTest;
Expand Down Expand Up @@ -64,6 +65,7 @@ public static Test suite() {
suiteAll.addTestSuite(CDIPreferencePageTest.class);
suiteAll.addTestSuite(NewCDIWizardTest.class);
suiteAll.addTestSuite(NewCDIWebProjectWizardTest.class);
suiteAll.addTestSuite(NewBeansXMLCreationWizardTest.class);

suite.addTestSuite(CAELProposalFilteringTest.class);
suite.addTestSuite(CDISearchParticipantTest.class);
Expand Down
@@ -0,0 +1,92 @@
/*******************************************************************************
* Copyright (c) 2017 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.cdi.ui.test.wizard;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.jboss.tools.cdi.core.CDICoreNature;
import org.jboss.tools.cdi.core.CDIVersion;
import org.jboss.tools.cdi.ui.wizard.NewBeansXMLCreationWizard;
import org.jboss.tools.test.util.JobUtils;
import org.jboss.tools.test.util.ResourcesUtils;
import org.jboss.tools.test.util.WorkbenchUtils;

import junit.framework.TestCase;

public class NewBeansXMLCreationWizardTest extends TestCase{

static NewBeansXMLCreationWizard wizard;
static WizardDialog dialog;
static IProject project;
static IProject projectWithoutCDI;

protected void setUp() throws Exception {
project = ResourcesUtils.importProject("org.jboss.tools.cdi.core.test", "/projects/CDITestCDISupportNotEnabled");
project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

projectWithoutCDI = ResourcesUtils.importProject("org.jboss.tools.cdi.core.test", "/projects/FacetedProject");
projectWithoutCDI.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
JobUtils.waitForIdle();
}

protected void tearDown() throws CoreException {
if(project != null) {
project.delete(true, true, new NullProgressMonitor());
}
if(projectWithoutCDI != null) {
projectWithoutCDI.delete(true, true, new NullProgressMonitor());
}
if(dialog != null) {
dialog.close();
}
}

private static void cleanDefferedEvents() {
while (Display.getCurrent().readAndDispatch());
}

public void testCreateBeansXMLAndEnableCDI() throws CoreException {
assertNull(project.getNature(CDICoreNature.NATURE_ID));
openWizard(project);
wizard.setVersion(CDIVersion.CDI_1_0);
wizard.performFinish();
assertNotNull(project.getNature(CDICoreNature.NATURE_ID));
}

//CDI support should not be enabled
public void testCreateBeansXMLNoCDIDependency() throws CoreException {
assertNull(projectWithoutCDI.getNature(CDICoreNature.NATURE_ID));
openWizard(projectWithoutCDI);
wizard.setVersion(CDIVersion.CDI_1_0);
wizard.performFinish();
assertNull(projectWithoutCDI.getNature(CDICoreNature.NATURE_ID));
}

private void openWizard(IProject project) {
wizard= (NewBeansXMLCreationWizard)WorkbenchUtils.findWizardByDefId(NewBeansXMLCreationWizard.WIZARD_ID);
wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(project));

dialog = new WizardDialog(
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
wizard);
dialog.create();
dialog.setBlockOnOpen(false);
dialog.open();
cleanDefferedEvents();
}

}

0 comments on commit 246bf84

Please sign in to comment.