1 package com.aragost.javahg;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.List;
6
7 import org.junit.Assert;
8 import org.junit.Test;
9
10 import com.aragost.javahg.commands.AddCommand;
11 import com.aragost.javahg.commands.CommitCommand;
12 import com.aragost.javahg.commands.IncomingCommand;
13 import com.aragost.javahg.commands.LogCommand;
14 import com.aragost.javahg.commands.PushCommand;
15 import com.aragost.javahg.commands.UpdateCommand;
16 import com.aragost.javahg.test.AbstractTestCase;
17 import com.google.common.io.Files;
18
19 public class OverlayRepositoryTest extends AbstractTestCase {
20
21 @Test
22 public void testCreateBundle() throws IOException {
23 Bundle bundle = createBundle();
24 Assert.assertEquals(2, bundle.getChangesets().size());
25 OverlayRepository repo = bundle.getOverlayRepository();
26
27 Assert.assertEquals(3, repo.getChangesetCache().size());
28 List<Changeset> changesets = LogCommand.on(repo).rev("0:tip").execute();
29 Assert.assertEquals(4, changesets.size());
30 Assert.assertEquals(4, repo.getChangesetCache().size());
31 for (Changeset changeset : changesets) {
32 Assert.assertSame(repo, changeset.getRepository());
33 changeset.getExtra();
34 }
35 bundle.close();
36 }
37
38 @Test
39 public void testImportBundle() throws IOException {
40 Bundle bundle = createBundle();
41 OverlayRepository repo = bundle.getOverlayRepository();
42 Changeset overlayTip = repo.tip();
43 BaseRepository baseRepository = repo.getBaseRepository();
44 Assert.assertEquals(2, baseRepository.getChangesetCache().size());
45 Assert.assertEquals(3, repo.getChangesetCache().size());
46 bundle.pushToRepository();
47 Assert.assertEquals(2, baseRepository.getChangesetCache().size());
48 Assert.assertEquals(3, repo.getChangesetCache().size());
49
50 Changeset baseTip = LogCommand.on(baseRepository).rev("tip").single();
51 Assert.assertEquals(overlayTip.getNode(), baseTip.getNode());
52
53 for (Changeset cs : baseRepository.getChangesetCache().asMap().values()) {
54 Assert.assertSame(baseRepository, cs.getRepository());
55 }
56
57 for (Changeset cs : bundle.getChangesets()) {
58 Assert.assertSame(repo, cs.getRepository());
59 }
60
61 bundle.close();
62 }
63
64
65
66
67
68
69
70 private Bundle createBundle() throws IOException {
71 BaseRepository mainRepo = getTestRepository();
72 createChangeset();
73 createChangeset();
74 BaseRepository otherRepo = getTestRepository2();
75 PushCommand.on(mainRepo).execute(otherRepo.getDirectory().getPath());
76 UpdateCommand.on(otherRepo).execute();
77 Files.write(new byte[1], new File(otherRepo.getDirectory(), "other"));
78 AddCommand.on(otherRepo).execute();
79 CommitCommand.on(otherRepo).message("other 1").user("user").execute();
80 Files.write(new byte[2], new File(otherRepo.getDirectory(), "other"));
81 CommitCommand.on(otherRepo).message("other 2").user("user").execute();
82 Bundle bundle = IncomingCommand.on(mainRepo).execute(otherRepo);
83 return bundle;
84 }
85 }