Search in sources :

Example 1 with AddExclusionOperation

use of org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation in project m2e-core-tests by tesla.

the class AddExclusionOperationTest method testMissingDependency_withDependencies.

@Test
public void testMissingDependency_withDependencies() throws Exception {
    // 
    document.setText(// 
    StructuredModelManager.getModelManager(), // 
    "<project><dependencies>" + // 
    "<dependency><groupId>AAA</groupId><artifactId>BBB</artifactId><version>1.0</version></dependency>" + // 
    "<dependency><groupId>AAAB</groupId><artifactId>BBB</artifactId><version>1.0</version></dependency>" + "</dependencies></project>");
    PomEdits.performOnDOMDocument(new OperationTuple(tempModel, new AddExclusionOperation(d, e)));
    assertEquals("Expected no dependency: " + d.toString() + "\n" + document.getText(), 0, dependencyCount(tempModel, d));
    assertEquals("Dependency Count: \n" + document.getText(), 2, getDependencyCount(tempModel));
}
Also used : OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) AddExclusionOperation(org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation) Test(org.junit.Test)

Example 2 with AddExclusionOperation

use of org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation in project m2e-core-tests by tesla.

the class AddExclusionOperationTest method testMissingDependency_emptyDependenciesElement.

@Test
public void testMissingDependency_emptyDependenciesElement() throws Exception {
    // 
    document.setText(// 
    StructuredModelManager.getModelManager(), // 
    "<project><dependencies>" + "</dependencies></project>");
    PomEdits.performOnDOMDocument(new OperationTuple(tempModel, new AddExclusionOperation(d, e)));
    assertEquals("Expected no dependency: " + d.toString() + "\n" + document.getText(), 0, dependencyCount(tempModel, d));
}
Also used : OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) AddExclusionOperation(org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation) Test(org.junit.Test)

Example 3 with AddExclusionOperation

use of org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation in project m2e-core-tests by tesla.

the class AddExclusionOperationTest method testAddExclusion.

@Test
public void testAddExclusion() throws Exception {
    // 
    document.setText(// 
    StructuredModelManager.getModelManager(), // 
    "<project><dependencies>" + // 
    "<dependency><groupId>AAA</groupId><artifactId>BBB</artifactId><version>1.0</version></dependency>" + // 
    "<dependency><groupId>AAAB</groupId><artifactId>BBB</artifactId><version>1.0</version></dependency>" + // 
    "<dependency><groupId>AAA</groupId><artifactId>BBBB</artifactId><version>1.0</version></dependency>" + "</dependencies></project>");
    PomEdits.performOnDOMDocument(new OperationTuple(tempModel, new AddExclusionOperation(d, e)));
    assertEquals("Expected no dependency: " + d.toString() + "\n" + document.getText(), 1, dependencyCount(tempModel, d));
    assertTrue("Has exclusion " + e.toString() + "\n" + document.getText(), hasExclusion(tempModel, d, e));
    assertEquals("Exclusions", 1, getExclusionCount(tempModel, d));
    assertEquals("Dependency Count: \n" + document.getText(), 3, getDependencyCount(tempModel));
}
Also used : OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) AddExclusionOperation(org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation) Test(org.junit.Test)

Example 4 with AddExclusionOperation

use of org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation in project m2e-core by eclipse-m2e.

the class ExcludeArtifactRefactoring method checkFinalConditions0.

