View Javadoc

    1   package net.sourceforge.pmd.properties;
    2
    3   import java.util.Comparator;
    4   import java.util.HashMap;
    5   import java.util.Map;
    6   import java.util.Observer;
    7   import java.util.Set;
    8
    9   import net.sourceforge.pmd.PropertyDescriptor;
    10  import net.sourceforge.pmd.lang.rule.properties.TypeMultiProperty;
    11  import net.sourceforge.pmd.lang.rule.properties.TypeProperty;
    12
    13  /**
    14   * Evaluates the functionality of the TypeProperty descriptor by testing its ability to catch creation
    15   * errors (illegal args), flag invalid Type values per the allowable packages, and serialize/deserialize
    16   * groups of types onto/from a string buffer.
    17   *
    18   * We're using java.lang classes for 'normal' constructors and applying java.util types as ones we expect
    19   * to fail.
    20   *
    21   * @author Brian Remedios
    22   */
    23  public class TypePropertyTest extends AbstractPropertyDescriptorTester {
    24
    25  	private static final Class[] javaLangClasses = new Class[] { String.class, Integer.class, Thread.class, Object.class, Runtime.class };
    26  	private static final Class[] javaUtilTypes = new Class[] { HashMap.class, Map.class, Comparator.class, Set.class, Observer.class };
    27
    28  	public TypePropertyTest() {
    29  		super();
    30  	}
    31
    32  	/**
    33  	 * Method createValue.
    34  	 * @param count int
    35  	 * @return Object
    36  	 */
    37  	protected Object createValue(int count) {
    38
    39  		if (count == 1) return randomChoice(javaLangClasses);
    40
    41  		Object[] values = new Object[count];
    42  		for (int i=0; i<values.length; i++) values[i] = createValue(1);
    43  		return values;
    44  	}
    45
    46  	/**
    47  	 * Method createBadValue.
    48  	 * @param count int
    49  	 * @return Object
    50  	 */
    51  	protected Object createBadValue(int count) {
    52
    53  		if (count == 1) return randomChoice(javaUtilTypes);
    54
    55  		Object[] values = new Object[count];
    56  		for (int i=0; i<values.length; i++) values[i] = createBadValue(1);
    57  		return values;
    58  	}
    59
    60  	/**
    61  	 * Method createProperty.
    62  	 * @param multiValue boolean
    63  	 * @return PropertyDescriptor
    64  	 */
    65  	protected PropertyDescriptor createProperty(boolean multiValue) {
    66
    67  		return multiValue ?
    68  			new TypeMultiProperty("testType", "Test type property", javaLangClasses, new String[] { "java.lang" }, 1.0f) :
    69  			new TypeProperty("testType", "Test type property", javaLangClasses[0], new String[] { "java.lang" }, 1.0f);
    70  			}
    71
    72  	/**
    73  	 * Method createProperty.
    74  	 * @param multiValue boolean
    75  	 * @return PropertyDescriptor
    76  	 */
    77  	protected PropertyDescriptor createBadProperty(boolean multiValue) {
    78
    79  		return multiValue ?
    80  			new TypeMultiProperty("testType", "Test type property", new Class[]{Set.class}, new String[] { "java.lang" }, 1.0f) :
    81  			new TypeProperty("testType", "Test type property", javaLangClasses[0], new String[] { "java.util" }, 1.0f);
    82  			}
    83
    84      public static junit.framework.Test suite() {
    85          return new junit.framework.JUnit4TestAdapter(TypePropertyTest.class);
    86      }
    87  }