View Javadoc

1   package net.sourceforge.pmd.properties;
2   
3   import net.sourceforge.pmd.PropertyDescriptor;
4   import net.sourceforge.pmd.lang.rule.properties.CharacterMultiProperty;
5   import net.sourceforge.pmd.lang.rule.properties.CharacterProperty;
6   
7   import org.junit.Test;
8   
9   /**
10   * Evaluates the functionality of the CharacterProperty descriptor by testing its ability to catch creation
11   * errors (illegal args), flag invalid characters, and serialize/deserialize any default values.
12   *  
13   * @author Brian Remedios
14   */
15  public class CharacterPropertyTest extends AbstractPropertyDescriptorTester {
16  
17  	private static final char delimiter = '|';
18  	private static final char[] charSet = filter(allChars.toCharArray(), delimiter);
19  	
20  	public CharacterPropertyTest() {
21  		super();
22  	}
23  
24  	/**
25  	 * Method createValue.
26  	 * @param count int
27  	 * @return Object
28  	 */
29  	protected Object createValue(int count) {
30  		
31  		if (count == 1) return new Character(randomChar(charSet));
32  		
33  		Character[] values = new Character[count];
34  		for (int i=0; i<values.length; i++) values[i] = (Character)createValue(1);
35  		return values;
36  	}
37  
38  	/**
39  	 * Method createBadValue.
40  	 * @param count int
41  	 * @return Object
42  	 */
43  	protected Object createBadValue(int count) {
44  		
45  		if (count == 1) return null;
46  		
47  		Character[] values = new Character[count];
48  		for (int i=0; i<values.length; i++) values[i] = (Character)createBadValue(1);
49  		return values;
50  	}
51  	
52  	 @Test
53  	 public void testErrorForBad() { }	// not until char properties use illegal chars
54  		
55  	
56  	/**
57  	 * Method createProperty.
58  	 * @param multiValue boolean
59  	 * @return PropertyDescriptor
60  	 */
61  	protected PropertyDescriptor createProperty(boolean multiValue) {
62  		
63  		return multiValue ?
64  			new CharacterMultiProperty("testCharacter", "Test character property", new Character[] {'a', 'b', 'c'}, 1.0f, delimiter) :
65  			new CharacterProperty("testCharacter", "Test character property", 'a', 1.0f);
66  	}
67  
68  	/**
69  	 * Creates a bad property that is missing either its name or description or includes a delimiter
70  	 * in the set of legal values.
71  	 * 
72  	 * @param multiValue boolean
73  	 * @return PropertyDescriptor
74  	 */
75  	protected PropertyDescriptor createBadProperty(boolean multiValue) {
76  		
77  		return multiValue ?
78  			new CharacterMultiProperty("testCharacter", "Test character property", new Character[] {'a', 'b', 'c'}, 1.0f, delimiter) :
79  			new CharacterProperty("", "Test character property", 'a', 1.0f);
80  	}
81  	
82      public static junit.framework.Test suite() {
83          return new junit.framework.JUnit4TestAdapter(CharacterPropertyTest.class);
84      }
85  }