KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > tivano > junit > ParametrizationTestDummy


1 /*
2  * ParametrizationTestDummy.java
3  *
4  * Created on 3. März 2002, 19:47
5  */

6
7 package de.tivano.junit;
8
9 import java.io.File JavaDoc;
10 import java.lang.reflect.Field JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 import junit.framework.TestCase;
14
15 /**
16  * Helper class to test the parametrized test suite. Basically, this class defines
17  * parametrizable attributes of every allowable type, both accessible directly
18  * and via method methods. In addition, it defines test methods to check if the
19  * parametrized fields are set to the expected values.
20  * @author richard
21  */

22 public class ParametrizationTestDummy extends TestCase {
23     
24     private Properties JavaDoc expectedData;
25     private String JavaDoc keyPrefix;
26     
27     public boolean fieldBoolean;
28     public char fieldChar;
29     public byte fieldByte;
30     public short fieldShort;
31     public int fieldInt;
32     public long fieldLong;
33     public float fieldFloat;
34     public double fieldDouble;
35     public String JavaDoc fieldString;
36     public File JavaDoc fieldFile;
37     
38     public Boolean JavaDoc fieldBooleanObj;
39     public Character JavaDoc fieldCharObj;
40     public Byte JavaDoc fieldByteObj;
41     public Short JavaDoc fieldShortObj;
42     public Integer JavaDoc fieldIntObj;
43     public Long JavaDoc fieldLongObj;
44     public Float JavaDoc fieldFloatObj;
45     public Double JavaDoc fieldDoubleObj;
46     
47     private boolean methodBoolean;
48     private char methodChar;
49     private byte methodByte;
50     private short methodShort;
51     private int methodInt;
52     private long methodLong;
53     private float methodFloat;
54     private double methodDouble;
55     private String JavaDoc methodString;
56     private File JavaDoc methodFile;
57     
58     private Boolean JavaDoc methodBooleanObj;
59     private Character JavaDoc methodCharObj;
60     private Byte JavaDoc methodByteObj;
61     private Short JavaDoc methodShortObj;
62     private Integer JavaDoc methodIntObj;
63     private Long JavaDoc methodLongObj;
64     private Float JavaDoc methodFloatObj;
65     private Double JavaDoc methodDoubleObj;
66     
67     // This is a field that is accessible both by a setter and as a public field.
68
// The setter is expected to set it to the negative of its argument, so
69
// that we can use the value later to make sure that the setter really has been
70
// called.
71
public int special;
72     
73     // Don't set this in the associated properties -
74
// we're expecting it to be left alone...
75
public String JavaDoc leaveMeAlone = "DONT_EVER_CHANGE_THIS_VALUE";
76     
77     
78     public ParametrizationTestDummy(String JavaDoc name) {
79         super(name);
80     }
81     
82     /** Set the properties object and key prefix to check data against. */
83     public void setExpectedData(String JavaDoc keyPrefix, Properties JavaDoc expectedData) {
84         this.expectedData = expectedData;
85         this.keyPrefix = keyPrefix;
86     }
87     
88     /** Setter for property methodBoolean.
89      * @param methodBoolean New value of property methodBoolean.
90      */

91     public void setMethodBoolean(boolean methodBoolean) {
92         this.methodBoolean = methodBoolean;
93     }
94     
95     /** Setter for property methodBooleanObj.
96      * @param methodBooleanObj New value of property methodBooleanObj.
97      */

98     public void setMethodBooleanObj(java.lang.Boolean JavaDoc methodBooleanObj) {
99         this.methodBooleanObj = methodBooleanObj;
100     }
101     
102     /** Setter for property methodChar.
103      * @param methodChar New value of property methodChar.
104      */

105     public void setMethodChar(char methodChar) {
106         this.methodChar = methodChar;
107     }
108     
109     /** Setter for property methodCharObj.
110      * @param methodCharObj New value of property methodCharObj.
111      */

112     public void setMethodCharObj(java.lang.Character JavaDoc methodCharObj) {
113         this.methodCharObj = methodCharObj;
114     }
115     
116     /** Setter for property methodDouble.
117      * @param methodDouble New value of property methodDouble.
118      */

119     public void setMethodDouble(double methodDouble) {
120         this.methodDouble = methodDouble;
121     }
122     
123     /** Setter for property methodDoubleObj.
124      * @param methodDoubleObj New value of property methodDoubleObj.
125      */

126     public void setMethodDoubleObj(java.lang.Double JavaDoc methodDoubleObj) {
127         this.methodDoubleObj = methodDoubleObj;
128     }
129     
130     /** Setter for property methodFile.
131      * @param methodFile New value of property methodFile.
132      */

133     public void setMethodFile(File JavaDoc methodFile) {
134         this.methodFile = methodFile;
135     }
136     
137     /** Setter for property methodFloat.
138      * @param methodFloat New value of property methodFloat.
139      */

140     public void setMethodFloat(float methodFloat) {
141         this.methodFloat = methodFloat;
142     }
143     
144     /** Setter for property methodFloatObj.
145      * @param methodFloatObj New value of property methodFloatObj.
146      */

147     public void setMethodFloatObj(java.lang.Float JavaDoc methodFloatObj) {
148         this.methodFloatObj = methodFloatObj;
149     }
150     
151     /** Setter for property methodInt.
152      * @param methodInt New value of property methodInt.
153      */

154     public void setMethodInt(int methodInt) {
155         this.methodInt = methodInt;
156     }
157     
158     /** Setter for property methodIntObj.
159      * @param methodIntObj New value of property methodIntObj.
160      */

161     public void setMethodIntObj(java.lang.Integer JavaDoc methodIntObj) {
162         this.methodIntObj = methodIntObj;
163     }
164     
165     /** Setter for property methodLong.
166      * @param methodLong New value of property methodLong.
167      */

168     public void setMethodLong(long methodLong) {
169         this.methodLong = methodLong;
170     }
171     
172     /** Setter for property methodLongObj.
173      * @param methodLongObj New value of property methodLongObj.
174      */

175     public void setMethodLongObj(java.lang.Long JavaDoc methodLongObj) {
176         this.methodLongObj = methodLongObj;
177     }
178     
179     /** Setter for property methodShort.
180      * @param methodShort New value of property methodShort.
181      */

182     public void setMethodShort(short methodShort) {
183         this.methodShort = methodShort;
184     }
185     
186     /** Setter for property methodShortObj.
187      * @param methodShortObj New value of property methodShortObj.
188      */

189     public void setMethodShortObj(java.lang.Short JavaDoc methodShortObj) {
190         this.methodShortObj = methodShortObj;
191     }
192     
193     /** Setter for property methodString.
194      * @param methodString New value of property methodString.
195      */

196     public void setMethodString(java.lang.String JavaDoc methodString) {
197         this.methodString = methodString;
198     }
199
200     /** Setter for property special.
201      * This sets the <code>special</code> property to the negative of <code>value</code>.
202      * @param value The value
203      */

204     public void setSpecial(int value) {
205         // Set this to the negative value, so that we can check later that the
206
// property really has been set by calling the setter method.
207
this.special = -value;
208     }
209     
210     /** Setter for property methodByteObj.
211      * @param methodByteObj New value of property methodByteObj.
212      */

213     public void setMethodByteObj(java.lang.Byte JavaDoc methodByteObj) {
214         this.methodByteObj = methodByteObj;
215     }
216     
217     /** Setter for property methodByte.
218      * @param methodByte New value of property methodByte.
219      */

220     public void setMethodByte(byte methodByte) {
221         this.methodByte = methodByte;
222     }
223     
224    /** Validate the "standard" data against the expected data. */
225     public void testStandardData() throws IllegalAccessException JavaDoc {
226         // Paranoia code: Make sure we do *have* the expected data...
227
assertNotNull("Expected data not set", expectedData);
228         assertNotNull("Key prefix not set", keyPrefix);
229         // Go over all of the fields by reflection.
230
Field JavaDoc[] allFields = getClass().getDeclaredFields();
231         // Paranoia code: We've got 40 fields in this class at the time
232
// of writing this method, make sure we're getting them all...
233
assertEquals("ParametrizationTestDummy.testStandardData(), " + keyPrefix +
234                      ": Field count mismatch:", 40, allFields.length);
235         for (int i=0; i<allFields.length; i++) {
236             String JavaDoc name = allFields[i].getName();
237             if (name.startsWith("field") || name.startsWith("method")) {
238                 String JavaDoc value = allFields[i].get(this).toString();
239                 String JavaDoc expected = expectedData.getProperty(keyPrefix + "." + name);
240                 assertEquals("ParametrizationTestDummy.testStandardData(), " + keyPrefix +
241                              ": Data for property '" + name + "':", expected, value);
242             }
243         }
244     }
245     
246     /** Test if the paramterization really prefers the setter to a public field. */
247     public void testSetterMethodAndPublicField() {
248         // Paranoia code: Make sure we do *have* the expected data...
249
assertNotNull("ParametrizationTestDummy.testSetterMethodAndPublicField(): Expected data not set:", expectedData);
250         assertNotNull("ParametrizationTestDummy.testSetterMethodAndPublicField(): Key prefix not set", keyPrefix);
251         int expected = -Integer.parseInt(expectedData.getProperty(keyPrefix + ".special"));
252         assertEquals("ParametrizationTestDummy.testSetterMethodAndPublicField(), " + keyPrefix + ":",
253                      expected, special);
254     }
255     
256     /** Test if the "leave me alone" field has indeed been left alone by the parametrization... */
257     public void testLeaveMeAlone() {
258         assertEquals("ParametrizationTestDummy.testLeaveMeAlone(): ", "DONT_EVER_CHANGE_THIS_VALUE", leaveMeAlone);
259     }
260     
261     /** A test that always fails. Needed to check that the parametrization decorator actually calls
262      * the decorated test.
263      */

264     public void testFailAlways() {
265         fail("ParametrizationTestDummy.testFailAlways(): This test is expected to fail");
266     }
267  }
268
Popular Tags