1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.gitrepo;
11
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.hamcrest.Matchers.containsInAnyOrder;
14 import static org.hamcrest.Matchers.is;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import java.nio.charset.StandardCharsets;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.eclipse.jgit.gitrepo.BareSuperprojectWriter.BareWriterConfig;
23 import org.eclipse.jgit.gitrepo.RepoCommand.RemoteReader;
24 import org.eclipse.jgit.junit.RepositoryTestCase;
25 import org.eclipse.jgit.lib.ObjectId;
26 import org.eclipse.jgit.lib.ObjectReader;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.jgit.revwalk.RevCommit;
29 import org.junit.Test;
30
31 public class BareSuperprojectWriterTest extends RepositoryTestCase {
32
33 private static final String SHA1_A = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
34
35 @Override
36 public void setUp() throws Exception {
37 super.setUp();
38 }
39
40 @Test
41 public void write_setGitModulesContents() throws Exception {
42 try (Repository bareRepo = createBareRepository()) {
43 RepoProject repoProject = new RepoProject("subprojectX", "path/to",
44 "refs/heads/branch-x", "remote", "");
45 repoProject.setUrl("http://example.com/a");
46
47 RemoteReader mockRemoteReader = mock(RemoteReader.class);
48 when(mockRemoteReader.sha1("http://example.com/a",
49 "refs/heads/branch-x"))
50 .thenReturn(ObjectId.fromString(SHA1_A));
51
52 BareSuperprojectWriter w = new BareSuperprojectWriter(bareRepo,
53 null, "refs/heads/master", author, mockRemoteReader,
54 BareWriterConfig.getDefault(), List.of());
55
56 RevCommit commit = w.write(Arrays.asList(repoProject));
57
58 String contents = readContents(bareRepo, commit, ".gitmodules");
59 List<String> contentLines = Arrays
60 .asList(contents.split("\n"));
61 assertThat(contentLines.get(0),
62 is("[submodule \"subprojectX\"]"));
63 assertThat(contentLines.subList(1, contentLines.size()),
64 containsInAnyOrder(is("\tbranch = refs/heads/branch-x"),
65 is("\tpath = path/to"),
66 is("\turl = http://example.com/a")));
67 }
68 }
69
70 @Test
71 public void write_setExtraContents() throws Exception {
72 try (Repository bareRepo = createBareRepository()) {
73 RepoProject repoProject = new RepoProject("subprojectX", "path/to",
74 "refs/heads/branch-x", "remote", "");
75 repoProject.setUrl("http://example.com/a");
76
77 RemoteReader mockRemoteReader = mock(RemoteReader.class);
78 when(mockRemoteReader.sha1("http://example.com/a",
79 "refs/heads/branch-x"))
80 .thenReturn(ObjectId.fromString(SHA1_A));
81
82 BareSuperprojectWriter w = new BareSuperprojectWriter(bareRepo,
83 null, "refs/heads/master", author, mockRemoteReader,
84 BareWriterConfig.getDefault(),
85 List.of(new BareSuperprojectWriter.ExtraContent("x",
86 "extra-content")));
87
88 RevCommit commit = w.write(Arrays.asList(repoProject));
89
90 String contents = readContents(bareRepo, commit, "x");
91 assertThat(contents, is("extra-content"));
92 }
93 }
94
95 private String readContents(Repository repo, RevCommit commit,
96 String path) throws Exception {
97 String idStr = commit.getId().name() + ":" + path;
98 ObjectId modId = repo.resolve(idStr);
99 try (ObjectReader reader = repo.newObjectReader()) {
100 return new String(
101 reader.open(modId).getCachedBytes(Integer.MAX_VALUE),
102 StandardCharsets.UTF_8);
103
104 }
105 }
106 }