You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2014/04/21 16:51:30 UTC

svn commit: r1588892 - /maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/CaseTest.java

Author: hboutemy
Date: Mon Apr 21 14:51:30 2014
New Revision: 1588892

URL: http://svn.apache.org/r1588892
Log:
added case test to check and document corner cases with toLowerCase/toUpperCase

Added:
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/CaseTest.java   (with props)

Added: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/CaseTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/CaseTest.java?rev=1588892&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/CaseTest.java (added)
+++ maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/CaseTest.java Mon Apr 21 14:51:30 2014
@@ -0,0 +1,95 @@
+package org.apache.maven.shared.utils;
+
+/*
+ * 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.
+ */
+
+import java.util.Locale;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test case for character case changes, to precisely point the situations when character case comparison doesn't
+ * give intuitive result, or why one should avoid {@link String#toUpperCase()} and {@link String#toLowerCase()}
+ * (platform locale dependent, with sometimes unexpected results)
+ * but <b>prefer {@link String#equalsIgnoreCase(String)} when possible</b>.
+ * 
+ * @author Hervé Boutemy
+ * @see <a href="http://sim.ivi.co/2011/07/trap-of-case-insensitive-string.html">Simple Smiles - Xuelei Fan's Blog</a>
+ */
+public class CaseTest
+    extends Assert
+{
+    private final static Locale LOCALE_TURKISH = new Locale( "tr" );
+
+    /** common ASCII 'i' */
+    private final static char DOTTED_i = '\u0069';
+
+    /** common ASCII 'I' */
+    private final static char DOTLESS_I = '\u0049';
+
+    /** turkish dotless i = ı */
+    private final static char DOTLESS_i = '\u0131';
+
+    /** turkish dotted I = Ä° */
+    private final static char DOTTED_I = '\u0130';
+
+    /**
+     * test the known case of upper I which doesn't give commonly expected i in Turkish locale, but i (dotless i).
+     * @see <a href="http://mattryall.net/blog/2009/02/the-infamous-turkish-locale-bug">The infamous Turkish locale bug</a>
+     */
+    @Test
+    public void testTurkishI()
+    {
+        // check common i and I
+        assertEquals( "common lowercase i should have a dot", 'i', DOTTED_i );
+        assertEquals( "common uppercase I should not have a dot", 'I', DOTLESS_I );
+
+        final String iIıİ = "iIıİ";
+
+        // check source encoding doesn't wreck havoc */
+        assertEquals( "misc i directly in (UTF-8) source", iIıİ, "" + DOTTED_i + DOTLESS_I + DOTLESS_i + DOTTED_I );
+
+        // check toUpperCase and toLowerCase difference with turkish and english locales
+        assertEquals( "'iIıİ'.toUpperCase('tr')=='İIIİ'", "" + DOTTED_I + DOTLESS_I + DOTLESS_I + DOTTED_I,
+                      iIıİ.toUpperCase( LOCALE_TURKISH ) );
+        assertEquals( "'iIıİ'.toLowerCase('tr')=='iııi'", "" + DOTTED_i + DOTLESS_i + DOTLESS_i + DOTTED_i,
+                      iIıİ.toLowerCase( LOCALE_TURKISH ) );
+        assertEquals( "'iIıİ'.toUpperCase('en')=='IIIİ'", "" + DOTLESS_I + DOTLESS_I + DOTLESS_I + DOTTED_I,
+                      iIıİ.toUpperCase( Locale.ENGLISH ) );
+        assertEquals( "'iIıİ'.toLowerCase('en')=='iiıi'", "" + DOTTED_i + DOTTED_i + DOTLESS_i + DOTTED_i,
+                      iIıİ.toLowerCase( Locale.ENGLISH ) );
+
+        // check equalsIgnoreCase() , which has no locale
+        for ( int i = 0; i < iIıİ.length(); i++ )
+        {
+            char currentI = iIıİ.charAt( i );
+
+            StringBuilder sb = new StringBuilder( iIıİ.length() );
+            for ( int j = 0; j < iIıİ.length(); j++ )
+            {
+                sb.append( currentI );
+            }
+            String current = sb.toString();
+
+            assertTrue( "'" + current + "'.equalsIgnoreCase('" + iIıİ + "')", current.equalsIgnoreCase( iIıİ ) );
+        }
+    }
+
+}

Propchange: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/CaseTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/CaseTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/CaseTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain