More Charset test fixes.

Bug: 10210999

(cherry picked from commit c44b103bfd1a79762811d2125e9b94ce37300a44)

Change-Id: If28d9e2eb3d1d148863f0a08c1e17371918fcaa2
diff --git a/expectations/brokentests.txt b/expectations/brokentests.txt
index ae70c55..24813dd 100644
--- a/expectations/brokentests.txt
+++ b/expectations/brokentests.txt
@@ -793,12 +793,6 @@
   substring: "junit.framework.AssertionFailedError: expected:<1.1> but was:<2.0>"
 },
 {
-  description: "We don't permit runtime switching of the default charset (and neither does the RI)",
-  result: EXEC_FAILED,
-  name: "org.apache.harmony.nio_char.tests.java.nio.charset.CharsetTest#test_defaultCharset",
-  substring: "expected:<ISO-8859-1> but was:<UTF-8>"
-},
-{
   description: "this test needs external interaction",
   result: UNSUPPORTED,
   names: [
diff --git a/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetTest.java b/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetTest.java
index c658c48..99b8c44 100644
--- a/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetTest.java
+++ b/harmony-tests/src/test/java/tests/api/java/nio/charset/CharsetTest.java
@@ -107,30 +107,21 @@
 		}
 	}
 
-	/*
-	 * Test the method isSupported(String) with empty string.
-	 *
-	 */
-	public void testIsSupported_EmptyString() {
-		try {
-			Charset.isSupported("");
-		} catch (IllegalArgumentException e) {
-                        // FIXME: Commented out since RI does throw IAE
-                        // fail("Should not throw IllegalArgumentException!");
-		}
-	}
+  public void testIsSupported_EmptyString() {
+    try {
+      Charset.isSupported("");
+      fail();
+    } catch (IllegalArgumentException expected) {
+    }
+  }
 
-	/*
-	 * Test the method isSupported(String) with a string starting with ".".
-	 *
-	 */
-	public void testIsSupported_InvalidInitialCharacter() {
-		try {
-			Charset.isSupported(".char");
-		} catch (IllegalArgumentException e) {
-			fail("Should not throw IllegalArgumentException!");
-		}
-	}
+  public void testIsSupported_InvalidInitialCharacter() {
+    try {
+      Charset.isSupported(".char");
+      fail();
+    } catch (IllegalArgumentException expected) {
+    }
+  }
 
 	/*
 	 * Test the method isSupported(String) with illegal charset name.
@@ -224,31 +215,21 @@
 		assertEquals(1, c.aliases().toArray().length);
 	}
 
-	/*
-	 * Test the constructor with empty canonical name.
-	 *
-	 */
-	public void testConstructor_EmptyCanonicalName() {
-		try {
-			new MockCharset("", new String[0]);
-		} catch (IllegalCharsetNameException e) {
-                        // FIXME: Commented out since RI does throw IAE
-                        // fail("Should not throw IllegalArgumentException!");
-		}
-	}
+  public void testConstructor_EmptyCanonicalName() {
+    try {
+      new MockCharset("", new String[0]);
+      fail();
+    } catch (IllegalCharsetNameException expected) {
+    }
+  }
 
-	/*
-	 * Test the constructor with illegal canonical name: starting with neither a
-	 * digit nor a letter.
-	 *
-	 */
-	public void testConstructor_IllegalCanonicalName_Initial() {
-		try {
-			new MockCharset("-123", new String[] { "mock" });
-		} catch (IllegalCharsetNameException e) {
-			fail("Should not throw IllegalArgumentException!");
-		}
-	}
+  public void testConstructor_IllegalCanonicalName_Initial() {
+    try {
+      new MockCharset("-123", new String[] { "mock" });
+      fail();
+    } catch (IllegalCharsetNameException expected) {
+    }
+  }
 
 	/*
 	 * Test the constructor with illegal canonical name, illegal character in
@@ -315,31 +296,22 @@
 		assertEquals(0, c.aliases().toArray().length);
 	}
 
-	/*
-	 * Test the constructor with empty aliases.
-	 *
-	 */
-	public void testConstructor_EmptyAliases() {
-		try {
-			new MockCharset("mockChar", new String[] { "" });
-		} catch (IllegalCharsetNameException e) {
-                        // FIXME: Commented out since RI does throw IAE
-			// fail("Should not throw IllegalArgumentException!");
-		}
-	}
+  public void testConstructor_EmptyAliases() {
+    try {
+      new MockCharset("mockChar", new String[] { "" });
+      fail();
+    } catch (IllegalCharsetNameException expected) {
+    }
+  }
 
-	/*
-	 * Test the constructor with illegal aliases: starting with neither a digit
-	 * nor a letter.
-	 *
-	 */
-	public void testConstructor_IllegalAliases_Initial() {
-		try {
-			new MockCharset("mockChar", new String[] { "mock", "-123" });
-		} catch (IllegalCharsetNameException e) {
-			fail("Should not throw IllegalArgumentException!");
-		}
-	}
+  // Test the constructor with illegal aliases: starting with neither a digit nor a letter.
+  public void testConstructor_IllegalAliases_Initial() {
+    try {
+      new MockCharset("mockChar", new String[] { "mock", "-123" });
+      fail();
+    } catch (IllegalCharsetNameException e) {
+    }
+  }
 
 	/*
 	 * Test the constructor with illegal aliase, illegal character in the
diff --git a/luni/src/main/java/java/nio/charset/Charset.java b/luni/src/main/java/java/nio/charset/Charset.java
index 02cd42f..882cca7 100644
--- a/luni/src/main/java/java/nio/charset/Charset.java
+++ b/luni/src/main/java/java/nio/charset/Charset.java
@@ -175,10 +175,11 @@
      *             <code>aliases</code>.
      */
     protected Charset(String canonicalName, String[] aliases) {
-        // check whether the given canonical name is legal
+        // Check whether the given canonical name is legal.
         checkCharsetName(canonicalName);
         this.canonicalName = canonicalName;
-        // check each alias and put into a set
+
+        // Collect and check each unique alias.
         this.aliasesSet = new HashSet<String>();
         if (aliases != null) {
             for (String alias : aliases) {
@@ -192,15 +193,21 @@
         if (name.isEmpty()) {
             throw new IllegalCharsetNameException(name);
         }
-        int length = name.length();
-        for (int i = 0; i < length; ++i) {
-            if (!isValidCharsetNameCharacter(name.charAt(i))) {
+        if (!isValidCharsetNameStart(name.charAt(0))) {
+            throw new IllegalCharsetNameException(name);
+        }
+        for (int i = 1; i < name.length(); ++i) {
+            if (!isValidCharsetNamePart(name.charAt(i))) {
                 throw new IllegalCharsetNameException(name);
             }
         }
     }
 
-    private static boolean isValidCharsetNameCharacter(char c) {
+    private static boolean isValidCharsetNameStart(char c) {
+        return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
+    }
+
+    private static boolean isValidCharsetNamePart(char c) {
         return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
                 c == '-' || c == '.' || c == ':' || c == '_';
     }