Coverage Report - javax.faces.convert.CharacterConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
CharacterConverter
88%
23/26
100%
8/8
3.4
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * 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,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package javax.faces.convert;
 17  
 
 18  
 import javax.faces.component.UIComponent;
 19  
 import javax.faces.context.FacesContext;
 20  
 import javax.faces.internal.ConvertUtil;
 21  
 import javax.faces.internal.FacesMessageUtil;
 22  
 
 23  
 import org.seasar.framework.util.AssertionUtil;
 24  
 
 25  
 /**
 26  
  * @author shot
 27  
  */
 28  
 public class CharacterConverter implements Converter {
 29  
 
 30  
     public static final String CONVERTER_ID = "javax.faces.Character";
 31  
 
 32  2
     public static final String CONVERSION_OBJECT_ID = CharacterConverter.class
 33  
             .getName()
 34  
             + ".CONVERSION";
 35  
 
 36  1
     public static final String CONVERSION_STRING_ID = CharacterConverter.class
 37  
             .getName()
 38  
             + ".CONVERSION_STRING";
 39  
 
 40  12
     public CharacterConverter() {
 41  12
     }
 42  
 
 43  
     public Object getAsObject(FacesContext context, UIComponent component,
 44  
             String value) throws ConverterException {
 45  5
         AssertionUtil.assertNotNull("FacesContext", context);
 46  4
         AssertionUtil.assertNotNull("UIComponent", component);
 47  3
         if (value == null) {
 48  1
             return null;
 49  
         }
 50  2
         value = value.trim();
 51  2
         if (value.length() < 1) {
 52  1
             Object[] args = ConvertUtil.createExceptionMessageArgs(component,
 53  
                     value);
 54  1
             throw new ConverterException(FacesMessageUtil.getMessage(context,
 55  
                     getObjectMessageId(), args));
 56  
         }
 57  
         try {
 58  1
             return new Character(value.charAt(0));
 59  0
         } catch (Exception e) {
 60  0
             Object[] args = ConvertUtil.createExceptionMessageArgs(component,
 61  
                     value);
 62  0
             throw new ConverterException(FacesMessageUtil.getMessage(context,
 63  
                     getObjectMessageId(), args), e);
 64  
         }
 65  
     }
 66  
 
 67  
     public String getAsString(FacesContext context, UIComponent component,
 68  
             Object value) throws ConverterException {
 69  7
         AssertionUtil.assertNotNull("FacesContext", context);
 70  6
         AssertionUtil.assertNotNull("UIComponent", component);
 71  5
         if (value == null) {
 72  1
             return "";
 73  
         }
 74  
         try {
 75  4
             return (value instanceof String) ? (String) value
 76  
                     : ((Character) value).toString();
 77  1
         } catch (Exception e) {
 78  1
             Object[] args = ConvertUtil.createExceptionMessageArgs(component,
 79  
                     value);
 80  1
             throw new ConverterException(FacesMessageUtil.getMessage(context,
 81  
                     getStringMessageId(), args), e);
 82  
         }
 83  
     }
 84  
 
 85  
     protected String getObjectMessageId() {
 86  1
         return CONVERSION_OBJECT_ID;
 87  
     }
 88  
 
 89  
     protected String getStringMessageId() {
 90  1
         return CONVERSION_STRING_ID;
 91  
     }
 92  
 
 93  
 }