|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2019 Microsoft Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Microsoft Corporation - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.jdt.ls.core.internal; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertNotNull; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | + |
| 20 | +import org.eclipse.core.runtime.CoreException; |
| 21 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 22 | +import org.eclipse.jdt.core.IJavaProject; |
| 23 | +import org.eclipse.jdt.core.IPackageFragment; |
| 24 | +import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 25 | +import org.eclipse.jdt.core.refactoring.CompilationUnitChange; |
| 26 | +import org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange; |
| 27 | +import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest; |
| 28 | +import org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences; |
| 29 | +import org.eclipse.lsp4j.RenameFile; |
| 30 | +import org.eclipse.lsp4j.ResourceOperation; |
| 31 | +import org.eclipse.lsp4j.TextDocumentEdit; |
| 32 | +import org.eclipse.lsp4j.TextEdit; |
| 33 | +import org.eclipse.lsp4j.WorkspaceEdit; |
| 34 | +import org.eclipse.ltk.core.refactoring.CompositeChange; |
| 35 | +import org.eclipse.text.edits.InsertEdit; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | +import org.junit.runner.RunWith; |
| 39 | +import org.mockito.runners.MockitoJUnitRunner; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Yan Zhang |
| 43 | + * |
| 44 | + */ |
| 45 | + |
| 46 | +@RunWith(MockitoJUnitRunner.class) |
| 47 | +public class ChangeUtilTest extends AbstractProjectsManagerBasedTest { |
| 48 | + |
| 49 | + private ClientPreferences clientPreferences; |
| 50 | + |
| 51 | + private IPackageFragmentRoot sourceFolder; |
| 52 | + |
| 53 | + @Before |
| 54 | + public void setup() throws Exception { |
| 55 | + IJavaProject javaProject = newEmptyProject(); |
| 56 | + sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src")); |
| 57 | + clientPreferences = preferenceManager.getClientPreferences(); |
| 58 | + when(clientPreferences.isResourceOperationSupported()).thenReturn(true); |
| 59 | + } |
| 60 | + |
| 61 | + // Text Changes |
| 62 | + @Test |
| 63 | + public void testConvertCompilationUnitChange() throws CoreException { |
| 64 | + IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null); |
| 65 | + ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null); |
| 66 | + CompilationUnitChange change = new CompilationUnitChange("insertText", cu); |
| 67 | + String newText = "// some content"; |
| 68 | + change.setEdit(new InsertEdit(0, newText)); |
| 69 | + |
| 70 | + WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change); |
| 71 | + |
| 72 | + assertEquals(edit.getDocumentChanges().size(), 1); |
| 73 | + |
| 74 | + TextDocumentEdit textDocumentEdit = edit.getDocumentChanges().get(0).getLeft(); |
| 75 | + assertNotNull(textDocumentEdit); |
| 76 | + assertEquals(textDocumentEdit.getEdits().size(), 1); |
| 77 | + |
| 78 | + TextEdit textEdit = textDocumentEdit.getEdits().get(0); |
| 79 | + assertEquals(textEdit.getNewText(), newText); |
| 80 | + } |
| 81 | + |
| 82 | + // Resource Changes |
| 83 | + @Test |
| 84 | + public void testConvertRenameCompilationUnitChange() throws CoreException { |
| 85 | + IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null); |
| 86 | + ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null); |
| 87 | + String newName = "ENew.java"; |
| 88 | + RenameCompilationUnitChange change = new RenameCompilationUnitChange(cu, newName); |
| 89 | + String oldUri = JDTUtils.toURI(cu); |
| 90 | + String newUri = ResourceUtils.fixURI(URI.create(oldUri).resolve(newName)); |
| 91 | + |
| 92 | + WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change); |
| 93 | + |
| 94 | + assertEquals(edit.getDocumentChanges().size(), 1); |
| 95 | + |
| 96 | + ResourceOperation resourceOperation = edit.getDocumentChanges().get(0).getRight(); |
| 97 | + assertTrue(resourceOperation instanceof RenameFile); |
| 98 | + |
| 99 | + assertEquals(((RenameFile) resourceOperation).getOldUri(), oldUri); |
| 100 | + assertEquals(((RenameFile) resourceOperation).getNewUri(), newUri); |
| 101 | + } |
| 102 | + |
| 103 | + //Composite Changes |
| 104 | + @Test |
| 105 | + public void testConvertSimpleCompositeChange() throws CoreException { |
| 106 | + IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null); |
| 107 | + ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null); |
| 108 | + CompositeChange change = new CompositeChange("simple composite change"); |
| 109 | + |
| 110 | + RenameCompilationUnitChange resourceChange = new RenameCompilationUnitChange(cu, "ENew.java"); |
| 111 | + change.add(resourceChange); |
| 112 | + CompilationUnitChange textChange = new CompilationUnitChange("insertText", cu); |
| 113 | + textChange.setEdit(new InsertEdit(0, "// some content")); |
| 114 | + change.add(textChange); |
| 115 | + |
| 116 | + WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change); |
| 117 | + assertEquals(edit.getDocumentChanges().size(), 2); |
| 118 | + assertTrue(edit.getDocumentChanges().get(0).getRight() instanceof RenameFile); |
| 119 | + assertTrue(edit.getDocumentChanges().get(1).getLeft() instanceof TextDocumentEdit); |
| 120 | + } |
| 121 | +} |
0 commit comments