logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattsic...@apache.org
Subject svn commit: r1615837 - in /logging/log4j/log4j2/trunk/log4j-core/src: main/java/org/apache/logging/log4j/core/util/Charsets.java test/java/org/apache/logging/log4j/core/util/StandardCharsetsTest.java
Date Tue, 05 Aug 2014 01:48:51 GMT
Author: mattsicker
Date: Tue Aug  5 01:48:51 2014
New Revision: 1615837

URL: http://svn.apache.org/r1615837
Log:
Backport StandardCharsets from JDK 1.7.
Add tests to verify presence of standard character sets.

Added:
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/util/StandardCharsetsTest.java
Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Charsets.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Charsets.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Charsets.java?rev=1615837&r1=1615836&r2=1615837&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Charsets.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Charsets.java
Tue Aug  5 01:48:51 2014
@@ -21,16 +21,44 @@ import java.nio.charset.Charset;
 import org.apache.logging.log4j.status.StatusLogger;
 
 /**
- * Charset utilities.
+ * Charset utilities. Contains the standard character sets guaranteed to be available on
all implementations of the
+ * Java platform. Parts adapted from JDK 1.7 (in particular, the {@code java.nio.charset.StandardCharsets}
class).
+ *
+ * @see java.nio.charset.Charset
  */
 public final class Charsets {
 
     /**
-     * UTF-8 Charset.
+     * Seven-bit ASCII. ISO646-US. The Basic Latin block of the Unicode character set.
+     */
+    public static final Charset US_ASCII = Charset.forName("US-ASCII");
+
+    /**
+     * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
+     */
+    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
+
+    /**
+     * Eight-bit UCS Transformation Format.
      */
     public static final Charset UTF_8 = Charset.forName("UTF-8");
 
     /**
+     * Sixteen-bit UCS Transformation Format, big-endian byte order.
+     */
+    public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
+
+    /**
+     * Sixteen-bit UCS Transformation Format, little-endian byte order.
+     */
+    public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
+
+    /**
+     * Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order
mark.
+     */
+    public static final Charset UTF_16 = Charset.forName("UTF-16");
+
+    /**
      * Returns a Charset, if possible the Charset for the specified {@code charsetName},
otherwise (if the specified
      * {@code charsetName} is {@code null} or not supported) this method returns the platform
default Charset.
      *

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/util/StandardCharsetsTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/util/StandardCharsetsTest.java?rev=1615837&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/util/StandardCharsetsTest.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/util/StandardCharsetsTest.java
Tue Aug  5 01:48:51 2014
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.util;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.junit.Assert.*;
+
+@RunWith(Parameterized.class)
+public class StandardCharsetsTest {
+
+    private final String charsetName;
+    private final Charset expectedCharset;
+
+    public StandardCharsetsTest(final String charsetName, final Charset expectedCharset)
{
+        this.charsetName = charsetName;
+        this.expectedCharset = expectedCharset;
+    }
+
+    @Parameterized.Parameters
+    public static Collection<Object[]> data() {
+        return Arrays.asList(
+            new Object[][]{
+                { "US-ASCII", Charsets.US_ASCII },
+                { "ISO-8859-1", Charsets.ISO_8859_1 },
+                { "UTF-8", Charsets.UTF_8 },
+                { "UTF-16BE", Charsets.UTF_16BE },
+                { "UTF-16LE", Charsets.UTF_16LE },
+                { "UTF-16", Charsets.UTF_16 }
+            }
+        );
+    }
+
+    @Test
+    public void testSupportsStandardCharset() throws Exception {
+        assertTrue(Charset.isSupported(charsetName));
+        assertSame(expectedCharset, Charsets.getSupportedCharset(charsetName));
+    }
+
+    @Test
+    public void testCharsetTranslatesProperly() throws Exception {
+        final String expected = "This string contains only ASCII characters to test all the
standard charsets";
+        final ByteBuffer encoded = expectedCharset.encode(expected);
+        final CharBuffer decoded = expectedCharset.decode(encoded);
+        final String actual = decoded.toString();
+        assertEquals(expected, actual);
+    }
+}
\ No newline at end of file



Mime
View raw message