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. 330
      org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java

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

@ -185,21 +185,24 @@ public class RepoCommandTest extends RepositoryTestCase {
} }
} }
@Test private Repository cloneRepository(Repository repo, boolean bare)
public void runTwiceIsNOP() throws Exception { throws Exception {
Repository child = Git.cloneRepository() Repository r = Git.cloneRepository()
.setURI(groupADb.getDirectory().toURI().toString()) .setURI(repo.getDirectory().toURI().toString())
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call() .setDirectory(createUniqueTestGitDir(true)).setBare(bare).call()
.getRepository();
Repository dest = Git.cloneRepository()
.setURI(db.getDirectory().toURI().toString())
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
.getRepository(); .getRepository();
if (bare) {
assertTrue(r.isBare());
} else {
assertFalse(r.isBare());
}
return r;
}
assertTrue(dest.isBare()); @Test
assertTrue(child.isBare()); public void runTwiceIsNOP() throws Exception {
try (Repository child = cloneRepository(groupADb, true);
Repository dest = cloneRepository(db, true)) {
StringBuilder xmlContent = new StringBuilder(); StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>") .append("<manifest>")
@ -215,44 +218,28 @@ public class RepoCommandTest extends RepositoryTestCase {
RevCommit commit = cmd RevCommit commit = cmd
.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) .setRecordRemoteBranch(true).setRecordSubmoduleLabels(true)
.setRecordSubmoduleLabels(true)
.call(); .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) .setRecordRemoteBranch(true).setRecordSubmoduleLabels(true)
.setRecordSubmoduleLabels(true)
.call(); .call();
String idStr = commit.getId().name() + ":" + ".gitmodules"; String idStr = commit.getId().name() + ":" + ".gitmodules";
assertEquals(firstIdStr, idStr); 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()
.getRepository();
Repository dest = Git.cloneRepository()
.setURI(db.getDirectory().toURI().toString())
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
.getRepository();
assertTrue(dest.isBare());
assertTrue(child.isBare());
StringBuilder xmlContent = new StringBuilder(); StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>") .append("<manifest>")
@ -266,27 +253,25 @@ public class RepoCommandTest extends RepositoryTestCase {
repos.put("platform/base", child); repos.put("platform/base", child);
RevCommit commit = cmd RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(CHARSET))) .setInputStream(new ByteArrayInputStream(
.setRemoteReader(repos) xmlContent.toString().getBytes(CHARSET)))
.setURI("platform/") .setRemoteReader(repos).setURI("platform/")
.setTargetURI("platform/superproject") .setTargetURI("platform/superproject")
.setRecordRemoteBranch(true) .setRecordRemoteBranch(true).setRecordSubmoduleLabels(true)
.setRecordSubmoduleLabels(true)
.call(); .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)
.getCachedBytes(Integer.MAX_VALUE);
Config base = new Config(); Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "base", "url"); String subUrl = cfg.getString("submodule", "base", "url");
assertEquals(subUrl, "../base"); assertEquals(subUrl, "../base");
} }
}
child.close();
dest.close();
} }
@Test @Test
@ -299,52 +284,33 @@ 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())
.setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
.getRepository();
assertTrue(dest.isBare());
RevCommit commit = new RepoCommand(dest) RevCommit commit = new RepoCommand(dest)
.setInputStream(new ByteArrayInputStream( .setInputStream(new ByteArrayInputStream(
xmlContent.toString().getBytes(CHARSET))) xmlContent.toString().getBytes(CHARSET)))
.setRemoteReader(new IndexedRepos()) .setRemoteReader(new IndexedRepos()).setURI("platform/")
.setURI("platform/")
.setTargetURI("platform/superproject") .setTargetURI("platform/superproject")
.setRecordRemoteBranch(true) .setRecordRemoteBranch(true).setIgnoreRemoteFailures(true)
.setIgnoreRemoteFailures(true) .setRecordSubmoduleLabels(true).call();
.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)
.getCachedBytes(Integer.MAX_VALUE);
Config base = new Config(); Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "base", "url"); String subUrl = cfg.getString("submodule", "base", "url");
assertEquals(subUrl, "https://host.com/platform/base"); 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))
.setBare(true).call().getRepository();
Repository dest = Git.cloneRepository()
.setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true))
.setBare(true).call().getRepository();
assertTrue(dest.isBare());
assertTrue(child.isBare());
StringBuilder xmlContent = new StringBuilder(); StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>") .append("<manifest>")
@ -358,38 +324,31 @@ public class RepoCommandTest extends RepositoryTestCase {
repos.put("plugins/cookbook", child); repos.put("plugins/cookbook", child);
RevCommit commit = cmd RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(CHARSET))) .setInputStream(new ByteArrayInputStream(
.setRemoteReader(repos) xmlContent.toString().getBytes(CHARSET)))
.setURI("") .setRemoteReader(repos).setURI("").setTargetURI("gerrit")
.setTargetURI("gerrit") .setRecordRemoteBranch(true).setRecordSubmoduleLabels(true)
.setRecordRemoteBranch(true)
.setRecordSubmoduleLabels(true)
.call(); .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)
.getCachedBytes(Integer.MAX_VALUE);
Config base = new Config(); Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "plugins/cookbook", "url"); String subUrl = cfg.getString("submodule", "plugins/cookbook",
"url");
assertEquals(subUrl, "../plugins/cookbook"); 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))
.setBare(true).call().getRepository();
Repository dest = Git.cloneRepository()
.setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true))
.setBare(true).call().getRepository();
String abs = "https://chromium.googlesource.com"; String abs = "https://chromium.googlesource.com";
String repoUrl = "https://chromium.googlesource.com/chromium/src"; String repoUrl = "https://chromium.googlesource.com/chromium/src";
boolean fetchSlash = false; boolean fetchSlash = false;
@ -400,9 +359,11 @@ public class RepoCommandTest extends RepositoryTestCase {
String baseUrl = baseSlash ? abs + "/" : abs; String baseUrl = baseSlash ? abs + "/" : abs;
StringBuilder xmlContent = new StringBuilder(); StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") xmlContent.append(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>") .append("<manifest>")
.append("<remote name=\"origin\" fetch=\"" + fetchUrl + "\" />") .append("<remote name=\"origin\" fetch=\""
+ fetchUrl + "\" />")
.append("<default revision=\"master\" remote=\"origin\" />") .append("<default revision=\"master\" remote=\"origin\" />")
.append("<project path=\"src\" name=\"chromium/src\" />") .append("<project path=\"src\" name=\"chromium/src\" />")
.append("</manifest>"); .append("</manifest>");
@ -412,41 +373,37 @@ public class RepoCommandTest extends RepositoryTestCase {
repos.put(repoUrl, child); repos.put(repoUrl, child);
RevCommit commit = cmd RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(CHARSET))) .setInputStream(new ByteArrayInputStream(
.setRemoteReader(repos) xmlContent.toString().getBytes(CHARSET)))
.setURI(baseUrl) .setRemoteReader(repos).setURI(baseUrl)
.setTargetURI("gerrit") .setTargetURI("gerrit").setRecordRemoteBranch(true)
.setRecordRemoteBranch(true) .setRecordSubmoduleLabels(true).call();
.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)
.getCachedBytes(Integer.MAX_VALUE);
Config base = new Config(); Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "src", "url"); String subUrl = cfg.getString("submodule", "src",
assertEquals("https://chromium.googlesource.com/chromium/src", subUrl); "url");
assertEquals(
"https://chromium.googlesource.com/chromium/src",
subUrl);
} }
fetchSlash = !fetchSlash; fetchSlash = !fetchSlash;
} while (fetchSlash); } while (fetchSlash);
baseSlash = !baseSlash; baseSlash = !baseSlash;
} while (baseSlash); } while (baseSlash);
child.close(); }
dest.close();
} }
@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))
.setBare(true).call().getRepository();
Repository dest = Git.cloneRepository()
.setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true))
.setBare(true).call().getRepository();
String abs = "https://chromium.googlesource.com"; String abs = "https://chromium.googlesource.com";
String repoUrl = "https://chromium.googlesource.com/chromium/src"; String repoUrl = "https://chromium.googlesource.com/chromium/src";
boolean fetchSlash = false; boolean fetchSlash = false;
@ -457,9 +414,11 @@ public class RepoCommandTest extends RepositoryTestCase {
String baseUrl = baseSlash ? abs + "/" : abs; String baseUrl = baseSlash ? abs + "/" : abs;
StringBuilder xmlContent = new StringBuilder(); StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") xmlContent.append(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>") .append("<manifest>")
.append("<remote name=\"origin\" fetch=\"" + fetchUrl + "\" />") .append("<remote name=\"origin\" fetch=\""
+ fetchUrl + "\" />")
.append("<default revision=\"master\" remote=\"origin\" />") .append("<default revision=\"master\" remote=\"origin\" />")
.append("<project path=\"src\" name=\"chromium/src\" />") .append("<project path=\"src\" name=\"chromium/src\" />")
.append("</manifest>"); .append("</manifest>");
@ -469,30 +428,30 @@ public class RepoCommandTest extends RepositoryTestCase {
repos.put(repoUrl, child); repos.put(repoUrl, child);
RevCommit commit = cmd RevCommit commit = cmd
.setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(CHARSET))) .setInputStream(new ByteArrayInputStream(
.setRemoteReader(repos) xmlContent.toString().getBytes(CHARSET)))
.setURI(baseUrl) .setRemoteReader(repos).setURI(baseUrl)
.setTargetURI(abs + "/superproject") .setTargetURI(abs + "/superproject")
.setRecordRemoteBranch(true) .setRecordRemoteBranch(true)
.setRecordSubmoduleLabels(true) .setRecordSubmoduleLabels(true).call();
.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)
.getCachedBytes(Integer.MAX_VALUE);
Config base = new Config(); Config base = new Config();
BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
String subUrl = cfg.getString("submodule", "src", "url"); String subUrl = cfg.getString("submodule", "src",
"url");
assertEquals("../chromium/src", subUrl); assertEquals("../chromium/src", subUrl);
} }
fetchSlash = !fetchSlash; fetchSlash = !fetchSlash;
} while (fetchSlash); } while (fetchSlash);
baseSlash = !baseSlash; baseSlash = !baseSlash;
} while (baseSlash); } while (baseSlash);
child.close(); }
dest.close();
} }
@Test @Test
@ -513,12 +472,13 @@ 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(
new FileReader(hello))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"master world", content); "master world", content);
} }
}
@Test @Test
public void testRepoManifestGroups() throws Exception { public void testRepoManifestGroups() throws Exception {
@ -603,20 +563,22 @@ 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(
new FileReader(hello))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close();
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
public void testBareRepo() throws Exception { public void testBareRepo() throws Exception {
@ -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",
gitmodules.exists());
// The first line of .gitmodules file should be expected // The first line of .gitmodules file should be expected
BufferedReader reader = new BufferedReader(new FileReader(gitmodules)); try (BufferedReader reader = new BufferedReader(
new FileReader(gitmodules))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close(); assertEquals(
assertEquals("The first line of .gitmodules file should be as expected", "The first line of .gitmodules file should be as expected",
"[submodule \"foo\"]", content); "[submodule \"foo\"]", content);
}
// The gitlink should be the same as remote head sha1 // The gitlink should be the same as remote head sha1
String gitlink = localDb.resolve(Constants.HEAD + ":foo").name(); String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
localDb.close();
String remote = defaultDb.resolve(Constants.HEAD).name(); String remote = defaultDb.resolve(Constants.HEAD).name();
assertEquals("The gitlink should be the same as remote head", remote, assertEquals("The gitlink should be the same as remote head",
gitlink); remote, gitlink);
}
} }
@Test @Test
@ -677,12 +642,13 @@ 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(
new FileReader(hello))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
} }
}
@Test @Test
public void testRevisionBranch() throws Exception { public void testRevisionBranch() throws Exception {
@ -703,12 +669,13 @@ 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(
new FileReader(hello))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
} }
}
@Test @Test
public void testRevisionTag() throws Exception { public void testRevisionTag() throws Exception {
@ -729,12 +696,13 @@ 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(
new FileReader(hello))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
} }
}
@Test @Test
public void testRevisionBare() throws Exception { public void testRevisionBare() throws Exception {
@ -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,23 +758,25 @@ 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(
new FileReader(hello))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close();
assertEquals("The Hello file should have expected content", assertEquals("The Hello file should have expected content",
"branch world", content); "branch world", content);
} }
}
}
@Test @Test
public void testReplaceManifestBare() throws Exception { public void testReplaceManifestBare() throws Exception {
@ -841,22 +811,24 @@ 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();
BufferedReader reader = new BufferedReader(new FileReader(dotmodules));
boolean foo = false; boolean foo = false;
boolean bar = false; boolean bar = false;
while (true) { while (true) {
@ -868,10 +840,10 @@ public class RepoCommandTest extends RepositoryTestCase {
if (line.contains("submodule \"bar\"")) if (line.contains("submodule \"bar\""))
bar = true; bar = true;
} }
reader.close();
assertTrue("The bar submodule should exist", bar); assertTrue("The bar submodule should exist", bar);
assertFalse("The foo submodule shouldn't exist", foo); assertFalse("The foo submodule shouldn't exist", foo);
} }
}
@Test @Test
public void testRemoveOverlappingBare() throws Exception { public void testRemoveOverlappingBare() throws Exception {
@ -896,16 +868,19 @@ 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();
BufferedReader reader = new BufferedReader(new FileReader(dotmodules));
boolean foo = false; boolean foo = false;
boolean foobar = false; boolean foobar = false;
boolean a = false; boolean a = false;
@ -920,11 +895,11 @@ public class RepoCommandTest extends RepositoryTestCase {
if (line.contains("submodule \"a\"")) if (line.contains("submodule \"a\""))
a = true; a = true;
} }
reader.close();
assertTrue("The foo submodule should exist", foo); assertTrue("The foo submodule should exist", foo);
assertFalse("The foo/bar submodule shouldn't exist", foobar); assertFalse("The foo/bar submodule shouldn't exist", foobar);
assertTrue("The a submodule should exist", a); assertTrue("The a submodule should exist", a);
} }
}
@Test @Test
public void testIncludeTag() throws Exception { public void testIncludeTag() throws Exception {
@ -959,12 +934,13 @@ 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(
new FileReader(hello))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close();
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 {
StringBuilder xmlContent = new StringBuilder(); StringBuilder xmlContent = new StringBuilder();
@ -1165,12 +1141,13 @@ 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(
new FileReader(hello))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close();
assertEquals("submodule content should be as expected", assertEquals("submodule content should be as expected",
"branch world", content); "branch world", content);
} }
}
@Test @Test
public void testDefaultRemoteRevision() throws Exception { public void testDefaultRemoteRevision() throws Exception {
@ -1191,12 +1168,13 @@ 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(
new FileReader(hello))) {
String content = reader.readLine(); String content = reader.readLine();
reader.close();
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() {
// Find the longest common prefix ends with "/" as rootUri. // Find the longest common prefix ends with "/" as rootUri.

Loading…
Cancel
Save