KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > usages > FindFeatureUsagesTest


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
20 package org.netbeans.jmi.javamodel.usages;
21
22 import java.util.*;
23 import junit.textui.TestRunner;
24 import org.netbeans.jmi.javamodel.*;
25 import org.netbeans.jmi.javamodel.MethodInvocation;
26 import org.netbeans.jmi.javamodel.VariableAccess;
27 import org.netbeans.jmi.javamodel.codegen.Utility;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.junit.NbTestSuite;
30 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
31
32 /**
33  * @author Daniel Prusa
34  */

35 public class FindFeatureUsagesTest extends NbTestCase {
36
37     /** Need to be defined because of JUnit */
38     public FindFeatureUsagesTest(String JavaDoc name) {
39         super(name);
40         
41     }
42     
43     public static NbTestSuite suite() {
44         NbTestSuite suite = new NbTestSuite();
45         suite.addTest(new FindFeatureUsagesTest("testColorPicker"));
46         suite.addTest(new FindFeatureUsagesTest("testColorPreview"));
47         suite.addTest(new FindFeatureUsagesTest("testTed"));
48         return suite;
49     }
50     
51     /** Use for execution inside IDE */
52     public static void main(java.lang.String JavaDoc[] args) {
53         TestRunner.run(suite());
54     }
55     
56     public void testColorPicker() {
57         Map map = collectFeatures("examples.colorpicker.ColorPicker");
58         // fields
59
checkUsers(map, "blueSlider", 8);
60         checkUsers(map, "colorPreview1", 7);
61         checkUsers(map, "colorPreviewPanel", 7);
62         checkUsers(map, "greenSlider", 8);
63         checkUsers(map, "redSlider", 8);
64         checkUsers(map, "sliderPanel", 7);
65         // methods
66
checkUsers(map, "blueSliderStateChanged", 1);
67         checkUsers(map, "exitForm", 1);
68         checkUsers(map, "greenSliderStateChanged", 1);
69         checkUsers(map, "initComponents", 1);
70         checkUsers(map, "main", 0);
71         checkUsers(map, "redSliderStateChanged", 1);
72     }
73
74     public void testColorPreview() {
75         Map map = collectFeatures("examples.colorpicker.ColorPreview");
76         // fields
77
checkUsers(map, "blue", 5);
78         checkUsers(map, "green", 5);
79         checkUsers(map, "propertyChangeSupport", 6);
80         checkUsers(map, "red", 5);
81         // methods
82
checkUsers(map, "addPropertyChangeListener", 0);
83         checkUsers(map, "getBlue", 0);
84         checkUsers(map, "getGreen", 0);
85         checkUsers(map, "getRed", 0);
86         checkUsers(map, "removePropertyChangeListener", 0);
87         checkUsers(map, "setBlue", 1);
88         checkUsers(map, "setGreen", 1);
89         checkUsers(map, "setRed", 1);
90     }
91     
92     public void testTed() {
93         Map map = collectFeatures("examples.texteditor.Ted");
94         // fields
95
checkUsers(map, "textBox", 8);
96         checkUsers(map, "tedMenuBar", 7);
97         checkUsers(map, "saveMenuItem", 8);
98         checkUsers(map, "saveAsMenuItem", 7);
99         checkUsers(map, "openMenuItem", 8);
100         checkUsers(map, "jSeparator1", 2);
101         checkUsers(map, "fileName", 7);
102         // methods
103
checkUsers(map, "doSave", 2);
104         checkUsers(map, "doSaveAs", 2);
105         checkUsers(map, "initComponents", 1);
106         checkUsers(map, "openMenuItemActionPerformed", 1);
107         checkUsers(map, "saveAsMenuItemActionPerformed", 1);
108         checkUsers(map, "saveMenuItemActionPerformed", 1);
109         checkUsers(map, "newMenuItemActionPerformed", 1);
110         checkUsers(map, "exitMenuItemActionPerformed", 1);
111         checkUsers(map, "findMenuItemActionPerformed", 1);
112     }
113     
114     private Map collectFeatures (String JavaDoc className) {
115         Utility.beginTrans(false);
116         try {
117             HashMap map = new HashMap();
118             JavaClass jc = Utility.findClass(className);
119             for (Iterator iter = jc.getFeatures().iterator(); iter.hasNext();) {
120                 Feature f = (Feature) iter.next();
121                 String JavaDoc name;
122                 if ((f instanceof Field) || (f instanceof Method)) {
123                     name = f.getName();
124                 } else if (f instanceof Constructor) {
125                     name = "<init>";
126                 } else {
127                     continue;
128                 }
129                 map.put(name, f);
130             }
131             return map;
132         } finally {
133             Utility.endTrans();
134         }
135     }
136     
137     private void checkUsers(Map map, String JavaDoc name, int expectedCount) {
138         Utility.beginTrans(false);
139         try {
140             Feature feature = (Feature)map.get(name);
141             assertNotNull(feature);
142             Collection usages = feature.getReferences();
143
144             if (expectedCount != usages.size()) {
145                 fail("feature: " + name + ", expected # of usages: " + expectedCount + ", usages found: " + usages.size());
146             }
147
148             Class JavaDoc usageClass = feature instanceof Field ? VariableAccess.class : MethodInvocation.class;
149
150             for (Iterator iter = usages.iterator(); iter.hasNext();) {
151                 Object JavaDoc usage = iter.next();
152                 if (!usageClass.isAssignableFrom(usage.getClass())) {
153                     fail("feature: " + name + ", expected an instance of " + usageClass.getName() +
154                         ", found " + usage.getClass().getName());
155                 }
156             }
157         } finally {
158             Utility.endTrans();
159         }
160     }
161         
162 }
163
Popular Tags