diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/FormatTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/FormatTests.java index 7bbeae43cb..b2432f2800 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/FormatTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/FormatTests.java @@ -45,6 +45,8 @@ import java.math.BigDecimal; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -98,34 +100,57 @@ extends BaseTest result = create() + .select(T639_ID(), T639_BIG_DECIMAL()) + .from(T639()) + .orderBy(T639_ID()) + .fetch(); + + for (int i = 2; i <= result.size(); i++) { + testFormatDecimalAlignment(result.format(i)); + } + } + + private void testFormatDecimalAlignment(String format) { + Pattern pattern = Pattern.compile("(\\s*-?\\d+)(\\.?(?:\\d*\\s*))"); + + // Collect lenghts of strings to both sides of the decimal point + Set lhs = new HashSet(); + Set rhs = new HashSet(); - // Collect decimal point indexes - Set decimalPointIndexSet = new HashSet(); for (String formatLine : format.split("\n")) { - // Include only data lines - if (formatLine.startsWith("|")) { - decimalPointIndexSet.add(formatLine.indexOf(".")); + + // Include only data lines (lines starting with an ID value | 13 |) + if (formatLine.matches("^\\|\\s*\\d+\\|.*") && !formatLine.contains("{null}")) { + + // Find the value in the second column + String bigDecimalValue = formatLine.replaceAll("^\\|\\s*\\d+\\|([^\\|]+)\\|", "$1"); + + // $1: The left side of the decimal point + // $2: The right side of the decimal point, or \\s+ + Matcher matcher = pattern.matcher(bigDecimalValue); + + assertTrue(matcher.find()); + lhs.add(matcher.group(1).length()); + rhs.add(matcher.group(2).length()); } } - // Remove -1 position - decimalPointIndexSet.remove(-1); - // Check if all decimal points have the same position - assertEquals(1, decimalPointIndexSet.size()); + assertEquals(1, lhs.size()); + assertEquals(1, rhs.size()); } @Test diff --git a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java index a0f39ffc47..ff9010062c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java @@ -940,10 +940,12 @@ class ResultImpl implements Result, AttachableInternal { public final String format(int maxRecords) { final int COL_MIN_WIDTH = 4; final int COL_MAX_WIDTH = 50; + // Numeric columns have greater max width because values are aligned final int NUM_COL_MAX_WIDTH = 100; - final int MAX_RECORDS = 50; + // The max number of records that will be considered for formatting purposes + final int MAX_RECORDS = min(50, maxRecords); // Get max decimal places for numeric type columns Map, Integer> decimalPlacesMap = new HashMap, Integer>(); @@ -1074,7 +1076,7 @@ class ResultImpl implements Result, AttachableInternal { return sb.toString(); } - private String alignNumberValue(Integer columnDecimalPlaces, String value) { + private static final String alignNumberValue(Integer columnDecimalPlaces, String value) { if (!"{null}".equals(value) && columnDecimalPlaces != 0) { int decimalPlaces = getDecimalPlaces(value); int rightPadSize = value.length() + columnDecimalPlaces - decimalPlaces; @@ -1091,7 +1093,7 @@ class ResultImpl implements Result, AttachableInternal { return value; } - private Integer getDecimalPlaces(String value) { + private static final int getDecimalPlaces(String value) { int decimalPlaces = 0; int dotIndex = value.indexOf("."); @@ -1199,7 +1201,7 @@ class ResultImpl implements Result, AttachableInternal { } } - private final String format0(Object value) { + private static final String format0(Object value) { String formatted; if (value == null) { @@ -1214,10 +1216,6 @@ class ResultImpl implements Result, AttachableInternal { else if (value instanceof EnumType) { formatted = ((EnumType) value).getLiteral(); } - else if (value instanceof Number) { - // Remove insignificant zeros - formatted = value.toString().replaceAll("(?:(\\..*[^0])0+|\\.0+)$", "$1"); - } else { formatted = value.toString(); }