Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: acd66d64f26036c6962e4ad29e037a8346591dde (plain) (blame)
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 * 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.tests.ccvs.core.subscriber;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSMergeSubscriber;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.core.CVSTag;
import org.eclipse.team.internal.ccvs.core.ICVSFile;
import org.eclipse.team.internal.ccvs.core.client.Command;
import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
import org.eclipse.team.tests.ccvs.core.CVSTestSetup;


/**
 * Tests the CVSMergeSubscriber
 */
public class CVSMergeSubscriberTest extends CVSSyncSubscriberTest {
	
	public static Test suite() {
		String testName = System.getProperty("eclipse.cvs.testName");
		if (testName == null) {
			TestSuite suite = new TestSuite(CVSMergeSubscriberTest.class);
			return new CVSTestSetup(suite);
		} else {
			return new CVSTestSetup(new CVSMergeSubscriberTest(testName));
		}
	}
	
	public CVSMergeSubscriberTest() {
		super();
	}
	
	public CVSMergeSubscriberTest(String name) {
		super(name);
	}
	
	private IProject branchProject(IProject project, CVSTag root, CVSTag branch) throws TeamException {
		IProject copy = checkoutCopy(project, "-copy");
		tagProject(project, root, false);
		tagProject(project, branch, false);
		updateProject(copy, branch, false);
		return copy;
	}
	
	private void mergeResources(CVSMergeSubscriber subscriber, IProject project, String[] resourcePaths, boolean allowOverwrite) throws CoreException, TeamException, InvocationTargetException, InterruptedException {
		IResource[] resources = getResources(project, resourcePaths);
		getSyncInfoSource().mergeResources(subscriber, resources, allowOverwrite);
	}
	
