Skip to content

Commit

Permalink
MONDRIAN: Change behavior of Format when negative number follows a '|…
Browse files Browse the repository at this point in the history
…' (vertical

    bar). Logged MONDRIAN-687 to reflect the fact that behavior is different
    from SSAS 2005.

[git-p4: depot-paths = "//open/mondrian-release/3.1/": change = 13362]
  • Loading branch information
julianhyde committed Jan 26, 2010
1 parent da75342 commit c076ef5
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/main/mondrian/util/Bug.java
Expand Up @@ -156,6 +156,14 @@ public class Bug {
*/
public static final boolean BugMondrian675Fixed = false;

/**
* Whether
* <a href="http://jira.pentaho.com/browse/MONDRIAN-675">bug MONDRIAN-687,
* "Format treats negative numbers differently than SSAS"</a>
* is fixed.
*/
public static final boolean BugMondrian687Fixed = false;

/**
* Returns whether to avoid a test because the memory monitor may cause it
* to fail.
Expand Down
48 changes: 47 additions & 1 deletion src/main/mondrian/util/Format.java
Expand Up @@ -354,6 +354,29 @@ void format(double n, StringBuilder buf) {
} else {
i = 0;
if (formats[0].isApplicableTo(n)) {
if (!Bug.BugMondrian687Fixed) {
// Special case for format strings with style,
// like "|#|style='red'". JPivot expects the
// '-' to immediately precede the digits, viz
// "|-6|style='red'|", not "-|6|style='red'|".
// This is not consistent with SSAS 2005, hence
// the bug.
//
// But for other formats, we want '-' to precede
// literals, viz '-$6' not '$-6'. This is SSAS
// 2005's behavior too.
int size = buf.length();
buf.append('-');
n = -n;
formats[i].format(n, buf);
if (buf.substring(size, size + 2).equals(
"-|"))
{
buf.setCharAt(size, '|');
buf.setCharAt(size + 1, '-');
}
return;
}
buf.append('-');
n = -n;
} else {
Expand Down Expand Up @@ -399,6 +422,29 @@ void format(long n, StringBuilder buf) {
} else {
i = 0;
if (formats[0].isApplicableTo(n)) {
if (!Bug.BugMondrian687Fixed) {
// Special case for format strings with style,
// like "|#|style='red'". JPivot expects the
// '-' to immediately precede the digits, viz
// "|-6|style='red'|", not "-|6|style='red'|".
// This is not consistent with SSAS 2005, hence
// the bug.
//
// But for other formats, we want '-' to precede
// literals, viz '-$6' not '$-6'. This is SSAS
// 2005's behavior too.
final int size = buf.length();
buf.append('-');
n = -n;
formats[i].format(n, buf);
if (buf.substring(size, size + 2).equals(
"-|"))
{
buf.setCharAt(size, '|');
buf.setCharAt(size + 1, '-');
}
return;
}
buf.append('-');
n = -n;
} else {
Expand Down Expand Up @@ -1180,7 +1226,7 @@ private static final Token nfe(
return new Token(code, flags, token);
}

public static final List<Token> getTokenList()
public static List<Token> getTokenList()
{
return Collections.unmodifiableList(Arrays.asList(tokens));
}
Expand Down
81 changes: 77 additions & 4 deletions testsrc/main/mondrian/util/FormatTest.java
Expand Up @@ -223,6 +223,12 @@ public void testTrickyNumbers() {
// if there are no digits right to decimal.
checkFormat(null, new BigDecimal("23"), "#.#", "23.");
checkFormat(null, new BigDecimal("0"), "#.#", ".");

// escaped semicolon
final String formatString = "$\\;#;(\\;#);\\\\;Z";
checkFormat(null, new BigDecimal("1"), formatString, "$;1");
checkFormat(null, new BigDecimal("-1"), formatString, "(;1)");
checkFormat(null, new BigDecimal("0"), formatString, "Z");
}

/**
Expand Down Expand Up @@ -335,17 +341,84 @@ public void testNegativeZero() {
* MONDRIAN-686</a>, "Regression: JPivot output invalid - New Variance
* Percent column".
*/
public void testNegativePercentWithStyle() {
public void testPercentWithStyle() {
checkFormat(
null,
new BigDecimal("0.0364"),
"|#.00%|style='green'",
"|3.64%|style='green'");
}

/**
* Test case for bug <a href="http://jira.pentaho.com/browse/MONDRIAN-687">
* MONDRIAN-687</a>, "Format treats negative numbers differently than SSAS".
*/
public void testNegativePercentWithStyle() {
if (Bug.BugMondrian687Fixed) {
checkFormat(
null,
new BigDecimal("-0.0364"),
"|#.00%|style=red",
"-|3.64%|style=red");
} else {
checkFormat(
null,
new BigDecimal("-0.0364"),
"|#.00%|style='red'",
"|-3.64%|style='red'"); // confirmed on SSAS 2005
}

// exercise code for long (and int) values
if (Bug.BugMondrian687Fixed) {
checkFormat(
null,
-364,
"|#.00|style=red",
"-|364.00|style=red");
} else {
checkFormat(
null,
-364,
"|#.00|style=red",
"|-364.00|style=red"); // confirmed on SSAS 2005
}

// now with multiple alternate formats
checkFormat(
null,
364,
"|#.00|style='green';|-#.000|style='red'",
"|364.00|style='green'"); // confirmed on SSAS 2005
checkFormat(
null,
new BigDecimal("-0.0364"),
"|#.00%|style='red'",
"-|3.64%|style='red'"); // confirmed on SSAS 2005
-364,
"|#.00|style='green';|-#.000|style='red'",
"|-364.000|style='red'"); // confirmed on SSAS 2005
}

/**
* Single quotes in format string. SSAS 2005 removes them; Mondrian should
* also.
*/
public void testSingleQuotes() {
if (Bug.BugMondrian687Fixed) {
checkFormat(
null,
3.64,
"|#.00|style='deep red'",
"-|364.00|style=deep red"); // confirmed on SSAS 2005
checkFormat(
null,
3.64,
"|#.00|style=\\'deep red\\'",
"-|364.00|style='deep red'"); // confirmed on SSAS 2005
} else {
checkFormat(
null,
-364,
"|#.00|style='deep red'",
"|-364.00|style='deep red'");
}
}

public void testNegativePercent() {
Expand Down

0 comments on commit c076ef5

Please sign in to comment.