KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > attributes > test > AttributesTestCase


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.attributes.test;
17
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.io.File JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import org.apache.commons.attributes.Attributes;
27 import org.apache.commons.attributes.AttributeIndex;
28 import junit.framework.TestCase;
29
30 public class AttributesTestCase extends TestCase {
31     
32     public void testClassAttributes () throws Exception JavaDoc {
33         
34         /**
35          * @Dependency ( SampleService.class, "super-sample" )
36          */

37         Class JavaDoc c = SuperSample.class;
38         assertEquals (1, Attributes.getAttributes (c).size ());
39         assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ());
40         assertTrue (Attributes.hasAttributeType (c, Dependency.class));
41         assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "super-sample" )));
42     }
43         
44     public void testMethodAttributes () throws Exception JavaDoc {
45         /**
46          * @Dependency ( SampleService.class, "super-some-method-sample" )
47          * @ThreadSafe ()
48          */

49         Method JavaDoc m = SuperSample.class.getMethod ("someMethod", new Class JavaDoc[]{ Integer.TYPE });
50         assertEquals (2, Attributes.getAttributes (m).size ());
51         assertEquals (1, Attributes.getAttributes (m, Dependency.class).size ());
52         assertEquals (1, Attributes.getAttributes (m, ThreadSafe.class).size ());
53         assertTrue (Attributes.hasAttributeType (m, Dependency.class));
54         assertTrue (Attributes.hasAttributeType (m, ThreadSafe.class));
55         assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "super-some-method-sample" )));
56         assertTrue (Attributes.hasAttribute (m, new ThreadSafe ()));
57     }
58         
59     public void testFieldAttributes () throws Exception JavaDoc {
60         /**
61          * @ThreadSafe ()
62          * @Dependency ( SampleService.class, "super-field" )
63          */

64         Field JavaDoc f = SuperSample.class.getField ("field");
65         assertEquals (2, Attributes.getAttributes (f).size ());
66         assertEquals (1, Attributes.getAttributes (f, ThreadSafe.class).size ());
67         assertEquals (1, Attributes.getAttributes (f, Dependency.class).size ());
68         assertTrue (Attributes.hasAttribute (f, new ThreadSafe ()));
69         assertTrue (Attributes.hasAttribute (f, new Dependency ( SampleService.class, "super-field" ) ));
70         assertTrue (Attributes.hasAttributeType (f, ThreadSafe.class));
71         assertTrue (Attributes.hasAttributeType (f, Dependency.class));
72     }
73     
74     public void testDefaultConstructorAttributes () throws Exception JavaDoc {
75         /**
76          * @Dependency ( SampleService.class, "sample-ctor1" )
77          */

78         Constructor JavaDoc c = SuperSample.class.getDeclaredConstructor (new Class JavaDoc[0]);
79         assertEquals (1, Attributes.getAttributes (c).size ());
80         assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ());
81         assertTrue (Attributes.hasAttributeType (c, Dependency.class));
82         assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample-ctor1" )));
83     }
84     
85     public void testConstructorAttributes () throws Exception JavaDoc {
86         /**
87          * @Dependency ( SampleService.class, "sample-ctor2" )
88          */

89         Constructor JavaDoc c = SuperSample.class.getDeclaredConstructor (new Class JavaDoc[]{ String JavaDoc.class, (new String JavaDoc[0][0]).getClass () } );
90         assertEquals (1, Attributes.getAttributes (c).size ());
91         assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ());
92         assertTrue (Attributes.hasAttributeType (c, Dependency.class));
93         assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample-ctor2" )));
94         
95         assertEquals (1, Attributes.getParameterAttributes (c, 1).size ());
96         assertEquals (1, Attributes.getParameterAttributes (c, 1, ThreadSafe.class).size ());
97         assertTrue (Attributes.hasParameterAttributeType (c, 1, ThreadSafe.class));
98         assertTrue (Attributes.hasParameterAttribute (c, 1, new ThreadSafe ()));
99     }
100     
101     public void testClassInheritance () throws Exception JavaDoc {
102         Class JavaDoc c = Sample.class;
103         assertEquals (5, Attributes.getAttributes (c).size ());
104         assertEquals (4, Attributes.getAttributes (c, Dependency.class).size ());
105         assertTrue (Attributes.hasAttributeType (c, Dependency.class));
106         assertTrue (Attributes.hasAttributeType (c, ThreadSafe.class));
107         assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample" )));
108         assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "super-sample" )));
109         assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample-if-1-c" )));
110         assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample-if-2-c" )));
111         assertTrue (Attributes.hasAttribute (c, new ThreadSafe ()));
112     }
113     
114     public void testMethodInheritance () throws Exception JavaDoc {
115         Method JavaDoc m = Sample.class.getMethod ("someMethod", new Class JavaDoc[]{ Integer.TYPE });
116         assertEquals (4, Attributes.getAttributes (m).size ());
117         assertEquals (4, Attributes.getAttributes (m, Dependency.class).size ());
118         assertTrue (Attributes.hasAttributeType (m, Dependency.class));
119         assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "super-some-method-sample" )));
120         assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "sample-some-method2" ) ));
121         assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "sample-if-1" ) ));
122         assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "sample-if-2" ) ));
123     }
124     
125     public void testPrivateMethodNonInheritance () throws Exception JavaDoc {
126         Method JavaDoc m = Sample.class.getDeclaredMethod ("privateMethod", new Class JavaDoc[]{});
127         assertEquals (1, Attributes.getAttributes (m).size ());
128         assertEquals (1, Attributes.getAttributes (m, Dependency.class).size ());
129         assertTrue (Attributes.hasAttributeType (m, Dependency.class));
130         assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "sample-privateMethod" ) ));
131         
132         m = SuperSample.class.getDeclaredMethod ("privateMethod", new Class JavaDoc[]{});
133         assertEquals (1, Attributes.getAttributes (m).size ());
134         assertEquals (1, Attributes.getAttributes (m, Dependency.class).size ());
135         assertTrue (Attributes.hasAttributeType (m, Dependency.class));
136         assertTrue (Attributes.hasAttribute (m, new Dependency ( SampleService.class, "super-privateMethod" ) ));
137     }
138
139     public void testParameterAndReturnAttributes () throws Exception JavaDoc {
140         Method JavaDoc m = Sample.class.getMethod ("methodWithAttributes", new Class JavaDoc[]{ Integer.TYPE, Integer.TYPE });
141         assertEquals (0, Attributes.getAttributes (m).size ());
142         assertEquals (2, Attributes.getReturnAttributes (m).size ());
143         assertTrue (Attributes.hasReturnAttribute (m, new Dependency ( SampleService.class, "sample-return" ) ));
144         assertTrue (Attributes.hasReturnAttribute (m, new Dependency ( SampleService.class, "sample-if-return" ) ));
145                 
146         assertEquals (0, Attributes.getParameterAttributes (m, 0).size ());
147         assertEquals (2, Attributes.getParameterAttributes (m, 1).size ());
148         assertTrue (Attributes.hasParameterAttribute (m, 1, new Dependency ( SampleService.class, "sample-if-param-2" ) ));
149         assertTrue (Attributes.hasParameterAttribute (m, 1, new ThreadSafe () ));
150     }
151     
152     public void testFieldNonInheritance () throws Exception JavaDoc {
153         Field JavaDoc f = SuperSample.class.getField ("field");
154         assertEquals (2, Attributes.getAttributes (f).size ());
155         assertEquals (1, Attributes.getAttributes (f, ThreadSafe.class).size ());
156         assertEquals (1, Attributes.getAttributes (f, Dependency.class).size ());
157         assertTrue (Attributes.hasAttributeType (f, ThreadSafe.class));
158         assertTrue (Attributes.hasAttributeType (f, Dependency.class));
159         assertTrue (Attributes.hasAttribute (f, new ThreadSafe ()));
160         assertTrue (Attributes.hasAttribute (f, new Dependency ( SampleService.class, "super-field" )));
161         
162         f = Sample.class.getField ("field");
163         assertEquals (1, Attributes.getAttributes (f).size ());
164         assertEquals (1, Attributes.getAttributes (f, ThreadSafe.class).size ());
165         assertTrue (Attributes.hasAttributeType (f, ThreadSafe.class));
166         assertTrue (Attributes.hasAttribute (f, new ThreadSafe ()));
167                 
168         f = SuperSample.class.getField ("noAttributesInSubClass");
169         assertEquals (1, Attributes.getAttributes (f).size ());
170         assertEquals (1, Attributes.getAttributes (f, Dependency.class).size ());
171         assertTrue (Attributes.hasAttribute (f, new Dependency ( SampleService.class, "super-noattrs" )));
172         assertTrue (Attributes.hasAttributeType (f, Dependency.class));
173         
174         f = Sample.class.getField ("noAttributesInSubClass");
175         assertEquals (0, Attributes.getAttributes (f).size ());
176         assertEquals (0, Attributes.getAttributes (f, Dependency.class).size ());
177         assertTrue (!Attributes.hasAttribute (f, new Dependency ( SampleService.class, "super-noattrs" )));
178         assertTrue (!Attributes.hasAttributeType (f, Dependency.class));
179     }
180     
181     public void testNoAttributes () throws Exception JavaDoc {
182         Method JavaDoc m = Sample.class.getMethod ("methodWithNoAttributes", new Class JavaDoc[0]);
183         assertEquals (0, Attributes.getAttributes (m).size ());
184     }
185     
186     /**
187      * Ensure that loading a class with the same name from two different class loaders
188      * won't mess up the attribute cache.
189      */

