Search in sources :

Example 16 with GitSynchronizeDataSet

use of org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet in project egit by eclipse.

the class GitResourceVariantComparatorTest method shouldReturnTrueWhenShortContentIsDifferent.

/**
 * Comparing two files that have the same content and content length should
 * return true
 *
 * @throws Exception
 */
@Test
@SuppressWarnings("boxing")
public void shouldReturnTrueWhenShortContentIsDifferent() throws Exception {
    // when
    byte[] localContent = "very long long content".getBytes("UTF-8");
    byte[] remoteContent = "very long long content".getBytes("UTF-8");
    GitSynchronizeData data = new GitSynchronizeData(repo, HEAD, HEAD, true);
    GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data);
    GitResourceVariantComparator grvc = new GitResourceVariantComparator(dataSet);
    // given
    IFile local = mock(IFile.class);
    when(local.exists()).thenReturn(true);
    when(local.getProject()).thenReturn(project.getProject());
    when(local.getContents()).thenReturn(new ByteArrayInputStream(localContent));
    IStorage storage = mock(IStorage.class);
    when(storage.getContents()).thenReturn(new ByteArrayInputStream(remoteContent));
    IResourceVariant remote = mock(IResourceVariant.class);
    when(remote.isContainer()).thenReturn(false);
    when(remote.getStorage(any(IProgressMonitor.class))).thenReturn(storage);
    // then
    assertTrue(grvc.compare(local, remote));
}
Also used : GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) GitSynchronizeDataSet(org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet) ByteArrayInputStream(java.io.ByteArrayInputStream) IStorage(org.eclipse.core.resources.IStorage) IResourceVariant(org.eclipse.team.core.variants.IResourceVariant) Test(org.junit.Test)

Example 17 with GitSynchronizeDataSet

use of org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet in project egit by eclipse.

the class GitResourceVariantTreeTest method shoulReturnSameResourceVariant.

/**
 * Check if getResourceVariant() does return the same resource that was
 * committed. Passes only when it is run as a single test, not as a part of
 * largest test suite
 *
 * @throws Exception
 */
