harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvj...@apache.org
Subject svn commit: r648118 - in /harmony/enhanced/classlib/branches/java6/modules/text/src: main/java/java/text/ test/java/org/apache/harmony/text/tests/java/text/
Date Tue, 15 Apr 2008 05:23:22 GMT
Author: lvjing
Date: Mon Apr 14 22:23:13 2008
New Revision: 648118

URL: http://svn.apache.org/viewvc?rev=648118&view=rev
Log:
Apply patch for HARMONY-5717,[classlib][text] uplift NumberFormat and DecimalFormat classes
of java.text for Java6

Modified:
    harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/DecimalFormat.java
    harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/NumberFormat.java
    harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/DecimalFormatTest.java
    harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/NumberFormatTest.java

Modified: harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/DecimalFormat.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/DecimalFormat.java?rev=648118&r1=648117&r2=648118&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/DecimalFormat.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/DecimalFormat.java
Mon Apr 14 22:23:13 2008
@@ -23,6 +23,7 @@
 import java.io.ObjectStreamField;
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.math.RoundingMode;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Currency;
@@ -48,6 +49,9 @@
     private static final int CURRENT_SERIAL_VERTION = 3;
 
     private transient int serialVersionOnStream = 3;
+    
+    // set default RoundingMode to RoundingMode.HALF_EVEN
+    private transient RoundingMode roundingMode = RoundingMode.HALF_EVEN;
 
     /**
      * Constructs a new DecimalFormat for formatting and parsing numbers for the
@@ -561,6 +565,9 @@
     public void setMaximumFractionDigits(int value) {
         super.setMaximumFractionDigits(value);
         dform.setMaximumFractionDigits(value);
+        
+        // update the RoundingIncrement
+		setRoundingIncrement(value);
     }
 
     /**
@@ -896,4 +903,77 @@
                     }
                 });
     }
+    
+	/**
+	 * Get the RoundingMode of this DecimalFormat
+	 * 
+	 * @return the RoundingMode
+	 */
+	public RoundingMode getRoundingMode() {
+		// return the RoundingMode of this DecimalFormat
+		return roundingMode;
+	}
+
+	/**
+	 * Set the RoundingMode of this DecimalFormat
+	 * 
+	 * @param roundingMode
+	 *            the given RoundingMode
+	 */
+	public void setRoundingMode(RoundingMode roundingMode) {
+		if (roundingMode == null) {
+			// when the given RoundingMode is null, throw NullPointerException
+			throw new NullPointerException();
+		}
+
+		// update the RoundingMode with the given RoundingMode
+		this.roundingMode = roundingMode;
+
+		// update the RoundingMode of com.ibm.icu.text.DecimalFormat
+		switch(roundingMode){
+		case CEILING:
+			dform.setRoundingMode(com.ibm.icu.math.BigDecimal.ROUND_CEILING);
+			break;
+		case DOWN:
+			dform.setRoundingMode(com.ibm.icu.math.BigDecimal.ROUND_DOWN);
+			break;
+		case UP:
+			dform.setRoundingMode(com.ibm.icu.math.BigDecimal.ROUND_UP);
+			break;
+		case FLOOR:
+			dform.setRoundingMode(com.ibm.icu.math.BigDecimal.ROUND_FLOOR);
+			break;
+		case HALF_DOWN:
+			dform.setRoundingMode(com.ibm.icu.math.BigDecimal.ROUND_HALF_DOWN);
+			break;
+		case HALF_UP:
+			dform.setRoundingMode(com.ibm.icu.math.BigDecimal.ROUND_HALF_UP);
+			break;
+		case UNNECESSARY:
+			dform.setRoundingMode(com.ibm.icu.math.BigDecimal.ROUND_UNNECESSARY);
+			break;
+		case HALF_EVEN:
+			dform.setRoundingMode(com.ibm.icu.math.BigDecimal.ROUND_HALF_EVEN);
+			break;
+		default:
+			break;
+		}
+		
+		// update the RoundingIncrement
+		setRoundingIncrement(getMaximumFractionDigits());
+	}
+
+	/*
+	 * Update the RoundingIncrement according to the given MaxFractionDigits
+	 */
+	private void setRoundingIncrement(int value) {
+		int maxFractionDigits = value;
+		// when value < 0, set maximumFractionDigits to 0 and update the
+		// RoundingIncreament
+		if (maxFractionDigits < 0) {
+			maxFractionDigits = 0;
+		}
+		double roundingIncrement = 1.0 / Math.pow(10, maxFractionDigits);
+		dform.setRoundingIncrement(roundingIncrement);
+	}
 }

Modified: harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/NumberFormat.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/NumberFormat.java?rev=648118&r1=648117&r2=648118&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/NumberFormat.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/text/src/main/java/java/text/NumberFormat.java
Mon Apr 14 22:23:13 2008
@@ -22,6 +22,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamField;
+import java.math.RoundingMode;
 import java.util.Currency;
 import java.util.Locale;
 
