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.BaseRepository;
36  import com.aragost.javahg.Changeset;
37  import com.aragost.javahg.Repository;
38  import com.aragost.javahg.internals.Utils;
39  import com.aragost.javahg.test.AbstractTestCase;
40  import com.google.common.io.Files;
41  
42  public class PushCommandTest extends AbstractTestCase {
43  
44      @Test
45      public void testEmptyPush() throws IOException {
46          Repository repo = getTestRepository();
47          File dir = getTestRepository2().getDirectory();
48  
49          List<Changeset> changesets = PushCommand.on(repo).execute(dir.getPath());
50          Assert.assertEquals(0, changesets.size());
51      }
52  
53      @Test
54      public void testPush() throws IOException {
55          Repository repoA = getTestRepository();
56          Changeset a = createChangeset();
57  
58          File dirB = createMercurialRepository();
59  
60          List<Changeset> changesets = PushCommand.on(repoA).execute(dirB.getPath());
61          Changeset b = Utils.single(changesets);
62          Assert.assertSame(a, b);
63  
64          deleteTempDir(dirB);
65      }
66  
67      @Test(expected = ExecutionException.class)
68      public void testPushWithNoDefault() throws IOException {
69          Repository repo = getTestRepository();
70          PushCommand.on(repo).execute();
71      }
72  
73      @Test(expected = NullPointerException.class)
74      public void testPushNullSource() throws IOException {
75          Repository repo = getTestRepository();
76          PushCommand.on(repo).execute(null);
77      }
78  
79      @Test
80      public void testPushCreatingNewHead() throws IOException {
81          Repository repoA = getTestRepository();
82          Changeset base = createChangeset();
83  
84          File dir = createTempDir();
85          BaseRepository repoB = Repository.clone(dir, repoA.getDirectory().getPath());
86  
87          Changeset a = createChangeset();
88          update(base);
89          Changeset b = createChangeset();
90  
91          Files.write("a".getBytes(), new File(repoB.getDirectory(), "file"));
92          AddCommand.on(repoB).execute();
93          CommitCommand.on(repoB).message("m").user("u").execute();
94          PushCommand push = PushCommand.on(repoA);
95          try {
96              push.execute(repoB.getDirectory().getPath());
97              assertFailedExecution(push);
98          } catch (ExecutionException e) {
99              Assert.assertTrue(e.getMessage().startsWith("push creates new remote head"));
100         }
101 
102         List<Changeset> changesets = PushCommand.on(repoA).force().execute(repoB.getDirectory().getPath());
103         Assert.assertEquals(2, changesets.size());
104         Assert.assertTrue(changesets.contains(a));
105         Assert.assertTrue(changesets.contains(b));
106 
107         repoB.close();
108         deleteTempDir(dir);
109     }
110 }