Browse Source

RepoCommandTest: Refactor to use try-with-resource

Change-Id: If37ce4447feb431169a75594194a7ef02e362d4e
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.0
David Pursehouse 7 years ago
parent
commit
1512035451
  1. 768
      org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java

768
org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java

@ -185,108 +185,93 @@ public class RepoCommandTest extends RepositoryTestCase {
} }
} }
private Repository cloneRepository(Repository repo, boolean bare)
throws Exception {
Repository r = Git.cloneRepository()
.setURI(repo.getDirectory().toURI().toString())
.setDirectory(createUniqueTestGitDir(true)).setBare(bare).call()
.getRepository();
if (bare) {
assertTrue(r.isBare());
} else {
assertFalse(r.isBare());
}
return r;
}
@Test @Test
public void runTwiceIsNOP() throws Exception { public void runTwiceIsNOP() throws Exception {
Repository child = Git.cloneRepository() try (Repository child = cloneRepository(groupADb, true);
.setURI(groupADb.getDirectory().toURI().toString()) Repository dest = cloneRepository(db, true)) {
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call() StringBuilder xmlContent = new StringBuilder();
.getRepository(); xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>")
Repository dest = Git.cloneRepository() .append("<remote name=\"remote1\" fetch=\"..\" />")
.setURI(db.getDirectory().toURI().toString()) .append("<default revision=\"master\" remote=\"remote1\" />")
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call() .append("<project path=\"base\" name=\"platform/base\" />")
.getRepository(); .append("</manifest>");
RepoCommand cmd = new RepoCommand(dest);
assertTrue(dest.isBare()); IndexedRepos repos = new IndexedRepos();
assertTrue(child.isBare()); repos.put("platform/base", child);
StringBuilder xmlContent = new StringBuilder(); RevCommit commit = cmd
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") .setInputStream(new ByteArrayInputStream(
.append("<manifest>") xmlContent.toString().getBytes(CHARSET)))
.append("<remote name=\"remote1\" fetch=\"..\" />") .setRemoteReader(repos).setURI("platform/")
.append("<default revision=\"master\" remote=\"remote1\" />") .setTargetURI("platform/superproject")
.append("<project path=\"base\" name=\"platform/base\" />") .setRecordRemoteBranch(true).setRecordSubmoduleLabels(true)
.append("</manifest>"); .call();
RepoCommand cmd = new RepoCommand(dest);
IndexedRepos repos = new IndexedRepos();
repos.put("platform/base", child);
RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream(
xmlContent.toString().getBytes(CHARSET)))
.setRemoteReader(repos)
.setURI("platform/")
.setTargetURI("platform/superproject")
.setRecordRemoteBranch(true)
.setRecordSubmoduleLabels(true)
.call();
String firstIdStr = commit.getId().name() + ":" + ".gitmodules"; String firstIdStr = commit.getId().name() + ":" + ".gitmodules";
commit = new RepoCommand(dest) commit = new RepoCommand(dest)
.setInputStream(new ByteArrayInputStream( .setInputStream(new ByteArrayInputStream(
xmlContent.toString().getBytes(CHARSET))) xmlContent.toString().getBytes(CHARSET)))
.setRemoteReader(repos) .setRemoteReader(repos).setURI("platform/")
.setURI("platform/") .setTargetURI("platform/superproject")
.setTargetURI("platform/superproject") .setRecordRemoteBranch(true).setRecordSubmoduleLabels(true)
.setRecordRemoteBranch(true) .call();
.setRecordSubmoduleLabels(true) String idStr = commit.getId().name() + ":" + ".gitmodules";
.call(); assertEquals(firstIdStr, idStr);
String idStr = commit.getId().name() + ":" + ".gitmodules"; }
assertEquals(firstIdStr, idStr);
child.close();
dest.close();
} }
@Test @Test
public void androidSetup() throws Exception { public void androidSetup() throws Exception {
Repository child = Git.cloneRepository() try (Repository child = cloneRepository(groupADb, true);
.setURI(groupADb.getDirectory().toURI().toString()) Repository dest = cloneRepository(db, true)) {
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call() StringBuilder xmlContent = new StringBuilder();
.getRepository(); xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>")
Repository dest = Git.cloneRepository() .append("<remote name=\"remote1\" fetch=\"..\" />")
.setURI(db.getDirectory().toURI().toString()) .append("<default revision=\"master\" remote=\"remote1\" />")
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call() .append("<project path=\"base\" name=\"platform/base\" />")
.getRepository(); .append("</manifest>");
RepoCommand cmd = new RepoCommand(dest);
assertTrue(dest.isBare()); IndexedRepos repos = new IndexedRepos();
assertTrue(child.isBare()); repos.put("platform/base", child);
StringBuilder xmlContent = new StringBuilder(); RevCommit commit = cmd
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") .setInputStream(new ByteArrayInputStream(
.append("<manifest>") xmlContent.toString().getBytes(CHARSET)))
.append("<remote name=\"remote1\" fetch=\"..\" />") .setRemoteReader(repos).setURI("platform/")
.append("<default revision=\"master\" remote=\"remote1\" />") .setTargetURI("platform/superproject")
.append("<project path=\"base\" name=\"platform/base\" />") .setRecordRemoteBranch(true).setRecordSubmoduleLabels(true)
.append("</manifest>"); .call();
RepoCommand cmd = new RepoCommand(dest);
IndexedRepos repos = new IndexedRepos();
repos.put("platform/base", child);
RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(CHARSET)))
.setRemoteReader(repos)
.setURI("platform/")
.setTargetURI("platform/superproject")
.setRecordRemoteBranch(true)
.setRecordSubmoduleLabels(true)
.call();
String idStr = commit.getId().name() + ":" + ".gitmodules"; String idStr = commit.getId().name() + ":" + ".gitmodules";
ObjectId modId = dest.resolve(idStr); ObjectId modId = dest.resolve(idStr);
try (ObjectReader reader = dest.newObjectReader()) { try (ObjectReader reader = dest.newObjectReader()) {
byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE); byte[] bytes = reader.open(modId)
Config base = new Config(); .getCachedBytes(Integer.MAX_VALUE);
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); Config base = new Config();
String subUrl = cfg.getString("submodule", "base", "url"); BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
assertEquals(subUrl, "../base"); String subUrl = cfg.getString("submodule", "base", "url");
assertEquals(subUrl, "../base");
}
} }
child.close();
dest.close();
} }
@Test @Test
@ -299,200 +284,174 @@ public class RepoCommandTest extends RepositoryTestCase {
.append("<project path=\"base\" name=\"platform/base\" />") .append("<project path=\"base\" name=\"platform/base\" />")
.append("</manifest>"); .append("</manifest>");
Repository dest = Git.cloneRepository() try (Repository dest = cloneRepository(db, true)) {
.setURI(db.getDirectory().toURI().toString()) RevCommit commit = new RepoCommand(dest)
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call() .setInputStream(new ByteArrayInputStream(
.getRepository(); xmlContent.toString().getBytes(CHARSET)))
.setRemoteReader(new IndexedRepos()).setURI("platform/")
assertTrue(dest.isBare()); .setTargetURI("platform/superproject")
.setRecordRemoteBranch(true).setIgnoreRemoteFailures(true)
RevCommit commit = new RepoCommand(dest) .setRecordSubmoduleLabels(true).call();
.setInputStream(new ByteArrayInputStream(
xmlContent.toString().getBytes(CHARSET))) String idStr = commit.getId().name() + ":" + ".gitmodules";
.setRemoteReader(new IndexedRepos()) ObjectId modId = dest.resolve(idStr);
.setURI("platform/")
.setTargetURI("platform/superproject") try (ObjectReader reader = dest.newObjectReader()) {
.setRecordRemoteBranch(true) byte[] bytes = reader.open(modId)
.setIgnoreRemoteFailures(true) .getCachedBytes(Integer.MAX_VALUE);
.setRecordSubmoduleLabels(true) Config base = new Config();
.call(); BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "base", "url");
String idStr = commit.getId().name() + ":" + ".gitmodules"; assertEquals(subUrl, "https://host.com/platform/base");
ObjectId modId = dest.resolve(idStr); }
try (ObjectReader reader = dest.newObjectReader()) {
byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE);
Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "base", "url");
assertEquals(subUrl, "https://host.com/platform/base");
} }
dest.close();
} }
@Test @Test
public void gerritSetup() throws Exception { public void gerritSetup() throws Exception {
Repository child = try (Repository child = cloneRepository(groupADb, true);
Git.cloneRepository().setURI(groupADb.getDirectory().toURI().toString()) Repository dest = cloneRepository(db, true)) {
.setDirectory(createUniqueTestGitDir(true)) StringBuilder xmlContent = new StringBuilder();
.setBare(true).call().getRepository(); xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>")
Repository dest = Git.cloneRepository() .append("<remote name=\"remote1\" fetch=\".\" />")
.setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true)) .append("<default revision=\"master\" remote=\"remote1\" />")
.setBare(true).call().getRepository(); .append("<project path=\"plugins/cookbook\" name=\"plugins/cookbook\" />")
.append("</manifest>");
RepoCommand cmd = new RepoCommand(dest);
assertTrue(dest.isBare()); IndexedRepos repos = new IndexedRepos();
assertTrue(child.isBare()); repos.put("plugins/cookbook", child);
StringBuilder xmlContent = new StringBuilder(); RevCommit commit = cmd
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") .setInputStream(new ByteArrayInputStream(
.append("<manifest>") xmlContent.toString().getBytes(CHARSET)))
.append("<remote name=\"remote1\" fetch=\".\" />") .setRemoteReader(repos).setURI("").setTargetURI("gerrit")
.append("<default revision=\"master\" remote=\"remote1\" />") .setRecordRemoteBranch(true).setRecordSubmoduleLabels(true)
.append("<project path=\"plugins/cookbook\" name=\"plugins/cookbook\" />") .call();
.append("</manifest>");
RepoCommand cmd = new RepoCommand(dest);
IndexedRepos repos = new IndexedRepos();
repos.put("plugins/cookbook", child);
RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(CHARSET)))
.setRemoteReader(repos)
.setURI("")
.setTargetURI("gerrit")
.setRecordRemoteBranch(true)
.setRecordSubmoduleLabels(true)
.call();
String idStr = commit.getId().name() + ":" + ".gitmodules";
ObjectId modId = dest.resolve(idStr);
try (ObjectReader reader = dest.newObjectReader()) { String idStr = commit.getId().name() + ":" + ".gitmodules";
byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE); ObjectId modId = dest.resolve(idStr);
Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); try (ObjectReader reader = dest.newObjectReader()) {
String subUrl = cfg.getString("submodule", "plugins/cookbook", "url"); byte[] bytes = reader.open(modId)
assertEquals(subUrl, "../plugins/cookbook"); .getCachedBytes(Integer.MAX_VALUE);
Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "plugins/cookbook",
"url");
assertEquals(subUrl, "../plugins/cookbook");
}
} }
child.close();
dest.close();
} }
@Test @Test
public void absoluteRemoteURL() throws Exception { public void absoluteRemoteURL() throws Exception {
Repository child = try (Repository child = cloneRepository(groupADb, true);
Git.cloneRepository().setURI(groupADb.getDirectory().toURI().toString()) Repository dest = cloneRepository(db, true)) {
.setDirectory(createUniqueTestGitDir(true)) String abs = "https://chromium.googlesource.com";
.setBare(true).call().getRepository(); String repoUrl = "https://chromium.googlesource.com/chromium/src";
Repository dest = Git.cloneRepository() boolean fetchSlash = false;
.setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true)) boolean baseSlash = false;
.setBare(true).call().getRepository();
String abs = "https://chromium.googlesource.com";
String repoUrl = "https://chromium.googlesource.com/chromium/src";
boolean fetchSlash = false;
boolean baseSlash = false;
do {
do { do {
String fetchUrl = fetchSlash ? abs + "/" : abs; do {
String baseUrl = baseSlash ? abs + "/" : abs; String fetchUrl = fetchSlash ? abs + "/" : abs;
String baseUrl = baseSlash ? abs + "/" : abs;
StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") StringBuilder xmlContent = new StringBuilder();
.append("<manifest>") xmlContent.append(
.append("<remote name=\"origin\" fetch=\"" + fetchUrl + "\" />") "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<default revision=\"master\" remote=\"origin\" />") .append("<manifest>")
.append("<project path=\"src\" name=\"chromium/src\" />") .append("<remote name=\"origin\" fetch=\""
.append("</manifest>"); + fetchUrl + "\" />")
RepoCommand cmd = new RepoCommand(dest); .append("<default revision=\"master\" remote=\"origin\" />")
.append("<project path=\"src\" name=\"chromium/src\" />")
IndexedRepos repos = new IndexedRepos(); .append("</manifest>");
repos.put(repoUrl, child); RepoCommand cmd = new RepoCommand(dest);
RevCommit commit = cmd IndexedRepos repos = new IndexedRepos();
.setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(CHARSET))) repos.put(repoUrl, child);
.setRemoteReader(repos)
.setURI(baseUrl) RevCommit commit = cmd
.setTargetURI("gerrit") .setInputStream(new ByteArrayInputStream(
.setRecordRemoteBranch(true) xmlContent.toString().getBytes(CHARSET)))
.setRecordSubmoduleLabels(true) .setRemoteReader(repos).setURI(baseUrl)
.call(); .setTargetURI("gerrit").setRecordRemoteBranch(true)
.setRecordSubmoduleLabels(true).call();
String idStr = commit.getId().name() + ":" + ".gitmodules";
ObjectId modId = dest.resolve(idStr); String idStr = commit.getId().name() + ":" + ".gitmodules";
ObjectId modId = dest.resolve(idStr);
try (ObjectReader reader = dest.newObjectReader()) {
byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE); try (ObjectReader reader = dest.newObjectReader()) {
Config base = new Config(); byte[] bytes = reader.open(modId)
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); .getCachedBytes(Integer.MAX_VALUE);
String subUrl = cfg.getString("submodule", "src", "url"); Config base = new Config();
assertEquals("https://chromium.googlesource.com/chromium/src", subUrl); BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
} String subUrl = cfg.getString("submodule", "src",
fetchSlash = !fetchSlash; "url");
} while (fetchSlash); assertEquals(
baseSlash = !baseSlash; "https://chromium.googlesource.com/chromium/src",
} while (baseSlash); subUrl);
child.close(); }
dest.close(); fetchSlash = !fetchSlash;
} while (fetchSlash);
baseSlash = !baseSlash;
} while (baseSlash);
}
} }
@Test @Test
public void absoluteRemoteURLAbsoluteTargetURL() throws Exception { public void absoluteRemoteURLAbsoluteTargetURL() throws Exception {
Repository child = try (Repository child = cloneRepository(groupADb, true);
Git.cloneRepository().setURI(groupADb.getDirectory().toURI().toString()) Repository dest = cloneRepository(db, true)) {
.setDirectory(createUniqueTestGitDir(true)) String abs = "https://chromium.googlesource.com";
.setBare(true).call().getRepository(); String repoUrl = "https://chromium.googlesource.com/chromium/src";
Repository dest = Git.cloneRepository() boolean fetchSlash = false;
.setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true)) boolean baseSlash = false;
.setBare(true).call().getRepository();
String abs = "https://chromium.googlesource.com";
String repoUrl = "https://chromium.googlesource.com/chromium/src";
boolean fetchSlash = false;
boolean baseSlash = false;
do {
do { do {
String fetchUrl = fetchSlash ? abs + "/" : abs; do {
String baseUrl = baseSlash ? abs + "/" : abs; String fetchUrl = fetchSlash ? abs + "/" : abs;
String baseUrl = baseSlash ? abs + "/" : abs;
StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") StringBuilder xmlContent = new StringBuilder();
.append("<manifest>") xmlContent.append(
.append("<remote name=\"origin\" fetch=\"" + fetchUrl + "\" />") "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<default revision=\"master\" remote=\"origin\" />") .append("<manifest>")
.append("<project path=\"src\" name=\"chromium/src\" />") .append("<remote name=\"origin\" fetch=\""
.append("</manifest>"); + fetchUrl + "\" />")
RepoCommand cmd = new RepoCommand(dest); .append("<default revision=\"master\" remote=\"origin\" />")
.append("<project path=\"src\" name=\"chromium/src\" />")
IndexedRepos repos = new IndexedRepos(); .append("</manifest>");
repos.put(repoUrl, child); RepoCommand cmd = new RepoCommand(dest);
RevCommit commit = cmd IndexedRepos repos = new IndexedRepos();
.setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(CHARSET))) repos.put(repoUrl, child);
.setRemoteReader(repos)
.setURI(baseUrl) RevCommit commit = cmd
.setTargetURI(abs + "/superproject") .setInputStream(new ByteArrayInputStream(
.setRecordRemoteBranch(true) xmlContent.toString().getBytes(CHARSET)))
.setRecordSubmoduleLabels(true) .setRemoteReader(repos).setURI(baseUrl)
.call(); .setTargetURI(abs + "/superproject")
.setRecordRemoteBranch(true)
String idStr = commit.getId().name() + ":" + ".gitmodules"; .setRecordSubmoduleLabels(true).call();
ObjectId modId = dest.resolve(idStr);
String idStr = commit.getId().name() + ":" + ".gitmodules";
try (ObjectReader reader = dest.newObjectReader()) { ObjectId modId = dest.resolve(idStr);
byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE);
Config base = new Config(); try (ObjectReader reader = dest.newObjectReader()) {
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); byte[] bytes = reader.open(modId)
String subUrl = cfg.getString("submodule", "src", "url"); .getCachedBytes(Integer.MAX_VALUE);
assertEquals("../chromium/src", subUrl); Config base = new Config();
} BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
fetchSlash = !fetchSlash; String subUrl = cfg.getString("submodule", "src",
} while (fetchSlash); "url");
baseSlash = !baseSlash; assertEquals("../chromium/src", subUrl);
} while (baseSlash); }
child.close(); fetchSlash = !fetchSlash;
dest.close(); } while (fetchSlash);
baseSlash = !baseSlash;
} while (baseSlash);
}
} }
@Test @Test
@ -513,11 +472,12 @@ public class RepoCommandTest extends RepositoryTestCase {
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
assertTrue("submodule should be checked out", hello.exists()); assertTrue("submodule should be checked out", hello.exists());
BufferedReader reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
String content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"master world", content); "master world", content);
}
} }
@Test @Test
@ -603,19 +563,21 @@ public class RepoCommandTest extends RepositoryTestCase {
// The original file should exist // The original file should exist
File hello = new File(localDb.getWorkTree(), "foo/hello.txt"); File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
assertTrue("The original file should exist", hello.exists()); assertTrue("The original file should exist", hello.exists());
BufferedReader reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
String content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("The original file should have expected content", assertEquals("The original file should have expected content",
"master world", content); "master world", content);
}
// The dest file should also exist // The dest file should also exist
hello = new File(localDb.getWorkTree(), "Hello"); hello = new File(localDb.getWorkTree(), "Hello");
assertTrue("The destination file should exist", hello.exists()); assertTrue("The destination file should exist", hello.exists());
reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("The destination file should have expected content", assertEquals("The destination file should have expected content",
"master world", content); "master world", content);
}
} }
@Test @Test
@ -638,24 +600,27 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri).call(); .setURI(rootUri).call();
// Clone it // Clone it
File directory = createTempDirectory("testBareRepo"); File directory = createTempDirectory("testBareRepo");
Repository localDb = Git.cloneRepository().setDirectory(directory) try (Repository localDb = Git.cloneRepository().setDirectory(directory)
.setURI(remoteDb.getDirectory().toURI().toString()).call() .setURI(remoteDb.getDirectory().toURI().toString()).call()
.getRepository(); .getRepository()) {
// The .gitmodules file should exist // The .gitmodules file should exist
File gitmodules = new File(localDb.getWorkTree(), ".gitmodules"); File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
assertTrue("The .gitmodules file should exist", gitmodules.exists()); assertTrue("The .gitmodules file should exist",
// The first line of .gitmodules file should be expected gitmodules.exists());
BufferedReader reader = new BufferedReader(new FileReader(gitmodules)); // The first line of .gitmodules file should be expected
String content = reader.readLine(); try (BufferedReader reader = new BufferedReader(
reader.close(); new FileReader(gitmodules))) {
assertEquals("The first line of .gitmodules file should be as expected", String content = reader.readLine();
"[submodule \"foo\"]", content); assertEquals(
// The gitlink should be the same as remote head sha1 "The first line of .gitmodules file should be as expected",
String gitlink = localDb.resolve(Constants.HEAD + ":foo").name(); "[submodule \"foo\"]", content);
localDb.close(); }
String remote = defaultDb.resolve(Constants.HEAD).name(); // The gitlink should be the same as remote head sha1
assertEquals("The gitlink should be the same as remote head", remote, String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
gitlink); String remote = defaultDb.resolve(Constants.HEAD).name();
assertEquals("The gitlink should be the same as remote head",
remote, gitlink);
}
} }
@Test @Test
@ -677,11 +642,12 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
BufferedReader reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
String content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
}
} }
@Test @Test
@ -703,11 +669,12 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
BufferedReader reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
String content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
}
} }
@Test @Test
@ -729,11 +696,12 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
BufferedReader reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
String content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
}
} }
@Test @Test
@ -757,14 +725,14 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri).call(); .setURI(rootUri).call();
// Clone it // Clone it
File directory = createTempDirectory("testRevisionBare"); File directory = createTempDirectory("testRevisionBare");
Repository localDb = Git.cloneRepository().setDirectory(directory) try (Repository localDb = Git.cloneRepository().setDirectory(directory)
.setURI(remoteDb.getDirectory().toURI().toString()).call() .setURI(remoteDb.getDirectory().toURI().toString()).call()
.getRepository(); .getRepository()) {
// The gitlink should be the same as oldCommitId // The gitlink should be the same as oldCommitId
String gitlink = localDb.resolve(Constants.HEAD + ":foo").name(); String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
localDb.close(); assertEquals("The gitlink is same as remote head",
assertEquals("The gitlink is same as remote head", oldCommitId.name(), oldCommitId.name(), gitlink);
gitlink); }
} }
@Test @Test
@ -790,22 +758,24 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri).call(); .setURI(rootUri).call();
// Clone it // Clone it
File directory = createTempDirectory("testCopyFileBare"); File directory = createTempDirectory("testCopyFileBare");
Repository localDb = Git.cloneRepository().setDirectory(directory) try (Repository localDb = Git.cloneRepository().setDirectory(directory)
.setURI(remoteDb.getDirectory().toURI().toString()).call() .setURI(remoteDb.getDirectory().toURI().toString()).call()
.getRepository(); .getRepository()) {
// The Hello file should exist // The Hello file should exist
File hello = new File(localDb.getWorkTree(), "Hello"); File hello = new File(localDb.getWorkTree(), "Hello");
assertTrue("The Hello file should exist", hello.exists()); assertTrue("The Hello file should exist", hello.exists());
// The foo/Hello file should be skipped. // The foo/Hello file should be skipped.
File foohello = new File(localDb.getWorkTree(), "foo/Hello"); File foohello = new File(localDb.getWorkTree(), "foo/Hello");
assertFalse("The foo/Hello file should be skipped", foohello.exists()); assertFalse("The foo/Hello file should be skipped",
localDb.close(); foohello.exists());
// The content of Hello file should be expected // The content of Hello file should be expected
BufferedReader reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
String content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("The Hello file should have expected content", assertEquals("The Hello file should have expected content",
"branch world", content); "branch world", content);
}
}
} }
@Test @Test
@ -841,36 +811,38 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri).call(); .setURI(rootUri).call();
// Clone it // Clone it
File directory = createTempDirectory("testReplaceManifestBare"); File directory = createTempDirectory("testReplaceManifestBare");
Repository localDb = Git.cloneRepository().setDirectory(directory) File dotmodules;
try (Repository localDb = Git.cloneRepository().setDirectory(directory)
.setURI(remoteDb.getDirectory().toURI().toString()).call() .setURI(remoteDb.getDirectory().toURI().toString()).call()
.getRepository(); .getRepository()) {
// The Hello file should not exist // The Hello file should not exist
File hello = new File(localDb.getWorkTree(), "Hello"); File hello = new File(localDb.getWorkTree(), "Hello");
assertFalse("The Hello file shouldn't exist", hello.exists()); assertFalse("The Hello file shouldn't exist", hello.exists());
// The Hello.txt file should exist // The Hello.txt file should exist
File hellotxt = new File(localDb.getWorkTree(), "Hello.txt"); File hellotxt = new File(localDb.getWorkTree(), "Hello.txt");
assertTrue("The Hello.txt file should exist", hellotxt.exists()); assertTrue("The Hello.txt file should exist", hellotxt.exists());
dotmodules = new File(localDb.getWorkTree(),
Constants.DOT_GIT_MODULES);
}
// The .gitmodules file should have 'submodule "bar"' and shouldn't // The .gitmodules file should have 'submodule "bar"' and shouldn't
// have // have
// 'submodule "foo"' lines. // 'submodule "foo"' lines.
File dotmodules = new File(localDb.getWorkTree(), try (BufferedReader reader = new BufferedReader(
Constants.DOT_GIT_MODULES); new FileReader(dotmodules))) {
localDb.close(); boolean foo = false;
BufferedReader reader = new BufferedReader(new FileReader(dotmodules)); boolean bar = false;
boolean foo = false; while (true) {
boolean bar = false; String line = reader.readLine();
while (true) { if (line == null)
String line = reader.readLine(); break;
if (line == null) if (line.contains("submodule \"foo\""))
break; foo = true;
if (line.contains("submodule \"foo\"")) if (line.contains("submodule \"bar\""))
foo = true; bar = true;
if (line.contains("submodule \"bar\"")) }
bar = true; assertTrue("The bar submodule should exist", bar);
assertFalse("The foo submodule shouldn't exist", foo);
} }
reader.close();
assertTrue("The bar submodule should exist", bar);
assertFalse("The foo submodule shouldn't exist", foo);
} }
@Test @Test
@ -896,34 +868,37 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri).call(); .setURI(rootUri).call();
// Clone it // Clone it
File directory = createTempDirectory("testRemoveOverlappingBare"); File directory = createTempDirectory("testRemoveOverlappingBare");
Repository localDb = Git.cloneRepository().setDirectory(directory) File dotmodules;
try (Repository localDb = Git.cloneRepository().setDirectory(directory)
.setURI(remoteDb.getDirectory().toURI().toString()).call() .setURI(remoteDb.getDirectory().toURI().toString()).call()
.getRepository(); .getRepository()) {
dotmodules = new File(localDb.getWorkTree(),
Constants.DOT_GIT_MODULES);
}
// The .gitmodules file should have 'submodule "foo"' and shouldn't // The .gitmodules file should have 'submodule "foo"' and shouldn't
// have // have
// 'submodule "foo/bar"' lines. // 'submodule "foo/bar"' lines.
File dotmodules = new File(localDb.getWorkTree(), try (BufferedReader reader = new BufferedReader(
Constants.DOT_GIT_MODULES); new FileReader(dotmodules))) {
localDb.close(); boolean foo = false;
BufferedReader reader = new BufferedReader(new FileReader(dotmodules)); boolean foobar = false;
boolean foo = false; boolean a = false;
boolean foobar = false; while (true) {
boolean a = false; String line = reader.readLine();
while (true) { if (line == null)
String line = reader.readLine(); break;
if (line == null) if (line.contains("submodule \"foo\""))
break; foo = true;
if (line.contains("submodule \"foo\"")) if (line.contains("submodule \"foo/bar\""))
foo = true; foobar = true;
if (line.contains("submodule \"foo/bar\"")) if (line.contains("submodule \"a\""))
foobar = true; a = true;
if (line.contains("submodule \"a\"")) }
a = true; assertTrue("The foo submodule should exist", foo);
assertFalse("The foo/bar submodule shouldn't exist", foobar);
assertTrue("The a submodule should exist", a);
} }
reader.close();
assertTrue("The foo submodule should exist", foo);
assertFalse("The foo/bar submodule shouldn't exist", foobar);
assertTrue("The a submodule should exist", a);
} }
@Test @Test
@ -959,11 +934,12 @@ public class RepoCommandTest extends RepositoryTestCase {
.call(); .call();
File hello = new File(localDb.getWorkTree(), "foo/hello.txt"); File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
assertTrue("submodule should be checked out", hello.exists()); assertTrue("submodule should be checked out", hello.exists());
BufferedReader reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
String content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"master world", content); "master world", content);
}
} }
@Test @Test
public void testRemoteAlias() throws Exception { public void testRemoteAlias() throws Exception {
@ -1165,11 +1141,12 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
BufferedReader reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
String content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
}
} }
@Test @Test
@ -1191,11 +1168,12 @@ public class RepoCommandTest extends RepositoryTestCase {
.setURI(rootUri) .setURI(rootUri)
.call(); .call();
File hello = new File(db.getWorkTree(), "foo/hello.txt"); File hello = new File(db.getWorkTree(), "foo/hello.txt");
BufferedReader reader = new BufferedReader(new FileReader(hello)); try (BufferedReader reader = new BufferedReader(
String content = reader.readLine(); new FileReader(hello))) {
reader.close(); String content = reader.readLine();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
}
} }
private void resolveRelativeUris() { private void resolveRelativeUris() {

Loading…
Cancel
Save