RefactoringStatus checkFinalConditions0(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    if (hierarchy == null || exclusionPoint == null) {
        return RefactoringStatus.createFatalErrorStatus(Messages.ExcludeArtifactRefactoring_unableToLocateProject);
    }
    changes = new ArrayList<>();
    Set<ArtifactKey> locatedKeys = new HashSet<>();
    List<IStatus> statuses = new ArrayList<>();
    SubMonitor monitor = SubMonitor.convert(pm, 3);
    List<Operation> exclusionOp = new ArrayList<>();
    // Exclusion point
    for (Entry<Dependency, Set<ArtifactKey>> entry : getDependencyExcludes(exclusionPoint, monitor.newChild(1)).entrySet()) {
        locatedKeys.addAll(entry.getValue());
        Dependency dependency = entry.getKey();
        if (contains(entry.getValue(), dependency)) {
            exclusionOp.add(new RemoveDependencyOperation(dependency));
        } else {
            for (ArtifactKey key : entry.getValue()) {
                if (!hasExclusion(exclusionPoint, dependency, key)) {
                    exclusionOp.add(new AddExclusionOperation(dependency, key));
                }
            }
        }
    }
    // Below exclusion point - pull up dependency to exclusion point
    for (ParentHierarchyEntry project : getWorkspaceDescendants()) {
        List<Operation> operations = new ArrayList<>();
        for (Entry<Dependency, Set<ArtifactKey>> entry : getDependencyExcludes(project, monitor.newChild(1)).entrySet()) {
            locatedKeys.addAll(entry.getValue());
            Dependency dependency = entry.getKey();
            operations.add(new RemoveDependencyOperation(dependency));
            if (!contains(entry.getValue(), dependency)) {
                if (!hasDependency(exclusionPoint, dependency)) {
                    exclusionOp.add(new AddDependencyOperation(dependency));
                }
                for (ArtifactKey key : entry.getValue()) {
                    if (!hasExclusion(exclusionPoint, dependency, key)) {
                        exclusionOp.add(new AddExclusionOperation(dependency, key));
                    }
                }
            }
        }
        if (operations.size() > 0) {
            IFile pom = project.getResource();
            changes.add(PomHelper.createChange(pom, new CompoundOperation(operations.toArray(new Operation[operations.size()])), getName(pom)));
        }
    }
    // Above exclusion - Add dep to exclusionPoint
    for (ParentHierarchyEntry project : getWorkspaceAncestors()) {
        for (Entry<Dependency, Set<ArtifactKey>> entry : getDependencyExcludes(project, monitor.newChild(1)).entrySet()) {
            locatedKeys.addAll(entry.getValue());
            Dependency dependency = entry.getKey();
            if (contains(entry.getValue(), dependency)) {
                IFile pom = project.getResource();
                if (pom != null) {
                    statuses.add(new Status(IStatus.INFO, PLUGIN_ID, NLS.bind(Messages.ExcludeArtifactRefactoring_removeDependencyFrom, toString(dependency), pom.getFullPath())));
                    changes.add(PomHelper.createChange(pom, new RemoveDependencyOperation(dependency), getName(pom)));
                }
            } else {
                exclusionOp.add(new AddDependencyOperation(dependency));
                for (ArtifactKey key : entry.getValue()) {
                    if (!hasExclusion(exclusionPoint, dependency, key)) {
                        exclusionOp.add(new AddExclusionOperation(dependency, key));
                    }
                }
            }
        }
    }
    if (!exclusionOp.isEmpty()) {
        IFile pom = exclusionPoint.getResource();
        changes.add(PomHelper.createChange(pom, new CompoundOperation(exclusionOp.toArray(new Operation[exclusionOp.size()])), getName(pom)));
    }
    if (statuses.size() == 1) {
        return RefactoringStatus.create(statuses.get(0));
    } else if (statuses.size() > 1) {
        return RefactoringStatus.create(new MultiStatus(PLUGIN_ID, 0, statuses.toArray(new IStatus[statuses.size()]), Messages.ExcludeArtifactRefactoring_errorCreatingRefactoring, null));
    } else if (locatedKeys.isEmpty()) {
        return RefactoringStatus.createFatalErrorStatus(Messages.ExcludeArtifactRefactoring_noTargets);
    } else if (locatedKeys.size() != excludes.length) {
        StringBuilder sb = new StringBuilder();
        for (ArtifactKey key : excludes) {
            if (!locatedKeys.contains(key)) {
                sb.append(key.toString()).append(',');
            }
        }
        sb.deleteCharAt(sb.length() - 1);
        return RefactoringStatus.createErrorStatus(NLS.bind(Messages.ExcludeArtifactRefactoring_failedToLocateArtifact, sb.toString()));
    }
    return new RefactoringStatus();
}
Also used : ParentHierarchyEntry(org.eclipse.m2e.core.ui.internal.util.ParentHierarchyEntry) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) HashSet(java.util.HashSet) Set(java.util.Set) IFile(org.eclipse.core.resources.IFile) AddDependencyOperation(org.eclipse.m2e.core.ui.internal.editing.AddDependencyOperation) RemoveDependencyOperation(org.eclipse.m2e.core.ui.internal.editing.RemoveDependencyOperation) ArrayList(java.util.ArrayList) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) AddExclusionOperation(org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation) AddDependencyOperation(org.eclipse.m2e.core.ui.internal.editing.AddDependencyOperation) RemoveDependencyOperation(org.eclipse.m2e.core.ui.internal.editing.RemoveDependencyOperation) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) Dependency(org.apache.maven.model.Dependency) CompoundOperation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation) AddExclusionOperation(org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation) HashSet(java.util.HashSet)

