tests/libjava-mauve/src/gnu/testlet/java/text/MessageFormat/attribute.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 /* attribute.java -- tests formatToCharacterIterator
       
     2    Copyright (C) 2003 Free Software Foundation
       
     3 
       
     4 This file is part of Mauve.
       
     5 
       
     6 Mauve is free software; you can redistribute it and/or modify
       
     7 it under the terms of the GNU General Public License as published by
       
     8 the Free Software Foundation; either version 2, or (at your option)
       
     9 any later version.
       
    10 
       
    11 Mauve is distributed in the hope that it will be useful, but
       
    12 WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14 General Public License for more details.
       
    15 
       
    16 You should have received a copy of the GNU General Public License
       
    17 along with Mauve; see the file COPYING.  If not, write to the
       
    18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
       
    19 02111-1307 USA.
       
    20 
       
    21 Linking this library statically or dynamically with other modules is
       
    22 making a combined work based on this library.  Thus, the terms and
       
    23 conditions of the GNU General Public License cover the whole
       
    24 combination.
       
    25 
       
    26 As a special exception, the copyright holders of this library give you
       
    27 permission to link this library with independent modules to produce an
       
    28 executable, regardless of the license terms of these independent
       
    29 modules, and to copy and distribute the resulting executable under
       
    30 terms of your choice, provided that you also meet, for each linked
       
    31 independent module, the terms and conditions of the license of that
       
    32 module.  An independent module is a module which is not derived from
       
    33 or based on this library.  If you modify this library, you may extend
       
    34 this exception to your version of the library, but you are not
       
    35 obligated to do so.  If you do not wish to do so, delete this
       
    36 exception statement from your version. */
       
    37 
       
    38 // TAGS: JDK1.4
       
    39 package gnu.testlet.java.text.MessageFormat;
       
    40 
       
    41 import gnu.testlet.Testlet;
       
    42 import gnu.testlet.TestHarness;
       
    43 import java.text.*;
       
    44 import java.math.BigInteger;
       
    45 import java.util.Map;
       
    46 
       
    47 public class attribute implements Testlet {
       
    48   private static class AttrTest {
       
    49     Object[] args;
       
    50     String expected;
       
    51     
       
    52     AttrTest(Object[] args, String expected)
       
    53     {
       
    54       this.args = args;
       
    55       this.expected = expected;
       
    56     }
       
    57   }
       
    58 
       
    59   private AttrTest[] tests = new AttrTest[] {
       
    60     new AttrTest(new Object[] {
       
    61       new Integer(10), "Hello !" }, "10 Hello !"),
       
    62     new AttrTest(new Object[] {
       
    63       new Float(70.1), "World !" }, "70.1 World !"),
       
    64     new AttrTest(new Object[] {
       
    65       new Float(70.1), new Integer(40) }, "70.1 40"),
       
    66     new AttrTest(new Object[] {
       
    67       new Float(70.1), new BigInteger("193289045") }, "70.1 193,289,045")    
       
    68   };
       
    69 
       
    70   final private void test_Basic(TestHarness harness)
       
    71   {
       
    72     MessageFormat format;
       
    73 
       
    74     try
       
    75       {
       
    76 	format = new MessageFormat("{0,number} {1}");
       
    77       }
       
    78     catch (Exception e)
       
    79       {
       
    80 	harness.debug(e);
       
    81 	harness.fail("Unexpected exception " + e);
       
    82 	return;
       
    83       }
       
    84     
       
    85     harness.checkPoint("null argument");
       
    86     try
       
    87       {
       
    88 	format.formatToCharacterIterator(null);
       
    89 	harness.debug("It should have thrown an exception here");
       
    90 	harness.check(false);
       
    91       }
       
    92     catch (NullPointerException _)
       
    93       {
       
    94 	harness.check(true); 
       
    95       }
       
    96     catch (Exception e)
       
    97       {
       
    98   	harness.debug(e);
       
    99 	harness.fail("Unexpected exception " + e);
       
   100       }
       
   101 
       
   102     harness.checkPoint("Illegal arguments");
       
   103     try
       
   104       {
       
   105 	format.formatToCharacterIterator
       
   106 	  (new Object[] { "Hello world !", "Hello 2" });
       
   107 	harness.check(false);
       
   108       }
       
   109     catch (IllegalArgumentException _)
       
   110       {
       
   111 	harness.check(true);
       
   112       }
       
   113     catch (Exception e)
       
   114       {
       
   115 	harness.debug(e);
       
   116 	harness.check(false, "unexpected exception");
       
   117       }
       
   118   }
       
   119   
       
   120   final private void test_StringBuild(TestHarness harness)
       
   121   {
       
   122     MessageFormat format;
       
   123 
       
   124     try
       
   125       {
       
   126 	format = new MessageFormat("{0,number} {1}");
       
   127       }
       
   128     catch (Exception e)
       
   129       {
       
   130 	harness.debug(e);
       
   131 	harness.fail("Unexpected exception " + e);
       
   132 	return;
       
   133       }
       
   134     
       
   135     harness.checkPoint("Valid arguments");
       
   136     for (int i = 0; i < tests.length; i++)
       
   137       {
       
   138 	try
       
   139 	  {
       
   140 	    AttributedCharacterIterator iterator =
       
   141 	      format.formatToCharacterIterator(tests[i].args);
       
   142 	
       
   143 	    harness.check(iteratorToString(iterator), tests[i].expected);
       
   144 	  }
       
   145 	catch (Exception e)
       
   146 	  {
       
   147 	    harness.debug(e);
       
   148 	    harness.check(false);
       
   149 	  }
       
   150       }
       
   151   }
       
   152 
       
   153   public void test(TestHarness harness)
       
   154   {
       
   155     test_Basic(harness);
       
   156     test_StringBuild(harness);
       
   157     test_Attributes(harness);
       
   158     test_FieldPos(harness);
       
   159   }
       
   160 
       
   161   final private void test_Attributes(TestHarness harness)
       
   162   { 
       
   163     harness.checkPoint("Attributes");
       
   164     try
       
   165       {
       
   166 	String before = "Original unmarked -- ";
       
   167 	String after = " -- marked";
       
   168 	String marked = "Hello world !";
       
   169 	String format_string = before + "{0}" + after;
       
   170 	MessageFormat format2 = new MessageFormat(format_string);
       
   171 	AttributedCharacterIterator iterator = 
       
   172 	  format2.formatToCharacterIterator(new Object[] { marked });
       
   173 	int[] range = new int[] { before.length(), marked.length() + before.length(),
       
   174 				  format_string.length() };
       
   175 	Object[] attrs = new Object[] { null, new Integer(0), null };
       
   176 	int i, j;
       
   177 	char c; 
       
   178 	for (c = iterator.first(), i = 0, j = 0; c != CharacterIterator.DONE; j++, c = iterator.next())
       
   179 	  {
       
   180 	    if (range[i] == j)
       
   181 	      i++;
       
   182 	    if (attrs[i] != null)
       
   183 	      {
       
   184 		Map m = iterator.getAttributes();
       
   185 		Object o = m.get(MessageFormat.Field.ARGUMENT);
       
   186 		
       
   187 		harness.check(o, attrs[i]);
       
   188 	      }
       
   189 	    else
       
   190 	      {
       
   191 		harness.check(iterator.getAttributes().get(MessageFormat.Field.ARGUMENT), null);
       
   192 	      }
       
   193 	  }
       
   194       }
       
   195     catch (Exception e)
       
   196       {
       
   197 	harness.debug(e);
       
   198 	harness.check(false);
       
   199       }
       
   200   }
       
   201 
       
   202   final private void test_FieldPos(TestHarness harness)
       
   203   {
       
   204     harness.checkPoint("Field position");
       
   205     try
       
   206       {
       
   207 	MessageFormat format = new MessageFormat("test field {0}");
       
   208 	FieldPosition pos = new FieldPosition(MessageFormat.Field.ARGUMENT);
       
   209 	StringBuffer output = new StringBuffer(25);
       
   210 
       
   211 	format.format(new Object[] { "position" }, output, pos);
       
   212 
       
   213 	harness.check(output.toString(), "test field position");
       
   214 	harness.check(pos.getBeginIndex(), 11);
       
   215 	harness.check(pos.getEndIndex(), 19);
       
   216       }
       
   217     catch (Exception e)
       
   218       {
       
   219 	harness.debug(e);
       
   220 	harness.check(false);
       
   221       }
       
   222   }
       
   223 
       
   224   private String iteratorToString(CharacterIterator iterator)
       
   225   {
       
   226     StringBuffer sb = new StringBuffer(iterator.getEndIndex()-iterator.getBeginIndex());
       
   227     
       
   228     for(char c = iterator.first(); c != CharacterIterator.DONE; c = iterator.next()) {
       
   229       sb.append(c);
       
   230     }
       
   231     
       
   232     return sb.toString();
       
   233   }
       
   234 }