From f92c4363395b40ae8f20a620660b17c93bed1e57 Mon Sep 17 00:00:00 2001 From: Jan-Peter Klein Date: Thu, 15 Jun 2023 13:12:10 +0200 Subject: [PATCH] reformat code and optimize imports --- .../cryptomator/ui/error/ErrorController.java | 81 ++++++++++--------- .../cryptomator/ui/error/ErrorDiscussion.java | 3 +- .../ui/error/ErrorControllerTest.java | 81 +++++++++---------- 3 files changed, 80 insertions(+), 85 deletions(-) diff --git a/src/main/java/org/cryptomator/ui/error/ErrorController.java b/src/main/java/org/cryptomator/ui/error/ErrorController.java index cc6ccf82f..ae006caee 100644 --- a/src/main/java/org/cryptomator/ui/error/ErrorController.java +++ b/src/main/java/org/cryptomator/ui/error/ErrorController.java @@ -42,13 +42,13 @@ public class ErrorController implements FxController { private static final String REPORT_BODY_TEMPLATE = """ OS: %s / %s App: %s / %s - + - + - + - + """; @@ -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 response){ + private void loadHttpResponse(HttpResponse response) { if (response.statusCode() == 200) { - Map errorDiscussionMap = new Gson().fromJson( - new InputStreamReader(response.body(),StandardCharsets.UTF_8), - new TypeToken>(){}.getType()); + Map errorDiscussionMap = new Gson().fromJson(// + new InputStreamReader(response.body(), StandardCharsets.UTF_8),// + new TypeToken>() { + }.getType()); - if(errorDiscussionMap.values().stream().anyMatch(this::isPartialMatchFilter)) { + if (errorDiscussionMap.values().stream().anyMatch(this::isPartialMatchFilter)) { Comparator comp = this::compareExactMatch; - Optional value = errorDiscussionMap.values().stream().min(comp - .thenComparing(this::compareSecondLevelMatch) - .thenComparing(this::compareThirdLevelMatch) - .thenComparing(this::compareIsAnswered) + Optional 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(); diff --git a/src/main/java/org/cryptomator/ui/error/ErrorDiscussion.java b/src/main/java/org/cryptomator/ui/error/ErrorDiscussion.java index cea9d3b9e..bc1663f6f 100644 --- a/src/main/java/org/cryptomator/ui/error/ErrorDiscussion.java +++ b/src/main/java/org/cryptomator/ui/error/ErrorDiscussion.java @@ -7,6 +7,7 @@ public class ErrorDiscussion { String url; Answer answer; - static class Answer{ + static class Answer { + } } diff --git a/src/test/java/org/cryptomator/ui/error/ErrorControllerTest.java b/src/test/java/org/cryptomator/ui/error/ErrorControllerTest.java index 0321a8493..6c4acb789 100644 --- a/src/test/java/org/cryptomator/ui/error/ErrorControllerTest.java +++ b/src/test/java/org/cryptomator/ui/error/ErrorControllerTest.java @@ -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);