harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r380639 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src: main/java/java/nio/charset/Charset.java test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
Date Fri, 24 Feb 2006 10:41:29 GMT
Author: tellison
Date: Fri Feb 24 02:41:28 2006
New Revision: 380639

URL: http://svn.apache.org/viewcvs?rev=380639&view=rev
Log:
Apply patch HARMONY-96 (Two new methods need to be implemented by java.nio.charset.Charset)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java?rev=380639&r1=380638&r2=380639&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
Fri Feb 24 02:41:28 2006
@@ -666,7 +666,19 @@
 	 */
 	public final int compareTo(Object obj) {
 		Charset that = (Charset) obj;
-		return this.canonicalName.compareToIgnoreCase(that.canonicalName);
+		return compareTo(that);
+	}
+
+	/**
+	 * Compares this charset with the given charset.
+	 * 
+	 * @param charset
+	 *            the given object to be compared with
+	 * @return a negative integer if less than the given object, a positive
+	 *         integer if larger than it, or 0 if equal to it
+	 */
+	public final int compareTo(Charset charset) {
+		return this.canonicalName.compareToIgnoreCase(charset.canonicalName);
 	}
 
 	/*
@@ -709,7 +721,28 @@
 	public final String toString() {
 		return "Charset[" + this.canonicalName + "]"; //$NON-NLS-1$//$NON-NLS-2$
 	}
-
+	
+    /**
+	 * Gets the system default charset from jvm.
+	 * 
+	 * @return the default charset
+	 */
+	public static Charset defaultCharset() {
+		Charset defaultCharset = null;
+		String encoding = (String) AccessController
+				.doPrivileged(new PrivilegedAction() {
+					public Object run() {
+						return System.getProperty("file.encoding"); //$NON-NLS-1$
+					}
+				});
+		try {
+			defaultCharset = Charset.forName(encoding);
+		} catch (UnsupportedCharsetException e) {
+			defaultCharset = Charset.forName("UTF-8"); //$NON-NLS-1$
+		}
+		return defaultCharset;
+	}
+   
 	/**
 	 * A comparator that ignores case.
 	 */

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java?rev=380639&r1=380638&r2=380639&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
Fri Feb 24 02:41:28 2006
@@ -19,6 +19,7 @@
 import java.nio.charset.IllegalCharsetNameException;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.Properties;
 import java.util.Set;
 
 import junit.framework.TestCase;
@@ -74,5 +75,59 @@
 		} catch (IllegalCharsetNameException e) {
 			// Expected
 		}
-	} 
+	}
+	
+    /**
+	 * @tests java.nio.charset.Charset#defaultCharset()
+	 */
+	public void test_defaultCharset() {
+		String charsetName = null;
+		String defaultCharsetName = null;
+		Properties oldProps = (Properties) System.getProperties().clone();
+		try {
+			// Normal behavior
+			charsetName = "UTF-8"; //$NON-NLS-1$
+			System.setProperty("file.encoding", charsetName);//$NON-NLS-1$
+			defaultCharsetName = Charset.defaultCharset().name();
+			assertEquals(charsetName, defaultCharsetName);
+
+			charsetName = "ISO-8859-1"; //$NON-NLS-1$
+			System.setProperty("file.encoding", charsetName);//$NON-NLS-1$
+			defaultCharsetName = Charset.defaultCharset().name();
+			assertEquals(charsetName, defaultCharsetName);
+
+			System.setProperties(oldProps);
+
+			// Unsupported behavior
+			charsetName = "IMPOSSIBLE-8"; //$NON-NLS-1$
+			System.setProperty("file.encoding", charsetName);//$NON-NLS-1$
+			defaultCharsetName = Charset.defaultCharset().name();
+			assertEquals("UTF-8", defaultCharsetName);
+
+			System.setProperties(oldProps);
+
+			// Null behavior
+			try {
+				Properties currentProps = System.getProperties();
+				currentProps.remove("file.encoding");//$NON-NLS-1$
+				Charset.defaultCharset().name();
+				fail("Should throw illegal IllegalArgumentException");//$NON-NLS-1$
+			} catch (IllegalArgumentException e) {
+				// expected
+			}
+			System.setProperties(oldProps);
+
+			// IllegalCharsetName behavior
+			try {
+				charsetName = "IMP~~OSSIBLE-8"; //$NON-NLS-1$
+				System.setProperty("file.encoding", charsetName);//$NON-NLS-1$
+				Charset.defaultCharset().name();
+				fail("Should throw IllegalCharsetNameException");//$NON-NLS-1$
+			} catch (IllegalCharsetNameException e) {
+				// expected
+			}
+		} finally {
+			System.setProperties(oldProps);
+		}
+	}
 }



Mime
View raw message