Example 5 with AddExclusionOperation

use of org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation in project m2e-core-tests by tesla.

the class AddExclusionOperationTest method testAddExclusion_existingExclusion.

@Test
public void testAddExclusion_existingExclusion() throws Exception {
    document.setText(// 
    StructuredModelManager.getModelManager(), "<project><dependencies>" + // 
    "<dependency><groupId>AAA</groupId><artifactId>BBB</artifactId><version>1.0</version></dependency>" + // 
    "<dependency><groupId>AAAB</groupId><artifactId>BBB</artifactId><version>1.0</version></dependency>" + // 
    "<dependency><groupId>AAA</groupId><artifactId>BBBB</artifactId><version>1.0</version>" + // 
    "<exclusions><exclusion><groupId>g</groupId><artifactId>b</artifactId><version>1.0</version></exclusion></exclusions></dependency>" + // 
    "</dependencies></project>");
    PomEdits.performOnDOMDocument(new OperationTuple(tempModel, new AddExclusionOperation(d, e)));
    assertEquals("Expected no dependency: " + d.toString() + "\n" + document.getText(), 1, dependencyCount(tempModel, d));
    assertTrue("Has exclusion " + e.toString() + "\n" + document.getText(), hasExclusion(tempModel, d, e));
    ArtifactKey key = new ArtifactKey("g", "b", "1.0", null);
    assertTrue("Existing Exclusion Present " + key.toString() + "\n" + document.getText(), hasExclusion(tempModel, d, key));
    assertEquals("Exclusions", 2, getExclusionCount(tempModel, d));
    assertEquals("Dependency Count: \n" + document.getText(), 3, getDependencyCount(tempModel));
}
Also used : OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) AddExclusionOperation(org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation) Test(org.junit.Test)

Aggregations

AddExclusionOperation (org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation)7 OperationTuple (org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple)6 Test (org.junit.Test)6 ArtifactKey (org.eclipse.m2e.core.embedder.ArtifactKey)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Dependency (org.apache.maven.model.Dependency)1 IFile (org.eclipse.core.resources.IFile)1 IStatus (org.eclipse.core.runtime.IStatus)1 MultiStatus (org.eclipse.core.runtime.MultiStatus)1 Status (org.eclipse.core.runtime.Status)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)1 AddDependencyOperation (org.eclipse.m2e.core.ui.internal.editing.AddDependencyOperation)1 CompoundOperation (org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation)1 Operation (org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation)1 RemoveDependencyOperation (org.eclipse.m2e.core.ui.internal.editing.RemoveDependencyOperation)1 ParentHierarchyEntry (org.eclipse.m2e.core.ui.internal.util.ParentHierarchyEntry)1