reformat code and optimize imports

This commit is contained in:
Jan-Peter Klein
2023-06-15 13:12:10 +02:00
parent f054a1af03
commit f92c436339
3 changed files with 80 additions and 85 deletions

View File

@@ -42,13 +42,13 @@ public class ErrorController implements FxController {
private static final String REPORT_BODY_TEMPLATE = """
OS: %s / %s
App: %s / %s
<!-- ✏ Please describe what happened as accurately as possible. -->
<!-- 📋 Please also copy and paste the detail text from the error window. -->
<!-- Text enclosed like this (chevrons, exclamation mark, two dashes) is not visible to others! -->
<!-- ❗ If the description or the detail text is missing, the discussion will be deleted. -->
""";
@@ -62,7 +62,7 @@ public class ErrorController implements FxController {
private final BooleanProperty copiedDetails = new SimpleBooleanProperty();
private final BooleanProperty errorSolutionFound = new SimpleBooleanProperty();
private final BooleanProperty isLoadingHttpResponse = new SimpleBooleanProperty();
private ErrorDiscussion matchingErrorDiscussion;
private ErrorDiscussion matchingErrorDiscussion;
@Inject
ErrorController(Application application, @Named("stackTrace") String stackTrace, ErrorCode errorCode, @Nullable Scene previousScene, Stage window, Environment environment, ExecutorService executorService) {
@@ -75,12 +75,12 @@ public class ErrorController implements FxController {
isLoadingHttpResponse.set(true);
HttpClient httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(URI.create(ERROR_CODES_URL))
HttpRequest httpRequest = HttpRequest.newBuilder()//
.uri(URI.create(ERROR_CODES_URL))//
.build();
httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofInputStream())
.thenAcceptAsync(this::loadHttpResponse, executorService)
.whenCompleteAsync((r,e) -> isLoadingHttpResponse.set(false), Platform::runLater);
httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofInputStream())//
.thenAcceptAsync(this::loadHttpResponse, executorService)//
.whenCompleteAsync((r, e) -> isLoadingHttpResponse.set(false), Platform::runLater);
}
@FXML
@@ -97,7 +97,7 @@ public class ErrorController implements FxController {
@FXML
public void showSolution() {
if(matchingErrorDiscussion != null){
if (matchingErrorDiscussion != null) {
application.getHostServices().showDocument(matchingErrorDiscussion.url);
}
}
@@ -130,21 +130,22 @@ public class ErrorController implements FxController {
CompletableFuture.delayedExecutor(2, TimeUnit.SECONDS, Platform::runLater).execute(() -> copiedDetails.set(false));
}
private void loadHttpResponse(HttpResponse<InputStream> response){
private void loadHttpResponse(HttpResponse<InputStream> response) {
if (response.statusCode() == 200) {
Map<String,ErrorDiscussion> errorDiscussionMap = new Gson().fromJson(
new InputStreamReader(response.body(),StandardCharsets.UTF_8),
new TypeToken<Map<String,ErrorDiscussion>>(){}.getType());
Map<String, ErrorDiscussion> errorDiscussionMap = new Gson().fromJson(//
new InputStreamReader(response.body(), StandardCharsets.UTF_8),//
new TypeToken<Map<String, ErrorDiscussion>>() {
}.getType());
if(errorDiscussionMap.values().stream().anyMatch(this::isPartialMatchFilter)) {
if (errorDiscussionMap.values().stream().anyMatch(this::isPartialMatchFilter)) {
Comparator<ErrorDiscussion> comp = this::compareExactMatch;
Optional<ErrorDiscussion> value = errorDiscussionMap.values().stream().min(comp
.thenComparing(this::compareSecondLevelMatch)
.thenComparing(this::compareThirdLevelMatch)
.thenComparing(this::compareIsAnswered)
Optional<ErrorDiscussion> value = errorDiscussionMap.values().stream().min(comp//
.thenComparing(this::compareSecondLevelMatch)//
.thenComparing(this::compareThirdLevelMatch)//
.thenComparing(this::compareIsAnswered)//
.thenComparing(this::compareUpvoteCount));
if(value.isPresent()){
if (value.isPresent()) {
matchingErrorDiscussion = value.get();
errorSolutionFound.set(true);
}
@@ -157,8 +158,8 @@ public class ErrorController implements FxController {
*
* @param errorDiscussion The ErrorDiscussion object to be checked.
* @return A boolean value indicating if the ErrorDiscussion object is a partial match:
* - true if the object's title contains the error code's method code,
* - false otherwise.
* - true if the object's title contains the error code's method code,
* - false otherwise.
*/
public boolean isPartialMatchFilter(ErrorDiscussion errorDiscussion) {
return errorDiscussion.title.contains(" " + errorCode.methodCode());
@@ -170,9 +171,9 @@ public class ErrorController implements FxController {
* @param ed1 The first ErrorDiscussion object.
* @param ed2 The second ErrorDiscussion object.
* @return An integer indicating which ErrorDiscussion object has a higher upvote count:
* - A positive value if ed2 has a higher upvote count than ed1,
* - A negative value if ed1 has a higher upvote count than ed2,
* - Or 0 if both upvote counts are equal.
* - A positive value if ed2 has a higher upvote count than ed1,
* - A negative value if ed1 has a higher upvote count than ed2,
* - Or 0 if both upvote counts are equal.
*/
public int compareUpvoteCount(ErrorDiscussion ed1, ErrorDiscussion ed2) {
return Integer.compare(ed2.upvoteCount, ed1.upvoteCount);
@@ -184,14 +185,14 @@ public class ErrorController implements FxController {
* @param ed1 The first ErrorDiscussion object.
* @param ed2 The second ErrorDiscussion object.
* @return An integer indicating the answered status of the ErrorDiscussion objects:
* - A negative value (-1) if ed1 is considered answered and ed2 is considered unanswered,
* - A positive value (1) if ed1 is considered unanswered and ed2 is considered answered,
* - Or 0 if both ErrorDiscussion objects are considered answered or unanswered.
* - A negative value (-1) if ed1 is considered answered and ed2 is considered unanswered,
* - A positive value (1) if ed1 is considered unanswered and ed2 is considered answered,
* - Or 0 if both ErrorDiscussion objects are considered answered or unanswered.
*/
public int compareIsAnswered(ErrorDiscussion ed1, ErrorDiscussion ed2) {
if (ed1.answer!=null && ed2.answer==null) {
if (ed1.answer != null && ed2.answer == null) {
return -1;
} else if (ed1.answer==null && ed2.answer!=null) {
} else if (ed1.answer == null && ed2.answer != null) {
return 1;
} else {
return 0;
@@ -204,9 +205,9 @@ public class ErrorController implements FxController {
* @param ed1 The first ErrorDiscussion object.
* @param ed2 The second ErrorDiscussion object.
* @return An integer indicating the comparison result based on the presence of an exact match with the error code in the titles:
* - A negative value (-1) if ed1 has an exact match with the error code in the title and ed2 does not have a match,
* - A positive value (1) if ed1 does not have a match and ed2 has an exact match with the error code in the title,
* - Or 0 if both ErrorDiscussion objects either have an exact match or do not have a match with the error code in the titles.
* - A negative value (-1) if ed1 has an exact match with the error code in the title and ed2 does not have a match,
* - A positive value (1) if ed1 does not have a match and ed2 has an exact match with the error code in the title,
* - Or 0 if both ErrorDiscussion objects either have an exact match or do not have a match with the error code in the titles.
*/
public int compareExactMatch(ErrorDiscussion ed1, ErrorDiscussion ed2) {
if (ed1.title.contains(getErrorCode()) && !ed2.title.contains(getErrorCode())) {
@@ -224,9 +225,9 @@ public class ErrorController implements FxController {
* @param ed1 The first ErrorDiscussion object.
* @param ed2 The second ErrorDiscussion object.
* @return An integer indicating the comparison result based on the presence of a second-level match with the error code in the titles:
* - A negative value (-1) if ed1 has a second-level match with the error code in the title and ed2 does not have a match,
* - A positive value (1) if ed1 does not have a match and ed2 has a second-level match with the error code in the title,
* - Or 0 if both ErrorDiscussion objects either have a second-level match or do not have a match with the error code in the titles.
* - A negative value (-1) if ed1 has a second-level match with the error code in the title and ed2 does not have a match,
* - A positive value (1) if ed1 does not have a match and ed2 has a second-level match with the error code in the title,
* - Or 0 if both ErrorDiscussion objects either have a second-level match or do not have a match with the error code in the titles.
*/
public int compareSecondLevelMatch(ErrorDiscussion ed1, ErrorDiscussion ed2) {
String value = " " + errorCode.methodCode() + ErrorCode.DELIM + errorCode.rootCauseCode();
@@ -245,9 +246,9 @@ public class ErrorController implements FxController {
* @param ed1 The first ErrorDiscussion object.
* @param ed2 The second ErrorDiscussion object.
* @return An integer indicating the comparison result based on the presence of a third-level match with the error code in the titles:
* - A negative value (-1) if ed1 has a third-level match with the error code in the title and ed2 does not have a match,
* - A positive value (1) if ed1 does not have a match and ed2 has a third-level match with the error code in the title,
* - Or 0 if both ErrorDiscussion objects either have a third-level match or do not have a match with the error code in the titles.
* - A negative value (-1) if ed1 has a third-level match with the error code in the title and ed2 does not have a match,
* - A positive value (1) if ed1 does not have a match and ed2 has a third-level match with the error code in the title,
* - Or 0 if both ErrorDiscussion objects either have a third-level match or do not have a match with the error code in the titles.
*/
public int compareThirdLevelMatch(ErrorDiscussion ed1, ErrorDiscussion ed2) {
String value = " " + errorCode.methodCode();

View File

@@ -7,6 +7,7 @@ public class ErrorDiscussion {
String url;
Answer answer;
static class Answer{
static class Answer {
}
}

View File

@@ -11,10 +11,10 @@ import org.mockito.Mockito;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.concurrent.ExecutorService;
class ErrorControllerTest {
Application application;
String stackTrace;
ErrorCode errorCode;
@@ -33,19 +33,12 @@ class ErrorControllerTest {
window = Mockito.mock(Stage.class);
environment = Mockito.mock(Environment.class);
executorService = Mockito.mock(ExecutorService.class);
errorController = new ErrorController(
application,
stackTrace,
errorCode,
previousScene,
window,
environment,
executorService);
errorController = new ErrorController(application, stackTrace, errorCode, previousScene, window, environment, executorService);
}
private ErrorDiscussion createErrorDiscussion(String title, int upvoteCount, ErrorDiscussion.Answer answer){
private ErrorDiscussion createErrorDiscussion(String title, int upvoteCount, ErrorDiscussion.Answer answer) {
ErrorDiscussion ed = new ErrorDiscussion();
ed.title =title;
ed.title = title;
ed.upvoteCount = upvoteCount;
ed.answer = answer;
return ed;
@@ -53,24 +46,24 @@ class ErrorControllerTest {
@ParameterizedTest
@CsvSource(textBlock = """
10, 5, -1
8, 15, 1
10, 10, 0
""")
10, 5, -1
8, 15, 1
10, 10, 0
""")
public void testCompareUpvoteCount(int leftUpvoteCount, int rightUpvoteCount, int expectedResult) {
var left = createErrorDiscussion("", leftUpvoteCount, null);
var right = createErrorDiscussion("", rightUpvoteCount, null);
int result = errorController.compareUpvoteCount(left, right);
Assertions.assertEquals(expectedResult,Integer.signum(result));
Assertions.assertEquals(expectedResult, Integer.signum(result));
}
@ParameterizedTest
@CsvSource(textBlock = """
false, false, 0
true, true, 0
true, false, -1
false, true, 1
""")
false, false, 0
true, true, 0
true, false, -1
false, true, 1
""")
public void testCompareIsAnswered(boolean leftIsAnswered, boolean rightIsAnswered, int expectedResult) {
var answer = new ErrorDiscussion.Answer();
var left = createErrorDiscussion("", 0, leftIsAnswered ? answer : null);
@@ -81,54 +74,54 @@ class ErrorControllerTest {
@ParameterizedTest
@CsvSource(textBlock = """
Error 0000:0000:0000, Error 0000:0000:0000, 0
Error 6HU1:12H1:HU7J, Error 0000:0000:0000, -1
Error 0000:0000:0000, Error 6HU1:12H1:HU7J, 1
""")
Error 0000:0000:0000, Error 0000:0000:0000, 0
Error 6HU1:12H1:HU7J, Error 0000:0000:0000, -1
Error 0000:0000:0000, Error 6HU1:12H1:HU7J, 1
""")
public void testCompareExactMatch(String leftTitle, String rightTitle, int expectedResult) {
Mockito.when(errorCode.toString()).thenReturn("6HU1:12H1:HU7J");
var left = createErrorDiscussion(leftTitle, 0, null);
var right = createErrorDiscussion(rightTitle, 0,null);
int result = errorController.compareExactMatch(left,right);
var right = createErrorDiscussion(rightTitle, 0, null);
int result = errorController.compareExactMatch(left, right);
Assertions.assertEquals(expectedResult, result);
}
@ParameterizedTest
@CsvSource(textBlock = """
Error 6HU1:12H1:0000, Error 6HU1:12H1:0000, 0
Error 0000:0000:0000, Error 0000:0000:0000, 0
Error 6HU1:12H1:0000, Error 0000:0000:0000, -1
Error 0000:0000:0000, Error 6HU1:12H1:0000, 1
""")
Error 6HU1:12H1:0000, Error 6HU1:12H1:0000, 0
Error 0000:0000:0000, Error 0000:0000:0000, 0
Error 6HU1:12H1:0000, Error 0000:0000:0000, -1
Error 0000:0000:0000, Error 6HU1:12H1:0000, 1
""")
public void testCompareSecondLevelMatch(String leftTitle, String rightTitle, int expectedResult) {
Mockito.when(errorCode.methodCode()).thenReturn("6HU1");
Mockito.when(errorCode.rootCauseCode()).thenReturn("12H1");
var left = createErrorDiscussion(leftTitle, 0, null);
var right = createErrorDiscussion(rightTitle, 0,null);
int result = errorController.compareSecondLevelMatch(left,right);
var right = createErrorDiscussion(rightTitle, 0, null);
int result = errorController.compareSecondLevelMatch(left, right);
Assertions.assertEquals(expectedResult, result);
}
@ParameterizedTest
@CsvSource(textBlock = """
Error 6HU1:0000:0000, Error 6HU1:0000:0000, 0
Error 0000:0000:0000, Error 0000:0000:0000, 0
Error 6HU1:0000:0000, Error 0000:0000:0000, -1
Error 0000:0000:0000, Error 6HU1:0000:0000, 1
""")
Error 6HU1:0000:0000, Error 6HU1:0000:0000, 0
Error 0000:0000:0000, Error 0000:0000:0000, 0
Error 6HU1:0000:0000, Error 0000:0000:0000, -1
Error 0000:0000:0000, Error 6HU1:0000:0000, 1
""")
public void testCompareThirdLevelMatch(String leftTitle, String rightTitle, int expectedResult) {
Mockito.when(errorCode.methodCode()).thenReturn("6HU1");
var left = createErrorDiscussion(leftTitle, 0, null);
var right = createErrorDiscussion(rightTitle, 0,null);
int result = errorController.compareThirdLevelMatch(left,right);
var right = createErrorDiscussion(rightTitle, 0, null);
int result = errorController.compareThirdLevelMatch(left, right);
Assertions.assertEquals(expectedResult, result);
}
@ParameterizedTest
@CsvSource(textBlock = """
Error 6HU1:0000:0000, true
Error 0000:0000:0000, false
""")
Error 6HU1:0000:0000, true
Error 0000:0000:0000, false
""")
public void testIsPartialMatchFilter(String title, boolean expectedResult) {
Mockito.when(errorCode.methodCode()).thenReturn("6HU1");
var ed = createErrorDiscussion(title, 0, null);