diff --git a/org.eclipse.ltk.core.refactoring.tests/src/org/eclipse/ltk/core/refactoring/tests/resource/ResourceRefactoringTests.java b/org.eclipse.ltk.core.refactoring.tests/src/org/eclipse/ltk/core/refactoring/tests/resource/ResourceRefactoringTests.java index c71b6f9..1ff2770 100644 --- a/org.eclipse.ltk.core.refactoring.tests/src/org/eclipse/ltk/core/refactoring/tests/resource/ResourceRefactoringTests.java +++ b/org.eclipse.ltk.core.refactoring.tests/src/org/eclipse/ltk/core/refactoring/tests/resource/ResourceRefactoringTests.java @@ -11,7 +11,9 @@ *******************************************************************************/ package org.eclipse.ltk.core.refactoring.tests.resource; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import junit.framework.Test; import junit.framework.TestCase; @@ -41,6 +43,7 @@ import org.eclipse.ltk.core.refactoring.RefactoringCore; import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ltk.core.refactoring.resource.CreateFileChange; import org.eclipse.ltk.core.refactoring.resource.DeleteResourcesDescriptor; import org.eclipse.ltk.core.refactoring.resource.MoveResourceChange; import org.eclipse.ltk.core.refactoring.resource.MoveResourcesDescriptor; @@ -95,7 +98,7 @@ Change undoChange= perform(new MoveResourceChange(testFolder, destination)); - IFolder movedResource= (IFolder) assertMove(testFolder, destination, null); + IFolder movedResource= (IFolder)assertMove(testFolder, destination, null); assertTrue(movedResource.getFile("myFile.txt").exists()); perform(undoChange); @@ -137,7 +140,7 @@ IFolder destination= fProject.createFolder("dest"); RefactoringContribution contribution= RefactoringCore.getRefactoringContribution(MoveResourcesDescriptor.ID); - MoveResourcesDescriptor descriptor= (MoveResourcesDescriptor) contribution.createDescriptor(); + MoveResourcesDescriptor descriptor= (MoveResourcesDescriptor)contribution.createDescriptor(); descriptor.setResourcesToMove(new IResource[] { file }); descriptor.setDestination(destination); @@ -161,14 +164,14 @@ IFolder destination= fProject.createFolder("dest"); RefactoringContribution contribution= RefactoringCore.getRefactoringContribution(MoveResourcesDescriptor.ID); - MoveResourcesDescriptor descriptor= (MoveResourcesDescriptor) contribution.createDescriptor(); + MoveResourcesDescriptor descriptor= (MoveResourcesDescriptor)contribution.createDescriptor(); descriptor.setResourcesToMove(new IResource[] { testFolder }); descriptor.setDestination(destination); Change undoChange= perform(descriptor); - IFolder movedResource= (IFolder) assertMove(testFolder, destination, null); + IFolder movedResource= (IFolder)assertMove(testFolder, destination, null); assertTrue(movedResource.getFile("myFile.txt").exists()); perform(undoChange); @@ -190,7 +193,7 @@ IFile file2= fProject.createFile(destination, "myFile.txt", content2); RefactoringContribution contribution= RefactoringCore.getRefactoringContribution(MoveResourcesDescriptor.ID); - MoveResourcesDescriptor descriptor= (MoveResourcesDescriptor) contribution.createDescriptor(); + MoveResourcesDescriptor descriptor= (MoveResourcesDescriptor)contribution.createDescriptor(); descriptor.setResourcesToMove(new IResource[] { file1 }); descriptor.setDestination(destination); @@ -208,7 +211,7 @@ public void testDeleteRefactoring1_bug343584() throws Exception { IFolder testFolder= fProject.createFolder("test"); fProject.createFile(testFolder, "myFile.txt", "hello"); - + IProject testProject2= ResourcesPlugin.getWorkspace().getRoot().getProject(SimpleTestProject.TEST_PROJECT_NAME + "2"); try { testProject2.create(null); @@ -233,7 +236,7 @@ IPath location= fProject.getProject().getLocation(); IFolder testFolder= fProject.createFolder("test"); fProject.createFile(testFolder, "myFile.txt", "hello"); - + IWorkspace workspace= ResourcesPlugin.getWorkspace(); String p2Name= "p2"; IProjectDescription p2Description= workspace.newProjectDescription(p2Name); @@ -242,28 +245,28 @@ p2.create(p2Description, null); p2.open(null); IPath p2Location= p2.getLocation(); - + RefactoringContribution contribution= RefactoringCore.getRefactoringContribution(DeleteResourcesDescriptor.ID); - DeleteResourcesDescriptor descriptor= (DeleteResourcesDescriptor) contribution.createDescriptor(); - + DeleteResourcesDescriptor descriptor= (DeleteResourcesDescriptor)contribution.createDescriptor(); + descriptor.setDeleteContents(true); descriptor.setResources(new IResource[] { fProject.getProject(), p2 }); - + perform(descriptor); - + assertFalse(fProject.getProject().exists()); assertFalse(p2.exists()); - + assertFalse(location.toFile().exists()); assertFalse(p2Location.toFile().exists()); } - + public void testDeleteRefactoring3_bug343584() throws Exception { IPath location= fProject.getProject().getLocation(); IFolder testFolder= fProject.createFolder("test"); IFile file= fProject.createFile(testFolder, "myFile.txt", "hello"); IPath fileLocation= file.getLocation(); - + IWorkspace workspace= ResourcesPlugin.getWorkspace(); String p2Name= "p2"; IProjectDescription p2Description= workspace.newProjectDescription(p2Name); @@ -272,29 +275,113 @@ p2.create(p2Description, null); p2.open(null); IPath p2Location= p2.getLocation(); - + try { RefactoringContribution contribution= RefactoringCore.getRefactoringContribution(DeleteResourcesDescriptor.ID); - DeleteResourcesDescriptor descriptor= (DeleteResourcesDescriptor) contribution.createDescriptor(); - + DeleteResourcesDescriptor descriptor= (DeleteResourcesDescriptor)contribution.createDescriptor(); + descriptor.setDeleteContents(false); descriptor.setResources(new IResource[] { fProject.getProject(), p2 }); - + perform(descriptor); - + assertFalse(fProject.getProject().exists()); assertFalse(p2.exists()); - + assertTrue(location.toFile().exists()); assertTrue(fileLocation.toFile().exists()); assertTrue(p2Location.toFile().exists()); - + } finally { EFS.getLocalFileSystem().getStore(location).delete(EFS.NONE, null); EFS.getLocalFileSystem().getStore(p2Location).delete(EFS.NONE, null); } } - + + /* + * Creates a file in the project's root. + */ + public void testCreateFileChange1() throws Exception { + String content= "hello"; + + IFile file= fProject.getProject().getFile("myFile.txt"); + InputStream inputStream= new ByteArrayInputStream(content.getBytes()); + + Change undoChange= perform(new CreateFileChange(file, inputStream)); + + assertTrue(file.exists()); + assertEquals(content, fProject.getContent(file)); + + perform(undoChange); + + assertFalse(file.exists()); + } + + /* + * Updates an existing file in the project's root. + */ + public void testCreateFileChange2() throws Exception { + String oldContent= "hi"; + String newContent= "hello"; + + IFile file= fProject.createFile(fProject.getProject(), "myFile.txt", oldContent); + InputStream inputStream= new ByteArrayInputStream(newContent.getBytes()); + + Change undoChange= perform(new CreateFileChange(file, inputStream)); + + assertTrue(file.exists()); + assertEquals(newContent, fProject.getContent(file)); + + perform(undoChange); + + assertTrue(file.exists()); + assertEquals(oldContent, fProject.getContent(file)); + } + + /* + * Updates an existing file in the project's root, but the overwrite flag + * is set to false. + */ + public void testCreateFileChange3() throws Exception { + String oldContent= "hi"; + String newContent= "hello"; + + IFile file= fProject.createFile(fProject.getProject(), "myFile.txt", oldContent); + InputStream inputStream= new ByteArrayInputStream(newContent.getBytes()); + + Change change= new CreateFileChange(file, inputStream, false); + assertTrue(change.isValid(null).hasFatalError()); + } + + /* + * Creates a file in a deep folder structure where some folders do not + * exist yet. + */ + public void testCreateFileChange4() throws Exception { + String content= "hello"; + + IFolder existingFolder= fProject.createFolder("test"); + IFolder newFolder1= existingFolder.getFolder("foo"); + IFolder newFolder2= newFolder1.getFolder("bar"); + IFile file= newFolder2.getFile("myFile.txt"); + InputStream inputStream= new ByteArrayInputStream(content.getBytes()); + + Change undoChange= perform(new CreateFileChange(file, inputStream)); + + assertTrue(existingFolder.exists()); + assertTrue(newFolder1.exists()); + assertTrue(newFolder2.exists()); + assertTrue(file.exists()); + assertEquals(content, fProject.getContent(file)); + + perform(undoChange); + + assertFalse(file.exists()); + assertFalse(newFolder2.exists()); + assertFalse(newFolder1.exists()); + assertTrue(existingFolder.exists()); + } + private Change perform(Change change) throws CoreException { PerformChangeOperation op= new PerformChangeOperation(change); op.run(null); @@ -327,10 +414,9 @@ assertTrue(res.getType() == source.getType()); if (res instanceof IFile) { - assertTrue(content.equals(fProject.getContent((IFile) res))); + assertTrue(content.equals(fProject.getContent((IFile)res))); } return res; } - } diff --git a/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/CreateFileChange.java b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/CreateFileChange.java new file mode 100644 index 0000000..d052125 --- /dev/null +++ b/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/CreateFileChange.java @@ -0,0 +1,197 @@ +/******************************************************************************* + * Copyright (c) 2013 Zend Technologies Ltd and others. + * All rights reserved. This program and the accompanying materials + * are 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: + * Zend Technologies Ltd - initial API and implementation + *******************************************************************************/ +package org.eclipse.ltk.core.refactoring.resource; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileInfo; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubProgressMonitor; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IResource; + +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.CompositeChange; +import org.eclipse.ltk.core.refactoring.RefactoringCore; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ltk.internal.core.refactoring.BasicElementLabels; +import org.eclipse.ltk.internal.core.refactoring.Messages; +import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages; + +/** + * {@link Change} that creates a file. + * + * @since 3.7 + */ +public class CreateFileChange extends ResourceChange { + + private IFile fFile; + + private InputStream fInputStream; + + private boolean fOverwrite; + + /** + * Create new file. + * + *
+ * If there is an existing file on the specified path then it will be replaced with the new
+ * file. Calling this constructor is the same as calling
+ * CreateFileChange(path, inputStream, true)
.
+ *