summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnMergeInfoTest.java
blob: b6d83ad9ed2d03108a023254c1bb7ce6364066ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.idea.svn;

import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vcs.*;
import com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl;
import com.intellij.openapi.vcs.versionBrowser.ChangeBrowserSettings;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.svn.api.Depth;
import org.jetbrains.idea.svn.dialogs.MergeContext;
import org.jetbrains.idea.svn.dialogs.WCInfo;
import org.jetbrains.idea.svn.history.SvnChangeList;
import org.jetbrains.idea.svn.history.SvnRepositoryLocation;
import org.jetbrains.idea.svn.info.Info;
import org.jetbrains.idea.svn.mergeinfo.BranchInfo;
import org.jetbrains.idea.svn.mergeinfo.OneShotMergeInfoHelper;
import org.jetbrains.idea.svn.mergeinfo.SvnMergeInfoCache;
import org.junit.Assert;
import org.junit.Test;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.util.SVNPathUtil;
import org.tmatesoft.svn.core.wc.SVNPropertyData;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCClient;

import java.io.File;
import java.io.IOException;
import java.util.List;

// TODO: Many tests in this class are written with direct SVNKit usage - could not utilize it for svn 1.8
public class SvnMergeInfoTest extends Svn17TestCase {

  private static final String CONTENT1 = "123\n456\n123";
  private static final String CONTENT2 = "123\n456\n123\n4";

  private File myBranchVcsRoot;
  private ProjectLevelVcsManagerImpl myProjectLevelVcsManager;
  private WCInfo myWCInfo;
  private OneShotMergeInfoHelper myOneShotMergeInfoHelper;

  private SvnVcs myVcs;
  private BranchInfo myMergeChecker;

  private File trunk;
  private File folder;
  private File folder1;
  private File f1;
  private File f2;

  private String myTrunkUrl;
  private String myBranchUrl;
  private MergeContext myMergeContext;

  @Override
  public void setUp() throws Exception {
    super.setUp();

    myTrunkUrl = myRepoUrl + "/trunk";
    myBranchUrl = myRepoUrl + "/branch";

    myBranchVcsRoot = new File(myTempDirFixture.getTempDirPath(), "branch");
    myBranchVcsRoot.mkdir();

    myProjectLevelVcsManager = (ProjectLevelVcsManagerImpl) ProjectLevelVcsManager.getInstance(myProject);
    myProjectLevelVcsManager.setDirectoryMapping(myBranchVcsRoot.getAbsolutePath(), SvnVcs.VCS_NAME);

    VirtualFile vcsRoot = LocalFileSystem.getInstance().findFileByIoFile(myBranchVcsRoot);
    Node node = new Node(vcsRoot, SVNURL.parseURIEncoded(myBranchUrl), SVNURL.parseURIEncoded(myRepoUrl));
    RootUrlInfo root = new RootUrlInfo(node, WorkingCopyFormat.ONE_DOT_SIX, vcsRoot, null);
    myWCInfo = new WCInfo(root, true, Depth.INFINITY);
    myMergeContext = new MergeContext(SvnVcs.getInstance(myProject), myTrunkUrl, myWCInfo, SVNPathUtil.tail(myTrunkUrl), vcsRoot);
    myOneShotMergeInfoHelper = new OneShotMergeInfoHelper(myMergeContext);

    myVcs = SvnVcs.getInstance(myProject);
    myVcs.getSvnConfiguration().setCheckNestedForQuickMerge(true);

    enableSilentOperation(VcsConfiguration.StandardConfirmation.ADD);
    enableSilentOperation(VcsConfiguration.StandardConfirmation.REMOVE);

    final String repoUrl = SVNURL.parseURIDecoded(myRepoUrl).toString();
    myMergeChecker = new BranchInfo(myVcs, repoUrl, repoUrl + "/branch", repoUrl + "/trunk", repoUrl + "/trunk");
  }

  @Test
  public void testSimpleNotMerged() throws Exception {
    createOneFolderStructure();

    // rev 3
    editAndCommit(trunk, f1);

    assertMergeResult(getTrunkChangeLists(), SvnMergeInfoCache.MergeCheckResult.NOT_MERGED);
  }