@Test
public void shoulReturnSameResourceVariant() throws Exception {
    // when
    String fileName = "Main.java";
    File file = testRepo.createFile(iProject, fileName);
    testRepo.appendContentAndCommit(iProject, file, "class Main {}", "initial commit");
    IFile mainJava = testRepo.getIFile(iProject, file);
    GitSynchronizeData data = new GitSynchronizeData(repo, HEAD, MASTER, false);
    GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data);
    GitSyncCache cache = GitSyncCache.getAllData(dataSet, new NullProgressMonitor());
    // given
    GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(cache, dataSet);
    // then
    // null variant indicates that resource wasn't changed
    assertNull(grvt.getResourceVariant(mainJava));
}
Also used : GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) GitSynchronizeDataSet(org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Test(org.junit.Test)

Example 18 with GitSynchronizeDataSet

use of org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet in project egit by eclipse.

the class GitResourceVariantTreeTest method shouldNotReturnNullOnSameResouceVariant.

@Test
public void shouldNotReturnNullOnSameResouceVariant() throws Exception {
    String modifiedFileName = "changingFile." + SampleModelProvider.SAMPLE_FILE_EXTENSION;
    String unchangedFileName = "notChangingFile." + SampleModelProvider.SAMPLE_FILE_EXTENSION;
    String removedFileName = "toBeRemovedFile." + SampleModelProvider.SAMPLE_FILE_EXTENSION;
    File modifiedFile = testRepo.createFile(iProject, modifiedFileName);
    File unchangedFile = testRepo.createFile(iProject, unchangedFileName);
    File removedFile = testRepo.createFile(iProject, removedFileName);
    testRepo.appendFileContent(modifiedFile, "My content is changing");
    testRepo.appendFileContent(unchangedFile, "My content is constant");
    testRepo.appendFileContent(removedFile, "I will be removed");
    IFile iModifiedFile = testRepo.getIFile(iProject, modifiedFile);
    IFile iUnchangedFile = testRepo.getIFile(iProject, unchangedFile);
    IFile iRemovedFile = testRepo.getIFile(iProject, removedFile);
    testRepo.trackAllFiles(iProject);
    RevCommit firstCommit = testRepo.commit("C1");
    testRepo.appendFileContent(modifiedFile, " My content has changed");
    testRepo.track(modifiedFile);
    testRepo.removeFromIndex(removedFile);
    RevCommit secondCommit = testRepo.commit("C2");
    // @formatter:off
    // History (X means has changed)
    // ------------------------------------------------------------
    // files					   C1 [HEAD]		  	C2
    // changingFile.sample   	|-----X----------|-------X-------|->
    // notChangingFile.sample	|-----X----------|---------------|->
    // toBeRemovedFile.sample	|-----X----------|-------X-------|->
    // -------------------------------------------------------------
    // @formatter:on
    testRepo.checkoutBranch(firstCommit.getName());
    iProject.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
    // Now synchronize the two commits using our logical model provider
    SampleModelProvider provider = new SampleModelProvider();
    // Get the affected resources
    ResourceMapping[] mappings = provider.getMappings(iModifiedFile, ResourceMappingContext.LOCAL_CONTEXT, new NullProgressMonitor());
    Set<IResource> includedResource = collectResources(mappings);
    Set<IResource> expectedIncludedResources = new HashSet<IResource>();
    expectedIncludedResources.add(iModifiedFile);
    expectedIncludedResources.add(iUnchangedFile);
    expectedIncludedResources.add(iRemovedFile);
    assertEquals(expectedIncludedResources, includedResource);
    // Synchronize the data
    final GitSynchronizeData data = new GitSynchronizeData(testRepo.getRepository(), firstCommit.getName(), secondCommit.getName(), true, includedResource);
    GitSynchronizeDataSet gitSynchDataSet = new GitSynchronizeDataSet(data);
    final GitResourceVariantTreeSubscriber subscriber = new GitResourceVariantTreeSubscriber(gitSynchDataSet);
    subscriber.init(new NullProgressMonitor());
    IResourceVariantTree sourceVariantTree = subscriber.getSourceTree();
    assertNotNull(sourceVariantTree);
    IResourceVariantTree remoteVariantTree = subscriber.getRemoteTree();
    assertNotNull(remoteVariantTree);
    // In the use case in which the file has been deleted the source variant is
    // not null whereas the remote variant is null.It seems quite logic.
    // However in the second use case we have the same result, the source variant is
    // not null whereas the remote is null. In both cases the null value does
    // not mean the same thing. In the first case, the null value means that
    // the resource is no longer in the repository and in the second the
    // null value means there is no change between the two versions.
    // Using these values I am not able to distinguish both case.
    // It is in contradiction with test #shouldReturnNullResourceVariant2()
    // and test #shoulReturnSameResourceVariant(). However I haven't found
    // another way to handle this case. Maybe something can be
    // done with ThreeWayDiffEntry.scan(tw) to force including in the cache
    // some entry even if they have not changed. For example,
    // ThreeWayDiffEntry.scan(tw,includedSource) or maybe try preventing the variant
    // tree to return null by walking throught the repository and looking for the file...
    IResourceVariant unchangedSourceVariant = sourceVariantTree.getResourceVariant(iUnchangedFile);
    IResourceVariant unchangedRemoteVariant = remoteVariantTree.getResourceVariant(iUnchangedFile);
    assertNotNull(unchangedSourceVariant);
    assertNotNull(unchangedRemoteVariant);
    IResourceVariant removedSourceVariant = sourceVariantTree.getResourceVariant(iRemovedFile);
    IResourceVariant removedRemoteVariant = remoteVariantTree.getResourceVariant(iRemovedFile);
    assertNotNull(removedSourceVariant);
    assertNull(removedRemoteVariant);
    GitSubscriberResourceMappingContext context = new GitSubscriberResourceMappingContext(subscriber, gitSynchDataSet);
    assertFalse(context.hasLocalChange(iUnchangedFile, new NullProgressMonitor()));
    assertFalse(context.hasRemoteChange(iUnchangedFile, new NullProgressMonitor()));
    assertFalse(context.hasLocalChange(iModifiedFile, new NullProgressMonitor()));
    assertTrue(context.hasRemoteChange(iModifiedFile, new NullProgressMonitor()));
    assertFalse(context.hasLocalChange(iRemovedFile, new NullProgressMonitor()));
    assertTrue(context.hasRemoteChange(iRemovedFile, new NullProgressMonitor()));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) IFile(org.eclipse.core.resources.IFile) SampleModelProvider(org.eclipse.egit.core.test.models.SampleModelProvider) IResourceVariant(org.eclipse.team.core.variants.IResourceVariant) GitSynchronizeDataSet(org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet) ResourceMapping(org.eclipse.core.resources.mapping.ResourceMapping) IResourceVariantTree(org.eclipse.team.core.variants.IResourceVariantTree) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) RevCommit(org.eclipse.jgit.revwalk.RevCommit) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with GitSynchronizeDataSet

use of org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet in project egit by eclipse.

the class GitResourceVariantTreeTest method shouldReturnDifferentResourceVariant.

/**
 * Create and commit Main.java file in master branch, then create branch
 * "test" checkout nearly created branch and modify Main.java file.
 * getResourceVariant() should obtain Main.java file content from "master"
 * branch. Passes only when it is run as a single test, not as a part of
 * largest test suite
 *
 * @throws Exception
 */