	/**
	 * Test the basic incoming changes cases
	 * - incoming addition
	 * - incoming deletion
	 * - incoming change
	 * - incoming addition of a folder containing files
	 */
	public void testIncomingChanges() throws InvocationTargetException, InterruptedException, CVSException, CoreException, IOException {
		// Create a test project
		IProject project = createProject("testIncomingChanges", new String[] { "file1.txt", "file2.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject copy = branchProject(project, root, branch);
		
		// Modify the branch
		addResources(copy, new String[] {"addition.txt", "folderAddition/", "folderAddition/new.txt"}, true);
		deleteResources(copy, new String[] {"folder1/a.txt"}, true);
		
		// modify file1 - make two revisions
		appendText(copy.getFile("file1.txt"), "Appended text 1", false);
		commitProject(copy);
		appendText(copy.getFile("file1.txt"), "Appended text 2", false);
		commitProject(copy);
		
		// modify file2 in both branch and head and ensure it's merged properly 
		appendText(copy.getFile("file2.txt"), "appended text", false);
		commitProject(copy);
		appendText(project.getFile("file2.txt"), "prefixed text", true);
		commitProject(project);		
				
		// create a merge subscriber
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		
		// check the sync states
		assertSyncEquals("testIncomingChanges", subscriber, project, 
				new String[]{"file1.txt", "file2.txt", "folder1/", "folder1/a.txt", "addition.txt", "folderAddition/", "folderAddition/new.txt"}, 
				true, 
				new int[]{
						SyncInfo.INCOMING |	SyncInfo.CHANGE, 
						SyncInfo.CONFLICTING |	SyncInfo.CHANGE,
						SyncInfo.IN_SYNC, 
						SyncInfo.INCOMING | SyncInfo.DELETION, 
						SyncInfo.INCOMING | SyncInfo.ADDITION, 
						SyncInfo.INCOMING | SyncInfo.ADDITION,				
						SyncInfo.INCOMING | SyncInfo.ADDITION});

		
		// Perform a merge
		mergeResources(subscriber, project, new String[]{"file1.txt", "file2.txt", "folder1/a.txt", "addition.txt", "folderAddition/", "folderAddition/new.txt"}, true);		

		// check the sync states for the workspace subscriber
		assertSyncEquals("testIncomingChanges", getWorkspaceSubscriber(), project, 
				new String[] { "file1.txt", "file2.txt", "folder1/", "folder1/a.txt", "addition.txt", "folderAddition/", "folderAddition/new.txt"}, 
				true, new int[] {
						SyncInfo.OUTGOING | SyncInfo.CHANGE,
						SyncInfo.OUTGOING | SyncInfo.CHANGE,
						SyncInfo.IN_SYNC,
						SyncInfo.OUTGOING  | SyncInfo.DELETION,
						SyncInfo.OUTGOING | SyncInfo.ADDITION,
						SyncInfo.IN_SYNC,
						SyncInfo.OUTGOING | SyncInfo.ADDITION});
	
		assertEndsWith(project.getFile("file1.txt"), "Appended text 1" + eol + "Appended text 2");
		assertStartsWith(project.getFile("file2.txt"), "prefixed text");
		assertEndsWith(project.getFile("file2.txt"), "appended text");
	}
	
	/**
	 * This tests tests that the cvs update command is sent properly with the two -j options to merge
	 * contents between two revisions into the workspaoce.
	 */
	// TODO: disabled until bug 346948 can be resolved
	public void _test46007() throws InvocationTargetException, InterruptedException, CVSException, CoreException, IOException {
		// Create a test project
		IProject project = createProject("test46007", new String[] { "file1.txt" });
		appendText(project.getFile("file1.txt"), "dummy", true);
		commitProject(project);
				
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject copy = branchProject(project, root, branch);
		
		// modify file1
		appendText(copy.getFile("file1.txt"), "Appended text 1", true);
		commitProject(copy);
		
		CVSTag root2 = new CVSTag("v1", CVSTag.VERSION);
		tagProject(copy, root2, true);
		appendText(copy.getFile("file1.txt"), "Appended text 2", false);
		commitProject(copy);
		CVSTag root3 = new CVSTag("v2", CVSTag.VERSION);
		tagProject(copy, root3, true);
		
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root2, root3);
		assertSyncEquals("test46007", subscriber, project, 
				new String[]{"file1.txt"}, 
				true, 
				new int[]{SyncInfo.CONFLICTING | SyncInfo.CHANGE});
		
		mergeResources(subscriber, project, new String[]{"file1.txt"}, true);		

		assertSyncEquals("test46007", getWorkspaceSubscriber(), project, 
				new String[] { "file1.txt"}, 
				true, new int[] {SyncInfo.OUTGOING | SyncInfo.CHANGE});
		
		// the change made before v1 in the branch should not of been merged into the
		// workspace since the start/end points do not include those changed. Hence,
		// the two -j options sent to the cvs server.
		assertEndsWith(project.getFile("file1.txt"), "Appended text 2");
		assertStartsWith(project.getFile("file1.txt"), "dummy");
	}	
	
	public void testMergableConflicts() throws IOException, TeamException, CoreException, InvocationTargetException, InterruptedException {
		// Create a test project
		IProject project = createProject("testMergableConflicts", new String[] { "file1.txt", "file2.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"), "some text\nwith several lines\n");
		setContentsAndEnsureModified(project.getFile("file2.txt"), "some text\nwith several lines\n");
		commitProject(project);
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// modify the branch
		appendText(branchedProject.getFile("file1.txt"), "first line\n", true);
		appendText(branchedProject.getFile("file2.txt"), "last line\n", false);
		commitProject(branchedProject);
		
		// modify HEAD
		appendText(project.getFile("file1.txt"), "last line\n", false);
		commitProject(project);
		// have one local change
		appendText(project.getFile("file2.txt"), "first line\n", true);
		
		// create a merge subscriber
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		
		// check the sync states
		assertSyncEquals("testMergableConflicts", subscriber, project, 
				new String[] { "file1.txt", "file2.txt"}, 
				true, new int[] {
							  SyncInfo.CONFLICTING | SyncInfo.CHANGE, 
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE});
		
		// Perform a merge
		mergeResources(subscriber, project, new String[] { 
													   "file1.txt",
		"file2.txt"}, 
		false /* allow overwrite */);
		
		// check the sync states for the workspace subscriber
		assertSyncEquals("testMergableConflicts", getWorkspaceSubscriber(), project, 
				new String[] { "file1.txt", "file2.txt"}, 
				true, new int[] {
							  SyncInfo.OUTGOING | SyncInfo.CHANGE,
							  		SyncInfo.OUTGOING  | SyncInfo.CHANGE});
		
		//TODO: How do we know if the right thing happened to the file contents?	
	}
	
	public void testUnmergableConflicts() throws IOException, TeamException, CoreException, InvocationTargetException, InterruptedException {
		// Create a test project
		IProject project = createProject("testUnmergableConflicts", new String[] { "delete.txt", "file1.txt", "file2.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"), "some text\nwith several lines\n");
		setContentsAndEnsureModified(project.getFile("file2.txt"), "some text\nwith several lines\n");
		commitProject(project);
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// modify the branch
		appendText(branchedProject.getFile("file1.txt"), "first line\n", true);
		appendText(branchedProject.getFile("file2.txt"), "last line\n", false);
		addResources(branchedProject, new String[] {"addition.txt"}, false);
		deleteResources(branchedProject, new String[] {"delete.txt", "folder1/a.txt"}, false);
		setContentsAndEnsureModified(branchedProject.getFile("folder1/b.txt"));
		commitProject(branchedProject);
		
		// modify local workspace
		appendText(project.getFile("file1.txt"), "conflict line\n", true);
		setContentsAndEnsureModified(project.getFile("folder1/a.txt"));
		deleteResources(project, new String[] {"delete.txt", "folder1/b.txt"}, false);
		addResources(project, new String[] {"addition.txt"}, false);
		appendText(project.getFile("file2.txt"), "conflict line\n", false);
		
		// create a merge subscriber
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		
		// check the sync states
		assertSyncEquals("testUnmergableConflicts", subscriber, project, 
				new String[] { "delete.txt", "file1.txt", "file2.txt", "addition.txt", "folder1/a.txt", "folder1/b.txt"}, 
				true, new int[] {
							  SyncInfo.IN_SYNC, /* TODO: is this OK */
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE, 
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE,
							  		SyncInfo.CONFLICTING | SyncInfo.ADDITION,
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE,
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE});
		
		// TODO: Should actually perform the merge and check the results
		// However, this would require the changes to be redone
		
		// commit to modify HEAD
		commitProject(project);
		
		// check the sync states
		assertSyncEquals("testUnmergableConflicts", subscriber, project, 
				new String[] { "delete.txt", "file1.txt", "file2.txt", "addition.txt", "folder1/a.txt", "folder1/b.txt"}, 
				true, new int[] {
							  SyncInfo.IN_SYNC, /* TODO: is this OK */
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE, 
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE,
							  		SyncInfo.CONFLICTING | SyncInfo.ADDITION,
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE,
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE});
		
		// Perform a merge
		mergeResources(subscriber, project, new String[] { "delete.txt", "file1.txt", "file2.txt", "addition.txt", "folder1/a.txt", "folder1/b.txt"}, true /* allow overwrite */);
		
		// check the sync states for the workspace subscriber
		assertSyncEquals("testUnmergableConflicts", getWorkspaceSubscriber(), project, 
				new String[] { "file1.txt", "file2.txt", "addition.txt", "folder1/a.txt", "folder1/b.txt"}, 
				true, new int[] {
							  SyncInfo.OUTGOING | SyncInfo.CHANGE,
							  		SyncInfo.OUTGOING | SyncInfo.CHANGE,
							  		SyncInfo.OUTGOING | SyncInfo.CHANGE,
							  		SyncInfo.OUTGOING | SyncInfo.DELETION,
							  		SyncInfo.OUTGOING | SyncInfo.ADDITION});
		assertDeleted("testUnmergableConflicts", project, new String[] { "delete.txt" });
		
		//TODO: How do we know if the right thing happend to the file contents?
	}
	
	public void testLocalScrub() throws IOException, TeamException, CoreException, InvocationTargetException, InterruptedException {
		// Create a test project
		IProject project = createProject("testLocalScrub", new String[] { "delete.txt", "file1.txt", "file2.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"), "some text\nwith several lines\n");
		setContentsAndEnsureModified(project.getFile("file2.txt"), "some text\nwith several lines\n");
		commitProject(project);
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// modify the branch
		appendText(branchedProject.getFile("file1.txt"), "first line\n", true);
		appendText(branchedProject.getFile("file2.txt"), "last line\n", false);
		addResources(branchedProject, new String[] {"addition.txt"}, false);
		deleteResources(branchedProject, new String[] {"delete.txt", "folder1/a.txt"}, false);
		setContentsAndEnsureModified(branchedProject.getFile("folder1/b.txt"));
		commitProject(branchedProject);
		
		// create a merge subscriber
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		
		// check the sync states
		assertSyncEquals("testLocalScrub", subscriber, project, 
				new String[] { "delete.txt", "file1.txt", "file2.txt", "addition.txt", "folder1/a.txt", "folder1/b.txt"}, 
				true, new int[] {
							  SyncInfo.INCOMING | SyncInfo.DELETION,
							  		SyncInfo.INCOMING | SyncInfo.CHANGE, 
							  		SyncInfo.INCOMING | SyncInfo.CHANGE,
							  		SyncInfo.INCOMING | SyncInfo.ADDITION,
							  		SyncInfo.INCOMING | SyncInfo.DELETION,
							  		SyncInfo.INCOMING | SyncInfo.CHANGE});
		
		//Refresh project to make sure we delete all existing files. See bug 403309
		project.refreshLocal(IProject.DEPTH_INFINITE, DEFAULT_MONITOR);
		
		// scrub the project contents
		IResource[] members = project.members();
		for (int i = 0; i < members.length; i++) {
			IResource resource = members[i];
			if (resource.getName().equals(".project")) continue;
			resource.delete(true, DEFAULT_MONITOR);
		}
		
		// update
		mergeResources(subscriber, project, 
				new String[] { 
						   "delete.txt", 
						   		"file1.txt", 
						   		"file2.txt", 
						   		"addition.txt", 
						   		"folder1/a.txt",
		"folder1/b.txt"}, 
		true /* allow overwrite */);
		
		// commit
		commitProject(project);
	}
	
	public void testBug37546MergeWantsToDeleteNewDirectories() throws CVSException, CoreException {		
		IProject project = createProject("testBug37546", new String[]{"file1.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"), "some text\nwith several lines\n");
		commitProject(project);
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// modify the branch
		addResources(branchedProject, new String[] {"folder2/", "folder2/c.txt"}, true);
		
		// modify HEAD and add the same folder
		addResources(project, new String[] {"folder2/"}, true);
		addResources(project, new String[] {"folder3/"}, true);
		addResources(project, new String[] {"folder4/", "folder4/d.txt"}, false);
		
		// create a merge subscriber
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		
		assertSyncEquals("testBug37546", subscriber, project, 
				new String[]{"folder2/", "folder2/c.txt", "folder3/", "folder4/", "folder4/d.txt"}, true, 
				new int[]{
						SyncInfo.IN_SYNC, 
								SyncInfo.INCOMING | SyncInfo.ADDITION,
								SyncInfo.IN_SYNC, SyncInfo.IN_SYNC, SyncInfo.IN_SYNC});		
	}
	
	/*Bug 53129  
	   Outgoing deletions in deleted folders are lost.
	 */
	public void testOutgoingDeletionAfterMergeBug53129() throws TeamException, CoreException, InvocationTargetException, InterruptedException {		
		IProject project = createProject("testBug53129", new String[]{"file1.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"), "some text\nwith several lines\n");
		commitProject(project);
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// modify the branch
		deleteResources(branchedProject, new String[] {"folder1/a.txt", "folder1/b.txt"}, true);
		
		// create a merge subscriber
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		
		assertSyncEquals("testBug53129 - 1", subscriber, project, 
				new String[]{"file1.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"}, true, 
				new int[]{
						SyncInfo.IN_SYNC, SyncInfo.IN_SYNC,
						SyncInfo.INCOMING | SyncInfo.DELETION,
						SyncInfo.INCOMING | SyncInfo.DELETION});
		
		mergeResources(subscriber, project, new String[]{"folder1/a.txt", "folder1/b.txt"}, true);
		
		assertSyncEquals("testBug53129 - 2", getWorkspaceSubscriber(), project, 
				new String[]{"file1.txt", "folder1", "folder1/a.txt", "folder1/b.txt"}, true, 
				new int[]{
						SyncInfo.IN_SYNC, SyncInfo.IN_SYNC, 
						SyncInfo.OUTGOING | SyncInfo.DELETION,
						SyncInfo.OUTGOING | SyncInfo.DELETION});
		
		IFolder f = project.getFolder("folder1");
		f.delete(true, null);
		
		assertSyncEquals("testBug53129 - 3", getWorkspaceSubscriber(), project, 
				new String[]{"file1.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"}, true, 
				new int[]{
						SyncInfo.IN_SYNC, 
						SyncInfo.IN_SYNC,
						SyncInfo.OUTGOING | SyncInfo.DELETION,
						SyncInfo.OUTGOING | SyncInfo.DELETION});
	}
	
	public void testDisconnectingProject() throws CoreException, IOException, TeamException, InterruptedException {
		// Create a test project (which commits it as well)
		//		Create a test project
		IProject project = createProject("testDisconnect", new String[] { "file1.txt", "file2.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"), "some text\nwith several lines\n");
		setContentsAndEnsureModified(project.getFile("file2.txt"), "some text\nwith several lines\n");
		commitProject(project);
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// modify the branch
		appendText(branchedProject.getFile("file1.txt"), "first line\n", true);
		appendText(branchedProject.getFile("file2.txt"), "last line\n", false);
		commitProject(branchedProject);
		
		// create a merge subscriber
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		
		RepositoryProvider.unmap(project);
		assertProjectRemoved(subscriber, project);
	}
	
	public void testMarkAsMerged() throws IOException, TeamException, CoreException, InvocationTargetException, InterruptedException {
		// Create a test project
		IProject project = createProject("testMarkAsMerged", new String[] { "delete.txt", "file1.txt", "file2.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		setContentsAndEnsureModified(project.getFile("file1.txt"), "some text\nwith several lines\n");
		setContentsAndEnsureModified(project.getFile("file2.txt"), "some text\nwith several lines\n");
		commitProject(project);
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// modify the branch
		appendText(branchedProject.getFile("file1.txt"), "first line\n", true);
		appendText(branchedProject.getFile("file2.txt"), "last line\n", false);
		addResources(branchedProject, new String[] {"addition.txt"}, false);
		deleteResources(branchedProject, new String[] {"delete.txt", "folder1/a.txt"}, false);
		setContentsAndEnsureModified(branchedProject.getFile("folder1/b.txt"));
		commitProject(branchedProject);
		
		// modify local workspace
		appendText(project.getFile("file1.txt"), "conflict line\n", true);
		setContentsAndEnsureModified(project.getFile("folder1/a.txt"));
		setContentsAndEnsureModified(project.getFile("delete.txt"));
		addResources(project, new String[] {"addition.txt"}, false);
		appendText(project.getFile("file2.txt"), "conflict line\n", false);
		
		// create a merge subscriber
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		
		// check the sync states
		assertSyncEquals("testMarkAsMergedConflicts", subscriber, project, 
				new String[] { "delete.txt", "file1.txt", "file2.txt", "addition.txt", "folder1/a.txt"}, 
				true, new int[] {
							  SyncInfo.CONFLICTING | SyncInfo.CHANGE,
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE, 
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE,
							  		SyncInfo.CONFLICTING | SyncInfo.ADDITION,
							  		SyncInfo.CONFLICTING | SyncInfo.CHANGE});
		
		markAsMerged(subscriber, project, new String[] { "delete.txt", "file1.txt", "file2.txt", "addition.txt", "folder1/a.txt"});
		
		// check the sync states
		assertSyncEquals("testMarkAsMerged", subscriber, project, 
				new String[] { "delete.txt", "file1.txt", "file2.txt", "addition.txt", "folder1/a.txt"}, 
				true, new int[] {
							  SyncInfo.IN_SYNC,
							  		SyncInfo.IN_SYNC, 
							  		SyncInfo.IN_SYNC,
							  		SyncInfo.IN_SYNC,
							  		SyncInfo.IN_SYNC});				
	} 
	
	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		getSyncInfoSource().tearDown();
		super.tearDown();
	}
	
	public void testDeletedAddition() throws TeamException, CoreException, InvocationTargetException, InterruptedException {
		IProject project = createProject("testDeletedAddition", new String[]{"file1.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// add a file to the branch
		addResources(branchedProject, new String[] {"folder2/", "folder2/added.txt"}, true);
		
		// Setup a merge by creating a merge subscriber
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		assertSyncEquals("testDeletedAddition", subscriber, project, 
				new String[]{"folder2/", "folder2/added.txt"}, true, 
				new int[]{
					SyncInfo.INCOMING | SyncInfo.ADDITION, 
					SyncInfo.INCOMING | SyncInfo.ADDITION
				});
		
		// Merge the change with HEAD
		mergeResources(subscriber, project, new String[]{"folder2/", "folder2/added.txt"}, true);
		assertSyncEquals("testDeletedAddition", subscriber, project, 
				new String[]{"folder2/", "folder2/added.txt"}, true, 
				new int[]{
					SyncInfo.IN_SYNC, 
					SyncInfo.IN_SYNC
				});

		// Delete the file from the branch
		deleteResources(branchedProject, new String[] {"folder2/added.txt"}, true);
		assertSyncEquals("testDeletedAddition", subscriber, project, 
				new String[]{"folder2/", "folder2/added.txt"}, true, 
				new int[]{
					SyncInfo.IN_SYNC, 
					SyncInfo.CONFLICTING | SyncInfo.CHANGE
				});
	}
	
	public void testFileAddedToBranch() throws InvocationTargetException, InterruptedException, CoreException, IOException {
		// Create a project
		IProject project = createProject(new String[] { "delete.txt", "file1.txt", "file2.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// Add a file to the branch
		addResources(branchedProject, new String[] {"folder2/", "folder2/added.txt"}, true);
		
		// Merge the file with HEAD but do not commit
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
		assertSyncEquals("testFileAddedToBranch", subscriber, project, 
				new String[]{"folder2/", "folder2/added.txt"}, true, 
				new int[]{
					SyncInfo.INCOMING | SyncInfo.ADDITION, 
					SyncInfo.INCOMING | SyncInfo.ADDITION
				});
		mergeResources(subscriber, project, new String[]{"folder2/", "folder2/added.txt"}, true /* allow overwrite */);
		
		// Ensure HEAD matches branch
		assertContentsEqual(project, branchedProject);
		
		// Ensure that the tag on the file is HEAD
		ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor(project.getFile("folder2/added.txt"));
		ResourceSyncInfo syncInfo = cvsFile.getSyncInfo();
		CVSTag tag = syncInfo.getTag();
		if (tag != null && !tag.equals(CVSTag.DEFAULT))
			fail("Invalid tag for added file");
		
		// Modify the file on the branch
		setContentsAndEnsureModified(branchedProject.getFile("folder2/added.txt"), "Unmergable contents");
		commitProject(branchedProject);
		
		// Merge with HEAD again and commit afterwards
		assertSyncEquals("testFileAddedToBranch", subscriber, project, 
				new String[]{"folder2/added.txt"}, true, 
				new int[]{
					SyncInfo.CONFLICTING | SyncInfo.CHANGE
				});
		mergeResources(subscriber, project, new String[]{"folder2/added.txt"}, true /* allow overwrite */);
		commitProject(project);

		// Ensure HEAD matches branch
		assertContentsEqual(project, branchedProject);
		
		// Modify the file on the branch again
		setContentsAndEnsureModified(branchedProject.getFile("folder2/added.txt"), "More unmergable");
		commitProject(branchedProject);
		
		// Merge with HEAD one last time
		assertSyncEquals("testFileAddedToBranch", subscriber, project, 
				new String[]{"folder2/added.txt"}, true, 
				new int[]{
					SyncInfo.CONFLICTING | SyncInfo.CHANGE
				});
		mergeResources(subscriber, project, new String[]{"folder2/added.txt"}, true /* allow overwrite */);

		// Ensure HEAD matches branch
		assertContentsEqual(project, branchedProject);
	}
	
    public void testBinaryAddition() throws CoreException {
    	// See bug 132255
    	KSubstOption option = CVSProviderPlugin.getPlugin().getDefaultTextKSubstOption();
    	try {
	    	CVSProviderPlugin.getPlugin().setDefaultTextKSubstOption(Command.KSUBST_TEXT_KEYWORDS_ONLY);
	    	IProject project = createProject(new String[] { "a.txt"});
			// Checkout and branch a copy
			CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
			CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
			IProject branchedProject = branchProject(project, root, branch);
			// Add a binary file to the branch that contains LFs
	    	create(branchedProject.getFile("binaryFile"), true);
	    	setContentsAndEnsureModified(branchedProject.getFile("binaryFile"), "/n/n\n\n");
	    	addResources(new IResource[] { branchedProject.getFile("binaryFile") });
	    	commitProject(branchedProject);
	    	// Merge the addition with HEAD
	    	CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
	    	getSyncInfoSource().refresh(subscriber, project);
	    	getSyncInfoSource().updateResources(subscriber, new IResource[] { project.getFile("binaryFile") });
	    	assertContentsEqual(branchedProject.getFile("binaryFile"), project.getFile("binaryFile"));
    	} finally {
    		CVSProviderPlugin.getPlugin().setDefaultTextKSubstOption(option);
    	}
    }
    
    public void testBinaryMarkAsMerged() throws CoreException, InvocationTargetException, InterruptedException {
    	// See bug 132255
    	KSubstOption option = CVSProviderPlugin.getPlugin().getDefaultTextKSubstOption();
    	try {
	    	CVSProviderPlugin.getPlugin().setDefaultTextKSubstOption(Command.KSUBST_TEXT_KEYWORDS_ONLY);
	    	IProject project = createProject(new String[] { "a.txt"});
			// Checkout and branch a copy
			CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
			CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
			IProject branchedProject = branchProject(project, root, branch);
			// Add a binary file to the branch that contains LFs
	    	create(branchedProject.getFile("binaryFile"), true);
	    	setContentsAndEnsureModified(branchedProject.getFile("binaryFile"), "/n/n\n\n");
	    	addResources(new IResource[] { branchedProject.getFile("binaryFile") });
	    	commitProject(branchedProject);
	    	// Add the same file to the project but don't share it
	    	create(project.getFile("binaryFile"), true);
	    	setContentsAndEnsureModified(project.getFile("binaryFile"), "/n/nSome Content\n\n");
	    	// Merge the addition with HEAD
	    	CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(project, root, branch);
	    	getSyncInfoSource().refresh(subscriber, project);
	    	getSyncInfoSource().markAsMerged(subscriber, new IResource[] { project.getFile("binaryFile") });
	    	assertIsBinary(project.getFile("binaryFile"));
    	} finally {
    		CVSProviderPlugin.getPlugin().setDefaultTextKSubstOption(option);
    	}
    }
    
	public void testMergeNewFileToBranch() throws InvocationTargetException, InterruptedException, CoreException, IOException {
		// Create a project
		IProject project = createProject(new String[] {"file1.txt"});
		
		// Checkout and branch a copy
		CVSTag root = new CVSTag("root_branch1", CVSTag.VERSION);
		CVSTag branch = new CVSTag("branch1", CVSTag.BRANCH);
		IProject branchedProject = branchProject(project, root, branch);
		
		// Add a file to HEAD
		addResources(project, new String[] {"added.txt"}, true);
		
		// Merge the file with branch but do not commit
		CVSMergeSubscriber subscriber = getSyncInfoSource().createMergeSubscriber(branchedProject, root, CVSTag.DEFAULT);
		assertSyncEquals("testFileAddedToBranch", subscriber, branchedProject, 
				new String[]{"added.txt"}, true, 
				new int[]{
					SyncInfo.INCOMING | SyncInfo.ADDITION
				});
		mergeResources(subscriber, branchedProject, new String[]{"added.txt"}, true /* allow overwrite */);
		
		// Ensure HEAD matches branch
		assertContentsEqual(project, branchedProject);
		
		// Ensure that the tag on the file is the branch tag
		ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor(branchedProject.getFile("added.txt"));
		ResourceSyncInfo syncInfo = cvsFile.getSyncInfo();
		assertEquals("Invalid tag for added file", syncInfo.getTag(), branch);
		
		// Add a file in a subfolder to HEAD
		addResources(project, new String[] {"folder1/added.txt"}, true);
		assertSyncEquals("testFileAddedToBranch", subscriber, branchedProject, 
				new String[]{"folder1/added.txt"}, true, 
				new int[]{
					SyncInfo.INCOMING | SyncInfo.ADDITION
				});
		mergeResources(subscriber, branchedProject, new String[]{"folder1/added.txt"}, true /* allow overwrite */);
		// Ensure HEAD matches branch
		assertContentsEqual(project, branchedProject);
		
		// Ensure that the tag on the file is the branch tag
		cvsFile = CVSWorkspaceRoot.getCVSFileFor(branchedProject.getFile("folder1/added.txt"));
		syncInfo = cvsFile.getSyncInfo();
		assertEquals("Invalid tag for added file", syncInfo.getTag(), branch);
	}
}

Back to the top