View Javadoc

1   /*
2    * #%L
3    * JavaHg
4    * %%
5    * Copyright (C) 2011 aragost Trifork ag
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
25   */
26  package com.aragost.javahg.commands;
27  
28  import java.io.IOException;
29  
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  import com.aragost.javahg.Changeset;
34  import com.aragost.javahg.Repository;
35  import com.aragost.javahg.test.AbstractTestCase;
36  
37  public class UpdateCommandTest extends AbstractTestCase {
38  
39      @Test
40      public void testWithMergeConflicts() throws IOException {
41          Repository repo = getTestRepository();
42          writeFile("a");
43          Changeset csetA = commit();
44          writeFile("b");
45          writeFile("a");
46          Changeset csetB = commit();
47          UpdateResult result = UpdateCommand.on(repo).rev(csetA).execute();
48          verifyResult(result, 1, 0, 1, 0);
49          writeFile("a");
50          result = UpdateCommand.on(repo).rev(csetB).execute();
51          verifyResult(result, 1, 0, 0, 1);
52      }
53  
54      @Test
55      public void testWithMerge() throws IOException {
56          Repository repo = getTestRepository();
57          writeFile("a", "1\n2\n3\n");
58          Changeset csetA = commit();
59          writeFile("b");
60          writeFile("a", "11\n2\n3\n");
61          Changeset csetB = commit();
62          UpdateResult result = UpdateCommand.on(repo).rev(csetA).execute();
63          verifyResult(result, 1, 0, 1, 0);
64          writeFile("a", "1\n2\n\33\n");
65          result = UpdateCommand.on(repo).rev(csetB).execute();
66          verifyResult(result, 1, 0, 0, 1);
67      }
68  
69      @Test
70      public void testWithManifestMergeConflict() throws IOException {
71          Repository repo = getTestRepository();
72          writeFile("a");
73          Changeset csetA = commit();
74          writeFile("a");
75          Changeset csetB = commit();
76          UpdateResult result = UpdateCommand.on(repo).rev(csetA).execute();
77          verifyResult(result, 1, 0, 0, 0);
78          deleteFile("a");
79          ManifestMergeOracle oracle = new ManifestMergeOracle();
80          result = UpdateCommand.on(repo).rev(csetB).execute(oracle);
81          Assert.assertEquals(1, oracle.getMissingAnswers().size());
82          verifyResult(result, 1, 0, 0, 0);
83  
84      }
85  
86      private void verifyResult(UpdateResult r, int updated, int merged, int removed, int unresolved) {
87          Assert.assertEquals("updated", updated, r.getUpdated());
88          Assert.assertEquals("merged", merged, r.getMerged());
89          Assert.assertEquals("removed", removed, r.getRemoved());
90          Assert.assertEquals("unresolved", unresolved, r.getUnresolved());
91      }
92  
93      @Test
94      public void testUpdateCrossesBranches() throws IOException {
95          Repository repo = getTestRepository();
96          writeFile("a");
97          Changeset csetA = commit();
98          writeFile("a");
99          Changeset csetB = commit();
100 
101         UpdateCommand.on(repo).rev(csetA).execute();
102         writeFile("a");
103 
104         commit();
105 
106         Assert.assertEquals(2, repo.getBaseRepository().heads().size());
107 
108         UpdateCommand.on(repo).rev(csetB).execute();
109         UpdateCommand command = UpdateCommand.on(repo);
110 
111         try {
112             command.execute();
113             Assert.fail("Expected exception");
114         } catch (ExecutionException e) {
115             Assert.assertFalse(command.isSuccessful());
116             Assert.assertTrue(command.crossedBranch());
117         }
118     }
119 }