  @Test
  public void testSimpleMerged() throws Exception {
    createOneFolderStructure();

    // rev 3
    editAndCommit(trunk, f1);

    // rev 4: record as merged into branch
    recordMerge(myBranchVcsRoot, myTrunkUrl, "-c", "3");
    commitFile(myBranchVcsRoot);
    updateFile(myBranchVcsRoot);

    assertMergeInfo(myBranchVcsRoot, "/trunk:3");
    assertMergeResult(getTrunkChangeLists(), SvnMergeInfoCache.MergeCheckResult.MERGED);
  }

  @Test
  public void testEmptyMergeinfoBlocks() throws Exception {
    createOneFolderStructure();

    // rev 3
    editAndCommit(trunk, f1);

    // rev 4: record as merged into branch
    merge(myBranchVcsRoot, myTrunkUrl, "-c", "3");
    commitFile(myBranchVcsRoot);
    updateFile(myBranchVcsRoot);
    // rev5: put blocking empty mergeinfo
    //runInAndVerifyIgnoreOutput("merge", "-c", "-3", myRepoUrl + "/trunk/folder", new File(myBranchVcsRoot, "folder").getAbsolutePath(), "--record-only"));
    merge(new File(myBranchVcsRoot, "folder"), myTrunkUrl + "/folder", "-r", "3:2");
    commitFile(myBranchVcsRoot);
    updateFile(myBranchVcsRoot);

    assertMergeInfo(myBranchVcsRoot, "/trunk:3");
    assertMergeInfo(new File(myBranchVcsRoot, "folder"), "");

    assertMergeResult(getTrunkChangeLists(), SvnMergeInfoCache.MergeCheckResult.NOT_MERGED);
  }

  @Test
  public void testNonInheritableMergeinfo() throws Exception {
    createOneFolderStructure();

    // rev 3
    editAndCommit(trunk, f1);

    // rev 4: record non inheritable merge
    setMergeInfo(myBranchVcsRoot, "/trunk:3*");
    commitFile(myBranchVcsRoot);
    updateFile(myBranchVcsRoot);

    assertMergeInfo(myBranchVcsRoot, "/trunk:3*");

    assertMergeResult(getTrunkChangeLists(), SvnMergeInfoCache.MergeCheckResult.NOT_MERGED);
  }

  @Test
  public void testOnlyImmediateInheritableMergeinfo() throws Exception {
    createOneFolderStructure();

    // rev 3
    editAndCommit(trunk, f1, CONTENT1);
    // rev4
    editAndCommit(trunk, f1, CONTENT2);

    updateFile(myBranchVcsRoot);

    // rev 4: record non inheritable merge
    setMergeInfo(myBranchVcsRoot, "/trunk:3,4");
    setMergeInfo(new File(myBranchVcsRoot, "folder"), "/trunk:3");
    commitFile(myBranchVcsRoot);
    updateFile(myBranchVcsRoot);

    assertMergeInfo(myBranchVcsRoot, "/trunk:3-4", "/trunk:3,4");
    assertMergeInfo(new File(myBranchVcsRoot, "folder"), "/trunk:3");

    final List<SvnChangeList> changeListList = getTrunkChangeLists();

    assertRevisions(changeListList, 4, 3);
    assertMergeResult(changeListList, SvnMergeInfoCache.MergeCheckResult.NOT_MERGED, SvnMergeInfoCache.MergeCheckResult.MERGED);
  }

  @Test
  public void testTwoPaths() throws Exception {
    createTwoFolderStructure(myBranchVcsRoot);

    // rev 3
    editFile(f1);
    editFile(f2);
    commitFile(trunk);

    updateFile(myBranchVcsRoot);

    // rev 4: record non inheritable merge
    setMergeInfo(myBranchVcsRoot, "/trunk:3");
    // this makes not merged for f2 path
    setMergeInfo(new File(myBranchVcsRoot, "folder/folder1"), "/trunk:3*");
    commitFile(myBranchVcsRoot);
    updateFile(myBranchVcsRoot);

    assertMergeInfo(myBranchVcsRoot, "/trunk:3");
    assertMergeInfo(new File(myBranchVcsRoot, "folder/folder1"), "/trunk:3*");

    final List<SvnChangeList> changeListList = getTrunkChangeLists();

    assertRevisions(changeListList, 3);
    assertMergeResult(changeListList, SvnMergeInfoCache.MergeCheckResult.NOT_MERGED);
  }

