Package javax.faces.convert

Examples of javax.faces.convert.NumberConverter



  public void testSettingFractDigitsAndSettingMinDigitsDoesNotAffectParsing()
  {
    // integerOnly is used only while parsing to create number objects
    NumberConverter converter = getNumberConverter();
    Mock mock = mock(UIComponent.class);
    UIComponent component = (UIComponent) mock.proxy();
    converter.setLocale(Locale.US);

    String[] inputs = {"23.10", "44.90876", "11111", "67859.0001"};
    Number[] expectedValues = {new Long(23), new Long(44), new Long(11111), new Long(67859)};

    setFacesContext(facesContext);
    try
    {
      for (int i = 0; i < inputs.length; i++)
      {
        // setting these values should not affect parsing.
        converter.setMaxFractionDigits(10);
        converter.setMaxIntegerDigits(1);
        converter.setMinFractionDigits(1);
        converter.setMinFractionDigits(0);
       
        // this should be taken care by the parsing code
        converter.setIntegerOnly(true);
        Number num = (Number) converter.getAsObject(facesContext, component, inputs[i]);
        assertEquals(expectedValues[i], num);
      }
    }
    finally
    {
View Full Code Here


  }

  public void testLocaleIsPickedUpFromViewRoot()
  {

    NumberConverter converter = getNumberConverter();
    Mock mock = mock(UIComponent.class);
    UIComponent component = (UIComponent) mock.proxy();

    String input = "1234.56";

    setFacesContext(facesContext);
    facesContext.getViewRoot().setLocale(Locale.US);

    try
    {
      // if we get a valid object, implies locale was indeed picked up.
      // otherwise we would have got a null pointer exception or other exception
      Object value = converter.getAsObject(facesContext, component, input);
      assertEquals(new Double(1234.56), value);
    }
    finally
    {
      setFacesContext(null);
View Full Code Here

  {
    Number[] inputValues = {new Long(9999), new Long(99), new Double(0.99), new Double(99999.567), new Long(9999)};
    boolean [] isGroupingUsed = {true, true, true, false, false };
    String[] expectedValues = {"9,999", "99", "0.99", "99999.567", "9999"};

    NumberConverter converter = getNumberConverter();
    Mock mock = mock(UIComponent.class);
    UIComponent component = (UIComponent) mock.proxy();
    converter.setLocale(Locale.US);
    setFacesContext(facesContext);
    try
    {
      for (int i = 0; i < inputValues.length; i++)
      {
        converter.setGroupingUsed(isGroupingUsed[i]);
        String out = converter.getAsString(facesContext, component, inputValues[i]);
        assertEquals(expectedValues[i], out);
      }
    }
    finally
    {
View Full Code Here

    int[] maxFractDigits = {0, 2, 2, 2};
    int[] maxIntDigits   = {4, 2, 4, 3};
    int[] minIntDigits   = {4, 1, 2, 1};
    int[] minFractDigits = {0, 2, 0, 2};

    NumberConverter converter   = getNumberConverter();
    Mock mock = mock(UIComponent.class);
    UIComponent component = (UIComponent) mock.proxy();

    setFacesContext(facesContext);
    try
    {
      converter.setLocale(Locale.US);
      for (int i = 0; i < maxFractDigits.length; i++)
      {
        converter.setMaxFractionDigits(maxFractDigits[i]);
        converter.setMaxIntegerDigits(maxIntDigits[i]);
        converter.setMinFractionDigits(minFractDigits[i]);
        converter.setMinIntegerDigits(minIntDigits[i]);
       
        String out = converter.getAsString(facesContext, component, inputValues[i]);
        assertEquals(expectedValues[i], out);  
      }
    }
    finally
    {
View Full Code Here

        setConverterIdString(NumberConverter.CONVERTER_ID);
    }

    protected Converter createConverter() throws JspException
    {
        NumberConverter converter = (NumberConverter) super.createConverter();
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        if (null != _currencyCode)
        {
            converter.setCurrencyCode(
                    (String) UIComponentELTagUtils.evaluateValueExpression(elContext, _currencyCode));
        }
        if (null != _currencySymbol)
        {
            converter.setCurrencySymbol(
                    (String) UIComponentELTagUtils.evaluateValueExpression(elContext, _currencySymbol));
        }
        if (null != _groupingUsed)
        {
            converter.setGroupingUsed(UIComponentELTagUtils.getBooleanValue(elContext, _groupingUsed));
        } else
        {
            converter.setGroupingUsed(true);
        }
        if (null != _integerOnly)
        {
            converter.setIntegerOnly(UIComponentELTagUtils.getBooleanValue(elContext, _integerOnly));
        } else
        {
            converter.setIntegerOnly(false);
        }
        if (null != _locale)
        {
            Locale locale;
            if (_locale.isLiteralText())
            {
                locale = new Locale(_locale.getExpressionString());
            } else
            {
                locale = (Locale) _locale.getValue(elContext);
                if (null == locale)
                {
                    locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
                }
            }
            converter.setLocale(locale);
        }
        if (null != _maxFractionDigits)
        {
            converter.setMaxFractionDigits(UIComponentELTagUtils.getIntegerValue(elContext, _maxFractionDigits));
        }
        if (null != _maxIntegerDigits)
        {
            converter.setMaxIntegerDigits(UIComponentELTagUtils.getIntegerValue(elContext, _maxIntegerDigits));
        }
        if (null != _minFractionDigits)
        {
            converter.setMinFractionDigits(UIComponentELTagUtils.getIntegerValue(elContext, _minFractionDigits));
        }
        if (null != _minIntegerDigits)
        {
            converter.setMinIntegerDigits(UIComponentELTagUtils.getIntegerValue(elContext, _minIntegerDigits));
        }
        if (null != _pattern)
        {
            converter.setPattern((String) UIComponentELTagUtils.evaluateValueExpression(elContext, _pattern));
        }
        if (null != _type)
        {
            converter.setType((String) UIComponentELTagUtils.evaluateValueExpression(elContext, _type));
        } else
        {
            converter.setType("number");
        }
        return converter;
    }
View Full Code Here

     * @see com.sun.facelets.tag.ObjectHandler#setAttributes(com.sun.facelets.FaceletContext, java.lang.Object)
     */
    @Override
    public void setAttributes(FaceletContext ctx, Object obj) {
        super.setAttributes(ctx, obj);
        NumberConverter c = (NumberConverter) obj;
        if (this.locale != null) {
            c.setLocale(ComponentSupport.getLocale(ctx, this.locale));
        }
    }
View Full Code Here


    // Create and configure a NumberConverter
    protected NumberConverter createNumberConverter() {

        NumberConverter nc = new NumberConverter();
        nc.setCurrencyCode("USD");
        nc.setCurrencySymbol("$");
        nc.setGroupingUsed(false);
        nc.setIntegerOnly(true);
        nc.setMaxFractionDigits(2);
        nc.setMaxIntegerDigits(10);
        nc.setMinFractionDigits(2);
        nc.setMinIntegerDigits(5);
        nc.setType("currency");
        return (nc);

    }
View Full Code Here

                Converter converter = usageContext.getConverter();
                FacesContext context = FacesContext.getCurrentInstance();
                if (converter == null && summaryValue != null) {
                    Class cls = summaryValue.getClass();
                    if (cls.equals(Double.class) || cls.equals(Float.class)) {
                        NumberConverter numberConverter = new NumberConverter();
                        numberConverter.setGroupingUsed(false);
                        numberConverter.setMaxFractionDigits(2);
                        converter = numberConverter;
                    } else {
                        converter = Rendering.getConverterForType(context, cls);
                    }
                }
View Full Code Here


    // Create and configure a NumberConverter
    protected NumberConverter createNumberConverter() {

        NumberConverter nc = new NumberConverter();
        nc.setCurrencyCode("USD");
        nc.setCurrencySymbol("$");
        nc.setGroupingUsed(false);
        nc.setIntegerOnly(true);
        nc.setMaxFractionDigits(2);
        nc.setMaxIntegerDigits(10);
        nc.setMinFractionDigits(2);
        nc.setMinIntegerDigits(5);
        nc.setType("currency");
        return (nc);

    }
View Full Code Here

    }

    public void testNumberConverterSpacesNBSP() throws Exception {
        UIInput text = new UIInput();
        NumberConverter converter = (NumberConverter) application.createConverter("javax.faces.Number");
        converter.setType("currency");
        converter.setLocale(Locale.FRANCE);
        String toConv = "12 345,68 " + '\u20aC';
        Number number = (Number) converter.getAsObject(getFacesContext(),
                                                       text,
                                                       toConv);
        assertTrue(number != null);

        converter.setType("number");
        toConv = "5" + "\u00a0" + "000" + "\u00a0" + "000,01";
        number = (Number) converter.getAsObject(getFacesContext(),
                                                text,
                                                toConv);
        assertTrue("Number was: " + number.toString() + ", expected 5000000.01",
                   "5000000.01".equals(number.toString()));
    }
View Full Code Here

TOP

Related Classes of javax.faces.convert.NumberConverter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.