@@ -737,5 +738,28 @@
             throw new InvalidObjectException(Messages.getString("text.02")); //$NON-NLS-1$
         }
     }
+    
+    /**
+	 * Get RoundingMode of NumberFormat
+	 * 
+	 * @return the RoundingMode
+	 */
+	public RoundingMode getRoundingMode() {
+		// The default implementation always throws
+		// UnsupportedOperationException
+		throw new UnsupportedOperationException();
+	}
+
+	/**
+	 * Set a given RoundingMode to this NumberFormat
+	 * 
+	 * @param roundingMode
+	 *            the given RoundingMode
+	 */
+	public void setRoundingMode(RoundingMode roundingMode) {
+		// The default implementation always throws
+		// UnsupportedOperationException
+		throw new UnsupportedOperationException();
+	}
 
 }

Modified: harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/DecimalFormatTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/DecimalFormatTest.java?rev=648118&r1=648117&r2=648118&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/DecimalFormatTest.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/DecimalFormatTest.java
Mon Apr 14 22:23:13 2008
@@ -20,6 +20,7 @@
 import java.io.ObjectInputStream;
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.math.RoundingMode;
 import java.text.AttributedCharacterIterator;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
@@ -1595,4 +1596,474 @@
         DecimalFormat format = (DecimalFormat)DecimalFormat.getInstance();
         format.setDecimalFormatSymbols(null);                     
     }
