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.File;
29  import java.io.IOException;
30  import java.util.List;
31  
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  import com.aragost.javahg.Changeset;
36  import com.aragost.javahg.Repository;
37  import com.aragost.javahg.test.AbstractTestCase;
38  import com.google.common.io.Files;
39  
40  public class PullCommandTest extends AbstractTestCase {
41  
42      @Test
43      public void testEmptyPull() throws IOException {
44          Repository repo = getTestRepository();
45          File dir = repo.getDirectory();
46  
47          List<Changeset> changesets = PullCommand.on(repo).execute(dir.getPath());
48          Assert.assertEquals(0, changesets.size());
49      }
50  
51      @Test
52      public void testPull() throws IOException {
53          Repository repoA = getTestRepository();
54          File dirA = repoA.getDirectory();
55  
56          writeFile("x", "abc");
57  
58          AddCommand add = AddCommand.on(repoA);
59          add.execute();
60  
61          CommitCommand commit = CommitCommand.on(repoA);
62          commit.message("added x").user("user");
63          commit.execute();
64  
65          Repository repoB = getTestRepository2();
66  
67          List<Changeset> changesets = PullCommand.on(repoB).execute(dirA.getPath());
68          Assert.assertEquals(1, changesets.size());
69          Changeset cs = changesets.get(0);
70          Assert.assertEquals("user", cs.getUser());
71          Assert.assertEquals("added x", cs.getMessage());
72      }
73  
74      @Test
75      public void testHTTPPull() throws IOException {
76          Repository repoA = getTestRepository();
77  
78          writeFile("x", "abc");
79  
80          AddCommand add = AddCommand.on(repoA);
81          add.execute();
82  
83          CommitCommand commit = CommitCommand.on(repoA);
84          commit.message("added x").user("user");
85          commit.execute();
86  
87          ServeState serveState = startServing(repoA);
88          try {
89              int port = serveState.getPort();
90  
91              Repository repoB = getTestRepository2();
92              List<Changeset> changesets = PullCommand.on(repoB).execute("http://localhost:" + port);
93              Assert.assertEquals(1, changesets.size());
94              Changeset cs = changesets.get(0);
95              Assert.assertEquals("user", cs.getUser());
96              Assert.assertEquals("added x", cs.getMessage());
97          } finally {
98              serveState.stop();
99          }
100     }
101 
102     @Test(expected = ExecutionException.class)
103     public void testPullWithNoDefault() throws IOException {
104         Repository repo = getTestRepository();
105         PullCommand.on(repo).execute();
106     }
107 
108     @Test(expected = NullPointerException.class)
109     public void testPullNullSource() throws IOException {
110         Repository repo = getTestRepository();
111         PullCommand.on(repo).execute(null);
112     }
113 
114     @Test
115     public void testWithDivergingBookmarks() throws IOException {
116         Repository repo1 = getTestRepository();
117         createChangeset();
118         BookmarksCommand.on(repo1).create("bm");
119         BookmarksCommand.on(repo1).list();
120 
121         File cloneDir = Files.createTempDir();
122         Repository clone = Repository.clone(cloneDir, repo1.getDirectory().getAbsolutePath());
123         Assert.assertEquals(1, BookmarksCommand.on(clone).list().size());
124 
125         Files.write("abc".getBytes(), new File(cloneDir, "clone"));
126         AddCommand.on(clone).execute("clone");
127         CommitCommand.on(clone).user("user").message("m").execute();
128         BookmarksCommand.on(clone).force().create("bm");
129         BookmarksCommand.on(clone).list();
130 
131         createChangeset();
132 
133         PullCommand.on(repo1).execute(clone.getDirectory().getAbsolutePath());
134         BookmarksCommand.on(repo1).list();
135 
136         clone.close();
137         deleteTempDir(cloneDir);
138     }
139 }