190     public void testClassLoaderKeying () throws Exception JavaDoc {
191         URLClassLoader JavaDoc cl1 = new URLClassLoader JavaDoc (new URL JavaDoc[]{new File JavaDoc ("target/cl1/").toURL ()}, getClass().getClassLoader ());
192         URLClassLoader JavaDoc cl2 = new URLClassLoader JavaDoc (new URL JavaDoc[]{new File JavaDoc ("target/cl2/").toURL ()}, getClass().getClassLoader ());
193         
194         Class JavaDoc cl1Class = cl1.loadClass ("TestClass");
195         Class JavaDoc cl2Class = cl2.loadClass ("TestClass");
196         
197         assertEquals ("[[TestAttribute 1]]", Attributes.getAttributes (cl1Class).toString ());
198         assertEquals ("[[TestAttribute TestClass]]", Attributes.getAttributes (cl2Class).toString ());
199     }
200     
201     public void testInnerClasses () throws Exception JavaDoc {
202         Class JavaDoc c = Sample.InnerSample.class;
203         assertEquals (1, Attributes.getAttributes (c).size ());
204         assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ());
205         assertTrue (Attributes.hasAttributeType (c, Dependency.class));
206         assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "inner-sample" )));
207     }
208     
209     public void testNamedParameters () throws Exception JavaDoc {
210         Method JavaDoc m = Sample.class.getMethod ("methodWithNamedParameters", new Class JavaDoc[]{ });
211         assertEquals (1, Attributes.getAttributes (m).size ());
212         assertEquals (1, Attributes.getAttributes (m, BeanAttribute.class).size ());
213         assertTrue (Attributes.hasAttributeType (m, BeanAttribute.class));
214         BeanAttribute ba = new BeanAttribute (1, "a");
215         ba.setName ("Smith, John \"Agent\"");
216         ba.setAnotherNumber (42);
217         assertTrue (Attributes.hasAttribute (m, ba));
218     }
219 }
Popular Tags