Index: src/main/java/java/util/Formatter.java =================================================================== --- src/main/java/java/util/Formatter.java (revision 660119) +++ src/main/java/java/util/Formatter.java (working copy) @@ -28,6 +28,7 @@ import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.BigInteger; +import java.math.MathContext; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.security.AccessController; @@ -1722,9 +1723,18 @@ boolean requireScientificRepresentation = true; double d = ((Number) argument).doubleValue(); d = Math.abs(d); - long l = Math.round(d); + if (Double.isInfinite(d)) { + precision = formatToken.getPrecision(); + precision--; + formatToken.setPrecision(precision); + transform_e(); + return; + } + BigDecimal b = new BigDecimal(d, new MathContext(precision)); + d = b.doubleValue(); + long l = b.longValue(); - if (l >= 1) { + if (d >= 1 && d < Math.pow(10, precision)) { if (l < Math.pow(10, precision)) { requireScientificRepresentation = false; precision -= String.valueOf(l).length(); @@ -1738,11 +1748,13 @@ } } else { - l = Math.round(d * Math.pow(10, 4)); - if (l >= 1) { + l = b.movePointRight(4).longValue(); + b.movePointLeft(4); + if (d >= Math.pow(10, -4) && d < 1) { requireScientificRepresentation = false; precision += 4 - String.valueOf(l).length(); - l = Math.round(d * Math.pow(10, precision + 1)); + l = b.movePointRight(precision + 1).longValue(); + b.movePointLeft(precision + 1); if (String.valueOf(l).length() <= formatToken .getPrecision()) { precision++; @@ -1747,10 +1759,9 @@ .getPrecision()) { precision++; } - l = Math.round(d * Math.pow(10, precision)); - if (l < Math.pow(10, precision - 4)) { - requireScientificRepresentation = true; - } else { + l = b.movePointRight(precision).longValue(); + b.movePointLeft(precision); + if (l >= Math.pow(10, precision - 4)) { formatToken.setPrecision(precision); } } @@ -2062,9 +2073,10 @@ private void transform_Z() { TimeZone timeZone = calendar.getTimeZone(); - result - .append(timeZone.getDisplayName(true, TimeZone.SHORT, - locale)); + result.append(timeZone + .getDisplayName( + timeZone.inDaylightTime(calendar.getTime()), + TimeZone.SHORT, locale)); } private void transform_z() { Index: src/test/api/common/org/apache/harmony/luni/tests/java/util/FormatterTest.java =================================================================== --- src/test/api/common/org/apache/harmony/luni/tests/java/util/FormatterTest.java (revision 660119) +++ src/test/api/common/org/apache/harmony/luni/tests/java/util/FormatterTest.java (working copy) @@ -29,6 +29,7 @@ import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.BigInteger; +import java.math.MathContext; import java.nio.charset.Charset; import java.security.Permission; import java.util.Arrays; @@ -34,6 +35,7 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Date; +import java.util.GregorianCalendar; import java.util.DuplicateFormatFlagsException; import java.util.FormatFlagsConversionMismatchException; import java.util.Formattable; @@ -56,6 +58,7 @@ import junit.framework.TestCase; public class FormatterTest extends TestCase { + private boolean root; class MockAppendable implements Appendable { public Appendable append(CharSequence arg0) throws IOException { @@ -247,12 +250,13 @@ assertEquals(0, fileWithContent.length()); f.close(); - // FIXME This exception will not be thrown out on linux. - try { - f = new Formatter(readOnly.getPath()); - fail("should throw FileNotFoundException"); - } catch (FileNotFoundException e) { - // expected + if(!root){ + try { + f = new Formatter(readOnly.getPath()); + fail("should throw FileNotFoundException"); + } catch (FileNotFoundException e) { + // expected + } } SecurityManager oldsm = System.getSecurityManager(); @@ -302,12 +306,13 @@ assertEquals(0, fileWithContent.length()); f.close(); - // FIXME This exception will not be thrown out on linux. - try { - f = new Formatter(readOnly.getPath(), "UTF-16BE"); - fail("should throw FileNotFoundException"); - } catch (FileNotFoundException e) { - // expected + if(!root){ + try { + f = new Formatter(readOnly.getPath(), "UTF-16BE"); + fail("should throw FileNotFoundException"); + } catch (FileNotFoundException e) { + // expected + } } SecurityManager oldsm = System.getSecurityManager(); @@ -365,14 +370,16 @@ assertEquals(0, fileWithContent.length()); f.close(); - try { - f = new Formatter(readOnly.getPath(), Charset.defaultCharset() - .name(), Locale.ITALY); - fail("should throw FileNotFoundException"); - } catch (FileNotFoundException e) { - // expected + if(!root){ + try { + f = new Formatter(readOnly.getPath(), Charset.defaultCharset() + .name(), Locale.ITALY); + fail("should throw FileNotFoundException"); + } catch (FileNotFoundException e) { + // expected + } } - + SecurityManager oldsm = System.getSecurityManager(); System.setSecurityManager(new MockSecurityManager()); try { @@ -406,14 +413,15 @@ assertEquals(0, fileWithContent.length()); f.close(); - // FIXME This exception will not be thrown out on linux. - try { - f = new Formatter(readOnly); - fail("should throw FileNotFoundException"); - } catch (FileNotFoundException e) { - // expected + if(!root){ + try { + f = new Formatter(readOnly); + fail("should throw FileNotFoundException"); + } catch (FileNotFoundException e) { + // expected + } } - + SecurityManager oldsm = System.getSecurityManager(); System.setSecurityManager(new MockSecurityManager()); try { @@ -447,12 +455,13 @@ assertEquals(0, fileWithContent.length()); f.close(); - // FIXME This exception will not be thrown out on linux. - try { - f = new Formatter(readOnly, Charset.defaultCharset().name()); - fail("should throw FileNotFoundException"); - } catch (FileNotFoundException e) { - // expected + if(!root){ + try { + f = new Formatter(readOnly, Charset.defaultCharset().name()); + fail("should throw FileNotFoundException"); + } catch (FileNotFoundException e) { + // expected + } } SecurityManager oldsm = System.getSecurityManager(); @@ -534,13 +543,14 @@ assertEquals(0, fileWithContent.length()); f.close(); - // FIXME This exception will not be thrown out on linux. - try { - f = new Formatter(readOnly.getPath(), Charset.defaultCharset() - .name(), Locale.ITALY); - fail("should throw FileNotFoundException"); - } catch (FileNotFoundException e) { - // expected + if(!root){ + try { + f = new Formatter(readOnly.getPath(), Charset.defaultCharset() + .name(), Locale.ITALY); + fail("should throw FileNotFoundException"); + } catch (FileNotFoundException e) { + // expected + } } SecurityManager oldsm = System.getSecurityManager(); @@ -4239,9 +4249,47 @@ } /** + * + * @tests test the short name for timezone whether uses DaylightTime or not + */ + public void test_DaylightTime() { + Calendar c1 = new GregorianCalendar(2007, 0, 1); + Calendar c2 = new GregorianCalendar(2007, 7, 1); + + for (String tz : TimeZone.getAvailableIDs()) { + if (tz.equals("America/Los_Angeles")) { + c1.setTimeZone(TimeZone.getTimeZone(tz)); + c2.setTimeZone(TimeZone.getTimeZone(tz)); + assertTrue(String.format("%1$tZ%2$tZ", c1, c2).equals("PSTPDT")); + } + if (tz.equals("America/Panama")) { + c1.setTimeZone(TimeZone.getTimeZone(tz)); + c2.setTimeZone(TimeZone.getTimeZone(tz)); + assertTrue(String.format("%1$tZ%2$tZ", c1, c2).equals("ESTEST")); + } + } + } + + /** + * @tests test scientific notation to follow RI's behavior + */ + public void test_ScientificNotation() { + Formatter f = new Formatter(); + MathContext mc = new MathContext(30); + BigDecimal value = new BigDecimal(0.1, mc); + f.format("%.30G", value); + + String result = f.toString(); + String expected = "0.100000000000000005551115123126";//$NON-NLS-1$ + assertEquals(expected, result); + } + + + /** * Setup resource files for testing */ protected void setUp() throws IOException { + root = System.getProperty("user.name").equalsIgnoreCase("root"); notExist = File.createTempFile("notexist", null); notExist.delete();