  @Test
  public void testWhenInfoInRepo() throws Exception {
    final File fullBranch = newFolder(myTempDirFixture.getTempDirPath(), "fullBranch");

    createTwoFolderStructure(fullBranch);
    // folder1 will be taken as branch wc root
    checkOutFile(myBranchUrl + "/folder/folder1", myBranchVcsRoot);

    // rev 3 : f2 changed
    editAndCommit(trunk, f2);

    // rev 4: record as merged into branch using full branch WC
    recordMerge(fullBranch, myTrunkUrl, "-c", "3");
    commitFile(fullBranch);
    updateFile(myBranchVcsRoot);

    final List<SvnChangeList> changeListList = getTrunkChangeLists();

    assertRevisions(changeListList, 3);
    assertMergeResult(changeListList.get(0), SvnMergeInfoCache.MergeCheckResult.MERGED);
  }

  @Test
  public void testMixedWorkingRevisions() throws Exception {
    createOneFolderStructure();

    // rev 3
    editAndCommit(trunk, f1);

    // rev 4: record non inheritable merge
    setMergeInfo(myBranchVcsRoot, "/trunk:3");
    commitFile(myBranchVcsRoot);
    // ! no update!

    assertMergeInfo(myBranchVcsRoot, "/trunk:3");

    final Info f1info = myVcs.getInfo(new File(myBranchVcsRoot, "folder/f1.txt"));
    assert f1info.getRevision().getNumber() == 2;

    final List<SvnChangeList> changeListList = getTrunkChangeLists();
    final SvnChangeList changeList = changeListList.get(0);

    assertMergeResult(changeList, SvnMergeInfoCache.MergeCheckResult.NOT_MERGED);

    // and after update
    updateFile(myBranchVcsRoot);
    myMergeChecker.clear();

    assertMergeResult(changeList, SvnMergeInfoCache.MergeCheckResult.MERGED);
  }

  private void createOneFolderStructure() throws InterruptedException, IOException {
    trunk = newFolder(myTempDirFixture.getTempDirPath(), "trunk");
    folder = newFolder(trunk, "folder");
    f1 = newFile(folder, "f1.txt");
    f2 = newFile(folder, "f2.txt");
    waitTime();

    importAndCheckOut(trunk);
  }

  private void createTwoFolderStructure(File branchFolder) throws InterruptedException, IOException {
    trunk = newFolder(myTempDirFixture.getTempDirPath(), "trunk");
    folder = newFolder(trunk, "folder");
    f1 = newFile(folder, "f1.txt");
    folder1 = newFolder(folder, "folder1");
    f2 = newFile(folder1, "f2.txt");
    waitTime();

    importAndCheckOut(trunk, branchFolder);
  }

  @NotNull
  private List<SvnChangeList> getTrunkChangeLists() throws com.intellij.openapi.vcs.VcsException {
    final CommittedChangesProvider<SvnChangeList, ChangeBrowserSettings> provider = myVcs.getCommittedChangesProvider();

    return provider.getCommittedChanges(provider.createDefaultSettings(), new SvnRepositoryLocation(myTrunkUrl), 0);
  }

  private void importAndCheckOut(@NotNull File trunk) throws IOException {
    importAndCheckOut(trunk, myBranchVcsRoot);
  }

  private void importAndCheckOut(@NotNull File trunk, @NotNull File branch) throws IOException {
    runInAndVerifyIgnoreOutput("import", "-m", "test", trunk.getAbsolutePath(), myTrunkUrl);
    runInAndVerifyIgnoreOutput("copy", "-m", "test", myTrunkUrl, myBranchUrl);

    FileUtil.delete(trunk);
    checkOutFile(myTrunkUrl, trunk);
    checkOutFile(myBranchUrl, branch);
  }

  @NotNull
  private VirtualFile editAndCommit(@NotNull File trunk, @NotNull File file) throws InterruptedException, IOException {
    return editAndCommit(trunk, file, CONTENT1);
  }

  @NotNull
  private VirtualFile editAndCommit(@NotNull File trunk, @NotNull File file, @NotNull String content)
    throws InterruptedException, IOException {
    final VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);

