Skip to content

Commit fe7a26c

Browse files
authored
fix some code smells and modernize the code in MercurialRepositoryTest (#4848)
1 parent 058998b commit fe7a26c

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

opengrok-indexer/src/test/java/org/opengrok/indexer/history/MercurialRepositoryTest.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.io.IOException;
4444
import java.io.InputStream;
4545
import java.net.URISyntaxException;
46+
import java.net.URL;
4647
import java.nio.file.Files;
4748
import java.nio.file.Path;
4849
import java.nio.file.Paths;
@@ -149,8 +150,9 @@ void testGetHistory() throws Exception {
149150
@Test
150151
void testGetHistorySubdir() throws Exception {
151152
// Add a subdirectory with some history.
152-
runHgCommand(repositoryRoot, "import",
153-
Paths.get(getClass().getResource("/history/hg-export-subdir.txt").toURI()).toString());
153+
URL exportURL = getClass().getResource("/history/hg-export-subdir.txt");
154+
assertNotNull(exportURL);
155+
runHgCommand(repositoryRoot, "import", Paths.get(exportURL.toURI()).toString());
154156

155157
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
156158
History hist = mr.getHistory(new File(repositoryRoot, "subdir"));
@@ -206,8 +208,9 @@ public static void runHgCommand(File reposRoot, String... args) {
206208
@Test
207209
void testGetHistoryBranch() throws Exception {
208210
// Branch the repo and add one changeset.
209-
runHgCommand(repositoryRoot, "unbundle",
210-
Paths.get(getClass().getResource("/history/hg-branch.bundle").toURI()).toString());
211+
URL bundleURL = getClass().getResource("/history/hg-branch.bundle");
212+
assertNotNull(bundleURL);
213+
runHgCommand(repositoryRoot, "unbundle", Paths.get(bundleURL.toURI()).toString());
211214
// Switch to the branch.
212215
runHgCommand(repositoryRoot, "update", "mybranch");
213216

@@ -249,26 +252,28 @@ void testGetHistoryBranch() throws Exception {
249252
@Test
250253
void testGetHistoryGet() throws Exception {
251254
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
252-
String exp_str = "This will be a first novel of mine.\n"
253-
+ "\n"
254-
+ "Chapter 1.\n"
255-
+ "\n"
256-
+ "Let's write some words. It began like this:\n"
257-
+ "\n"
258-
+ "...\n";
255+
String exp_str = """
256+
This will be a first novel of mine.
257+
258+
Chapter 1.
259+
260+
Let's write some words. It began like this:
261+
262+
...
263+
""";
259264
byte[] buffer = new byte[1024];
260265

261266
InputStream input = mr.getHistoryGet(repositoryRoot.getCanonicalPath(),
262267
"novel.txt", REVISIONS[0]);
263268
assertNotNull(input);
264269

265-
String str = "";
270+
StringBuilder stringBuilder = new StringBuilder();
266271
int len;
267272
while ((len = input.read(buffer)) > 0) {
268-
str += new String(buffer, 0, len);
273+
stringBuilder.append(new String(buffer, 0, len));
269274
}
270-
assertNotSame(0, str.length());
271-
assertEquals(exp_str, str);
275+
assertNotSame(0, stringBuilder.length());
276+
assertEquals(exp_str, stringBuilder.toString());
272277
}
273278

274279
/**
@@ -403,8 +408,9 @@ void testGetLastHistoryEntry() throws Exception {
403408
@ValueSource(booleans = {true, false})
404409
void testMergeCommits(boolean isMergeCommitsEnabled) throws Exception {
405410
// The bundle will add a branch and merge commit in the default branch.
406-
runHgCommand(repositoryRoot, "unbundle",
407-
Paths.get(getClass().getResource("/history/hg-merge.bundle").toURI()).toString());
411+
URL mergeURL = getClass().getResource("/history/hg-merge.bundle");
412+
assertNotNull(mergeURL);
413+
runHgCommand(repositoryRoot, "unbundle", Paths.get(mergeURL.toURI()).toString());
408414
runHgCommand(repositoryRoot, "update");
409415

410416
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
@@ -463,7 +469,7 @@ void testAnnotationPositive(Triple<String, List<String>, List<String>> triple) t
463469
HistoryGuru.completeAnnotationWithHistory(annotation, history, hgRepo);
464470
List<HistoryEntry> relevantEntries = history.getHistoryEntries().stream().
465471
filter(e -> annotation.getRevisions().contains(e.getRevision())).
466-
collect(Collectors.toList());
472+
toList();
467473
assertFalse(relevantEntries.isEmpty());
468474
for (HistoryEntry entry : relevantEntries) {
469475
assertTrue(annotation.getRevisions().contains(entry.getRevision()));
@@ -485,7 +491,9 @@ void testBuildTagListEmpty() throws Exception {
485491
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
486492
assertNotNull(hgRepo);
487493
hgRepo.buildTagList(new File(hgRepo.getDirectoryName()), CommandTimeoutType.INDEXER);
488-
assertEquals(0, hgRepo.getTagList().size());
494+
Set<TagEntry> tags = hgRepo.getTagList();
495+
assertNotNull(tags);
496+
assertEquals(0, tags.size());
489497
IOUtils.removeRecursive(repositoryRootPath);
490498
}
491499

@@ -498,6 +506,7 @@ void testBuildTagListInitial() throws Exception {
498506
assertNotNull(hgRepo);
499507
hgRepo.buildTagList(new File(hgRepo.getDirectoryName()), CommandTimeoutType.INDEXER);
500508
var tags = hgRepo.getTagList();
509+
assertNotNull(tags);
501510
assertEquals(1, tags.size());
502511
Set<TagEntry> expectedTags = new TreeSet<>();
503512
TagEntry tagEntry = new MercurialTagEntry(7, "start_of_novel");
@@ -525,8 +534,9 @@ void testBuildTagListOneMore() throws Exception {
525534
runHgCommand(this.repositoryRoot, "clone", this.repositoryRoot.toString(), repositoryRootPath.toString());
526535

527536
// Branch the repo and add one changeset.
528-
runHgCommand(repositoryRoot, "unbundle",
529-
Paths.get(getClass().getResource("/history/hg-branch.bundle").toURI()).toString());
537+
URL bundleURL = getClass().getResource("/history/hg-branch.bundle");
538+
assertNotNull(bundleURL);
539+
runHgCommand(repositoryRoot, "unbundle", Paths.get(bundleURL.toURI()).toString());
530540

531541
// Switch to the branch and add tag.
532542
final String myBranch = "mybranch";

0 commit comments

Comments
 (0)