View Javadoc

    1   package net.sourceforge.pmd.properties;
    2
    3   import net.sourceforge.pmd.PropertyDescriptor;
    4   import net.sourceforge.pmd.lang.rule.properties.IntegerMultiProperty;
    5   import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
    6
    7   import org.junit.Test;
    8
    9   /**
    10   * Evaluates the functionality of the IntegerProperty descriptor by testing its ability to catch creation
    11   * errors (illegal args), flag out-of-range test values, and serialize/deserialize groups of integers
    12   * onto/from a string buffer.
    13   *
    14   * @author Brian Remedios
    15   */
    16  public class IntegerPropertyTest extends AbstractPropertyDescriptorTester {
    17
    18  	private static final int MIN = 1;
    19  	private static final int MAX = 12;
    20  	private static final int SHIFT = 3;
    21
    22  	/**
    23  	 * Method createValue.
    24  	 * @param count int
    25  	 * @return Object
    26  	 */
    27  	protected Object createValue(int count) {
    28
    29  		if (count == 1) return Integer.valueOf(randomInt(MIN, MAX));
    30
    31  		Integer[] values = new Integer[count];
    32  		for (int i=0; i<values.length; i++) values[i] = (Integer)createValue(1);
    33  		return values;
    34  	}
    35
    36  	/**
    37  	 * Creates and returns (count) number of out-of-range Integer values
    38  	 *
    39  	 * @param count int
    40  	 * @return Object
    41  	 */
    42  	protected Object createBadValue(int count) {
    43
    44  		if (count == 1) return Integer.valueOf(
    45  			randomBool() ?
    46  					randomInt(MIN - SHIFT, MIN) :
    47  					randomInt(MAX, MAX + SHIFT)
    48  					);
    49
    50  		Integer[] values = new Integer[count];
    51  		for (int i=0; i<values.length; i++) values[i] = (Integer)createBadValue(1);
    52  		return values;
    53  	}
    54
    55  	 @Test
    56  	public void testErrorForBad() { }	// not until int properties get ranges
    57
    58  	/**
    59  	 * Method createProperty.
    60  	 * @param multiValue boolean
    61  	 * @return PropertyDescriptor
    62  	 */
    63  	protected PropertyDescriptor createProperty(boolean multiValue) {
    64
    65  		return multiValue ?
    66  			new IntegerMultiProperty("testInteger", "Test integer property", MIN, MAX, new Integer[] {MIN, MIN+1, MAX-1, MAX}, 1.0f) :
    67  			new IntegerProperty("testInteger", "Test integer property", MIN, MAX, MAX-1, 1.0f);
    68  		}
    69
    70  	/**
    71  	 * Method createBadProperty.
    72  	 * @param multiValue boolean
    73  	 * @return PropertyDescriptor
    74  	 */
    75  	protected PropertyDescriptor createBadProperty(boolean multiValue) {
    76
    77  		return multiValue ?
    78  			new IntegerMultiProperty("testInteger", "", MIN, MAX, new Integer[] {MIN-1, MAX}, 1.0f) :
    79  			new IntegerProperty("", "Test integer property", MIN, MAX, MAX+1, 1.0f);
    80  		}
    81
    82      public static junit.framework.Test suite() {
    83          return new junit.framework.JUnit4TestAdapter(IntegerPropertyTest.class);
    84      }
    85  }