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;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.List;
31  import java.util.Map;
32  
33  import junit.framework.Assert;
34  
35  import org.junit.Assume;
36  import org.junit.Test;
37  
38  import com.aragost.javahg.commands.AddCommand;
39  import com.aragost.javahg.commands.CommitCommand;
40  import com.aragost.javahg.commands.IncomingCommand;
41  import com.aragost.javahg.commands.LogCommand;
42  import com.aragost.javahg.internals.UnexpectedServerTerminationException;
43  import com.aragost.javahg.internals.Utils;
44  import com.aragost.javahg.test.AbstractTestCase;
45  import com.google.common.io.Files;
46  
47  public class RepositoryTest extends AbstractTestCase {
48  
49      @Test
50      public void testCreate() throws IOException {
51          File tmp = createTempDir();
52          File repoDir = new File(tmp, "repo");
53          Repository repo = Repository.create(repoDir);
54          Assert.assertEquals(repoDir, repo.getDirectory());
55  
56          File hgDir = new File(repoDir, ".hg");
57          Assert.assertTrue(hgDir.exists());
58          Assert.assertTrue(hgDir.isDirectory());
59  
60          repo.close();
61          deleteTempDir(tmp);
62      }
63  
64      @Test
65      public void testCreateWithExistingDirectory() throws IOException {
66          File repoDir = createTempDir();
67          Repository repo = Repository.create(repoDir);
68          Assert.assertEquals(repoDir, repo.getDirectory());
69  
70          File hgDir = new File(repoDir, ".hg");
71          Assert.assertTrue(hgDir.exists());
72          Assert.assertTrue(hgDir.isDirectory());
73  
74          repo.close();
75          deleteTempDir(repoDir);
76      }
77  
78      @Test
79      public void testOpen() throws IOException {
80          File dir = createMercurialRepository();
81          Assert.assertTrue(new File(dir, ".hg").exists());
82          Repository repo = Repository.open(dir);
83          repo.close();
84          deleteTempDir(dir);
85      }
86  
87      @Test
88      public void testClone() throws IOException {
89          Repository src = Repository.open(createMercurialRepository());
90          Files.write("q", new File(src.getDirectory(), "xxx"), utf8());
91          AddCommand.on(src).execute();
92          CommitCommand commit = CommitCommand.on(src);
93          commit.user("test").message("m");
94          commit.execute();
95          File tgt = Files.createTempDir();
96          Repository clone = Repository.clone(tgt, src.getDirectory().getAbsolutePath());
97          File xxx = new File(tgt, "xxx");
98          Assert.assertTrue(xxx.exists());
99          clone.close();
100         src.close();
101         deleteTempDir(tgt);
102         deleteTempDir(src.getDirectory());
103     }
104 
105     @Test(expected = IllegalArgumentException.class)
106     public void testOpenWithNoRepository() throws IOException {
107         File repoDir = Files.createTempDir();
108         try {
109             Repository.open(repoDir);
110         } finally {
111             deleteTempDir(repoDir);
112         }
113     }
114 
115     @Test
116     public void testCaching() throws IOException, InterruptedException {
117         Repository repo = getTestRepository();
118         writeFile("a");
119         Changeset csNode = commit();
120         String node = csNode.getNode();
121         Assert.assertNull(csNode.getData());
122         csNode.getRevision();
123         Assert.assertNotNull(csNode.getData());
124 
125         csNode = null;
126         System.gc(); // Should cause the csNode to be gc'ed and
127                      // removed from cache
128         Thread.sleep(100);
129         csNode = repo.changeset(node);
130         Assert.assertNull(csNode.getData());
131     }
132 
133     @Test
134     public void testBundleRepository() throws IOException {
135         BaseRepository repo = getTestRepository();
136         BaseRepository repo2 = getTestRepository2();
137 
138         writeFile("a");
139         Changeset cs = commit();
140 
141         Bundle bundle = IncomingCommand.on(repo2).execute(repo);
142         Repository repo3 = bundle.getOverlayRepository();
143         List<Changeset> changesets = LogCommand.on(repo3).execute();
144         Assert.assertEquals(1, changesets.size());
145         Assert.assertEquals(cs.getNode(), changesets.get(0).getNode());
146         bundle.close();
147     }
148 
149     @Test
150     public void testHgVersion() throws IOException {
151         BaseRepository repo = getTestRepository();
152         HgVersion hgVersion = repo.getHgVersion();
153         Assert.assertFalse(hgVersion.isUnknown());
154     }
155 
156     @Test
157     public void testTip() throws IOException {
158         BaseRepository repo = getTestRepository();
159         Assert.assertNull(repo.tip());
160         writeFile("a");
161         commit();
162         Changeset tip = repo.tip();
163         Assert.assertNotNull(tip);
164         Assert.assertEquals(0, tip.getRevision());
165     }
166 
167     @Test
168     public void testInvalidHgrc() throws IOException {
169         // This test case used to fail sporadic. Loop to increase
170         // likelyhood of it failing
171         for (int i = 0; i < 15; i++) {
172             RepositoryConfiguration conf = new RepositoryConfiguration();
173             conf.setHgrcPath(Utils.resourceAsFile("/test-hgrc-invalid").getPath());
174             File dir = Files.createTempDir();
175             try {
176                 Repository.create(conf, dir).close();
177                 Assert.fail("Exception expected");
178             } catch (UnexpectedServerTerminationException e) {
179                 // success
180             }
181             deleteTempDir(dir);
182         }
183     }
184 
185     @Test
186     public void testValidHgrc() throws IOException {
187         RepositoryConfiguration conf = new RepositoryConfiguration();
188         conf.setHgrcPath(Utils.resourceAsFile("/test-hgrc-valid").getPath());
189         File dir = Files.createTempDir();
190 
191         BaseRepository repo = Repository.create(conf, dir);
192         Files.write(new byte[0], new File(dir, "a"));
193         repo.workingCopy().add("a");
194         Changeset cs = CommitCommand.on(repo).message("m").execute();
195         // User is defined in the hgrc file
196         Assert.assertEquals("hgrc test user", cs.getUser());
197         repo.close();
198         deleteTempDir(dir);
199     }
200     
201     @Test
202     public void testPhases() throws IOException {
203         BaseRepository repo = getTestRepository();
204         Changeset cs1 = createChangeset();
205         Changeset cs2 = createChangeset();
206         Map<Changeset, Phase> phases = repo.phases("all()");
207         Assert.assertEquals(2, phases.size());
208         Phase p;
209         if (repo.getHgVersion().isBefore(HgVersion.fromString("2.1"))) {
210             p = null;
211         } else {
212             p = Phase.DRAFT;
213         }
214         Assert.assertEquals(p, phases.get(cs1));
215         Assert.assertEquals(p, phases.get(cs2));
216     }
217     
218     @Test
219     public void testCloneWithLargefileExtension() throws IOException {
220         BaseRepository repo = getTestRepository();
221         // Mercurial version 2.0+ is needed for the largefiles extension to be packages with Mercurial
222         Assume.assumeTrue(repo.getHgVersion().getMajor() >= 2);
223         writeFile(".hg/hgrc", "[extensions]\nlargefiles = \n");
224         createChangeset();
225         writeFile("x", "x");
226         // TODO: It seems Mercurial has a problem with the file is added via the server
227         execHgCommand(repo, "add", "--large", repo.file("x").getAbsolutePath());
228         execHgCommand(repo, "commit", "-m", "m", "-u", "testuser");
229         File targetDir = createTempDir();
230         RepositoryConfiguration repoConf = new RepositoryConfiguration();
231         repoConf.setHgrcPath(repo.file(".hg/hgrc").getAbsolutePath());
232         BaseRepository clone = Repository.clone(repoConf, targetDir, repo.getDirectory().getAbsolutePath());
233         Assert.assertEquals(1, clone.tip().getRevision());
234         File x = new File(clone.getDirectory(), "x");
235         Assert.assertTrue(x.exists());
236         clone.close();
237         deleteTempDir(targetDir);
238     }
239 
240 }