KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > ImplementsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.jmi.javamodel;
20
21 import java.util.*;
22 import javax.jmi.reflect.RefAssociation;
23 import javax.jmi.reflect.RefAssociationLink;
24 import junit.textui.TestRunner;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.junit.NbTestSuite;
27 import org.netbeans.modules.javacore.jmiimpl.javamodel.ImplementsImpl;
28 import org.netbeans.jmi.javamodel.codegen.Utility;
29
30
31 /**
32  * @author Vladimir Hudec
33  *
34  * [TODO]: anonymous classes should be also considered
35  */

36 public class ImplementsTest extends NbTestCase {
37
38     /** Need to be defined because of JUnit */
39     public ImplementsTest(String JavaDoc name) {
40         super(name);
41         
42     }
43     
44     public static NbTestSuite suite() {
45         NbTestSuite suite = new NbTestSuite();
46         suite.addTest(new ImplementsTest("testAllLinks"));
47         suite.addTest(new ImplementsTest("testNullArguments"));
48         suite.addTest(new ImplementsTest("testImproperArguments"));
49         suite.addTest(new ImplementsTest("testAddRemoveAssociation"));
50         suite.addTest(new ImplementsTest("testAddRemoveInterface"));
51         suite.addTest(new ImplementsTest("testAddRemoveImplementor"));
52         return suite;
53     }
54     
55     /** Use for execution inside IDE */
56     public static void main(java.lang.String JavaDoc[] args) {
57         TestRunner.run(suite());
58     }
59
60     JavaModelPackage pkg;
61     ImplementsImpl implementsImpl;
62     int implementsNumOfClassesInterfaces;
63     int implementsNumOfInterfacesImplementors;
64     JavaClass class1, class2, class3, interface1, interface2, interface3;
65     int class1NumOfInterfaces, class2NumOfInterfaces, class3NumOfInterfaces;
66     int interface1NumOfImplementors, interface2NumOfImplementors, interface3NumOfImplementors;
67     
68     protected void setUp() {
69         class1 = Utility.findClass("org.netbeans.test.classes.Class1");
70         pkg = (JavaModelPackage) class1.refImmediatePackage();
71         implementsImpl = (ImplementsImpl) pkg.getImplements();
72         try { Thread.sleep(2000); } catch (Exception JavaDoc ex) {}
73         
74         assertNotNull("Class1", class1);
75         assertFalse("Class1 is instance of UnresolvedClass", class1 instanceof UnresolvedClass);
76         class2 = Utility.findClass("org.netbeans.test.classes.Class2");
77         assertNotNull("Class2", class2);
78         assertFalse("Class2 is instance of UnresolvedClass", class2 instanceof UnresolvedClass);
79         class3 = Utility.findClass("org.netbeans.test.classes.Class3");
80         assertNotNull("Class3", class3);
81         assertFalse("Class3 is instance of UnresolvedClass", class3 instanceof UnresolvedClass);
82         interface1 = Utility.findClass("org.netbeans.test.interfaces.Interface1");
83         assertNotNull("Interface1", interface1);
84         assertFalse("Interface1 is instance of UnresolvedClass", interface1 instanceof UnresolvedClass);
85         interface2 = Utility.findClass("org.netbeans.test.interfaces.Interface2");
86         assertNotNull("Interface2", interface2);
87         assertFalse("Interface2 is instance of UnresolvedClass", interface2 instanceof UnresolvedClass);
88         interface3 = Utility.findClass("org.netbeans.test.interfaces.Interface3");
89         assertNotNull("Interface3", interface3);
90         assertFalse("Interface3 is instance of UnresolvedClass", interface3 instanceof UnresolvedClass);
91
92         // Class1 implements Interface1
93
// Class2 extends Class1 implements Interface2
94
// Class3.Class4 implements Interface1
95
// Interface3 extends Interface1, Interface2 ->
96
// Interface3 implements Interface1
97
// Interface3 implements Interface1
98
implementsNumOfClassesInterfaces = 5;
99         implementsNumOfInterfacesImplementors = 5;
100         class1NumOfInterfaces = 1;
101         class2NumOfInterfaces = 1;
102         class3NumOfInterfaces = 0;
103         interface1NumOfImplementors = 3;
104         interface2NumOfImplementors = 2;
105         interface3NumOfImplementors = 0;
106
107         checkNumber("Class1 interfaces", class1NumOfInterfaces, class1.getInterfaces());
108         checkNumber("Class2 interfaces", class2NumOfInterfaces, class2.getInterfaces());
109         checkNumber("Class3 interfaces", class3NumOfInterfaces, class3.getInterfaces());
110         checkNumber("Interface1 implementors", interface1NumOfImplementors, interface1.getImplementors());
111         checkNumber("Interface2 implementors", interface2NumOfImplementors, interface2.getImplementors());
112         checkNumber("Interface3 implementors", interface3NumOfImplementors, interface3.getImplementors());
113     }
114     
115     protected void printAllLinks(RefAssociation refAssociation) {
116         Collection impls = refAssociation.refAllLinks();
117         int numOfLinks = 0;
118         for (Iterator it = impls.iterator(); it.hasNext(); ) {
119             numOfLinks++;
120             RefAssociationLink link = (RefAssociationLink) it.next();
121             JavaClass superInterface = (JavaClass) link.refFirstEnd();
122             JavaClass implementor = (JavaClass) link.refSecondEnd();
123             System.out.println(" #"+numOfLinks+": "+implementor.getName()+" implements "+superInterface.getName());
124         }
125     }
126     
127     protected void checkNumber(String JavaDoc msg, int expectedValue, int actualValue) {
128         assertEquals("Total number of "+msg, expectedValue, actualValue);
129     }
130     
131     protected void checkNumber(String JavaDoc msg, int expectedValue, Collection coll) {
132         if (expectedValue == 0) {
133             assertTrue("Total number of "+msg, coll.isEmpty());
134         }
135         else {
136             checkNumber(msg, expectedValue, coll.size());
137         }
138     }
139     
140     public void testAllLinks() {
141         Utility.beginTrans(false);
142         try {
143             Collection impls = implementsImpl.refAllLinks();
144             int numOfLinks = 0;
145             int numOfClassIntefaces = 0;
146             int numOfInterfaceImplementors = 0;
147             
148             String JavaDoc packageOfClasses = class1.getResource().getPackageName();
149             String JavaDoc packageOfInterfaces = interface1.getResource().getPackageName();
150             
151             for (Iterator it = impls.iterator(); it.hasNext(); ) {
152                 numOfLinks++;
153                 RefAssociationLink link = (RefAssociationLink) it.next();
154                 JavaClass superInterface = (JavaClass) link.refFirstEnd();
155                 JavaClass implementor = (JavaClass) link.refSecondEnd();
156                 if (implementor.getName().startsWith(packageOfClasses) || implementor.getName().startsWith(packageOfInterfaces))
157                     ++numOfClassIntefaces;
158                 if (superInterface.getName().startsWith(packageOfClasses) || superInterface.getName().startsWith(packageOfInterfaces))
159                     ++numOfInterfaceImplementors;
160                 System.out.println(" #"+numOfLinks+": "+implementor.getName()+" implements "+superInterface.getName());
161             }
162             
163             checkNumber("classes interfaces", implementsNumOfClassesInterfaces, numOfClassIntefaces);
164             checkNumber("interfaces implementors", implementsNumOfInterfacesImplementors, numOfInterfaceImplementors);
165         }
166         finally {
167             Utility.endTrans();
168         }
169     }
170     
171     public void testNullArguments() {
172         String JavaDoc msg = "Association's operation with null argument should throw exception";
173         boolean fail = true;
174         Utility.beginTrans(true);
175         try {
176             try {
177                 implementsImpl.exists(null, class1);
178                 fail(msg+" (exists)");
179             }
180             catch (NullPointerException JavaDoc ex) {
181             }
182             try {
183                 implementsImpl.exists(interface1, null);
184                 fail(msg+" (exists)");
185             }
186             catch (NullPointerException JavaDoc ex) {
187             }
188             try {
189                 implementsImpl.add(null, class1);
190                 fail(msg+" (add)");
191             }
192             catch (NullPointerException JavaDoc ex) {
193             }
194             try {
195                 implementsImpl.add(interface1, null);
196                 fail(msg+" (add)");
197             }
198             catch (NullPointerException JavaDoc ex) {
199             }
200             try {
201                 implementsImpl.remove(null, class1);
202                 fail(msg+" (remove)");
203             }
204             catch (NullPointerException JavaDoc ex) {
205             }
206             try {
207                 implementsImpl.remove(interface1, null);
208                 fail(msg+" (remove)");
209             }
210             catch (NullPointerException JavaDoc ex) {
211             }
212             try {
213                 implementsImpl.getImplementors(interface1).add(null);
214                 fail(msg+" (getImplementors.add)");
215             }
216             catch (NullPointerException JavaDoc ex) {
217             }
218             try {
219                 implementsImpl.getImplementors(interface3).iterator().remove();
220                 fail(msg+" (getImplementors.iterator.remove)");
221             }
222             catch (IllegalStateException JavaDoc ex) {
223             }
224             catch (NullPointerException JavaDoc ex) {
225                 // [TODO] - fix in ReferenceCol/ListWrapper
226
}
227             
228             fail = false;
229         }
230         finally {
231             Utility.endTrans(fail);
232         }
233     }
234     
235     public void testImproperArguments() {
236         String JavaDoc msg = "Association's operation with improper argument should throw exception";
237
238         Object JavaDoc obj = new Object JavaDoc();
239         boolean fail = true;
240         Utility.beginTrans(true);
241         try {
242             try {
243                 implementsImpl.getImplementors(interface1).add(obj);
244                 fail(msg+" (getImplementors.add)");
245             }
246             catch (javax.jmi.reflect.TypeMismatchException ex) {
247             }
248             
249             fail = false;
250         }
251         finally {
252             Utility.endTrans(fail);
253         }
254
255         fail = true;
256         Utility.beginTrans(true);
257         try {
258             try {
259                 implementsImpl.getInterfaces(class1).add(obj);
260                 fail(msg+" (getInterfaces.add)");
261             }
262             catch (javax.jmi.reflect.TypeMismatchException ex) {
263             }
264             
265             fail = false;
266         }
267         finally {
268             Utility.endTrans(fail);
269         }
270
271         fail = true;
272         Utility.beginTrans(true);
273         try {
274             try {
275                 implementsImpl.add(interface1, class1);
276                 fail(msg+" (Adding existing association)");
277             }
278             catch (javax.jmi.reflect.WrongSizeException ex) {
279             }
280             
281             fail = false;
282         }
283         finally {
284             Utility.endTrans(fail);
285         }
286
287         fail = true;
288         Utility.beginTrans(true);
289         try {
290             boolean result = implementsImpl.remove(interface2, class1);
291             assertFalse("Trying remove nonexisting association", result);
292             
293             fail = false;
294         }
295         finally {
296             Utility.endTrans(fail);
297         }
298     }
299     
300     public void testAddRemoveAssociation() {
301         boolean fail = true;
302         Utility.beginTrans(true);
303         try {
304             implementsImpl.add(interface2, class1);
305             
306             checkNumber("Class1 interfaces", class1NumOfInterfaces+1, class1.getInterfaces());
307             checkNumber("Interface2 implementors", interface2NumOfImplementors+1, interface2.getImplementors());
308             
309             fail = false;
310         }
311         finally {
312             Utility.endTrans(fail);
313         }
314         
315         fail = true;
316         Utility.beginTrans(true);
317         try {
318             implementsImpl.remove(interface2, class1);
319             
320             checkNumber("Class1 interfaces", class1NumOfInterfaces, class1.getInterfaces());
321             checkNumber("Interface2 implementors", interface2NumOfImplementors, interface2.getImplementors());
322             
323             fail = false;
324         }
325         finally {
326             Utility.endTrans(fail);
327         }
328     }
329     
330     public void testAddRemoveInterface() {
331         boolean fail = true;
332         Utility.beginTrans(true);
333         try {
334             List interfaces = implementsImpl.getInterfaces(class1);
335             interfaces.add(interface2);
336             
337             checkNumber("Class1 interfaces", class1NumOfInterfaces+1, class1.getInterfaces());
338             checkNumber("Interface2 implementors", interface2NumOfImplementors+1, interface2.getImplementors());
339             
340             fail = false;
341         }
342         finally {
343             Utility.endTrans(fail);
344         }
345         
346         fail = true;
347         Utility.beginTrans(true);
348         try {
349             List interfaces = implementsImpl.getInterfaces(class1);
350             interfaces.remove(interface2);
351             
352             checkNumber("Class1 interfaces", class1NumOfInterfaces, class1.getInterfaces());
353             checkNumber("Interface2 implementors", interface2NumOfImplementors, interface2.getImplementors());
354             
355             fail = false;
356         }
357         finally {
358             Utility.endTrans(fail);
359         }
360
361         fail = true;
362         Utility.beginTrans(true);
363         try {
364             List interfaces = implementsImpl.getInterfaces(class1);
365             List list = new ArrayList();
366             list.add(interface2);
367             list.add(interface3);
368             interfaces.addAll(list);
369             
370             checkNumber("Class1 interfaces", class1NumOfInterfaces+2, class1.getInterfaces());
371             checkNumber("Interface2 implementors", interface2NumOfImplementors+1, interface2.getImplementors());
372             checkNumber("Interface3 implementors", interface3NumOfImplementors+1, interface3.getImplementors());
373             
374             fail = false;
375         }
376         finally {
377             Utility.endTrans(fail);
378         }
379         
380         fail = true;
381         Utility.beginTrans(true);
382         try {
383             List interfaces = implementsImpl.getInterfaces(class1);
384             List list = new ArrayList();
385             list.add(interface2);
386             list.add(interface3);
387             interfaces.removeAll(list);
388             
389             checkNumber("Class1 interfaces", class1NumOfInterfaces, class1.getInterfaces());
390             checkNumber("Interface2 implementors", interface2NumOfImplementors, interface2.getImplementors());
391             checkNumber("Interface3 implementors", interface3NumOfImplementors, interface3.getImplementors());
392             
393             fail = false;
394         }
395         finally {
396             Utility.endTrans(fail);
397         }
398
399         fail = true;
400         Utility.beginTrans(true);
401         try {
402             List interfaces = implementsImpl.getInterfaces(class1);
403             ListIterator it = interfaces.listIterator();
404             while (it.hasNext())
405                 it.next();
406             it.add(interface2);
407             
408             checkNumber("Class1 interfaces", class1NumOfInterfaces+1, class1.getInterfaces());
409             checkNumber("Interface2 implementors", interface2NumOfImplementors+1, interface2.getImplementors());
410             
411             fail = false;
412         }
413         finally {
414             Utility.endTrans(fail);
415         }
416         
417         fail = true;
418         Utility.beginTrans(true);
419         try {
420             List interfaces = implementsImpl.getInterfaces(class1);
421             ListIterator it = interfaces.listIterator();
422             while (it.hasNext()) {
423                 JavaClass _interface = (JavaClass) it.next();
424                 if (_interface.equals(interface2)) {
425                     it.remove();
426                     break;
427                 }
428             }
429             
430             checkNumber("Class1 interfaces", class1NumOfInterfaces, class1.getInterfaces());
431             checkNumber("Interface2 implementors", interface2NumOfImplementors, interface2.getImplementors());
432             
433             fail = false;
434         }
435         finally {
436             Utility.endTrans(fail);
437         }
438     }
439     
440     public void testAddRemoveImplementor() {
441         boolean fail = true;
442         Utility.beginTrans(true);
443         try {
444             Collection implementors = implementsImpl.getImplementors(interface2);
445             implementors.add(class1);
446             
447             checkNumber("Class1 interfaces", class1NumOfInterfaces+1, class1.getInterfaces());
448             checkNumber("Interface2 implementors", interface2NumOfImplementors+1, interface2.getImplementors());
449             
450             fail = false;
451         }
452         finally {
453             Utility.endTrans(fail);
454         }
455         
456         fail = true;
457         Utility.beginTrans(true);
458         try {
459             Collection implementors = implementsImpl.getImplementors(interface2);
460             implementors.remove(class1);
461             
462             checkNumber("Class1 interfaces", class1NumOfInterfaces, class1.getInterfaces());
463             checkNumber("Interface2 implementors", interface2NumOfImplementors, interface2.getImplementors());
464             
465             fail = false;
466         }
467         finally {
468             Utility.endTrans(fail);
469         }
470
471         fail = true;
472         Utility.beginTrans(true);
473         try {
474             Collection implementors = implementsImpl.getImplementors(interface2);
475             List list = new ArrayList();
476             list.add(class1);
477             list.add(class3);
478             implementors.addAll(list);
479             
480             checkNumber("Class1 interfaces", class1NumOfInterfaces+1, class1.getInterfaces());
481             checkNumber("Class3 interfaces", class3NumOfInterfaces+1, class3.getInterfaces());
482             checkNumber("Interface2 implementors", interface2NumOfImplementors+2, interface2.getImplementors());
483             
484             fail = false;
485         }
486         finally {
487             Utility.endTrans(fail);
488         }
489         
490         fail = true;
491         Utility.beginTrans(true);
492         try {
493             Collection implementors = implementsImpl.getImplementors(interface2);
494             List list = new ArrayList();
495             list.add(class1);
496             list.add(class3);
497             implementors.removeAll(list);
498             
499             checkNumber("Class1 interfaces", class1NumOfInterfaces, class1.getInterfaces());
500             checkNumber("Class3 interfaces", class3NumOfInterfaces, class3.getInterfaces());
501             checkNumber("Interface2 implementors", interface2NumOfImplementors, interface2.getImplementors());
502             
503             fail = false;
504         }
505         finally {
506             Utility.endTrans(fail);
507         }
508
509         fail = true;
510         Utility.beginTrans(true);
511         try {
512             Collection implementors = implementsImpl.getImplementors(interface2);
513             implementors.add(class1);
514
515             checkNumber("Class1 interfaces", class1NumOfInterfaces+1, class1.getInterfaces());
516             checkNumber("Interface2 implementors", interface2NumOfImplementors+1, interface2.getImplementors());
517             
518             fail = false;
519         }
520         finally {
521             Utility.endTrans(fail);
522         }
523         
524         fail = true;
525         Utility.beginTrans(true);
526         try {
527             Collection implementors = implementsImpl.getImplementors(interface2);
528             Iterator it = implementors.iterator();
529             while (it.hasNext()) {
530                 JavaClass _implementor = (JavaClass) it.next();
531                 if (_implementor.equals(class1)) {
532                     it.remove();
533                     break;
534                 }
535             }
536             
537             checkNumber("Class1 interfaces", class1NumOfInterfaces, class1.getInterfaces());
538             checkNumber("Interface2 implementors", interface2NumOfImplementors, interface2.getImplementors());
539             
540             fail = false;
541         }
542         finally {
543             Utility.endTrans(fail);
544         }
545     }
546     
547 }
548
Popular Tags