/* * 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. */ package org.apache.harmony.tests.java.text; import java.text.ChoiceFormat; import java.text.FieldPosition; import java.text.MessageFormat; import java.text.ParsePosition; import java.util.Locale; import junit.framework.TestCase; public class ChoiceFormatTest extends TestCase { double[] limits = new double[] { 0, 1, ChoiceFormat.nextDouble(1), ChoiceFormat.nextDouble(2) }; String[] formats = new String[] { "Less than one", "one", "Between one and two", "Greater than two" }; ChoiceFormat f1 = new ChoiceFormat(limits, formats); /** * @tests java.text.ChoiceFormat#ChoiceFormat(double[], java.lang.String[]) */ public void test_Constructor$D$Ljava_lang_String() { // Test for method java.text.ChoiceFormat(double [], java.lang.String // []) String formattedString; double[] appleLimits = { 1, 2, 3, 4, 5 }; String[] appleFormats = { "Tiny Apple", "Small Apple", "Medium Apple", "Large Apple", "Huge Apple" }; ChoiceFormat cf = new ChoiceFormat(appleLimits, appleFormats); formattedString = cf.format(Double.NEGATIVE_INFINITY); assertTrue("a) Incorrect format returned: " + formattedString, formattedString.equals("Tiny Apple")); formattedString = cf.format(0.5d); assertTrue("b) Incorrect format returned: " + formattedString, formattedString.equals("Tiny Apple")); formattedString = cf.format(1d); assertTrue("c) Incorrect format returned: " + formattedString, formattedString.equals("Tiny Apple")); formattedString = cf.format(1.5d); assertTrue("d) Incorrect format returned: " + formattedString, formattedString.equals("Tiny Apple")); formattedString = cf.format(2d); assertTrue("e) Incorrect format returned: " + formattedString, formattedString.equals("Small Apple")); formattedString = cf.format(2.5d); assertTrue("f) Incorrect format returned: " + formattedString, formattedString.equals("Small Apple")); formattedString = cf.format(3d); assertTrue("g) Incorrect format returned: " + formattedString, formattedString.equals("Medium Apple")); formattedString = cf.format(4d); assertTrue("h) Incorrect format returned: " + formattedString, formattedString.equals("Large Apple")); formattedString = cf.format(5d); assertTrue("i) Incorrect format returned: " + formattedString, formattedString.equals("Huge Apple")); formattedString = cf.format(5.5d); assertTrue("j) Incorrect format returned: " + formattedString, formattedString.equals("Huge Apple")); formattedString = cf.format(6.0d); assertTrue("k) Incorrect format returned: " + formattedString, formattedString.equals("Huge Apple")); formattedString = cf.format(Double.POSITIVE_INFINITY); assertTrue("l) Incorrect format returned: " + formattedString, formattedString.equals("Huge Apple")); } /** * @tests java.text.ChoiceFormat#ChoiceFormat(java.lang.String) */ public void test_ConstructorLjava_lang_String() { // Test for method java.text.ChoiceFormat(java.lang.String) String formattedString; String patternString = "-2#Inverted Orange| 0#No Orange| 0 5); assertTrue("Not greater 0", ChoiceFormat.nextDouble(0) > 0); assertTrue("Not greater -5", ChoiceFormat.nextDouble(-5) > -5); assertTrue("Not NaN", Double.isNaN(ChoiceFormat.nextDouble(Double.NaN))); } /** * @tests java.text.ChoiceFormat#nextDouble(double, boolean) */ public void test_nextDoubleDZ() { // Test for method double java.text.ChoiceFormat.nextDouble(double, // boolean) assertTrue("Not greater 0", ChoiceFormat.nextDouble(0, true) > 0); assertTrue("Not less 0", ChoiceFormat.nextDouble(0, false) < 0); } /** * @tests java.text.ChoiceFormat#parse(java.lang.String, * java.text.ParsePosition) */ public void test_parseLjava_lang_StringLjava_text_ParsePosition() { // Test for method java.lang.Number // java.text.ChoiceFormat.parse(java.lang.String, // java.text.ParsePosition) ChoiceFormat format = new ChoiceFormat("1#one|2#two|3#three"); assertEquals("Case insensitive", 0, format .parse("One", new ParsePosition(0)).intValue()); ParsePosition pos = new ParsePosition(0); Number result = f1.parse("Greater than two", pos); assertTrue("Not a Double1", result instanceof Double); assertTrue("Wrong value ~>2", result.doubleValue() == ChoiceFormat .nextDouble(2)); assertEquals("Wrong position ~16", 16, pos.getIndex()); pos = new ParsePosition(0); assertTrue("Incorrect result", Double.isNaN(f1.parse("12one", pos) .doubleValue())); assertEquals("Wrong position ~0", 0, pos.getIndex()); pos = new ParsePosition(2); result = f1.parse("12one and two", pos); assertTrue("Not a Double2", result instanceof Double); assertEquals("Ignored parse position", 1.0D, result.doubleValue(), 0.0D); assertEquals("Wrong position ~5", 5, pos.getIndex()); } /** * @tests java.text.ChoiceFormat#previousDouble(double) */ public void test_previousDoubleD() { // Test for method double java.text.ChoiceFormat.previousDouble(double) assertTrue("Not less 5", ChoiceFormat.previousDouble(5) < 5); assertTrue("Not less 0", ChoiceFormat.previousDouble(0) < 0); assertTrue("Not less -5", ChoiceFormat.previousDouble(-5) < -5); assertTrue("Not NaN", Double.isNaN(ChoiceFormat .previousDouble(Double.NaN))); } /** * @tests java.text.ChoiceFormat#setChoices(double[], java.lang.String[]) */ public void test_setChoices$D$Ljava_lang_String() { // Test for method void java.text.ChoiceFormat.setChoices(double [], // java.lang.String []) ChoiceFormat f = (ChoiceFormat) f1.clone(); double[] l = new double[] { 0, 1 }; String[] fs = new String[] { "0", "1" }; f.setChoices(l, fs); assertTrue("Limits copied", f.getLimits() == l); assertTrue("Formats copied", f.getFormats() == fs); } /** * @tests java.text.ChoiceFormat#toPattern() */ public void test_toPattern() { // Regression for HARMONY-59 ChoiceFormat cf = new ChoiceFormat(""); assertEquals("", cf.toPattern()); cf = new ChoiceFormat("-1#NEGATIVE_ONE|0#ZERO|1#ONE|1