1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.harmony.text.tests.java.text;
19
20import java.text.ChoiceFormat;
21import java.text.FieldPosition;
22import java.text.MessageFormat;
23import java.text.ParsePosition;
24
25import junit.framework.TestCase;
26
27public class ChoiceFormatTest extends TestCase {
28
29    double[] limits = new double[] { 0, 1, ChoiceFormat.nextDouble(1),
30            ChoiceFormat.nextDouble(2) };
31
32    String[] formats = new String[] { "Less than one", "one",
33            "Between one and two", "Greater than two" };
34
35    ChoiceFormat f1 = new ChoiceFormat(limits, formats);
36
37    /**
38     * @tests java.text.ChoiceFormat#ChoiceFormat(double[], java.lang.String[])
39     */
40    public void test_Constructor$D$Ljava_lang_String() {
41        // Test for method java.text.ChoiceFormat(double [], java.lang.String
42        // [])
43        String formattedString;
44        double[] appleLimits = { 1, 2, 3, 4, 5 };
45        String[] appleFormats = { "Tiny Apple", "Small Apple", "Medium Apple",
46                "Large Apple", "Huge Apple" };
47        ChoiceFormat cf = new ChoiceFormat(appleLimits, appleFormats);
48
49        formattedString = cf.format(Double.NEGATIVE_INFINITY);
50        assertTrue("a) Incorrect format returned: " + formattedString,
51                formattedString.equals("Tiny Apple"));
52        formattedString = cf.format(0.5d);
53        assertTrue("b) Incorrect format returned: " + formattedString,
54                formattedString.equals("Tiny Apple"));
55        formattedString = cf.format(1d);
56        assertTrue("c) Incorrect format returned: " + formattedString,
57                formattedString.equals("Tiny Apple"));
58        formattedString = cf.format(1.5d);
59        assertTrue("d) Incorrect format returned: " + formattedString,
60                formattedString.equals("Tiny Apple"));
61        formattedString = cf.format(2d);
62        assertTrue("e) Incorrect format returned: " + formattedString,
63                formattedString.equals("Small Apple"));
64        formattedString = cf.format(2.5d);
65        assertTrue("f) Incorrect format returned: " + formattedString,
66                formattedString.equals("Small Apple"));
67        formattedString = cf.format(3d);
68        assertTrue("g) Incorrect format returned: " + formattedString,
69                formattedString.equals("Medium Apple"));
70        formattedString = cf.format(4d);
71        assertTrue("h) Incorrect format returned: " + formattedString,
72                formattedString.equals("Large Apple"));
73        formattedString = cf.format(5d);
74        assertTrue("i) Incorrect format returned: " + formattedString,
75                formattedString.equals("Huge Apple"));
76        formattedString = cf.format(5.5d);
77        assertTrue("j) Incorrect format returned: " + formattedString,
78                formattedString.equals("Huge Apple"));
79        formattedString = cf.format(6.0d);
80        assertTrue("k) Incorrect format returned: " + formattedString,
81                formattedString.equals("Huge Apple"));
82        formattedString = cf.format(Double.POSITIVE_INFINITY);
83        assertTrue("l) Incorrect format returned: " + formattedString,
84                formattedString.equals("Huge Apple"));
85    }
86
87    /**
88     * @tests java.text.ChoiceFormat#ChoiceFormat(java.lang.String)
89     */
90    public void test_ConstructorLjava_lang_String() {
91        // Test for method java.text.ChoiceFormat(java.lang.String)
92        String formattedString;
93        String patternString = "-2#Inverted Orange| 0#No Orange| 0<Almost No Orange| 1#Normal Orange| 2#Expensive Orange";
94        ChoiceFormat cf = new ChoiceFormat(patternString);
95
96        formattedString = cf.format(Double.NEGATIVE_INFINITY);
97        assertTrue("a) Incorrect format returned: " + formattedString,
98                formattedString.equals("Inverted Orange"));
99        formattedString = cf.format(-3);
100        assertTrue("b) Incorrect format returned: " + formattedString,
101                formattedString.equals("Inverted Orange"));
102        formattedString = cf.format(-2);
103        assertTrue("c) Incorrect format returned: " + formattedString,
104                formattedString.equals("Inverted Orange"));
105        formattedString = cf.format(-1);
106        assertTrue("d) Incorrect format returned: " + formattedString,
107                formattedString.equals("Inverted Orange"));
108        formattedString = cf.format(-0);
109        assertTrue("e) Incorrect format returned: " + formattedString,
110                formattedString.equals("No Orange"));
111        formattedString = cf.format(0);
112        assertTrue("f) Incorrect format returned: " + formattedString,
113                formattedString.equals("No Orange"));
114        formattedString = cf.format(0.1);
115        assertTrue("g) Incorrect format returned: " + formattedString,
116                formattedString.equals("Almost No Orange"));
117        formattedString = cf.format(1);
118        assertTrue("h) Incorrect format returned: " + formattedString,
119                formattedString.equals("Normal Orange"));
120        formattedString = cf.format(1.5);
121        assertTrue("i) Incorrect format returned: " + formattedString,
122                formattedString.equals("Normal Orange"));
123        formattedString = cf.format(2);
124        assertTrue("j) Incorrect format returned: " + formattedString,
125                formattedString.equals("Expensive Orange"));
126        formattedString = cf.format(3);
127        assertTrue("k) Incorrect format returned: " + formattedString,
128                formattedString.equals("Expensive Orange"));
129        formattedString = cf.format(Double.POSITIVE_INFINITY);
130        assertTrue("l) Incorrect format returned: " + formattedString,
131                formattedString.equals("Expensive Orange"));
132
133    }
134
135    /**
136     * @tests java.text.ChoiceFormat#applyPattern(java.lang.String)
137     */
138    public void test_applyPatternLjava_lang_String() {
139        // Test for method void
140        // java.text.ChoiceFormat.applyPattern(java.lang.String)
141        ChoiceFormat f = (ChoiceFormat) f1.clone();
142        f.applyPattern("0#0|1#1");
143        assertTrue("Incorrect limits", java.util.Arrays.equals(f.getLimits(),
144                new double[] { 0, 1 }));
145        assertTrue("Incorrect formats", java.util.Arrays.equals(f.getFormats(),
146                new String[] { "0", "1" }));
147
148        //Regression for Harmony 540
149        double[] choiceLimits = { -1, 0, 1, ChoiceFormat.nextDouble(1) };
150        String[] choiceFormats = { "is negative", "is zero or fraction",
151                "is one", "is more than 1" };
152
153        f = new ChoiceFormat("");
154        f.applyPattern("-1#is negative|0#is zero or fraction|1#is one|1<is more than 1");
155        assertTrue("Incorrect limits", java.util.Arrays.equals(f.getLimits(),
156                choiceLimits));
157        assertTrue("Incorrect formats", java.util.Arrays.equals(f.getFormats(),
158                choiceFormats));
159
160        f = new ChoiceFormat("");
161        try {
162            f.applyPattern("-1#is negative|0#is zero or fraction|-1#is one|1<is more than 1");
163            fail("Expected IllegalArgumentException");
164        } catch (IllegalArgumentException e) {
165            // Expected
166        }
167
168        f = new ChoiceFormat("");
169        try {
170            f.applyPattern("-1is negative|0#is zero or fraction|1#is one|1<is more than 1");
171            fail("Expected IllegalArgumentException");
172        } catch (IllegalArgumentException e) {
173            // Expected
174        }
175
176        f = new ChoiceFormat("");
177        f.applyPattern("-1<is negative|0#is zero or fraction|1#is one|1<is more than 1");
178        choiceLimits[0] = ChoiceFormat.nextDouble(-1);
179        assertTrue("Incorrect limits", java.util.Arrays.equals(f.getLimits(),
180                choiceLimits));
181        assertTrue("Incorrect formats", java.util.Arrays.equals(f.getFormats(),
182                choiceFormats));
183
184        f = new ChoiceFormat("");
185        f.applyPattern("-1#is negative|0#is zero or fraction|1#is one|1<is more than 1");
186        String str = "org.apache.harmony.tests.java.text.ChoiceFormat";
187        f.applyPattern(str);
188        String ptrn = f.toPattern();
189        assertEquals("Return value should be empty string for invalid pattern",
190                0, ptrn.length());
191    }
192
193    /**
194     * @tests java.text.ChoiceFormat#clone()
195     */
196    public void test_clone() {
197        // Test for method java.lang.Object java.text.ChoiceFormat.clone()
198        ChoiceFormat f = (ChoiceFormat) f1.clone();
199        assertTrue("Not equal", f.equals(f1));
200        f.setChoices(new double[] { 0, 1, 2 }, new String[] { "0", "1", "2" });
201        assertTrue("Equal", !f.equals(f1));
202    }
203
204    /**
205     * @tests java.text.ChoiceFormat#equals(java.lang.Object)
206     */
207    public void test_equalsLjava_lang_Object() {
208        // Test for method boolean
209        // java.text.ChoiceFormat.equals(java.lang.Object)
210
211        String patternString = "-2#Inverted Orange| 0#No Orange| 0<Almost No Orange| 1#Normal Orange| 2#Expensive Orange";
212        double[] appleLimits = { 1, 2, 3, 4, 5 };
213        String[] appleFormats = { "Tiny Apple", "Small Apple", "Medium Apple",
214                "Large Apple", "Huge Apple" };
215        double[] orangeLimits = { -2, 0, ChoiceFormat.nextDouble(0), 1, 2 };
216        String[] orangeFormats = { "Inverted Orange", "No Orange",
217                "Almost No Orange", "Normal Orange", "Expensive Orange" };
218
219        ChoiceFormat appleChoiceFormat = new ChoiceFormat(appleLimits,
220                appleFormats);
221        ChoiceFormat orangeChoiceFormat = new ChoiceFormat(orangeLimits,
222                orangeFormats);
223        ChoiceFormat orangeChoiceFormat2 = new ChoiceFormat(patternString);
224        ChoiceFormat hybridChoiceFormat = new ChoiceFormat(appleLimits,
225                orangeFormats);
226
227        assertTrue("Apples should not equal oranges", !appleChoiceFormat
228                .equals(orangeChoiceFormat));
229        assertTrue("Different limit list--should not appear as equal",
230                !orangeChoiceFormat.equals(hybridChoiceFormat));
231        assertTrue("Different format list--should not appear as equal",
232                !appleChoiceFormat.equals(hybridChoiceFormat));
233        assertTrue("Should be equal--identical format", appleChoiceFormat
234                .equals(appleChoiceFormat));
235        assertTrue("Should be equals--same limits, same formats",
236                orangeChoiceFormat.equals(orangeChoiceFormat2));
237
238        ChoiceFormat f2 = new ChoiceFormat(
239                "0#Less than one|1#one|1<Between one and two|2<Greater than two");
240        assertTrue("Not equal", f1.equals(f2));
241    }
242
243    /**
244     * @tests java.text.ChoiceFormat#format(double, java.lang.StringBuffer,
245     *        java.text.FieldPosition)
246     */
247    public void test_formatDLjava_lang_StringBufferLjava_text_FieldPosition() {
248        // Test for method java.lang.StringBuffer
249        // java.text.ChoiceFormat.format(double, java.lang.StringBuffer,
250        // java.text.FieldPosition)
251        FieldPosition field = new FieldPosition(0);
252        StringBuffer buf = new StringBuffer();
253        String r = f1.format(-1, buf, field).toString();
254		assertEquals("Wrong choice for -1", "Less than one", r);
255        buf.setLength(0);
256        r = f1.format(0, buf, field).toString();
257		assertEquals("Wrong choice for 0", "Less than one", r);
258        buf.setLength(0);
259        r = f1.format(1, buf, field).toString();
260		assertEquals("Wrong choice for 1", "one", r);
261        buf.setLength(0);
262        r = f1.format(2, buf, field).toString();
263		assertEquals("Wrong choice for 2", "Between one and two", r);
264        buf.setLength(0);
265        r = f1.format(3, buf, field).toString();
266		assertEquals("Wrong choice for 3", "Greater than two", r);
267
268        // Regression test for HARMONY-1081
269        assertEquals(0, new ChoiceFormat("|").format(Double.NaN, new StringBuffer(), new FieldPosition(6)).length());
270        assertEquals(0, new ChoiceFormat("|").format(1, new StringBuffer(), new FieldPosition(6)).length());
271        assertEquals("Less than one", f1.format(Double.NaN, new StringBuffer(), field).toString());
272    }
273
274    /**
275     * @tests java.text.ChoiceFormat#format(long, java.lang.StringBuffer,
276     *        java.text.FieldPosition)
277     */
278    public void test_formatJLjava_lang_StringBufferLjava_text_FieldPosition() {
279        // Test for method java.lang.StringBuffer
280        // java.text.ChoiceFormat.format(long, java.lang.StringBuffer,
281        // java.text.FieldPosition)
282        FieldPosition field = new FieldPosition(0);
283        StringBuffer buf = new StringBuffer();
284        String r = f1.format(0.5, buf, field).toString();
285		assertEquals("Wrong choice for 0.5", "Less than one", r);
286        buf.setLength(0);
287        r = f1.format(1.5, buf, field).toString();
288		assertEquals("Wrong choice for 1.5", "Between one and two", r);
289        buf.setLength(0);
290        r = f1.format(2.5, buf, field).toString();
291		assertEquals("Wrong choice for 2.5", "Greater than two", r);
292    }
293
294    /**
295     * @tests java.text.ChoiceFormat#getFormats()
296     */
297    public void test_getFormats() {
298        // Test for method java.lang.Object []
299        // java.text.ChoiceFormat.getFormats()
300        String[] orgFormats = (String[]) formats.clone();
301        String[] f = (String[]) f1.getFormats();
302        assertTrue("Wrong formats", f.equals(formats));
303        f[0] = "Modified";
304        assertTrue("Formats copied", !f.equals(orgFormats));
305    }
306
307    /**
308     * @tests java.text.ChoiceFormat#getLimits()
309     */
310    public void test_getLimits() {
311        // Test for method double [] java.text.ChoiceFormat.getLimits()
312        double[] orgLimits = (double[]) limits.clone();
313        double[] l = f1.getLimits();
314        assertTrue("Wrong limits", l.equals(limits));
315        l[0] = 3.14527;
316        assertTrue("Limits copied", !l.equals(orgLimits));
317    }
318
319    /**
320     * @tests java.text.ChoiceFormat#hashCode()
321     */
322    public void test_hashCode() {
323        // Test for method int java.text.ChoiceFormat.hashCode()
324        ChoiceFormat f2 = new ChoiceFormat(
325                "0#Less than one|1#one|1<Between one and two|2<Greater than two");
326        assertTrue("Different hash", f1.hashCode() == f2.hashCode());
327    }
328
329    /**
330     * @tests java.text.ChoiceFormat#nextDouble(double)
331     */
332    public void test_nextDoubleD() {
333        // Test for method double java.text.ChoiceFormat.nextDouble(double)
334        assertTrue("Not greater 5", ChoiceFormat.nextDouble(5) > 5);
335        assertTrue("Not greater 0", ChoiceFormat.nextDouble(0) > 0);
336        assertTrue("Not greater -5", ChoiceFormat.nextDouble(-5) > -5);
337        assertTrue("Not NaN", Double.isNaN(ChoiceFormat.nextDouble(Double.NaN)));
338    }
339
340    /**
341     * @tests java.text.ChoiceFormat#nextDouble(double, boolean)
342     */
343    public void test_nextDoubleDZ() {
344        // Test for method double java.text.ChoiceFormat.nextDouble(double,
345        // boolean)
346        assertTrue("Not greater 0", ChoiceFormat.nextDouble(0, true) > 0);
347        assertTrue("Not less 0", ChoiceFormat.nextDouble(0, false) < 0);
348    }
349
350    /**
351     * @tests java.text.ChoiceFormat#parse(java.lang.String,
352     *        java.text.ParsePosition)
353     */
354    public void test_parseLjava_lang_StringLjava_text_ParsePosition() {
355        // Test for method java.lang.Number
356        // java.text.ChoiceFormat.parse(java.lang.String,
357        // java.text.ParsePosition)
358        ChoiceFormat format = new ChoiceFormat("1#one|2#two|3#three");
359		assertEquals("Case insensitive", 0, format
360				.parse("One", new ParsePosition(0)).intValue());
361
362        ParsePosition pos = new ParsePosition(0);
363        Number result = f1.parse("Greater than two", pos);
364        assertTrue("Not a Double1", result instanceof Double);
365        assertTrue("Wrong value ~>2", result.doubleValue() == ChoiceFormat
366                .nextDouble(2));
367		assertEquals("Wrong position ~16", 16, pos.getIndex());
368        pos = new ParsePosition(0);
369        assertTrue("Incorrect result", Double.isNaN(f1.parse("12one", pos)
370                .doubleValue()));
371		assertEquals("Wrong position ~0", 0, pos.getIndex());
372        pos = new ParsePosition(2);
373        result = f1.parse("12one and two", pos);
374        assertTrue("Not a Double2", result instanceof Double);
375		assertEquals("Ignored parse position", 1.0D, result.doubleValue(), 0.0D);
376		assertEquals("Wrong position ~5", 5, pos.getIndex());
377    }
378
379    /**
380     * @tests java.text.ChoiceFormat#previousDouble(double)
381     */
382    public void test_previousDoubleD() {
383        // Test for method double java.text.ChoiceFormat.previousDouble(double)
384        assertTrue("Not less 5", ChoiceFormat.previousDouble(5) < 5);
385        assertTrue("Not less 0", ChoiceFormat.previousDouble(0) < 0);
386        assertTrue("Not less -5", ChoiceFormat.previousDouble(-5) < -5);
387        assertTrue("Not NaN", Double.isNaN(ChoiceFormat
388                .previousDouble(Double.NaN)));
389    }
390
391    /**
392     * @tests java.text.ChoiceFormat#setChoices(double[], java.lang.String[])
393     */
394    public void test_setChoices$D$Ljava_lang_String() {
395        // Test for method void java.text.ChoiceFormat.setChoices(double [],
396        // java.lang.String [])
397        ChoiceFormat f = (ChoiceFormat) f1.clone();
398        double[] l = new double[] { 0, 1 };
399        String[] fs = new String[] { "0", "1" };
400        f.setChoices(l, fs);
401        assertTrue("Limits copied", f.getLimits() == l);
402        assertTrue("Formats copied", f.getFormats() == fs);
403    }
404
405	/**
406	 * @tests java.text.ChoiceFormat#toPattern()
407	 */
408	public void test_toPattern() {
409		// Regression for HARMONY-59
410		ChoiceFormat cf = new ChoiceFormat("");
411		assertEquals("", cf.toPattern());
412
413		cf = new ChoiceFormat("-1#NEGATIVE_ONE|0#ZERO|1#ONE|1<GREATER_THAN_ONE");
414		assertEquals("-1.0#NEGATIVE_ONE|0.0#ZERO|1.0#ONE|1.0<GREATER_THAN_ONE",
415				cf.toPattern());
416
417        MessageFormat mf = new MessageFormat("CHOICE {1,choice}");
418        String ptrn = mf.toPattern();
419		assertEquals("Unused message format returning incorrect pattern", "CHOICE {1,choice,}", ptrn
420				);
421
422        String pattern = f1.toPattern();
423        assertTrue(
424                "Wrong pattern: " + pattern,
425                pattern
426                        .equals("0.0#Less than one|1.0#one|1.0<Between one and two|2.0<Greater than two"));
427
428        cf = new ChoiceFormat(
429                "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+|2#is two |2<is more than 2.");
430        String str = "org.apache.harmony.tests.java.lang.share.MyResources2";
431        cf.applyPattern(str);
432        ptrn = cf.toPattern();
433		assertEquals("Return value should be empty string for invalid pattern",
434				0, ptrn.length());
435	}
436
437	/**
438	 * @tests java.text.ChoiceFormat#format(long)
439	 */
440	public void test_formatL() {
441		ChoiceFormat fmt = new ChoiceFormat(
442				"-1#NEGATIVE_ONE|0#ZERO|1#ONE|1<GREATER_THAN_ONE");
443
444		assertEquals("NEGATIVE_ONE", fmt.format(Long.MIN_VALUE));
445		assertEquals("NEGATIVE_ONE", fmt.format(-1));
446		assertEquals("ZERO", fmt.format(0));
447		assertEquals("ONE", fmt.format(1));
448		assertEquals("GREATER_THAN_ONE", fmt.format(Long.MAX_VALUE));
449	}
450
451	/**
452	 * @tests java.text.ChoiceFormat#format(double)
453	 */
454	public void test_formatD() {
455		ChoiceFormat fmt = new ChoiceFormat(
456				"-1#NEGATIVE_ONE|0#ZERO|1#ONE|1<GREATER_THAN_ONE");
457		assertEquals("NEGATIVE_ONE", fmt.format(Double.NEGATIVE_INFINITY));
458		assertEquals("NEGATIVE_ONE", fmt.format(-999999999D));
459		assertEquals("NEGATIVE_ONE", fmt.format(-1.1));
460		assertEquals("NEGATIVE_ONE", fmt.format(-1.0));
461		assertEquals("NEGATIVE_ONE", fmt.format(-0.9));
462		assertEquals("ZERO", fmt.format(0.0));
463		assertEquals("ZERO", fmt.format(0.9));
464		assertEquals("ONE", fmt.format(1.0));
465		assertEquals("GREATER_THAN_ONE", fmt.format(1.1));
466		assertEquals("GREATER_THAN_ONE", fmt.format(999999999D));
467		assertEquals("GREATER_THAN_ONE", fmt.format(Double.POSITIVE_INFINITY));
468	}
469}
470