1 package attrib4j.tests;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.Method;
5 import java.util.Arrays;
6
7 import junit.framework.TestCase;
8 import attrib4j.Attribute;
9 import attrib4j.Attributes;
10 import attrib4j.examples.attributes.DebugAttribute;
11 import attrib4j.examples.attributes.TraceAttribute;
12 import attrib4j.examples.attributes.TransactionAttribute;
13
14 /***
15 * Basic test to kick start development.
16 *
17 *
18 * Created: Tue Oct 15 18:33:22 2002
19 *
20 * @author <a href="mailto:mpollack@speakeasy.org">Mark Pollack</a>
21 */
22
23 public class AttributeTest extends TestCase {
24
25 /***
26 * Create a new Attribute test
27 * @param name of the test
28 */
29 public AttributeTest(String name) {
30 super(name);
31 }
32
33 /***
34 * Test the extraction of Class level attributes
35 * @throws ClassNotFoundException in case attribute classes are not
36 * resolved using Class.forName
37 */
38 public void testClassAttributes() throws ClassNotFoundException {
39 Class cls = attrib4j.examples.ExampleClass.class;
40
41 //Check to see if we have the right number of attributes.
42 Attribute[] custAttrs = Attributes.getAttributes(cls);
43 assertEquals(
44 "not right number of class attributes",
45 2,
46 custAttrs.length);
47
48 //Check the values of the Debug attribute.
49 DebugAttribute debugAttributeFilter = new DebugAttribute(true);
50 Class attributeFilter =
51 Class.forName("attrib4j.examples.attributes.DebugAttribute");
52 Attribute[] debugAttribs =
53 Attributes.getAttributes(cls, attributeFilter);
54 assertEquals(
55 "not correct number of DebugAttributes",
56 1,
57 debugAttribs.length);
58 DebugAttribute debugAttribute = (DebugAttribute) debugAttribs[0];
59 assertEquals(
60 "Debugging attribute has wrong value",
61 true,
62 debugAttribute.isDebug());
63
64 attributeFilter =
65 Class.forName("attrib4j.examples.attributes.TraceAttribute");
66 Attribute[] traceAttribs =
67 Attributes.getAttributes(cls, attributeFilter);
68 assertEquals(
69 "Not correct number of TraceAttributes",
70 1,
71 traceAttribs.length);
72 TraceAttribute traceAttribute = (TraceAttribute) traceAttribs[0];
73 assertEquals(
74 "Trace attribute has wrong value for group field",
75 "Test",
76 traceAttribute.getGroup());
77 assertEquals(
78 "Trace attribute has wrong value for level field",
79 1,
80 traceAttribute.getLevel());
81
82 /*
83 attributeFilter = Class.forName("attrib4j.attributes.JavadocAttribute");
84
85 Attribute[] javadocAttribs =
86 Attributes.getAttributes(cls, attributeFilter);
87 assertEquals(
88 "Not correct number of JavadocAttributes",
89 1,
90 javadocAttribs.length);
91 JavadocAttribute javadocAttrib = (JavadocAttribute) javadocAttribs[0];
92 assertEquals(
93 "Javadoc attribute has wrong value for name",
94 "author",
95 javadocAttrib.getName());
96 assertEquals(
97 "Javadoc attribute has wrong value for text",
98 "Tycho Brahe",
99 javadocAttrib.getText());
100 */
101
102 }
103
104 /***
105 * Test extraction of field level attributes.
106 */
107 public void testFieldAttributes() {
108 Class cls = attrib4j.examples.ExampleClass.class;
109
110 // Find attributes on all fields
111 //
112 Field[] fields = cls.getDeclaredFields();
113 int numFieldAttribs = 0;
114 for (int fi = 0; fi < fields.length; fi++) {
115 Field field = fields[fi];
116 System.out.println("ExampleClass Field custom attributes:");
117 System.out.println("--------------------------------");
118 Attribute[] fieldAttrs = Attributes.getAttributes(field);
119 if (fieldAttrs.length == 0) {
120 System.out.println("None found.");
121 } else {
122 numFieldAttribs++;
123 for (int i = 0; i < fieldAttrs.length; i++) {
124 System.out.println(fieldAttrs[i].toString());
125 }
126 }
127 System.out.println("\n");
128 }
129
130 assertEquals(
131 "not the right number of fields with attributes",
132 numFieldAttribs,
133 4);
134 }
135
136 /***
137 * Test extraction of method level attributes.
138 */
139 public void testMethodAttributes() {
140
141 Class cls = attrib4j.examples.ExampleClass.class;
142
143 //create method signature to pick appropriate overloaded method
144 String[] depositOOParamTypes = new String[2];
145 depositOOParamTypes[0] = "java.lang.Object";
146 depositOOParamTypes[1] = "java.lang.Object";
147
148 String[] depositIJParamTypes = new String[2];
149 depositIJParamTypes[0] = "int";
150 depositIJParamTypes[1] = "long";
151
152 Method[] methods = cls.getDeclaredMethods();
153 int numMethodAttribs = 0;
154 //loop over all the methods.
155 for (int mi = 0; mi < methods.length; mi++) {
156 Method method = methods[mi];
157 //get the attributes for this method.
158 Attribute[] methodAttrs = Attributes.getAttributes(method);
159 //get the methods parameter types.
160 Class[] paramTypes = method.getParameterTypes();
161 //convet parameter types to string arrays.
162 String[] paramTypesString =
163 new String[method.getParameterTypes().length];
164 for (int i = 0; i < paramTypes.length; i++) {
165 paramTypesString[i] = paramTypes[i].getName();
166 }
167 //pick appropriate method to perform comparison.
168 if (Arrays.equals(paramTypesString, depositOOParamTypes)) {
169 //methods only have attribute of this class.
170 DebugAttribute debugAttribute = (DebugAttribute) methodAttrs[0];
171 assertEquals(
172 "Debugging attribute on deposit(Object,Object) "
173 + "method has wrong value",
174 false,
175 debugAttribute.isDebug());
176 }
177
178 //Should have two attributes on this method. One from the
179 //interface and the other from the class itself.
180 if (Arrays.equals(paramTypesString, depositIJParamTypes)) {
181 //methods only have attribute of this class.
182 assertEquals(
183 "Expected debug and transaction attribute",
184 3,
185 methodAttrs.length);
186
187 //Test for debug attribute.
188 Attribute[] debugAttribute =
189 Attributes.getAttributes(method, DebugAttribute.class);
190
191 assertEquals("Only one debug attribute should be found",
192 1,
193 debugAttribute.length);
194 assertEquals(
195 "Debugging attribute on deposit(int,long) "
196 + "method has wrong value",
197 true,
198 ((DebugAttribute)debugAttribute[0]).isDebug());
199
200 //Test for transaction attribute.
201 Attribute[] transAttribute =
202 Attributes.getAttributes(method, TransactionAttribute.class);
203 assertEquals("Only one transaction attribute should be found",
204 1,
205 transAttribute.length);
206 assertEquals("Expected a 'Required' attribute property",
207 "Required",
208 ((TransactionAttribute)transAttribute[0]).getAsString());
209
210 //Test for trace attribute
211 Attribute[] traceAttribute =
212 Attributes.getAttributes(method, TraceAttribute.class);
213 assertEquals("Only one trace attribute should be found",
214 1,
215 traceAttribute.length);
216 assertEquals("Expected value of 1 for trace level",
217 1,
218 ((TraceAttribute)traceAttribute[0]).getLevel());
219
220 }
221
222 numMethodAttribs++;
223
224 }
225
226 assertEquals(
227 "not the correct number of method with attributes",
228 numMethodAttribs,
229 2);
230 }
231
232 } // AttributeTest
This page was automatically generated by Maven