diff options
author | Stefan Lay | 2010-07-07 14:00:00 +0000 |
---|---|---|
committer | Code Review | 2010-07-07 14:00:00 +0000 |
commit | 930d7bc70d40ba1d06e5f17629c86cb772f73319 (patch) | |
tree | b80544af548a14ed98373f8175a0a294c4aacdb8 /org.eclipse.egit.core.test | |
parent | 6a5df1536c8b9e7468e9567a52b9aafb7b14e09e (diff) | |
parent | ac38294b72801384287d4e289600e97503d3ac5c (diff) | |
download | egit-930d7bc70d40ba1d06e5f17629c86cb772f73319.tar.gz egit-930d7bc70d40ba1d06e5f17629c86cb772f73319.tar.xz egit-930d7bc70d40ba1d06e5f17629c86cb772f73319.zip |
Merge "EGit Core tests: fix PushOperation and add tests"
Diffstat (limited to 'org.eclipse.egit.core.test')
13 files changed, 1000 insertions, 24 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/storage/BlobStorageTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/storage/BlobStorageTest.java index e347116b10..ae612b683d 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/storage/BlobStorageTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/storage/BlobStorageTest.java @@ -33,6 +33,7 @@ public class BlobStorageTest extends GitTestCase { @After public void tearDown() throws Exception { + repository.close(); super.tearDown(); } @@ -41,7 +42,7 @@ public class BlobStorageTest extends GitTestCase { ObjectId id = createFile(repository, project.getProject(), "file", "data"); BlobStorage blobStorage = new BlobStorage(repository, "p/file", id); assertEquals("file", blobStorage.getName()); - assertEquals("data", slurpAndClose(blobStorage.getContents())); + assertEquals("data", testUtils.slurpAndClose(blobStorage.getContents())); assertEquals(Path.fromPortableString("p/file").toOSString(), blobStorage.getFullPath().toOSString()); } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java index 2628e2f58e..9b9db5e5eb 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java @@ -34,6 +34,7 @@ import org.eclipse.egit.core.test.GitTestCase; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.team.core.variants.IResourceVariant; +import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -53,6 +54,12 @@ public class GitResourceVariantComparatorTest extends GitTestCase { repo = RepositoryMapping.getMapping(iProject).getRepository(); } + @After + public void tearDown() throws Exception { + repo.close(); + super.tearDown(); + } + /*============================================ * compare(IResource, IResourceVariant) tests *============================================*/ diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeTest.java index c80bffb494..14c3a93771 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeTest.java @@ -75,6 +75,7 @@ public class GitResourceVariantTreeTest extends GitTestCase { new DisconnectProviderOperation(projects).execute(null); repo.close(); + super.tearDown(); } /** diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/DualRepositoryTestCase.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/DualRepositoryTestCase.java new file mode 100644 index 0000000000..d53c527a71 --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/DualRepositoryTestCase.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com> + * + * 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 + *******************************************************************************/ +package org.eclipse.egit.core.test; + +import java.io.File; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.Path; +import org.eclipse.egit.core.op.ConnectProviderOperation; + +public abstract class DualRepositoryTestCase { + + protected TestUtils testUtils = new TestUtils(); + + protected TestRepository repository1; + + protected TestRepository repository2; + + protected IProject testProject; + + protected IProject importProject(TestRepository repo, String projectName) + throws Exception { + IProject firstProject = ResourcesPlugin.getWorkspace().getRoot() + .getProject(projectName); + if (firstProject.exists()) + firstProject.delete(false, null); + IProjectDescription desc = ResourcesPlugin.getWorkspace() + .newProjectDescription(projectName); + File parentFile = repo.getRepository().getWorkDir(); + desc.setLocation(new Path(new File(parentFile, projectName).getPath())); + firstProject.create(desc, null); + firstProject.open(null); + ConnectProviderOperation cop = new ConnectProviderOperation( + firstProject, repo.getRepository().getDirectory()); + cop.execute(null); + return firstProject; + } +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/GitTestCase.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/GitTestCase.java index 6824e606c2..50a421cb7c 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/GitTestCase.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/GitTestCase.java @@ -12,7 +12,6 @@ import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ResourcesPlugin; @@ -29,6 +28,8 @@ import org.junit.Before; public abstract class GitTestCase { + protected final TestUtils testUtils = new TestUtils(); + protected TestProject project; protected File gitDir; @@ -43,17 +44,17 @@ public abstract class GitTestCase { project = new TestProject(true); gitDir = new File(project.getProject().getWorkspace().getRoot() .getRawLocation().toFile(), Constants.DOT_GIT); - TestUtils.rmrf(gitDir); + testUtils.deleteRecursive(gitDir); } @After public void tearDown() throws Exception { project.dispose(); - TestUtils.rmrf(gitDir); + testUtils.deleteRecursive(gitDir); } - protected ObjectId createFile(Repository repository, IProject project, String name, String content) throws IOException { - File file = new File(project.getProject().getLocation().toFile(), name); + protected ObjectId createFile(Repository repository, IProject actProject, String name, String content) throws IOException { + File file = new File(actProject.getProject().getLocation().toFile(), name); FileWriter fileWriter = new FileWriter(file); fileWriter.write(content); fileWriter.close(); @@ -61,8 +62,8 @@ public abstract class GitTestCase { return objectWriter.writeBlob(file); } - protected ObjectId createFileCorruptShort(Repository repository, IProject project, String name, String content) throws IOException { - ObjectId id = createFile(repository, project, name, content); + protected ObjectId createFileCorruptShort(Repository repository, IProject actProject, String name, String content) throws IOException { + ObjectId id = createFile(repository, actProject, name, content); File file = new File(repository.getDirectory(), "objects/" + id.name().substring(0,2) + "/" + id.name().substring(2)); byte[] readFully = IO.readFully(file); file.delete(); @@ -80,17 +81,4 @@ public abstract class GitTestCase { return objectWriter.writeTree(tree); } - protected String slurpAndClose(InputStream inputStream) throws IOException { - StringBuilder stringBuilder = new StringBuilder(); - try { - int ch; - while ((ch = inputStream.read()) != -1) { - stringBuilder.append((char)ch); - } - } finally { - inputStream.close(); - } - return stringBuilder.toString(); - } - } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestProject.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestProject.java index a20c4b4a3f..2ec001411e 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestProject.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestProject.java @@ -40,6 +40,7 @@ public class TestProject { private IPackageFragmentRoot sourceFolder; private String location; + private TestUtils testUtils = new TestUtils(); /** * @throws CoreException @@ -115,7 +116,7 @@ public class TestProject { if (project.exists()) project.delete(true, true, null); else - TestUtils.rmrf(new File(location)); + testUtils.deleteRecursive(new File(location)); } private IFolder createBinFolder() throws CoreException { diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java index 663b745fb2..6ab42936ba 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java @@ -14,6 +14,9 @@ import java.io.File; import java.io.IOException; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.egit.core.op.ConnectProviderOperation; import org.eclipse.jgit.api.CommitCommand; import org.eclipse.jgit.api.ConcurrentRefUpdateException; import org.eclipse.jgit.api.Git; @@ -61,6 +64,23 @@ public class TestRepository { } /** + * Creates a test repository from an existing Repository + * @param repository + * @throws IOException + */ + public TestRepository(Repository repository) throws IOException { + this.repository = repository; + try { + workdirPrefix = repository.getWorkDir().getCanonicalPath(); + } catch (IOException err) { + workdirPrefix = repository.getWorkDir().getAbsolutePath(); + } + workdirPrefix = workdirPrefix.replace('\\', '/'); + if (!workdirPrefix.endsWith("/")) //$NON-NLS-1$ + workdirPrefix += "/"; //$NON-NLS-1$ + } + + /** * @return the wrapped repository */ public Repository getRepository() { @@ -202,4 +222,15 @@ public class TestRepository { repository.close(); repository = null; } + + /** + * Connect a project to this repository + * @param project + * @throws CoreException + */ + public void connect(IProject project) throws CoreException { + ConnectProviderOperation op = new ConnectProviderOperation(project, + this.getRepository().getDirectory()); + op.execute(null); + } } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java index 487d86eaf3..3d3e1f59c6 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java @@ -1,6 +1,7 @@ /******************************************************************************* * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com> * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com> + * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com> * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -9,18 +10,35 @@ *******************************************************************************/ package org.eclipse.egit.core.test; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jgit.util.FS; public class TestUtils { + + public final static String AUTHOR = "The Author <The.author@some.com>"; + + public final static String COMMITTER = "The Commiter <The.committer@some.com>"; + /** * This method deletes a file / subtree * * @param d * file / folder to delete * @throws IOException + * if file can not be deleted */ - public static void rmrf(File d) throws IOException { + public void deleteRecursive(File d) throws IOException { if (!d.exists()) return; @@ -28,7 +46,7 @@ public class TestUtils { if (files != null) { for (int i = 0; i < files.length; ++i) { if (files[i].isDirectory()) - rmrf(files[i]); + deleteRecursive(files[i]); else if (!files[i].delete()) throw new IOException(files[i] + " in use or undeletable"); } @@ -38,4 +56,108 @@ public class TestUtils { assert !d.exists(); } + /** + * Create a "temporary" directory + * + * @param name + * the name of the directory + * @return a directory as child of a "temporary" folder in the user home + * directory; may or may not exist + * @throws IOException + */ + public File getTempDir(String name) throws IOException { + File userHome = FS.DETECTED.userHome(); + File rootDir = new File(userHome, "EGitCoreTestTempDir"); + File result = new File(rootDir, name); + if (result.exists()) + deleteRecursive(result); + return result; + } + + /** + * Cleanup: delete the "temporary" folder and all children + * + * @throws IOException + */ + public void deleteTempDirs() throws IOException { + File userHome = FS.DETECTED.userHome(); + File rootDir = new File(userHome, "EGitCoreTestTempDir"); + if (rootDir.exists()) + deleteRecursive(rootDir); + } + + /** + * Read the stream into a String + * + * @param inputStream + * @return the contents of the stream + * @throws IOException + */ + public String slurpAndClose(InputStream inputStream) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + try { + int ch; + while ((ch = inputStream.read()) != -1) { + stringBuilder.append((char) ch); + } + } finally { + inputStream.close(); + } + return stringBuilder.toString(); + } + + /** + * Add a file to an existing project + * + * @param project + * the project + * @param path + * e.g. "folder1/folder2/test.txt" + * @param content + * the contents + * @return the file + * @throws Exception + * if the file can not be created + */ + public IFile addFileToProject(IProject project, String path, String content) + throws Exception { + IPath filePath = new Path(path); + IFolder folder = null; + for (int i = 0; i < filePath.segmentCount() - 1; i++) { + if (folder == null) { + folder = project.getFolder(filePath.segment(i)); + } else { + folder = folder.getFolder(filePath.segment(i)); + } + folder.create(false, true, null); + } + IFile file = project.getFile(filePath); + file.create(new ByteArrayInputStream(content.getBytes(project + .getDefaultCharset())), true, null); + return file; + } + + /** + * Create a project in the local file system + * + * @param parentFile + * the parent + * @param projectName + * project name + * @return the project with a location pointing to the local file system + * @throws Exception + */ + public IProject createProjectInLocalFileSystem(File parentFile, + String projectName) throws Exception { + IProject firstProject = ResourcesPlugin.getWorkspace().getRoot() + .getProject(projectName); + if (firstProject.exists()) + firstProject.delete(false, null); + IProjectDescription desc = ResourcesPlugin.getWorkspace() + .newProjectDescription(projectName); + desc.setLocation(new Path(new File(parentFile, projectName).getPath())); + firstProject.create(desc, null); + firstProject.open(null); + return firstProject; + } } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/DiscardChangesOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/DiscardChangesOperationTest.java new file mode 100644 index 0000000000..5b2e60d271 --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/DiscardChangesOperationTest.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com> + * + * 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 + *******************************************************************************/ +package org.eclipse.egit.core.test.op; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.egit.core.Activator; +import org.eclipse.egit.core.op.DiscardChangesOperation; +import org.eclipse.egit.core.test.DualRepositoryTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.jgit.lib.Constants; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DiscardChangesOperationTest extends DualRepositoryTestCase { + + File workdir; + + IProject project; + + String projectName = "DiscardChangesTest"; + + @Before + public void setUp() throws Exception { + + workdir = testUtils.getTempDir("Repository1"); + + repository1 = new TestRepository(new File(workdir, Constants.DOT_GIT)); + + // now we create a project in repo1 + project = testUtils + .createProjectInLocalFileSystem(workdir, projectName); + testUtils.addFileToProject(project, "folder1/file1.txt", "Hello world"); + + repository1.connect(project); + + project.accept(new IResourceVisitor() { + + public boolean visit(IResource resource) throws CoreException { + if (resource instanceof IFile) { + try { + repository1 + .track(EFS.getStore(resource.getLocationURI()) + .toLocalFile(0, null)); + } catch (IOException e) { + throw new CoreException(Activator.error(e.getMessage(), + e)); + } + } + return true; + } + }); + repository1.commit("Initial commit"); + } + + @After + public void tearDown() throws Exception { + project.close(null); + project.delete(false, false, null); + repository1.dispose(); + repository1 = null; + testUtils.deleteRecursive(workdir); + } + + @Test + public void testDiscardChanges() throws Exception { + IFile file = project.getFile(new Path("folder1/file1.txt")); + String contents = testUtils.slurpAndClose(file.getContents()); + assertEquals("Hello world", contents); + + file.setContents(new ByteArrayInputStream("Changed".getBytes(project + .getDefaultCharset())), 0, null); + + contents = testUtils.slurpAndClose(file.getContents()); + assertEquals("Changed", contents); + + DiscardChangesOperation dcop = new DiscardChangesOperation( + new IResource[] { file }); + dcop.execute(new NullProgressMonitor()); + + contents = testUtils.slurpAndClose(file.getContents()); + assertEquals("Hello world", contents); + + } +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/ListRemoteOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/ListRemoteOperationTest.java new file mode 100644 index 0000000000..4b38e19ec3 --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/ListRemoteOperationTest.java @@ -0,0 +1,178 @@ +/******************************************************************************* + * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com> + * + * 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 + *******************************************************************************/ +package org.eclipse.egit.core.test.op; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.egit.core.Activator; +import org.eclipse.egit.core.op.CloneOperation; +import org.eclipse.egit.core.op.ListRemoteOperation; +import org.eclipse.egit.core.test.DualRepositoryTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.URIish; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ListRemoteOperationTest extends DualRepositoryTestCase { + + File workdir; + + File workdir2; + + String projectName = "ListRemoteTest"; + + /** + * Set up repository1 with branch "master", create some project and commit + * it; then clone into repository2; finally create a branch "test" on top of + * "master" in repository2 + * + * @throws Exception + */ + @Before + public void setUp() throws Exception { + + workdir = testUtils.getTempDir("Repository1"); + workdir2 = testUtils.getTempDir("Repository2"); + + repository1 = new TestRepository(new File(workdir, Constants.DOT_GIT)); + + // now we create a project in repo1 + IProject project = testUtils.createProjectInLocalFileSystem(workdir, + projectName); + testUtils.addFileToProject(project, "folder1/file1.txt", "Hello world"); + + repository1.connect(project); + + project.accept(new IResourceVisitor() { + + public boolean visit(IResource resource) throws CoreException { + if (resource instanceof IFile) { + try { + repository1 + .track(EFS.getStore(resource.getLocationURI()) + .toLocalFile(0, null)); + } catch (IOException e) { + throw new CoreException(Activator.error(e.getMessage(), + e)); + } + } + return true; + } + }); + + repository1.commit("Initial commit"); + + // let's get rid of the project + project.delete(false, false, null); + + // let's clone repository1 to repository2 + URIish uri = new URIish("file:///" + + repository1.getRepository().getDirectory().toString()); + CloneOperation clop = new CloneOperation(uri, true, null, workdir2, + "refs/heads/master", "origin"); + clop.run(null); + + repository2 = new TestRepository(new Repository(new File(workdir2, + Constants.DOT_GIT))); + // we push to branch "test" of repository2 + RefUpdate createBranch = repository2.getRepository().updateRef( + "refs/heads/test"); + createBranch.setNewObjectId(repository2.getRepository().resolve( + "refs/heads/master")); + createBranch.update(); + } + + @After + public void tearDown() throws Exception { + repository1.dispose(); + repository2.dispose(); + testUtils.deleteRecursive(workdir); + testUtils.deleteRecursive(workdir2); + } + + /** + * List the refs both ways + * + * @throws Exception + */ + @Test + public void testListRemote() throws Exception { + + URIish uri = new URIish("file:///" + + repository2.getRepository().getDirectory().getPath()); + ListRemoteOperation lrop = new ListRemoteOperation(repository1 + .getRepository(), uri); + lrop.run(null); + assertEquals(4, lrop.getRemoteRefs().size()); + assertNotNull(lrop.getRemoteRef("refs/heads/test")); + + uri = new URIish("file:///" + + repository1.getRepository().getDirectory().getPath()); + lrop = new ListRemoteOperation(repository2.getRepository(), uri); + lrop.run(new NullProgressMonitor()); + assertEquals(2, lrop.getRemoteRefs().size()); + assertNotNull(lrop.getRemoteRef("refs/heads/master")); + } + + /** + * Call getRemoteRefs without having run the op + * + * @throws Exception + */ + @Test + public void testIllegalStateException() throws Exception { + + URIish uri = new URIish("file:///" + + repository2.getRepository().getDirectory().getPath()); + ListRemoteOperation lrop = new ListRemoteOperation(repository1 + .getRepository(), uri); + try { + lrop.getRemoteRefs(); + fail("Expected Exception not thrown"); + } catch (IllegalStateException e) { + // expected + } + } + + /** + * Test with illegal URI + * + * @throws Exception + */ + @Test + public void testIllegalURI() throws Exception { + + URIish uri = new URIish("file:///" + "no/path"); + ListRemoteOperation lrop = new ListRemoteOperation(repository1 + .getRepository(), uri); + try { + lrop.run(new NullProgressMonitor()); + fail("Expected Exception not thrown"); + } catch (InvocationTargetException e) { + // expected + } + } +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/PushOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/PushOperationTest.java new file mode 100644 index 0000000000..48fa2d134d --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/PushOperationTest.java @@ -0,0 +1,264 @@ +/******************************************************************************* + * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com> + * + * 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 + *******************************************************************************/ +package org.eclipse.egit.core.test.op; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.egit.core.Activator; +import org.eclipse.egit.core.op.BranchOperation; +import org.eclipse.egit.core.op.CloneOperation; +import org.eclipse.egit.core.op.CommitOperation; +import org.eclipse.egit.core.op.PushOperation; +import org.eclipse.egit.core.op.PushOperationResult; +import org.eclipse.egit.core.op.PushOperationSpecification; +import org.eclipse.egit.core.op.TrackOperation; +import org.eclipse.egit.core.test.DualRepositoryTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.egit.core.test.TestUtils; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.transport.RemoteRefUpdate; +import org.eclipse.jgit.transport.URIish; +import org.eclipse.jgit.transport.RemoteRefUpdate.Status; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class PushOperationTest extends DualRepositoryTestCase { + + File workdir; + + File workdir2; + + String projectName = "PushTest"; + + /** + * Set up repository1 with branch "master", create some project and commit + * it; then clone into repository2; finally create a branch "test" on top of + * "master" in repository2 + * + * @throws Exception + */ + @Before + public void setUp() throws Exception { + + workdir = testUtils.getTempDir("Repository1"); + workdir2 = testUtils.getTempDir("Repository2"); + + repository1 = new TestRepository(new File(workdir, Constants.DOT_GIT)); + + // now we create a project in repo1 + IProject project = testUtils.createProjectInLocalFileSystem(workdir, + projectName); + testUtils.addFileToProject(project, "folder1/file1.txt", "Hello world"); + + repository1.connect(project); + + project.accept(new IResourceVisitor() { + + public boolean visit(IResource resource) throws CoreException { + if (resource instanceof IFile) { + try { + repository1 + .track(EFS.getStore(resource.getLocationURI()) + .toLocalFile(0, null)); + } catch (IOException e) { + throw new CoreException(Activator.error(e.getMessage(), + e)); + } + } + return true; + } + }); + + repository1.commit("Initial commit"); + + // let's get rid of the project + project.delete(false, false, null); + + // let's clone repository1 to repository2 + URIish uri = new URIish("file:///" + + repository1.getRepository().getDirectory().toString()); + CloneOperation clop = new CloneOperation(uri, true, null, workdir2, + "refs/heads/master", "origin"); + clop.run(null); + + repository2 = new TestRepository(new Repository(new File(workdir2, + Constants.DOT_GIT))); + // we push to branch "test" of repository2 + RefUpdate createBranch = repository2.getRepository().updateRef( + "refs/heads/test"); + createBranch.setNewObjectId(repository2.getRepository().resolve( + "refs/heads/master")); + createBranch.update(); + } + + @After + public void tearDown() throws Exception { + repository1.dispose(); + repository2.dispose(); + repository1 = null; + repository2 = null; + testUtils.deleteRecursive(workdir); + testUtils.deleteRecursive(workdir2); + } + + /** + * Push from repository1 "master" into "test" of repository2. + * + * @throws Exception + */ + @Test + public void testPush() throws Exception { + + // push from repository1 to repository2 + PushOperation pop = createPushOperation(); + pop.run(new NullProgressMonitor()); + assertEquals(Status.UP_TO_DATE, getStatus(pop.getOperationResult())); + + // let's add a new file to the project shared with repository1 + IProject proj = importProject(repository1, projectName); + ArrayList<IFile> files = new ArrayList<IFile>(); + IFile newFile = testUtils.addFileToProject(proj, "folder2/file2.txt", + "New file"); + files.add(newFile); + IFile[] fileArr = files.toArray(new IFile[files.size()]); + + TrackOperation trop = new TrackOperation(fileArr); + trop.execute(null); + CommitOperation cop = new CommitOperation(fileArr, files, files, + TestUtils.AUTHOR, TestUtils.COMMITTER, "Added file"); + cop.execute(null); + + proj.delete(false, false, null); + + pop = createPushOperation(); + pop.run(null); + assertEquals(Status.OK, getStatus(pop.getOperationResult())); + + try { + // assert that we can not run this again + pop.run(null); + fail("Expected Exception not thrown"); + } catch (IllegalStateException e) { + // expected + } + + pop = createPushOperation(); + pop.run(null); + assertEquals(Status.UP_TO_DATE, getStatus(pop.getOperationResult())); + + String newFilePath = newFile.getFullPath().toOSString(); + + File testFile = new File(workdir2, newFilePath); + assertFalse(testFile.exists()); + testFile = new File(workdir, newFilePath); + assertTrue(testFile.exists()); + + // check out test and verify the file is there + BranchOperation bop = new BranchOperation(repository2.getRepository(), + "refs/heads/test"); + bop.execute(null); + testFile = new File(workdir2, newFilePath); + assertTrue(testFile.exists()); + } + + /** + * We should get an {@link IllegalStateException} if we run + * getOperationResult before run() + * + * @throws Exception + */ + @Test + public void testIllegalStateExceptionOnGetResultWithoutRun() + throws Exception { + // push from repository1 to repository2 + PushOperation pop = createPushOperation(); + try { + pop.getOperationResult(); + fail("Expected Exception not thrown"); + } catch (IllegalStateException e) { + // expected + } + } + + /** + * We should get an {@link IllegalStateException} if the spec was re-used + * + * @throws Exception + */ + @Test + public void testPushWithReusedSpec() throws Exception { + + PushOperationSpecification spec = new PushOperationSpecification(); + // the remote is repo2 + URIish remote = new URIish("file:///" + + repository2.getRepository().getDirectory().toString()); + // update master upon master + List<RemoteRefUpdate> refUpdates = new ArrayList<RemoteRefUpdate>(); + RemoteRefUpdate update = new RemoteRefUpdate(repository1 + .getRepository(), "HEAD", "refs/heads/test", false, null, null); + refUpdates.add(update); + spec.addURIRefUpdates(remote, refUpdates); + + PushOperation pop = new PushOperation(repository1.getRepository(), + spec, false, null); + pop.run(null); + + pop = new PushOperation(repository1.getRepository(), spec, false, null); + try { + pop.run(null); + fail("Expected Exception not thrown"); + } catch (IllegalStateException e) { + // expected + } + } + + private Status getStatus(PushOperationResult operationResult) { + URIish uri = operationResult.getURIs().iterator().next(); + return operationResult.getPushResult(uri).getRemoteUpdates().iterator() + .next().getStatus(); + } + + private PushOperation createPushOperation() throws Exception { + // set up push from repository1 to repository2 + // we can not re-use the RemoteRefUpdate!!! + PushOperationSpecification spec = new PushOperationSpecification(); + // the remote is repo2 + URIish remote = new URIish("file:///" + + repository2.getRepository().getDirectory().toString()); + // update master upon master + List<RemoteRefUpdate> refUpdates = new ArrayList<RemoteRefUpdate>(); + RemoteRefUpdate update = new RemoteRefUpdate(repository1 + .getRepository(), "HEAD", "refs/heads/test", false, null, null); + refUpdates.add(update); + spec.addURIRefUpdates(remote, refUpdates); + // now we can construct the push operation + PushOperation pop = new PushOperation(repository1.getRepository(), + spec, false, null); + return pop; + } + +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TagOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TagOperationTest.java new file mode 100644 index 0000000000..7abc2c9dde --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TagOperationTest.java @@ -0,0 +1,114 @@ +package org.eclipse.egit.core.test.op; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.egit.core.Activator; +import org.eclipse.egit.core.op.TagOperation; +import org.eclipse.egit.core.test.DualRepositoryTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.egit.core.test.TestUtils; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.PersonIdent; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Tag; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TagOperationTest extends DualRepositoryTestCase { + + File workdir; + + String projectName = "TagTest"; + + @Before + public void setUp() throws Exception { + + workdir = testUtils.getTempDir("Repository1"); + + repository1 = new TestRepository(new File(workdir, Constants.DOT_GIT)); + + // now we create a project in repo1 + IProject project = testUtils.createProjectInLocalFileSystem(workdir, + projectName); + testUtils.addFileToProject(project, "folder1/file1.txt", "Hello world"); + + repository1.connect(project); + + project.accept(new IResourceVisitor() { + + public boolean visit(IResource resource) throws CoreException { + if (resource instanceof IFile) { + try { + repository1 + .track(EFS.getStore(resource.getLocationURI()) + .toLocalFile(0, null)); + } catch (IOException e) { + throw new CoreException(Activator.error(e.getMessage(), + e)); + } + } + return true; + } + }); + + repository1.commit("Initial commit"); + } + + @After + public void tearDown() throws Exception { + repository1.dispose(); + repository1 = null; + testUtils.deleteRecursive(workdir); + } + + @Test + public void addTag() throws Exception { + assertTrue("Tags should be empty", repository1.getRepository().getTags().isEmpty()); + Tag newTag = new Tag(repository1.getRepository()); + newTag.setTag("TheNewTag"); + newTag.setMessage("Well, I'm the tag"); + newTag.setAuthor(new PersonIdent(TestUtils.AUTHOR)); + newTag.setObjId(repository1.getRepository().resolve("refs/heads/master")); + TagOperation top = new TagOperation(repository1.getRepository(), newTag, false); + top.execute(new NullProgressMonitor()); + assertFalse("Tags should not be empty", repository1.getRepository().getTags().isEmpty()); + + try { + top.execute(null); + fail("Expected Exception not thrown"); + } catch (CoreException e) { + // expected + } + + top = new TagOperation(repository1.getRepository(), newTag, true); + try { + top.execute(null); + fail("Expected Exception not thrown"); + } catch (CoreException e) { + // expected + } + Ref tagRef = repository1.getRepository().getTags().get("TheNewTag"); + Tag tag = repository1.getRepository().mapTag(tagRef.getName()); + newTag.setMessage("Another message"); + assertFalse("Messages should differ", tag.getMessage().equals( + newTag.getMessage())); + top.execute(null); + tag = repository1.getRepository().mapTag(tagRef.getName()); + assertTrue("Messages be same", tag.getMessage().equals( + newTag.getMessage())); + } + +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TrackUntrackOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TrackUntrackOperationTest.java new file mode 100644 index 0000000000..ce6635f2f3 --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TrackUntrackOperationTest.java @@ -0,0 +1,119 @@ +/******************************************************************************* + * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com> + * + * 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 + *******************************************************************************/ +package org.eclipse.egit.core.test.op; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.egit.core.op.TrackOperation; +import org.eclipse.egit.core.op.UntrackOperation; +import org.eclipse.egit.core.test.DualRepositoryTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.jgit.dircache.DirCache; +import org.eclipse.jgit.lib.Constants; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TrackUntrackOperationTest extends DualRepositoryTestCase { + + File workdir; + + IProject project; + + String projectName = "TrackTest"; + + @Before + public void setUp() throws Exception { + + workdir = testUtils.getTempDir("Repository1"); + + repository1 = new TestRepository(new File(workdir, Constants.DOT_GIT)); + + // now we create a project in repo1 + project = testUtils + .createProjectInLocalFileSystem(workdir, projectName); + testUtils.addFileToProject(project, "folder1/file1.txt", "Hello world"); + + repository1.connect(project); + } + + @After + public void tearDown() throws Exception { + project.close(null); + project.delete(false, false, null); + repository1.dispose(); + repository1 = null; + testUtils.deleteRecursive(workdir); + } + + @Test + public void testTrackFiles() throws Exception { + + final ArrayList<IFile> files = new ArrayList<IFile>(); + + DirCache cache = DirCache.read(repository1.getRepository()); + assertEquals("Wrong cache entry count", 0, cache.getEntryCount()); + + project.accept(new IResourceVisitor() { + + public boolean visit(IResource resource) throws CoreException { + if (resource instanceof IFile) + files.add((IFile) resource); + return true; + } + }); + + IFile[] fileArr = files.toArray(new IFile[files.size()]); + + TrackOperation trop = new TrackOperation(fileArr); + trop.execute(new NullProgressMonitor()); + + cache.read(); + assertEquals("Wrong cache entry count", 2, cache.getEntryCount()); + + UntrackOperation utop = new UntrackOperation(Arrays.asList(fileArr)); + utop.execute(new NullProgressMonitor()); + + cache.read(); + assertEquals("Wrong cache entry count", 0, cache.getEntryCount()); + + } + + @Test + public void testTrackProject() throws Exception { + + final ArrayList<IContainer> containers = new ArrayList<IContainer>(); + containers.add(project); + + DirCache cache = DirCache.read(repository1.getRepository()); + + assertEquals("Wrong cache entry count", 0, cache.getEntryCount()); + + IContainer[] fileArr = containers.toArray(new IContainer[containers + .size()]); + + TrackOperation trop = new TrackOperation(fileArr); + trop.execute(new NullProgressMonitor()); + + cache.read(); + assertEquals("Wrong cache entry count", 2, cache.getEntryCount()); + } + +} |