Skip to content

Commit

Permalink
fixup! fixup! Too big error message JabRef#4827 (adding unit tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpolishc committed May 14, 2019
1 parent 4865eae commit b9641f9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
20 changes: 3 additions & 17 deletions src/main/java/org/jabref/logic/shared/DBMSConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.jabref.logic.l10n.Localization;
import org.jabref.logic.shared.exception.InvalidDBMSConnectionPropertiesException;
import org.jabref.logic.util.strings.StringFormatter;
import org.jabref.model.database.shared.DBMSType;
import org.jabref.model.database.shared.DatabaseConnection;

Expand Down Expand Up @@ -39,8 +40,8 @@ public DBMSConnection(DBMSConnectionProperties connectionProperties) throws SQLE
// Some systems like PostgreSQL retrieves 0 to every exception.
// Therefore a stable error determination is not possible.
LOGGER.error("Could not connect to database: " + e.getMessage() + " - Error code: " + e.getErrorCode());
StringBuilder formattedErrorMessage = formatErrorMessage(e);
throw new SQLException(formattedErrorMessage.toString(), e);
String formattedErrorMessage = new StringFormatter().formatErrorMessage(e);
throw new SQLException(formattedErrorMessage, e);
}
}

Expand Down Expand Up @@ -71,19 +72,4 @@ public static Set<DBMSType> getAvailableDBMSTypes() {
}
return dbmsTypes;
}

private StringBuilder formatErrorMessage(SQLException e) {
String message = e.getMessage();
StringBuilder errorFormatter = new StringBuilder();
int wordCounter = 0;
for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) == ' ' && ++wordCounter % 15 == 0) {
errorFormatter.append(" ").append(System.lineSeparator());

continue;
}
errorFormatter.append(message.charAt(i));
}
return errorFormatter;
}
}
18 changes: 18 additions & 0 deletions src/main/java/org/jabref/logic/util/strings/StringFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jabref.logic.util.strings;

public class StringFormatter {

public String formatErrorMessage(Exception e) {
String message = e.getMessage();
StringBuilder errorFormatter = new StringBuilder();
int wordCounter = 0;
for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) == ' ' && ++wordCounter % 25 == 0) {
errorFormatter.append(" ").append(System.lineSeparator());
continue;
}
errorFormatter.append(message.charAt(i));
}
return errorFormatter.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jabref.logic.util.strings;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class StringFormatterTest {

private String error = "The server time zone value 'MSK' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.";

@Test
void formatErrorMessage() {
StringFormatter stringFormatter = new StringFormatter();
Exception exceptionUnderTest = new Exception(error);
String errorToBeThrown = stringFormatter.formatErrorMessage(exceptionUnderTest);
String[] lines = errorToBeThrown.split(System.lineSeparator());
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
String[] split = line.split("\\s+");
if (i != lines.length - 1) {
assertEquals(25, split.length);
}
}

}
}

0 comments on commit b9641f9

Please sign in to comment.