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