Search in sources :

Example 11 with IErlProject

use of org.erlide.engine.model.root.IErlProject in project erlide_eclipse by erlang.

the class DialyzerUtilsTest method dialyzeWithExternalInclude.

@Test
public void dialyzeWithExternalInclude() throws Exception {
    // http://www.assembla.com/spaces/erlide/tickets/608-dialyzer---navigate-to-external-includes-from-markers
    File externalFile = null;
    IErlProject erlProject = null;
    File externalIncludesFile = null;
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    try {
        // given
        // an erlang project and an external file not in any project
        final String projectName = "testproject";
        erlProject = ErlideTestUtils.createErlProject(projectName);
        final String externalFileName = "external9.hrl";
        externalFile = ErlideTestUtils.createTmpFile(externalFileName, "f([_ | _]=L) ->\n    atom_to_list(L).\n");
        externalIncludesFile = ErlideTestUtils.createTmpFile("external_includes", externalFile.getAbsolutePath());
        DialyzerMarkerUtils.removeDialyzerMarkersFor(root);
        final IProject project = erlProject.getWorkspaceProject();
        if (!project.isOpen()) {
            project.open(null);
        }
        final IPath location = new Path(externalFile.getAbsolutePath());
        final IFile file = project.getFile(location.lastSegment());
        file.createLink(location, IResource.NONE, null);
        // when
        // putting dialyzer warning markers on the external file
        final String message = "test message";
        final int lineNumber = 2;
        final IErlElementLocator model = ErlangEngine.getInstance().getModel();
        DialyzerMarkerUtils.addDialyzerWarningMarker(model, externalFile.getAbsolutePath(), lineNumber, message);
        // then
        // the marker should have the proper file name and the include file
        // should appear in External Files
        final IMarker[] markers = root.findMarkers(DialyzerMarkerUtils.DIALYZE_WARNING_MARKER, true, IResource.DEPTH_INFINITE);
        assertWithMessage("Markers count").that(markers.length).isGreaterThan(0);
        for (final IMarker marker : markers) {
            // for some reason, when running on Hudson, we get two identical
            // markers...
            final String path = (String) marker.getAttribute(DialyzerMarkerUtils.PATH_ATTRIBUTE);
            final IPath p = new Path(path);
            assertEquals(externalFileName, p.lastSegment());
            assertWithMessage("line number").that(lineNumber).isEqualTo(marker.getAttribute(IMarker.LINE_NUMBER));
            assertEquals(message, marker.getAttribute(IMarker.MESSAGE));
        }
    } finally {
        DialyzerMarkerUtils.removeDialyzerMarkersFor(root);
        if (externalIncludesFile != null && externalIncludesFile.exists()) {
            externalIncludesFile.delete();
        }
        if (externalFile != null && externalFile.exists()) {
            externalFile.delete();
        }
        if (erlProject != null) {
            ErlideTestUtils.deleteProject(erlProject);
        }
    }
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IPath(org.eclipse.core.runtime.IPath) IErlElementLocator(org.erlide.engine.model.root.IErlElementLocator) IMarker(org.eclipse.core.resources.IMarker) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IProject(org.eclipse.core.resources.IProject) Test(org.junit.Test)

Example 12 with IErlProject

use of org.erlide.engine.model.root.IErlProject in project erlide_eclipse by erlang.

the class DialyzerUtilsTest method dialyzeMarkerOnFile.

@Test
public void dialyzeMarkerOnFile() throws Exception {
    IErlProject erlProject = null;
    try {
        // given
        // an erlang module in an erlang project
        final String projectName = "testproject";
        erlProject = ErlideTestUtils.createErlProject(projectName);
        final String moduleName = "test.erl";
        final IErlModule erlModule = ErlideTestUtils.createModule(erlProject, moduleName, "-module(test).\n-export([f/0]).\n-f() ->\n    atom_to_list(\"hej\").\n");
        IMarker[] markers = erlProject.getWorkspaceProject().findMarkers(DialyzerMarkerUtils.DIALYZE_WARNING_MARKER, true, IResource.DEPTH_INFINITE);
        assertEquals(0, markers.length);
        // when
        // putting a dialyzer warning on it
        final int lineNumber = 3;
        final String message = "test message";
        final IErlElementLocator model = ErlangEngine.getInstance().getModel();
        DialyzerMarkerUtils.addDialyzerWarningMarker(model, erlModule.getResource().getLocation().toPortableString(), lineNumber, message);
        // then
        // there should be a marker with proper file name and the proper
        // line number
        markers = erlProject.getWorkspaceProject().findMarkers(DialyzerMarkerUtils.DIALYZE_WARNING_MARKER, true, IResource.DEPTH_INFINITE);
        assertEquals(1, markers.length);
        final IMarker marker = markers[0];
        assertEquals(moduleName, marker.getResource().getName());
        assertEquals(lineNumber, marker.getAttribute(IMarker.LINE_NUMBER));
        assertEquals(message, marker.getAttribute(IMarker.MESSAGE));
    } finally {
        if (erlProject != null) {
            ErlideTestUtils.deleteProject(erlProject);
        }
    }
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IErlModule(org.erlide.engine.model.root.IErlModule) IErlElementLocator(org.erlide.engine.model.root.IErlElementLocator) IMarker(org.eclipse.core.resources.IMarker) Test(org.junit.Test)

Example 13 with IErlProject

use of org.erlide.engine.model.root.IErlProject in project erlide_eclipse by erlang.

the class ErlModelCacheTest method checkThatCachesAreEmptyWhenProjectIsRemoved.

@Test
public void checkThatCachesAreEmptyWhenProjectIsRemoved() throws CoreException {
    IErlProject project = null;
    try {
        // given
        // a project with a module, and some searches that fills the model
        // cache
        final String projectName = "testprojectx";
        project = ErlideTestUtils.createErlProject(projectName);
        final String moduleName = "f.erl";
        final IErlModule module = ErlideTestUtils.createModule(project, moduleName, "-module(f).\n-include(\"a.hrl\").\n-export([f/0]).\n-record(rec2, {a, b}).\n" + "f() ->\n    lists:reverse([1, 0]),\n    lists:reverse([1, 0], [2]).\n");
        module.open(null);
        final IErlElementLocator model = ErlangEngine.getInstance().getModel();
        final IErlModule module2 = model.findModuleFromProject(project, moduleName, null, IErlElementLocator.Scope.PROJECT_ONLY);
        // final ErlModelCache cache = ErlModelCache.getDefault();
        // final Set<IErlModule> modulesByName2 = cache
        // .getModulesByName(ListsUtils.withoutExtension(moduleName));
        // when
        // deleting the project
        ErlideTestUtils.deleteProject(project);
        // then
        // the model cache shouldn't know about the module anymore
        assertEquals(module2, module);
    // final Set<IErlModule> modulesByName = cache
    // .getModulesByName(ListsUtils.withoutExtension(moduleName));
    // assertTrue(!modulesByName2.isEmpty());
    // assertTrue(modulesByName.isEmpty());
    } finally {
        if (project != null && project.exists()) {
            ErlideTestUtils.deleteProject(project);
        }
    }
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IErlModule(org.erlide.engine.model.root.IErlModule) IErlElementLocator(org.erlide.engine.model.root.IErlElementLocator) Test(org.junit.Test)

Example 14 with IErlProject

use of org.erlide.engine.model.root.IErlProject in project erlide_eclipse by erlang.

the class ErlModelCacheTest method checkThatNewModulesInNewProjectsAreCorrect.

@Test
public void checkThatNewModulesInNewProjectsAreCorrect() throws Exception {
    IErlProject project = null;
    IErlProject project2 = null;
    try {
        // given
        // a project with an external module and searching for it so the
        // cache
        // is updated
        final String projectName = "testprojecta";
        project = ErlideTestUtils.createErlProject(projectName);
        final String externalName = "xyz.erl";
        final File externalFile = ErlideTestUtils.createTmpFile(externalName, "-module(xyz).\nf([_ | _]=L ->\n    atom_to_list(L).\n");
        final String absolutePath = externalFile.getAbsolutePath();
        final File externalsFile = ErlideTestUtils.createTmpFile("x.erlidex", absolutePath);
        ((ErlProject) project).setExternalModulesFile(externalsFile.getAbsolutePath());
        project.open(null);
        final IErlElementLocator model = ErlangEngine.getInstance().getModel();
        final IErlModule findModule = model.findModuleFromProject(project, externalName, null, IErlElementLocator.Scope.PROJECT_ONLY);
        // final ErlModelCache cache = ErlModelCache.getDefault();
        // final Set<IErlModule> modulesByName = cache
        // .getModulesByName(ListsUtils
        // .withoutExtension(externalName));
        // when
        // creating a new project with a module with the same name and
        // searching
        // for it
        final String projectName2 = "testprojectb";
        project2 = ErlideTestUtils.createErlProject(projectName2);
        final IErlModule module = ErlideTestUtils.createModule(project2, externalName, "-module(xyz).\n");
        final IErlModule findModule2 = model.findModuleFromProject(project, externalName, null, IErlElementLocator.Scope.ALL_PROJECTS);
        // final Set<IErlModule> modulesByName2 = cache
        // .getModulesByName(ListsUtils
        // .withoutExtension(externalName));
        // then
        // the new module should be found
        assertNotNull(findModule);
        assertEquals(module, findModule2);
    // assertTrue(modulesByName2.contains(module));
    // final SetView<IErlModule> difference = Sets.difference(
    // modulesByName2, modulesByName);
    // assertEquals(1, difference.size());
    // assertEquals(module, difference.toArray()[0]);
    } finally {
        if (project != null && project.exists()) {
            ErlideTestUtils.deleteProject(project);
        }
        if (project2 != null && project2.exists()) {
            ErlideTestUtils.deleteProject(project2);
        }
    }
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) ErlProject(org.erlide.engine.internal.model.root.ErlProject) IErlProject(org.erlide.engine.model.root.IErlProject) IErlModule(org.erlide.engine.model.root.IErlModule) IErlElementLocator(org.erlide.engine.model.root.IErlElementLocator) File(java.io.File) Test(org.junit.Test)

Example 15 with IErlProject

use of org.erlide.engine.model.root.IErlProject in project erlide_eclipse by erlang.

the class ProjectCreator method createProject.

public IProject createProject() throws CoreException {
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final IProject newProjectHandle = workspace.getRoot().getProject(name);
    if (newProjectHandle.exists()) {
        throw new CoreException(Status.OK_STATUS);
    }
    final IProjectDescription description = workspace.newProjectDescription(name);
    description.setLocationURI(location);
    // // update the referenced project if provided
    if (referencedProjects != null) {
        description.setReferencedProjects(referencedProjects);
    }
    // create the new project operation
    final IRunnableWithProgress op = new IRunnableWithProgress() {

        @Override
        public void run(final IProgressMonitor monitor) throws InvocationTargetException {
            final CreateProjectOperation op1 = new CreateProjectOperation(description, WizardMessages.NewProject_windowTitle);
            try {
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=219901
                // Making this undoable would be a bad idea
                op1.execute(monitor, notifier);
                newProjectHandle.open(monitor);
                description.setNatureIds(new String[] { ErlangCore.NATURE_ID });
                newProjectHandle.setDescription(description, null);
                final BuilderTool builder = info.getBuilder();
                ErlangNature.setErlangProjectBuilder(newProjectHandle, builder);
                createBuilderConfig(builder);
                createFolders(newProjectHandle, Lists.newArrayList(info.getOutputDir()), monitor);
                createFolders(newProjectHandle, info.getSourceDirs(), monitor);
                createFolders(newProjectHandle, info.getIncludeDirs(), monitor);
                createConfig(newProjectHandle, info.getConfigType(), monitor);
                final IErlProject erlProject = ErlangEngine.getInstance().getModel().getErlangProject(newProjectHandle);
                erlProject.setConfigType(info.getConfigType());
                final BuilderProperties builderProperties = new BuilderProperties();
                builderProperties.setBuilderTool(builder);
                builderProperties.setCompileTarget(info.getBuilderData().get("compile"));
                builderProperties.setCleanTarget(info.getBuilderData().get("clean"));
                erlProject.setBuilderProperties(builderProperties);
                erlProject.setProperties(info);
            } catch (final Exception e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    // run the new project creation operation
    try {
        context.run(false, true, op);
    } catch (final InterruptedException e) {
        return null;
    } catch (final InvocationTargetException e) {
        final Throwable t = e.getTargetException();
        if (t instanceof ExecutionException && t.getCause() instanceof CoreException) {
            final CoreException cause = (CoreException) t.getCause();
            StatusAdapter status;
            if (cause.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) {
                status = new StatusAdapter(StatusUtil.newStatus(IStatus.WARNING, NLS.bind(WizardMessages.NewProject_caseVariantExistsError, newProjectHandle.getName()), cause));
            } else {
                status = new StatusAdapter(StatusUtil.newStatus(cause.getStatus().getSeverity(), WizardMessages.NewProject_errorMessage, cause));
            }
            status.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, WizardMessages.NewProject_errorMessage);
            StatusManager.getManager().handle(status, StatusManager.BLOCK);
        } else {
            final StatusAdapter status = new StatusAdapter(new Status(IStatus.WARNING, ErlideUIPlugin.PLUGIN_ID, 0, NLS.bind(WizardMessages.NewProject_internalError, t.getMessage()), t));
            status.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, WizardMessages.NewProject_errorMessage);
            StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.BLOCK);
        }
        return null;
    }
    return newProjectHandle;
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IResourceStatus(org.eclipse.core.resources.IResourceStatus) StatusAdapter(org.eclipse.ui.statushandlers.StatusAdapter) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) BuilderProperties(org.erlide.engine.model.builder.BuilderProperties) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IProjectDescription(org.eclipse.core.resources.IProjectDescription) CreateProjectOperation(org.eclipse.ui.ide.undo.CreateProjectOperation) ExecutionException(org.eclipse.core.commands.ExecutionException) BuilderTool(org.erlide.engine.model.builder.BuilderTool)

Aggregations

IErlProject (org.erlide.engine.model.root.IErlProject)123 IErlModule (org.erlide.engine.model.root.IErlModule)57 Test (org.junit.Test)47 IPath (org.eclipse.core.runtime.IPath)30 IProject (org.eclipse.core.resources.IProject)29 ErlProject (org.erlide.engine.internal.model.root.ErlProject)25 File (java.io.File)19 Path (org.eclipse.core.runtime.Path)16 CoreException (org.eclipse.core.runtime.CoreException)13 ErlModelException (org.erlide.engine.model.ErlModelException)13 IErlElementLocator (org.erlide.engine.model.root.IErlElementLocator)13 IErlModel (org.erlide.engine.model.root.IErlModel)13 ArrayList (java.util.ArrayList)11 IErlElement (org.erlide.engine.model.IErlElement)10 IFile (org.eclipse.core.resources.IFile)9 IResource (org.eclipse.core.resources.IResource)9 HashSet (java.util.HashSet)7 IProjectDescription (org.eclipse.core.resources.IProjectDescription)7 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)6 IErlPreprocessorDef (org.erlide.engine.model.erlang.IErlPreprocessorDef)6