tests/libjava-mauve/src/gnu/testlet/java/text/SimpleDateFormat/Localization.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.2
       
     2 
       
     3 // Copyright (C) 2004 Andrew John Hughes  <gnu_andrew@member.fsf.org>
       
     4 
       
     5 // This file is part of Mauve.
       
     6 
       
     7 // Mauve is free software; you can redistribute it and/or modify
       
     8 // it under the terms of the GNU General Public License as published by
       
     9 // the Free Software Foundation; either version 2, or (at your option)
       
    10 // any later version.
       
    11 
       
    12 // Mauve is distributed in the hope that it will be useful,
       
    13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 // GNU General Public License for more details.
       
    16 
       
    17 // You should have received a copy of the GNU General Public License
       
    18 // along with Mauve; see the file COPYING.  If not, write to
       
    19 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    20 // Boston, MA 02111-1307, USA.  */
       
    21 
       
    22 package gnu.testlet.java.text.SimpleDateFormat;
       
    23 
       
    24 import gnu.testlet.TestHarness;
       
    25 import gnu.testlet.Testlet;
       
    26 
       
    27 import java.util.Locale;
       
    28 import java.text.SimpleDateFormat;
       
    29 
       
    30 /**
       
    31  * Check for correct cloning behaviour in the SimpleDateFormat
       
    32  * class.
       
    33  */
       
    34 public class Localization
       
    35   implements Testlet
       
    36 {
       
    37 
       
    38   /**
       
    39    * Runs the test using the specified harness.
       
    40    * 
       
    41    * @param harness the test harness (<code>null</code> not allowed).
       
    42    */
       
    43   public void test(TestHarness harness)
       
    44   {
       
    45     SimpleDateFormat format = null;
       
    46     String standard = "GyMdkHmsSEDFwWahKzYeugAZ";
       
    47     String pattern = "EEE, d MMM yyyy HH:mm:ss Z";
       
    48     Locale locale = Locale.GERMAN;
       
    49     harness.checkPoint("German locale, standard pattern characters " +
       
    50 		       "in pattern.");
       
    51     try
       
    52       {
       
    53 	format = new SimpleDateFormat(pattern, locale);
       
    54 	harness.check(true);
       
    55       }
       
    56     catch (IllegalArgumentException e)
       
    57       {
       
    58 	harness.debug(e);
       
    59 	harness.check(false);
       
    60       }
       
    61     String local = format.getDateFormatSymbols().getLocalPatternChars();
       
    62     harness.check(format.toPattern(), pattern, "Non-localized pattern " +
       
    63 		  "comes back as is with toPattern().");
       
    64     String localizedPattern = translateLocalizedPattern(pattern,
       
    65 							standard,
       
    66 							local);
       
    67     harness.check(format.toLocalizedPattern(), localizedPattern,
       
    68 		  "Non-localized pattern comes back localized with " +
       
    69 		  "toLocalizedPattern().");
       
    70     harness.checkPoint("German locale, German pattern characters in pattern.");
       
    71     format = null;
       
    72     try
       
    73       {
       
    74 	format = new SimpleDateFormat(localizedPattern, locale);
       
    75 	harness.check(false);
       
    76       }
       
    77     catch (IllegalArgumentException e)
       
    78       {
       
    79 	harness.check(true);
       
    80       }
       
    81     try
       
    82       {
       
    83 	format = new SimpleDateFormat(pattern, locale);
       
    84 	format.applyLocalizedPattern(localizedPattern);
       
    85 	harness.check(true);
       
    86       }
       
    87     catch (IllegalArgumentException e)
       
    88       {
       
    89 	harness.debug(e);
       
    90 	harness.check(false);
       
    91       }
       
    92     local = format.getDateFormatSymbols().getLocalPatternChars();
       
    93     harness.check(format.toLocalizedPattern(), localizedPattern,
       
    94 		  "Localized pattern comes back as is with " +
       
    95 		  "toLocalizedPattern().");
       
    96     harness.check(format.toPattern(), pattern,
       
    97 		  "Localized pattern comes back standardised with " +
       
    98 		  "toPattern().");
       
    99   }
       
   100 
       
   101   /* Taken from GNU Classpath's java.text.SimpleDateFormat */
       
   102   // Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004 Free Software
       
   103   // Foundation, Inc.
       
   104   /* This version has been altered to account for differing char lengths */
       
   105 
       
   106   /**
       
   107    * Translates either from or to a localized variant of the pattern
       
   108    * string.  For example, in the German locale, 't' (for 'tag') is
       
   109    * used instead of 'd' (for 'date').  This method translates
       
   110    * a localized pattern (such as 'ttt') to a non-localized pattern
       
   111    * (such as 'ddd'), or vice versa.  Non-localized patterns use
       
   112    * a standard set of characters, which match those of the U.S. English
       
   113    * locale.
       
   114    *
       
   115    * @param pattern the pattern to translate.
       
   116    * @param oldChars the old set of characters (used in the pattern).
       
   117    * @param newChars the new set of characters (which will be used in the
       
   118    *                 pattern).
       
   119    * @return a version of the pattern using the characters in
       
   120    *         <code>newChars</code>.
       
   121    */
       
   122   private String translateLocalizedPattern(String pattern,
       
   123 					   String oldChars, String newChars)
       
   124   {
       
   125     int len = pattern.length();
       
   126     StringBuffer buf = new StringBuffer(len);
       
   127     boolean quoted = false;
       
   128     for (int i = 0;  i < len;  i++)
       
   129       {
       
   130 	char ch = pattern.charAt(i);
       
   131 	if (ch == '\'')
       
   132 	  quoted = ! quoted;
       
   133 	if (! quoted)
       
   134 	  {
       
   135 	    int j = oldChars.indexOf(ch);
       
   136 	    if ((j >= 0) && j < newChars.length()) 
       
   137 	      ch = newChars.charAt(j);
       
   138 	  }
       
   139 	buf.append(ch);
       
   140       }
       
   141     return buf.toString();
       
   142   }
       
   143 
       
   144 }