@Test
public void shouldReturnDifferentResourceVariant() throws Exception {
    // when
    String fileName = "Main.java";
    File file = testRepo.createFile(iProject, fileName);
    testRepo.appendContentAndCommit(iProject, file, "class Main {}", "initial commit");
    IFile mainJava = testRepo.getIFile(iProject, file);
    testRepo.createAndCheckoutBranch(Constants.R_HEADS + Constants.MASTER, Constants.R_HEADS + "test");
    testRepo.appendContentAndCommit(iProject, file, "// test", "first commit");
    GitSynchronizeData data = new GitSynchronizeData(repo, HEAD, MASTER, true);
    GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data);
    GitSyncCache cache = GitSyncCache.getAllData(dataSet, new NullProgressMonitor());
    // given
    GitResourceVariantTree grvt = new GitBaseResourceVariantTree(cache, dataSet);
    // then
    IResourceVariant actual = grvt.getResourceVariant(mainJava);
    assertNotNull(actual);
    assertEquals(fileName, actual.getName());
    InputStream actualIn = actual.getStorage(new NullProgressMonitor()).getContents();
    byte[] actualByte = getBytesAndCloseStream(actualIn);
    InputStream expectedIn = mainJava.getContents();
    byte[] expectedByte = getBytesAndCloseStream(expectedIn);
    // assert arrays not equals
    assertFalse(Arrays.equals(expectedByte, actualByte));
}
Also used : GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) GitSynchronizeDataSet(org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet) InputStream(java.io.InputStream) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResourceVariant(org.eclipse.team.core.variants.IResourceVariant) Test(org.junit.Test)

Example 20 with GitSynchronizeDataSet

use of org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet in project egit by eclipse.

the class HistoryTest method queryHistoryThroughTeam.

/*
	 * This aims at exerting the behavior of the EGit history provider when used
	 * through the Team APIs. This is the behavior extenders will see when
	 * interfacing with EGit through the synchronize view.
	 *
	 * The exact comparison with which we've reached the synchronize perspective
	 * should not be relevant. To keep this test as short as possible, we'll
	 * only test a single comparison.
	 */
@Test
public void queryHistoryThroughTeam() throws IOException, CoreException {
    GitSynchronizeData gsd = new GitSynchronizeData(testRepository.getRepository(), MASTER, BRANCH, false);
    GitSynchronizeDataSet gsds = new GitSynchronizeDataSet(gsd);
    GitResourceVariantTreeSubscriber subscriber = new GitResourceVariantTreeSubscriber(gsds);
    subscriber.init(new NullProgressMonitor());
    IDiff diff = subscriber.getDiff(iFile2);
    assertTrue(diff instanceof IThreeWayDiff);
    IFileRevision sourceRevision = getSource(diff);
    IFileRevision destinationRevision = getDestination(diff);
    IFileRevision baseRevision = getBase(diff);
    assertRevisionMatchCommit(baseRevision, masterCommit2);
    assertRevisionMatchCommit(destinationRevision, branchCommit2);
    assertRevisionMatchCommit(sourceRevision, masterCommit3);
    final IFileHistory history = historyProvider.getFileHistoryFor(iFile2, IFileHistoryProvider.NONE, new NullProgressMonitor());
    assertNotNull(history);
    // no parent of masterCommit2 in file2's history
    IFileRevision[] parents = history.getContributors(baseRevision);
    assertEquals(0, parents.length);
    /*
		 * branchCommit1 did not contain file2, so the "child" of masterCommit2
		 * (branching point) in file2's history is branchCommit2.
		 */
    IFileRevision[] children = history.getTargets(baseRevision);
    List<RevCommit> expectedChildren = new ArrayList<RevCommit>(Arrays.asList(masterCommit3, branchCommit2));
    assertEquals(expectedChildren.size(), children.length);
    assertMatchingRevisions(Arrays.asList(children), expectedChildren);
}
Also used : GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFileHistory(org.eclipse.team.core.history.IFileHistory) GitSynchronizeDataSet(org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet) IThreeWayDiff(org.eclipse.team.core.diff.IThreeWayDiff) IFileRevision(org.eclipse.team.core.history.IFileRevision) ArrayList(java.util.ArrayList) GitResourceVariantTreeSubscriber(org.eclipse.egit.core.synchronize.GitResourceVariantTreeSubscriber) IDiff(org.eclipse.team.core.diff.IDiff) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

GitSynchronizeDataSet (org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet)29 GitSynchronizeData (org.eclipse.egit.core.synchronize.dto.GitSynchronizeData)26 Test (org.junit.Test)15 IFile (org.eclipse.core.resources.IFile)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IResourceVariant (org.eclipse.team.core.variants.IResourceVariant)8 IResource (org.eclipse.core.resources.IResource)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 HashSet (java.util.HashSet)6 IStorage (org.eclipse.core.resources.IStorage)6 ResourceMapping (org.eclipse.core.resources.mapping.ResourceMapping)6 IOException (java.io.IOException)4 GitResourceVariantTreeSubscriber (org.eclipse.egit.core.synchronize.GitResourceVariantTreeSubscriber)4 GitSubscriberResourceMappingContext (org.eclipse.egit.core.synchronize.GitSubscriberResourceMappingContext)4 Git (org.eclipse.jgit.api.Git)4 Repository (org.eclipse.jgit.lib.Repository)4 File (java.io.File)3 SubscriberScopeManager (org.eclipse.team.core.subscribers.SubscriberScopeManager)3 LinkedHashSet (java.util.LinkedHashSet)2