1 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 35 public class FindFeatureUsagesTest extends NbTestCase { 36 37 38 public FindFeatureUsagesTest(String 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 52 public static void main(java.lang.String [] args) { 53 TestRunner.run(suite()); 54 } 55 56 public void testColorPicker() { 57 Map map = collectFeatures("examples.colorpicker.ColorPicker"); 58 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 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 checkUsers(map, "blue", 5); 78 checkUsers(map, "green", 5); 79 checkUsers(map, "propertyChangeSupport", 6); 80 checkUsers(map, "red", 5); 81 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 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 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 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 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 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 usageClass = feature instanceof Field ? VariableAccess.class : MethodInvocation.class; 149 150 for (Iterator iter = usages.iterator(); iter.hasNext();) { 151 Object 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 |