+    
+    /**
+	 * @tests java.text.DecimalFormat#formatToCharacterIterator(java.lang.Object)
+	 */
+	public void test_formatToCharacterIteratorLjava_lang_Object__ArithmeticException() {
+		DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat
+				.getInstance(Locale.US);
+		decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
+		decimalFormat.setMaximumFractionDigits(0);
+		try {
+			// when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, throw
ArithmeticException
+			decimalFormat.formatToCharacterIterator(new Double(1.5));
+			fail("ArithmeticException expected");
+		} catch (ArithmeticException e) {
+			// expected
+		}
+	}
+
+	/**
+	 * @tests java.text.DecimalFormat#format(double, java.lang.StringBuffer,
+	 *        java.text.FieldPosition)
+	 */
+	public void test_formatDLjava_lang_StringBufferLjava_text_FieldPosition_ArithmeticException()
{
+		DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat
+				.getInstance(Locale.US);
+		decimalFormat.setMaximumFractionDigits(0);
+		decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
+
+		try {
+			// when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, throw
ArithmeticException
+			decimalFormat
+					.format(11.5, new StringBuffer(), new FieldPosition(0));
+			fail("ArithmeticException expected");
+		} catch (ArithmeticException e) {
+			// expected
+		}
+	}
+
+	/**
+	 * @tests java.text.DecimalFormat#format(long, java.lang.StringBuffer,
+	 *        java.text.FieldPosition)
+	 */
+	public void test_formatJLjava_lang_StringBufferLjava_text_FieldPosition_ArithmeticException()
{
+
+		final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
+		DecimalFormat decimalFormat = new DecimalFormat("00.0#E0", dfs);
+
+		decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
+		try {
+			// when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, throw
ArithmeticException
+			decimalFormat.format(99999, new StringBuffer(),
+					new FieldPosition(0));
+			fail("ArithmeticException expected");
+		} catch (ArithmeticException e) {
+			// expected
+		}
+	}
+
+	/**
+	 * @tests java.text.DecimalFormat#getRoundingMode()
+	 */
+	public void test_GetRoundingMode() {
+
+		// get the default RoundingMode of this DecimalFormat
+		DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat
+				.getInstance(Locale.US);
+		
+		// the default RoundingMode is HALF_EVEN
+		assertEquals("Incorrect default RoundingMode", decimalFormat.getRoundingMode(), RoundingMode.HALF_EVEN);
+
+		// set RoundingMode.HALF_DOWN of this DecimalFormat
+		decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
+		assertEquals("Returned incorrect RoundingMode", decimalFormat
+				.getRoundingMode(), RoundingMode.HALF_DOWN);
+
+	}
+
+	/**
+	 * @tests java.text.DecimalFormat#setRoundingMode(java.math.RoundingMode)
+	 */
+	public void test_SetRoudingMode_Ljava_math_RoundingMode() {
+		DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat
+				.getInstance(Locale.US);
+		// ignore the fraction part of a given value
+		decimalFormat.setMaximumFractionDigits(0);
+
+		// set RoundingMode.HALF_DOWN of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
+		String result = decimalFormat.format(11.3);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN",
+				"11", result);
+
+		result = decimalFormat.format(11.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN",
+				"11", result);
+
+		result = decimalFormat.format(11.6);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN",
+				"12", result);
+
+		// set RoundingMode.CEILING of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.CEILING);
+		result = decimalFormat.format(11.3);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING",
+				"12", result);
+
+		result = decimalFormat.format(-11.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING",
+				"-11", result);
+
+		// set RoundingMode.DOWN of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.DOWN);
+		result = decimalFormat.format(11.3);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN",
+				"11", result);
+
+		result = decimalFormat.format(-11.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN",
+				"-11", result);
+
+		result = decimalFormat.format(0);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN",
+				"0", result);
+
+		// set RoundingMode.FLOOR of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.FLOOR);
+		result = decimalFormat.format(11.3);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR",
+				"11", result);
+
+		result = decimalFormat.format(-11.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR",
+				"-12", result);
+
+		result = decimalFormat.format(0);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR",
+				"0", result);
+
+		// set RoundingMode.HALF_EVEN of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN);
+		result = decimalFormat.format(5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN",
+				"6", result);
+
+		result = decimalFormat.format(-5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN",
+				"-6", result);
+
+		result = decimalFormat.format(0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN",
+				"0", result);
+
+		// set RoundingMode.HALF_UP of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
+		result = decimalFormat.format(5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"6", result);
+
+		result = decimalFormat.format(-5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"-6", result);
+
+		result = decimalFormat.format(0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"0", result);
+
+		// the following assertion will fail on RI implementation, since the
+	    // implementation of ICU and RI are not identical.
+		result = decimalFormat.format(-0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"0", result);
+
+		// set RoundingMode.UP of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.UP);
+		result = decimalFormat.format(5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"6", result);
+
+		result = decimalFormat.format(-5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"-6", result);
+
+		result = decimalFormat.format(0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"1", result);
+
+		result = decimalFormat.format(-0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"-1", result);
+
+		// set RoundingMode.UNNECESSARY of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
+		
+		try {
+			// when rounding is needed but RoundingMode is set to RoundingMode.UNNECESSARY, throw
ArithmeticException
+			result = decimalFormat.format(5.5);
+			fail("ArithmeticException expected: RoundingMode.UNNECESSARY");
+		} catch (ArithmeticException e) {
+			// expected
+		}
+
+		result = decimalFormat.format(1.0);
+		assertEquals(
+				"Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY",
+				"1", result);
+
+		result = decimalFormat.format(-1.0);
+		assertEquals(
+				"Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY",
+				"-1", result);
+
+		try {
+			// when the given RoundingMode is null, throw NullPointerException
+			decimalFormat.setRoundingMode(null);
+			fail("NullPointerException expected");
+		} catch (NullPointerException e) {
+			// expected
+		}
+
+		// set MaxFractionDigits to 3, test different DecimalFormat format
+		// function with differnt RoundingMode
+		decimalFormat.setMaximumFractionDigits(3);
+
+		// set RoundingMode.HALF_DOWN of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
+		result = decimalFormat.format(11.5653);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN",
+				"11.565", result);
+
+		result = decimalFormat.format(11.5655);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN",
+				"11.565", result);
+
+		result = decimalFormat.format(11.5656);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN",
+				"11.566", result);
+
+		// set RoundingMode.CEILING of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.CEILING);
+		result = decimalFormat.format(11.5653);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING",
+				"11.566", result);
+
+		result = decimalFormat.format(-11.5653);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING",
+				"-11.565", result);
+
+		// set RoundingMode.DOWN of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.DOWN);
+		result = decimalFormat.format(11.5653);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN",
+				"11.565", result);
+
+		result = decimalFormat.format(-11.5653);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN",
+				"-11.565", result);
+
+		result = decimalFormat.format(0);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN",
+				"0", result);
+
+		// set RoundingMode.FLOOR of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.FLOOR);
+		result = decimalFormat.format(11.5653);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR",
+				"11.565", result);
+
+		result = decimalFormat.format(-11.5655);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR",
+				"-11.566", result);
+
+		result = decimalFormat.format(0);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR",
+				"0", result);
+
+		// set RoundingMode.HALF_EVEN of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN);
+		result = decimalFormat.format(11.5653);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN",
+				"11.565", result);
+
+		result = decimalFormat.format(-11.5655);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN",
+				"-11.566", result);
+
+		result = decimalFormat.format(11.5656);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN",
+				"11.566", result);
+
+		// set RoundingMode.HALF_UP of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
+		result = decimalFormat.format(11.5653);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"11.565", result);
+
+		result = decimalFormat.format(-11.5655);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"-11.566", result);
+
+		result = decimalFormat.format(11.5656);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"11.566", result);
+
+		// set RoundingMode.UP of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.UP);
+		result = decimalFormat.format(11.5653);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"11.566", result);
+
+		result = decimalFormat.format(-11.5655);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"-11.566", result);
+
+		// set RoundingMode.UNNECESSARY of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
+		result = decimalFormat.format(-11.565);
+		assertEquals(
+				"Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY",
+				"-11.565", result);
+
+		result = decimalFormat.format(11.565);
+		assertEquals(
+				"Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY",
+				"11.565", result);
+
+		// when setting MaxFractionDigits to negative value -2, default it as
+		// zero, test different DecimalFormat format
+		// function with differnt RoundingMode
+		decimalFormat.setMaximumFractionDigits(-2);
+
+		// set RoundingMode.HALF_DOWN of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
+		result = decimalFormat.format(11.3);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN",
+				"11", result);
+
+		result = decimalFormat.format(11.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN",
+				"11", result);
+
+		result = decimalFormat.format(11.6);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN",
+				"12", result);
+
+		// set RoundingMode.CEILING of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.CEILING);
+		result = decimalFormat.format(11.3);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING",
+				"12", result);
+
+		result = decimalFormat.format(-11.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING",
+				"-11", result);
+
+		// set RoundingMode.DOWN of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.DOWN);
+		result = decimalFormat.format(11.3);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN",
+				"11", result);
+
+		result = decimalFormat.format(-11.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN",
+				"-11", result);
+
+		result = decimalFormat.format(0);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN",
+				"0", result);
+
+		// set RoundingMode.FLOOR of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.FLOOR);
+		result = decimalFormat.format(11.3);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR",
+				"11", result);
+
+		result = decimalFormat.format(-11.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR",
+				"-12", result);
+
+		result = decimalFormat.format(0);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR",
+				"0", result);
+
+		// set RoundingMode.HALF_EVEN of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN);
+		result = decimalFormat.format(5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN",
+				"6", result);
+
+		result = decimalFormat.format(-5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN",
+				"-6", result);
+
+		result = decimalFormat.format(0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN",
+				"0", result);
+
+		// set RoundingMode.HALF_UP of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
+		result = decimalFormat.format(5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"6", result);
+
+		result = decimalFormat.format(-5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"-6", result);
+
+		result = decimalFormat.format(0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"0", result);
+
+		result = decimalFormat.format(-0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP",
+				"0", result);
+
+		// set RoundingMode.UP of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.UP);
+		result = decimalFormat.format(5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"6", result);
+
+		result = decimalFormat.format(-5.5);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"-6", result);
+
+		result = decimalFormat.format(0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"1", result);
+
+		result = decimalFormat.format(-0.2);
+		assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP",
+				"-1", result);
+
+		// set RoundingMode.UNNECESSARY of this DecimalFormat and test its
+		// behavior
+		decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
+
+		result = decimalFormat.format(1.0);
+		assertEquals(
+				"Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY",
+				"1", result);
+
+		result = decimalFormat.format(-1.0);
+		assertEquals(
+				"Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY",
+				"-1", result);
+
+	}
 }

