1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package libcore.java.util; 17 18import java.io.IOException; 19import java.util.Calendar; 20import java.util.Formattable; 21import java.util.Formatter; 22import java.util.FormatterClosedException; 23import java.util.IllegalFormatException; 24import java.util.IllegalFormatFlagsException; 25import java.util.Locale; 26import junit.framework.TestCase; 27 28public final class OldFormatterTest extends TestCase { 29 30 public void test_Formattable() { 31 Formattable ones = new Formattable() { 32 public void formatTo(Formatter formatter, int flags, int width, int precision) throws IllegalFormatException { 33 try { 34 formatter.out().append("111"); 35 } catch (IOException e) { 36 throw new RuntimeException(e); 37 } 38 } 39 }; 40 Formattable twos = new Formattable() { 41 public void formatTo(Formatter formatter, int flags, int width, int precision) throws IllegalFormatException { 42 try { 43 formatter.out().append("222"); 44 } catch (IOException e) { 45 throw new RuntimeException(e); 46 } 47 } 48 }; 49 50 assertEquals("aaa 111?", new Formatter().format("aaa %s?", ones).toString()); 51 assertEquals("aaa 111 bbb 222?", new Formatter().format("aaa %s bbb %s?", ones, twos).toString()); 52 } 53 54 public void test_formatLjava_util_LocaleLjava_lang_StringLjava_lang_Object() { 55 Double val = new Double(3.14); 56 Calendar cal = Calendar.getInstance(); 57 Formatter fLoc = null; 58 Formatter fNoL = null; 59 cal.set(2008, Calendar.SEPTEMBER, 23, 18, 30); 60 61 fLoc = new Formatter(Locale.GERMAN); 62 fNoL = new Formatter(Locale.GERMAN); 63 fLoc.format(Locale.US, "%f", val); 64 fNoL.format("%f", val); 65 assertFalse(fLoc.toString().equals(fNoL.toString())); 66 67 fLoc = new Formatter(Locale.FRANCE); 68 fNoL = new Formatter(Locale.FRANCE); 69 fLoc.format(Locale.US, "%f", val); 70 fNoL.format("%f", val); 71 assertFalse(fLoc.toString().equals(fNoL.toString())); 72 73 fLoc = new Formatter(Locale.US); 74 fNoL = new Formatter(Locale.US); 75 fLoc.format(Locale.US, "%f", val); 76 fNoL.format("%f", val); 77 assertTrue(fLoc.toString().equals(fNoL.toString())); 78 79 fLoc = new Formatter(Locale.GERMAN); 80 fNoL = new Formatter(Locale.GERMAN); 81 fLoc.format(Locale.US, "%tA %tB %td %tT", cal, cal, cal, cal); 82 fNoL.format("%tA %tB %td %tT", cal, cal, cal, cal); 83 assertFalse(fLoc.toString().equals(fNoL.toString())); 84 85 fLoc = new Formatter(Locale.FRANCE); 86 fNoL = new Formatter(Locale.FRANCE); 87 fLoc.format(Locale.US, "%tA %tB %td %tT", cal, cal, cal, cal); 88 fNoL.format("%tA %tB %td %tT", cal, cal, cal, cal); 89 assertFalse(fLoc.toString().equals(fNoL.toString())); 90 91 fLoc = new Formatter(Locale.US); 92 fNoL = new Formatter(Locale.US); 93 fLoc.format(Locale.US, "%tA %tB %td %tT", cal, cal, cal, cal); 94 fNoL.format("%tA %tB %td %tT", cal, cal, cal, cal); 95 assertTrue(fLoc.toString().equals(fNoL.toString())); 96 97 final String[] illFlags = { "%+ e", "%+ E", "%+ g", "%+ G", "%+ f", 98 "%+ a", "%+ A", "%-03e", "%-03E", "%-03g", "%-03G", "%-03f", 99 "%-03a", "%-03A" }; 100 for (int i = 0; i < illFlags.length; i++) { 101 try { 102 fLoc = new Formatter(Locale.US); 103 fLoc.format(Locale.FRANCE, illFlags[i], 1.23d); 104 fail("should throw IllegalFormatFlagsException"); 105 } catch (IllegalFormatFlagsException expected) { 106 } 107 try { 108 fLoc = new Formatter(Locale.CANADA); 109 fLoc.format(Locale.GERMAN, illFlags[i], (Double) null); 110 fail("should throw IllegalFormatFlagsException"); 111 } catch (IllegalFormatFlagsException expected) { 112 } 113 } 114 115 fLoc.close(); 116 try { 117 fLoc.format(Locale.GERMAN, "%f", val); 118 fail(); 119 } catch (FormatterClosedException expected) { 120 } 121 } 122} 123