diff options
Diffstat (limited to 'org.eclipse.egit.core.test')
4 files changed, 225 insertions, 6 deletions
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 f81713d2a8..cd5a5a87f1 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 @@ -24,6 +24,7 @@ import org.eclipse.jgit.api.JGitInternalException; import org.eclipse.jgit.api.NoHeadException; import org.eclipse.jgit.api.NoMessageException; import org.eclipse.jgit.api.WrongRepositoryStateException; +import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.errors.UnmergedPathException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.GitIndex; @@ -204,9 +205,27 @@ public class TestRepository { } public boolean inIndex(String path) throws IOException { +// String repoPath = getRepoRelativePath(path); +// GitIndex index = repository.getIndex(); +// return index.getEntry(repoPath) != null; String repoPath = getRepoRelativePath(path); - GitIndex index = repository.getIndex(); - return index.getEntry(repoPath) != null; + DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS()); + + return dc.getEntry(repoPath) != null; + } + + public long lastModifiedInIndex(String path) throws IOException { + String repoPath = getRepoRelativePath(path); + DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS()); + + return dc.getEntry(repoPath).getLastModified(); + } + + public int getDirCacheEntryLength(String path) throws IOException { + String repoPath = getRepoRelativePath(path); + DirCache dc = DirCache.read(repository.getIndexFile(), repository.getFS()); + + return dc.getEntry(repoPath).getLength(); } public String getRepoRelativePath(String path) { 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 d9e66590ad..33c97dd1ba 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 @@ -129,7 +129,8 @@ public class TestUtils { } else { folder = folder.getFolder(filePath.segment(i)); } - folder.create(false, true, null); + if (!folder.exists()) + folder.create(false, true, null); } IFile file = project.getFile(filePath); file.create(new ByteArrayInputStream(content.getBytes(project diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/AddOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/AddOperationTest.java new file mode 100644 index 0000000000..70f2d6ebf7 --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/AddOperationTest.java @@ -0,0 +1,199 @@ +/******************************************************************************* + * Copyright (C) 2010, Stefan Lay <stefan.lay@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 java.io.ByteArrayInputStream; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.egit.core.op.AddToIndexOperation; +import org.eclipse.egit.core.test.GitTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.Repository; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class AddOperationTest extends GitTestCase { + + private List<IResource> resources = new ArrayList<IResource>(); + + TestRepository testRepository; + + Repository repository; + + @Before + public void setUp() throws Exception { + super.setUp(); + gitDir = new File(project.getProject() + .getLocationURI().getPath(), Constants.DOT_GIT); + testRepository = new TestRepository(gitDir); + repository = testRepository.getRepository(); + testRepository.connect(project.getProject()); + } + + @After + public void tearDown() throws Exception { + testRepository.dispose(); + repository = null; + super.tearDown(); + } + + @Test + public void testTrackFile() throws Exception { + IFile file1 = testUtils.addFileToProject(project.getProject(), "a.txt", + "some text"); + + resources.add(file1); + new AddToIndexOperation(resources).execute(null); + + assertTrue(testRepository.inIndex(file1.getLocation() + .toPortableString())); + assertTrue(testRepository.getDirCacheEntryLength(file1.getLocation() + .toPortableString()) == 9); + } + + @Test + public void testTrackFilesInFolder() throws Exception { + IFile file1 = testUtils.addFileToProject(project.getProject(), + "sub/a.txt", "some text"); + IFile file2 = testUtils.addFileToProject(project.getProject(), + "sub/b.txt", "some text"); + + resources.add(project.getProject().getFolder("sub")); + new AddToIndexOperation(resources).execute(null); + + assertTrue(testRepository.inIndex(file1.getLocation() + .toPortableString())); + assertTrue(testRepository.inIndex(file2.getLocation() + .toPortableString())); + assertTrue(testRepository.getDirCacheEntryLength(file1.getLocation() + .toPortableString()) == 9); + assertTrue(testRepository.getDirCacheEntryLength(file2.getLocation() + .toPortableString()) == 9); + } + + @Test + public void testAddFile() throws Exception { + IFile file1 = testUtils.addFileToProject(project.getProject(), "a.txt", + "some text"); + + resources.add(file1); + new AddToIndexOperation(resources).execute(null); + + testRepository.commit("first commit"); + + assertEquals(file1.getLocalTimeStamp(), + testRepository.lastModifiedInIndex(file1.getLocation() + .toPortableString())); + + Thread.sleep(1000); + file1.setContents( + new ByteArrayInputStream("other text".getBytes(project.project + .getDefaultCharset())), 0, null); + + assertFalse(file1.getLocalTimeStamp() == testRepository + .lastModifiedInIndex(file1.getLocation().toPortableString())); + + new AddToIndexOperation(resources).execute(null); + + assertTrue(testRepository.inIndex(file1.getLocation() + .toPortableString())); + // does not work yet due to the racy git problem: DirCache.writeTo + // smudges the + // timestamp of an added file + assertEquals(file1.getLocalTimeStamp() / 10, + testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()) / 10); + } + + @Test + public void testAddFilesInFolder() throws Exception { + IFile file1 = testUtils.addFileToProject(project.getProject(), + "sub/a.txt", "some text"); + IFile file2 = testUtils.addFileToProject(project.getProject(), + "sub/b.txt", "some text"); + + resources.add(project.getProject().getFolder("sub")); + new AddToIndexOperation(resources).execute(null); + + testRepository.commit("first commit"); + + assertEquals(file1.getLocalTimeStamp(), + testRepository.lastModifiedInIndex(file1.getLocation() + .toPortableString())); + + file1.setContents( + new ByteArrayInputStream("other text".getBytes(project.project + .getDefaultCharset())), 0, null); + file2.setContents( + new ByteArrayInputStream("other text".getBytes(project.project + .getDefaultCharset())), 0, null); + + Thread.sleep(1000); + assertFalse(file1.getLocalTimeStamp() == testRepository + .lastModifiedInIndex(file1.getLocation().toPortableString())); + assertFalse(file2.getLocalTimeStamp() == testRepository + .lastModifiedInIndex(file1.getLocation().toPortableString())); + + new AddToIndexOperation(resources).execute(null); + + assertTrue(testRepository.inIndex(file1.getLocation() + .toPortableString())); + assertTrue(testRepository.inIndex(file2.getLocation() + .toPortableString())); + + assertEquals(file1.getLocalTimeStamp() / 10, + testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()) / 10); + assertEquals(file2.getLocalTimeStamp() / 10, + testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()) / 10); + } + + @Test + public void testAddFilesInFolderWithDerivedFile() throws Exception { + IFile file1 = testUtils.addFileToProject(project.getProject(), + "sub/a.txt", "some text"); + IFile file2 = testUtils.addFileToProject(project.getProject(), + "sub/b.txt", "some text"); + file2.setDerived(true, null); + + resources.add(project.getProject().getFolder("sub")); + new AddToIndexOperation(resources).execute(null); + + assertTrue(testRepository.inIndex(file1.getLocation() + .toPortableString())); + assertFalse(testRepository.inIndex(file2.getLocation() + .toPortableString())); + } + + @Test + public void testAddWholeProject() throws Exception { + IFile file1 = testUtils.addFileToProject(project.getProject(), + "sub/a.txt", "some text"); + IFile file2 = testUtils.addFileToProject(project.getProject(), + "sub/b.txt", "some text"); + file2.setDerived(true, null); + + resources.add(project.getProject()); + new AddToIndexOperation(resources).execute(null); + + assertTrue(testRepository.inIndex(file1.getLocation() + .toPortableString())); + assertFalse(testRepository.inIndex(file2.getLocation() + .toPortableString())); + } + +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/ResetOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/ResetOperationTest.java index 5da8306d4e..7fe76d88b3 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/ResetOperationTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/ResetOperationTest.java @@ -58,7 +58,7 @@ public class ResetOperationTest extends GitTestCase { @Test public void testHardReset() throws Exception { setupRepository(); - String fileInIndexPath = fileInIndex.getLocation().toOSString(); + String fileInIndexPath = fileInIndex.getLocation().toPortableString(); new ResetOperation(repository, initialCommit.getName(), ResetOperation.ResetType.HARD).execute(null); // .project must disappear, related Eclipse project must be deleted @@ -77,7 +77,7 @@ public class ResetOperationTest extends GitTestCase { @Test public void testSoftReset() throws Exception { setupRepository(); - String fileInIndexPath = fileInIndex.getLocation().toOSString(); + String fileInIndexPath = fileInIndex.getLocation().toPortableString(); new ResetOperation(repository, initialCommit.getName(), ResetOperation.ResetType.SOFT).execute(null); // .project must remain @@ -97,7 +97,7 @@ public class ResetOperationTest extends GitTestCase { @Test public void testMixedReset() throws Exception { setupRepository(); - String fileInIndexPath = fileInIndex.getLocation().toOSString(); + String fileInIndexPath = fileInIndex.getLocation().toPortableString(); new ResetOperation(repository, initialCommit.getName(), ResetOperation.ResetType.MIXED).execute(null); // .project must remain |