Modified: harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/NumberFormatTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/NumberFormatTest.java?rev=648118&r1=648117&r2=648118&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/NumberFormatTest.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/NumberFormatTest.java
Mon Apr 14 22:23:13 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.harmony.text.tests.java.text;
 
+import java.math.RoundingMode;
 import java.text.ChoiceFormat;
 import java.text.DecimalFormat;
 import java.text.FieldPosition;
@@ -236,4 +237,38 @@
 
     protected void tearDown() {
     }
+    
+    /**
+	 * @test java.text.NumberFormat#setRoundingMode(java.math.RoundingMode)
+	 */
+	public void test_setRoundingMode_NullRoundingMode() {
+		try {
+			// Create a subclass ChoiceFormat which doesn't support
+			// RoundingMode
+			ChoiceFormat choiceFormat = new ChoiceFormat(
+					"0#Less than one|1#one|1<Between one and two|2<Greater than two");
+			((NumberFormat) choiceFormat).setRoundingMode(null);
+			// Follow the behavior of RI
+			fail("UnsupportedOperationException expected");
+		} catch (UnsupportedOperationException e) {
+			// expected
+		}
+	}
+
+	/**
+	 * @test java.text.NumberFormat#setRoundingMode(java.math.RoundingMode)
+	 */
+	public void test_setRoundingMode_Normal() {
+		try {
+			// Create a subclass ChoiceFormat which doesn't support
+			// RoundingMode
+			ChoiceFormat choiceFormat = new ChoiceFormat(
+					"0#Less than one|1#one|1<Between one and two|2<Greater than two");
+			((NumberFormat) choiceFormat).setRoundingMode(RoundingMode.CEILING);
+			fail("UnsupportedOperationException expected");
+		} catch (UnsupportedOperationException e) {
+			// expected
+		}
+	}
+	
 }



Mime
View raw message