harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From qi...@apache.org
Subject svn commit: r661219 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Formatter.java test/api/common/org/apache/harmony/luni/tests/java/util/FormatterTest.java
Date Thu, 29 May 2008 06:01:22 GMT
Author: qiuxx
Date: Wed May 28 23:01:22 2008
New Revision: 661219

URL: http://svn.apache.org/viewvc?rev=661219&view=rev
Log:
Apply patch for HARMONY-5845, ([classlib][util] three bugs for Formatter)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/FormatterTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java?rev=661219&r1=661218&r2=661219&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java Wed
May 28 23:01:22 2008
@@ -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,19 +1748,20 @@
                 }
 
             } 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++;
                     }
-                    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() {

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/FormatterTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/FormatterTest.java?rev=661219&r1=661218&r2=661219&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/FormatterTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/FormatterTest.java
Wed May 28 23:01:22 2008
@@ -29,11 +29,13 @@
 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;
 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();
@@ -4238,10 +4248,49 @@
         assertEquals("Wrong dec float value from valueOf ", BigDecimalLayoutForm.DECIMAL_FLOAT,
decFloat);
     }
     
+    /*
+     * Regression test for Harmony-5845
+     * 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"));
+            }
+        }
+    }
+    
+    /*
+     * Regression test for Harmony-5845
+     * 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";
+        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();
 



Mime
View raw message