    return editAndCommit(trunk, vf, content);
  }

  @NotNull
  private VirtualFile editAndCommit(@NotNull File trunk, @NotNull VirtualFile file, @NotNull String content)
    throws InterruptedException, IOException {
    editFile(file, content);
    commitFile(trunk);

    return file;
  }

  private void editFile(@NotNull File file) throws InterruptedException {
    editFile(file, CONTENT1);
  }

  private void editFile(@NotNull File file, @NotNull String content) throws InterruptedException {
    final VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);

    editFile(vf, content);
  }

  private void editFile(@NotNull VirtualFile file, @NotNull String content) throws InterruptedException {
    VcsTestUtil.editFileInCommand(myProject, file, content);
    waitTime();
  }

  private void assertMergeInfo(@NotNull File file, @NotNull String... values) throws SVNException {
    // TODO: Replace with ClientFactory model
    final SvnVcs vcs = SvnVcs.getInstance(myProject);
    final SVNWCClient wcClient = vcs.getSvnKitManager().createWCClient();
    final SVNPropertyData data = wcClient.doGetProperty(file, "svn:mergeinfo", SVNRevision.UNDEFINED, SVNRevision.WORKING);
    assert data != null && data.getValue() != null;

    boolean result = false;

    for (String value : values) {
      result |= value.equals(data.getValue().getString());
    }

    assert result;
  }

  private void assertMergeResult(@NotNull List<SvnChangeList> changeLists, @NotNull SvnMergeInfoCache.MergeCheckResult... mergeResults)
    throws VcsException {
    myOneShotMergeInfoHelper.prepare();

    for (int i = 0; i < mergeResults.length; i++) {
      SvnChangeList changeList = changeLists.get(i);

      assertMergeResult(changeList, mergeResults[i]);
      assertMergeResultOneShot(changeList, mergeResults[i]);
    }
  }

  private void assertMergeResult(@NotNull SvnChangeList changeList, @NotNull SvnMergeInfoCache.MergeCheckResult mergeResult) {
    assert mergeResult.equals(myMergeChecker.checkList(changeList, myBranchVcsRoot.getAbsolutePath()));
  }

  private void assertMergeResultOneShot(@NotNull SvnChangeList changeList, @NotNull SvnMergeInfoCache.MergeCheckResult mergeResult) {
    Assert.assertEquals(mergeResult, myOneShotMergeInfoHelper.checkList(changeList));
  }

  private static void assertRevisions(@NotNull List<SvnChangeList> changeLists, @NotNull int... revisions) {
    for (int i = 0; i < revisions.length; i++) {
      assert changeLists.get(i).getNumber() == revisions[i];
    }
  }

  private void commitFile(@NotNull File file) throws IOException, InterruptedException {
    runInAndVerifyIgnoreOutput("ci", "-m", "test", file.getAbsolutePath());
    waitTime();
  }

  private void updateFile(@NotNull File file) throws IOException, InterruptedException {
    runInAndVerifyIgnoreOutput("up", file.getAbsolutePath());
    waitTime();
  }

  private void checkOutFile(@NotNull String url, @NotNull File directory) throws IOException {
    runInAndVerifyIgnoreOutput("co", url, directory.getAbsolutePath());
  }

  private void setMergeInfo(@NotNull File file, @NotNull String value) throws IOException, InterruptedException {
    runInAndVerifyIgnoreOutput("propset", "svn:mergeinfo", value, file.getAbsolutePath());
    waitTime();
  }

  private void merge(@NotNull File file, @NotNull String url, @NotNull String... revisions) throws IOException, InterruptedException {
    merge(file, url, false, revisions);
  }

  private void recordMerge(@NotNull File file, @NotNull String url, @NotNull String... revisions) throws IOException, InterruptedException {
    merge(file, url, true, revisions);
  }

  private void merge(@NotNull File file, @NotNull String url, boolean recordOnly, @NotNull String... revisions)
    throws IOException, InterruptedException {
    List<String> parameters = ContainerUtil.newArrayList();

    parameters.add("merge");
    ContainerUtil.addAll(parameters, revisions);
    parameters.add(url);
    parameters.add(file.getAbsolutePath());
    if (recordOnly) {
      parameters.add("--record-only");
    }

    runInAndVerifyIgnoreOutput(ArrayUtil.toObjectArray(parameters, String.class));
    waitTime();
  }

  @NotNull
  private static File newFile(File parent, String name) throws IOException {
    final File f1 = new File(parent, name);
    f1.createNewFile();
    return f1;
  }

  @NotNull
  private static File newFolder(String parent, String name) throws InterruptedException {
    final File trunk = new File(parent, name);
    trunk.mkdir();
    waitTime();
    return trunk;
  }

  @NotNull
  private static File newFolder(File parent, String name) throws InterruptedException {
    final File trunk = new File(parent, name);
    trunk.mkdir();
    waitTime();
    return trunk;
  }

  private static void waitTime() throws InterruptedException {
    Thread.sleep(100);
  }
}