1 /*******************************************************************************
2 * Copyright (C) 2015 Obeo and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org
.eclipse
.emf
.compare
.ide
.ui
.tests
.merge
;
11 import static org
.junit
.Assert
.assertEquals
;
12 import static org
.junit
.Assert
.assertTrue
;
16 import org
.eclipse
.core
.resources
.IFile
;
17 import org
.eclipse
.core
.resources
.IResource
;
18 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
19 import org
.eclipse
.emf
.compare
.egit
.internal
.merge
.TreeWalkResourceVariantTreeProvider
;
20 import org
.eclipse
.jgit
.revwalk
.RevCommit
;
21 import org
.eclipse
.jgit
.revwalk
.RevTree
;
22 import org
.eclipse
.jgit
.revwalk
.RevWalk
;
23 import org
.eclipse
.jgit
.treewalk
.AbstractTreeIterator
;
24 import org
.eclipse
.jgit
.treewalk
.NameConflictTreeWalk
;
25 import org
.eclipse
.jgit
.treewalk
.TreeWalk
;
26 import org
.eclipse
.team
.core
.variants
.IResourceVariant
;
27 import org
.junit
.Test
;
29 public class TreeWalkResourceVariantTreeProviderTest
extends VariantsTestCase
{
31 public void testTreeWalkTrees() throws Exception
{
32 File file1
= repository
.createFile(iProject
, "file1");
33 File file2
= repository
.createFile(iProject
, "file2");
35 repository
.appendContentAndCommit(iProject
, file1
, INITIAL_CONTENT_1
, "first file - initial commit");
36 RevCommit baseCommit
= repository
.appendContentAndCommit(iProject
, file2
, INITIAL_CONTENT_2
,
37 "second file - initial commit");
39 IFile iFile1
= repository
.getIFile(iProject
, file1
);
40 IFile iFile2
= repository
.getIFile(iProject
, file2
);
42 repository
.createAndCheckoutBranch(MASTER
, BRANCH
);
44 final String branchChanges
= "branch changes\n";
45 setContentsAndCommit(repository
, iFile2
, branchChanges
+ INITIAL_CONTENT_2
, "branch commit");
47 repository
.checkoutBranch(MASTER
);
49 final String masterChanges
= "\nsome changes";
50 setContentsAndCommit(repository
, iFile1
, INITIAL_CONTENT_1
+ masterChanges
, "master commit");
51 iProject
.refreshLocal(IResource
.DEPTH_INFINITE
, new NullProgressMonitor());
54 // as if we tried to merge branch into master
55 RevWalk walk
= new RevWalk(repo
);
57 RevTree baseTree
= walk
.parseTree(baseCommit
.getId());
58 RevTree sourceTree
= walk
.parseTree(repo
.resolve(MASTER
));
59 RevTree remoteTree
= walk
.parseTree(repo
.resolve(BRANCH
));
60 TreeWalk treeWalk
= new NameConflictTreeWalk(repo
);
61 int baseTreeIndex
= treeWalk
.addTree(baseTree
);
62 treeWalk
.addTree(sourceTree
);
63 treeWalk
.addTree(remoteTree
);
64 TreeWalkResourceVariantTreeProvider treeProvider
= new TreeWalkResourceVariantTreeProvider
.Builder()//
65 .setaBaseTree(treeWalk
.getTree(baseTreeIndex
, AbstractTreeIterator
.class))//
66 .setDircache(repo
.readDirCache())//
67 .setHeadTree(sourceTree
)//
68 .setMergeTree(remoteTree
)//
69 .setRepository(repo
)//
70 .setReader(repo
.newObjectReader())//
73 assertEquals(1, treeProvider
.getRoots().size());
74 assertTrue(treeProvider
.getRoots().contains(iProject
));
76 assertTrue(treeProvider
.getKnownResources().contains(iFile1
));
77 assertTrue(treeProvider
.getKnownResources().contains(iFile2
));
79 IResourceVariant file1BaseVariant
= treeProvider
.getBaseTree().getResourceVariant(iFile1
);
80 IResourceVariant file2BaseVariant
= treeProvider
.getBaseTree().getResourceVariant(iFile2
);
81 assertContentEquals(file1BaseVariant
, INITIAL_CONTENT_1
);
82 assertContentEquals(file2BaseVariant
, INITIAL_CONTENT_2
);
84 IResourceVariant file1TheirsVariant
= treeProvider
.getRemoteTree().getResourceVariant(iFile1
);
85 IResourceVariant file2TheirsVariant
= treeProvider
.getRemoteTree().getResourceVariant(iFile2
);
86 assertContentEquals(file1TheirsVariant
, INITIAL_CONTENT_1
);
87 assertContentEquals(file2TheirsVariant
, branchChanges
+ INITIAL_CONTENT_2
);
89 IResourceVariant file1OursVariant
= treeProvider
.getSourceTree().getResourceVariant(iFile1
);
90 IResourceVariant file2OursVariant
= treeProvider
.getSourceTree().getResourceVariant(iFile2
);
91 assertContentEquals(file1OursVariant
, INITIAL_CONTENT_1
+ masterChanges
);
92 assertContentEquals(file2OursVariant
, INITIAL_CONTENT_2
);