Skip to content

Commit

Permalink
Review Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ginoaugustine committed Feb 19, 2024
1 parent 6a9d16c commit 4d470e2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.opengrok.indexer.util;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.Objects;
Expand All @@ -47,26 +46,31 @@ public class ErrorMessageCollector implements Collector<String, StringJoiner, Op

private final String prefix;
private final String emptyString;
private final Boolean returnNullWhenEmpty;
private final boolean returnNullWhenEmpty;

/**
* Creates a collector with given prefix and
* returns optional empty for empty collection.
* @param prefix prefix before the joined string
*/
public ErrorMessageCollector(@NotNull String prefix) {
this(prefix, null);
Objects.requireNonNull(prefix);
this.prefix = prefix;
this.emptyString = "";
this.returnNullWhenEmpty = true;
}

/**
* Creates a string joiner with given prefix and empty string.
* @param prefix prefix before the joined string
* @param emptyString string to display if collection is empty, can be {@code null}
* @param emptyString string to display if collection is empty
*/
public ErrorMessageCollector(@NotNull String prefix, @Nullable String emptyString) {
public ErrorMessageCollector(@NotNull String prefix, @NotNull String emptyString) {
Objects.requireNonNull(prefix);
Objects.requireNonNull(emptyString);
this.prefix = prefix;
this.emptyString = Objects.isNull(emptyString) ? "" : emptyString;
this.returnNullWhenEmpty = Objects.isNull(emptyString);
this.emptyString = emptyString;
this.returnNullWhenEmpty = false;

}
/**
Expand All @@ -77,7 +81,7 @@ public ErrorMessageCollector(@NotNull String prefix, @Nullable String emptyStrin
@Override
public Supplier<StringJoiner> supplier() {
return () -> {
var joiner = new StringJoiner(", ", emptyString + prefix, "");
var joiner = new StringJoiner(", ", prefix, "");
joiner.setEmptyValue(emptyString);
return joiner;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public SearchHelper prepareExec(SortedSet<String> projects) {
searcher = RuntimeEnvironment.getInstance().getIndexSearcherFactory().newSearcher(reader);
} else {
errorMsg = projects.stream()
.collect(new ErrorMessageCollector(" for projects: ",
.collect(new ErrorMessageCollector("Failed to initialize search. Check the index for projects: ",
"Failed to initialize search. Check the index"))
.orElse("");
return this;
Expand Down Expand Up @@ -430,7 +430,7 @@ public SearchHelper prepareExec(SortedSet<String> projects) {
} catch (FileNotFoundException e) {

errorMsg = projects.stream()
.collect(new ErrorMessageCollector(" for projects: ",
.collect(new ErrorMessageCollector("Index database not found. Check the index for projects: ",
"Index database not found. Check the index"))
.orElse("");
errorMsg += "; " + e.getMessage();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/

/*
* Copyright (c) 2023, Oracle and/or its affiliates.
* Portions Copyright (c) 2023, Gino Augustine <[email protected]>.
*/
package org.opengrok.indexer.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Set;
import java.util.stream.Stream;

/**
* Represents a container for tests of {@link ErrorMessageCollector}.
*/
class ErrorMessageCollectorTest {
@Test
void noEmptyStringEmptyCollectionReturnOptionalEmpty() {
var collector = new ErrorMessageCollector("TestPrefix");
var returnValue = Stream.<String>empty().collect(collector);
Assertions.assertTrue(returnValue.isEmpty());
}
@Test
void emptyStringWithEmptyCollectionReturnOptionalEmpty() {
var collector = new ErrorMessageCollector("TestPrefix", "TestEmptyString");
var returnValue = Stream.<String>empty().collect(collector);
Assertions.assertEquals("TestEmptyString", returnValue.orElse(""));
}
@Test
void noEmptyStringWithMultiElementCollectionReturnJoinedString() {
var collector = new ErrorMessageCollector("TestPrefix ");
var returnValue = Set.of("a", "b").stream().collect(collector);
Assertions.assertTrue(returnValue.orElse("").startsWith("TestPrefix "));
}
@Test
void emptyStringWithMultiElementCollectionReturnJoinedStringWithoutEmptyString() {
var collector = new ErrorMessageCollector("TestPrefix ", "TestEmptyString");
var returnValue = Set.of("a", "b").stream().collect(collector);
Assertions.assertTrue(returnValue.orElse("").startsWith("TestPrefix "));
}

}

0 comments on commit 4d470e2

Please sign in to comment.