Coverage Report - javax.faces.validator.LongRangeValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
LongRangeValidator
78%
51/65
83%
20/24
1.789
 
 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.validator;
 17  
 
 18  
 import javax.faces.application.FacesMessage;
 19  
 import javax.faces.component.StateHolder;
 20  
 import javax.faces.component.UIComponent;
 21  
 import javax.faces.context.FacesContext;
 22  
 import javax.faces.internal.FacesMessageUtil;
 23  
 import javax.faces.internal.UIComponentUtil;
 24  
 import javax.faces.internal.UIInputUtil;
 25  
 
 26  
 import org.seasar.framework.util.AssertionUtil;
 27  
 import org.seasar.framework.util.LongConversionUtil;
 28  
 
 29  
 /**
 30  
  * @author shot
 31  
  */
 32  
 public class LongRangeValidator implements Validator, StateHolder {
 33  
 
 34  
     public static final String VALIDATOR_ID = "javax.faces.LongRange";
 35  
 
 36  
     public static final String TYPE_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.TYPE";
 37  
 
 38  
     public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.MAXIMUM";
 39  
 
 40  
     public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.MINIMUM";
 41  
 
 42  43
     private long maximum = Long.MAX_VALUE;
 43  
 
 44  43
     private long minimum = Long.MIN_VALUE;
 45  
 
 46  43
     private boolean transientValue = false;
 47  
 
 48  24
     public LongRangeValidator() {
 49  24
     }
 50  
 
 51  12
     public LongRangeValidator(long maximum) {
 52  12
         this.maximum = maximum;
 53  12
     }
 54  
 
 55  7
     public LongRangeValidator(long maximum, long minimum) {
 56  7
         this.maximum = maximum;
 57  7
         this.minimum = minimum;
 58  7
     }
 59  
 
 60  
     public void validate(FacesContext context, UIComponent component,
 61  
             Object value) throws ValidatorException {
 62  9
         AssertionUtil.assertNotNull("context", context);
 63  8
         AssertionUtil.assertNotNull("component", component);
 64  7
         if (UIInputUtil.isEmpty(value)) {
 65  2
             return;
 66  
         }
 67  5
         long longValue = 0;
 68  
         try {
 69  5
             longValue = LongConversionUtil.toLong(value).longValue();
 70  
 
 71  4
             if (maximum < Long.MAX_VALUE && minimum > Long.MIN_VALUE) {
 72  2
                 if (longValue < minimum || longValue > maximum) {
 73  2
                     Object[] args = { new Long(minimum), new Long(maximum),
 74  
                             UIComponentUtil.getLabel(component) };
 75  2
                     FacesMessage message = FacesMessageUtil.getMessage(context,
 76  
                             getNotInRangeMessageId(), convertArgs(args));
 77  2
                     throw new ValidatorException(message,
 78  
                             getNotInRangeMessageId(), args);
 79  
                 }
 80  2
             } else if (minimum > Long.MIN_VALUE) {
 81  1
                 if (longValue < minimum) {
 82  1
                     Object[] args = { new Long(minimum),
 83  
                             UIComponentUtil.getLabel(component) };
 84  1
                     FacesMessage message = FacesMessageUtil.getMessage(context,
 85  
                             getMinimumMessageId(), convertArgs(args));
 86  1
                     throw new ValidatorException(message,
 87  
                             getMinimumMessageId(), args);
 88  
                 }
 89  1
             } else if (maximum < Long.MAX_VALUE) {
 90  1
                 if (longValue > maximum) {
 91  1
                     Object[] args = { new Long(maximum),
 92  
                             UIComponentUtil.getLabel(component) };
 93  1
                     FacesMessage message = FacesMessageUtil.getMessage(context,
 94  
                             getMaximumMessageId(), convertArgs(args));
 95  1
                     throw new ValidatorException(message,
 96  
                             getMaximumMessageId(), args);
 97  
                 }
 98  
             }
 99  1
         } catch (NumberFormatException e) {
 100  1
             Object[] args = new Object[] { UIComponentUtil.getLabel(component) };
 101  1
             throw new ValidatorException(FacesMessageUtil.getMessage(context,
 102  
                     getTypeMessageId(), args), getTypeMessageId(), args);
 103  0
         }
 104  0
     }
 105  
 
 106  
     public boolean isTransient() {
 107  0
         return transientValue;
 108  
     }
 109  
 
 110  
     public void setTransient(boolean transientValue) {
 111  0
         this.transientValue = transientValue;
 112  0
     }
 113  
 
 114  
     public Object saveState(FacesContext context) {
 115  0
         Object[] values = new Object[2];
 116  0
         values[0] = new Long(maximum);
 117  0
         values[1] = new Long(minimum);
 118  0
         return values;
 119  
     }
 120  
 
 121  
     public void restoreState(FacesContext context, Object state) {
 122  0
         Object[] values = (Object[]) state;
 123  0
         maximum = ((Long) values[0]).longValue();
 124  0
         minimum = ((Long) values[1]).longValue();
 125  0
     }
 126  
 
 127  
     public long getMaximum() {
 128  6
         return maximum;
 129  
     }
 130  
 
 131  
     public long getMinimum() {
 132  5
         return minimum;
 133  
     }
 134  
 
 135  
     public void setMaximum(long maximum) {
 136  2
         this.maximum = maximum;
 137  2
     }
 138  
 
 139  
     public void setMinimum(long minimum) {
 140  9
         this.minimum = minimum;
 141  9
     }
 142  
 
 143  
     public boolean equals(Object obj) {
 144  11
         if (!(obj instanceof LongRangeValidator)) {
 145  1
             return false;
 146  
         }
 147  
 
 148  10
         LongRangeValidator v = (LongRangeValidator) obj;
 149  10
         return maximum == v.maximum && minimum == v.minimum;
 150  
     }
 151  
 
 152  
     public int hashCode() {
 153  0
         return (int) maximum * (int) minimum * 17;
 154  
     }
 155  
 
 156  
     protected String getMaximumMessageId() {
 157  2
         return MAXIMUM_MESSAGE_ID;
 158  
     }
 159  
 
 160  
     protected String getMinimumMessageId() {
 161  2
         return MINIMUM_MESSAGE_ID;
 162  
     }
 163  
 
 164  
     protected String getNotInRangeMessageId() {
 165  4
         return NOT_IN_RANGE_MESSAGE_ID;
 166  
     }
 167  
 
 168  
     protected String getTypeMessageId() {
 169  2
         return TYPE_MESSAGE_ID;
 170  
     }
 171  
 
 172  
     protected Object[] convertArgs(Object[] args) {
 173  4
         return args;
 174  
     }
 175  
 }