|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024, 2026 Advantest Europe GmbH and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Raghunandana Murthappa |
| 13 | + * Felix Schmid - adapted for copy refactoring test |
| 14 | + *******************************************************************************/ |
| 15 | +package org.eclipse.ltk.core.refactoring.tests.participants; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 19 | + |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | + |
| 24 | +import org.eclipse.core.runtime.CoreException; |
| 25 | +import org.eclipse.core.runtime.IPath; |
| 26 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 27 | +import org.eclipse.core.runtime.OperationCanceledException; |
| 28 | + |
| 29 | +import org.eclipse.core.resources.IContainer; |
| 30 | +import org.eclipse.core.resources.IFile; |
| 31 | +import org.eclipse.core.resources.IFolder; |
| 32 | +import org.eclipse.core.resources.IResource; |
| 33 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 34 | + |
| 35 | +import org.eclipse.text.edits.MultiTextEdit; |
| 36 | +import org.eclipse.text.edits.ReplaceEdit; |
| 37 | + |
| 38 | +import org.eclipse.ltk.core.refactoring.Change; |
| 39 | +import org.eclipse.ltk.core.refactoring.CheckConditionsOperation; |
| 40 | +import org.eclipse.ltk.core.refactoring.PerformRefactoringOperation; |
| 41 | +import org.eclipse.ltk.core.refactoring.RefactoringStatus; |
| 42 | +import org.eclipse.ltk.core.refactoring.TextFileChange; |
| 43 | +import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; |
| 44 | +import org.eclipse.ltk.core.refactoring.participants.CopyArguments; |
| 45 | +import org.eclipse.ltk.core.refactoring.participants.CopyParticipant; |
| 46 | +import org.eclipse.ltk.core.refactoring.participants.CopyProcessor; |
| 47 | +import org.eclipse.ltk.core.refactoring.participants.CopyRefactoring; |
| 48 | +import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; |
| 49 | +import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog; |
| 50 | +import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; |
| 51 | +import org.eclipse.ltk.core.refactoring.resource.CopyResourceChange; |
| 52 | +import org.eclipse.ltk.core.refactoring.tests.util.SimpleTestProject; |
| 53 | + |
| 54 | +class CopyRefactoringWithRefUpdateTest { |
| 55 | + |
| 56 | + private SimpleTestProject project; |
| 57 | + |
| 58 | + private static class RefUpdateParticipant extends CopyParticipant { |
| 59 | + private IFile file; |
| 60 | + private IContainer destination; |
| 61 | + |
| 62 | + @Override |
| 63 | + protected boolean initialize(Object element) { |
| 64 | + file= (IFile) element; |
| 65 | + destination= (IContainer) getArguments().getDestination(); |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String getName() { |
| 71 | + return "copy participant"; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context) throws OperationCanceledException { |
| 76 | + return new RefactoringStatus(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { |
| 81 | + IPath path = destination.getFullPath().append(file.getName()); |
| 82 | + return new CopyFileChange(path); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static class CopyFileChange extends Change { |
| 87 | + |
| 88 | + private final IPath newFile; |
| 89 | + |
| 90 | + protected CopyFileChange(final IPath newFile) { |
| 91 | + this.newFile = newFile; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void initializeValidationData(final IProgressMonitor pm) { |
| 96 | + // nothing to do |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public RefactoringStatus isValid(final IProgressMonitor pm) throws CoreException, OperationCanceledException { |
| 101 | + return new RefactoringStatus(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String getName() { |
| 106 | + return "copy file change"; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Change perform(final IProgressMonitor pm) throws CoreException { |
| 111 | + IFile file = (IFile) ResourcesPlugin.getWorkspace().getRoot().findMember(newFile); |
| 112 | + |
| 113 | + TextFileChange result= new TextFileChange("", file); |
| 114 | + MultiTextEdit root= new MultiTextEdit(); |
| 115 | + root.addChild(new ReplaceEdit(9, 12, "destFolder")); |
| 116 | + result.setEdit(root); |
| 117 | + result.perform(pm); |
| 118 | + |
| 119 | + return null; // no undo change necessary, the element will be deleted |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public IResource getModifiedElement() { |
| 124 | + return null; // not needed for test |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Object[] getAffectedObjects() { |
| 129 | + return null; // not needed for test |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static class TestCopyProcessor extends CopyProcessor { |
| 134 | + |
| 135 | + private IFile origin; |
| 136 | + private IFolder destination; |
| 137 | + private ReorgExecutionLog log; |
| 138 | + |
| 139 | + public TestCopyProcessor(IFile origin, IFolder destination) { |
| 140 | + this.origin = origin; |
| 141 | + this.destination = destination; |
| 142 | + log = new ReorgExecutionLog(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public Object[] getElements() { |
| 147 | + return new Object[] { origin }; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public String getIdentifier() { |
| 152 | + return "org.eclipse.ltk.core.refactoring.tests.CopyProcessor"; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public String getProcessorName() { |
| 157 | + return "copy processor"; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public boolean isApplicable() throws CoreException { |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { |
| 167 | + return new RefactoringStatus(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException, OperationCanceledException { |
| 172 | + return new RefactoringStatus(); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { |
| 177 | + return new CopyResourceChange(origin, log, destination); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants sharedParticipants) throws CoreException { |
| 182 | + RefUpdateParticipant participant= new RefUpdateParticipant(); |
| 183 | + participant.initialize(this, origin, new CopyArguments(destination, log)); |
| 184 | + return new RefactoringParticipant[] { participant }; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @BeforeEach |
| 189 | + void setUp() throws Exception { |
| 190 | + project= new SimpleTestProject(); |
| 191 | + } |
| 192 | + |
| 193 | + @AfterEach |
| 194 | + void tearDown() throws Exception { |
| 195 | + project.delete(); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + void testCopyRefactoringWithParticipants() throws Exception { |
| 200 | + IFolder srcFold= project.createFolder("originFolder"); |
| 201 | + IFolder destination= project.createFolder("destFolder"); |
| 202 | + // the copy should specify the package as "destFolder", while the origin says "originFolder" |
| 203 | + IFile origin= project.createFile(srcFold, "testFile.txt", "package: originFolder"); |
| 204 | + |
| 205 | + CopyRefactoring refactoring= new CopyRefactoring(new TestCopyProcessor(origin, destination)); |
| 206 | + PerformRefactoringOperation op= new PerformRefactoringOperation(refactoring, CheckConditionsOperation.ALL_CONDITIONS); |
| 207 | + ResourcesPlugin.getWorkspace().run(op, null); |
| 208 | + |
| 209 | + IFile copy = this.project.getProject().getFolder("destFolder").getFile("testFile.txt"); |
| 210 | + assertTrue(copy.exists(), "File is not copied"); |
| 211 | + |
| 212 | + // original file was not changed |
| 213 | + String originContent= project.getContent(origin); |
| 214 | + assertEquals("package: originFolder", originContent); |
| 215 | + |
| 216 | + // package of copy was changed |
| 217 | + String copyContent = project.getContent(copy); |
| 218 | + assertEquals("package: destFolder", copyContent); |
| 219 | + } |
| 220 | +} |
0 commit comments