1 19 package org.netbeans.jmi.javamodel; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.NoSuchElementException ; 26 27 import junit.textui.TestRunner; 28 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.NbTestSuite; 31 import org.netbeans.modules.javacore.jmiimpl.javamodel.TransientElement; 32 import org.netbeans.jmi.javamodel.codegen.Utility; 33 34 35 40 public class TestTest extends NbTestCase { 41 42 43 public TestTest(String name) { 44 super(name); 45 46 } 47 48 public static NbTestSuite suite() { 49 NbTestSuite suite = new NbTestSuite(); 50 suite.addTest(new TestTest("testTest")); 51 return suite; 52 } 53 54 55 public static void main(java.lang.String [] args) { 56 TestRunner.run(suite()); 57 } 58 59 JavaModelPackage pkg; 60 JavaClass class1, class2, class3; 61 JavaClass exception1, exception2; 62 JavaClass interface1, interface2, interface3; 63 64 protected void setUp() { 65 pkg = (JavaModelPackage) class1.refImmediatePackage(); 66 try { Thread.sleep(2000); } catch (Exception ex) {} 67 68 class1 = Utility.findClass("org.netbeans.test.classes.Class1"); 69 assertNotNull("Class1", class1); 70 assertFalse("Class1 is instance of UnresolvedClass", class1 instanceof UnresolvedClass); 71 class2 = Utility.findClass("org.netbeans.test.classes.Class2"); 72 assertNotNull("Class2", class2); 73 assertFalse("Class2 is instance of UnresolvedClass", class2 instanceof UnresolvedClass); 74 class3 = Utility.findClass("org.netbeans.test.classes.Class3"); 75 assertNotNull("Class3", class3); 76 assertFalse("Class3 is instance of UnresolvedClass", class3 instanceof UnresolvedClass); 77 78 exception1 = Utility.findClass("org.netbeans.test.exceptions.Exception1"); 79 assertNotNull("Exception1", exception1); 80 assertFalse("Exception1 is instance of UnresolvedClass", exception1 instanceof UnresolvedClass); 81 exception2 = Utility.findClass("org.netbeans.test.exceptions.Exception2"); 82 assertNotNull("Exception2", exception2); 83 assertFalse("Exception2 is instance of UnresolvedClass", exception2 instanceof UnresolvedClass); 84 85 interface1 = Utility.findClass("org.netbeans.test.interfaces.Interface1"); 86 assertNotNull("Interface1", interface1); 87 assertFalse("Interface1 is instance of UnresolvedClass", interface1 instanceof UnresolvedClass); 88 interface2 = Utility.findClass("org.netbeans.test.interfaces.Interface2"); 89 assertNotNull("Interface2", interface2); 90 assertFalse("Interface2 is instance of UnresolvedClass", interface2 instanceof UnresolvedClass); 91 interface3 = Utility.findClass("org.netbeans.test.interfaces.Interface3"); 92 assertNotNull("Interface3", interface3); 93 assertFalse("Interface3 is instance of UnresolvedClass", interface3 instanceof UnresolvedClass); 94 } 95 96 private class It implements Iterator { 97 private final LinkedList iterators = new LinkedList (); 98 private Iterator currentIterator; 99 private Object lastItem = null; 100 101 public It(TransientElement te) { 102 currentIterator = te.getChildren().iterator(); 103 } 104 105 public It(List list) { 106 currentIterator = list.iterator(); 107 } 108 109 public It(StatementBlock sb) { 110 currentIterator = sb.getStatements().iterator(); 111 } 112 113 public boolean hasNext() { 114 while (lastItem == null && (!iterators.isEmpty() || currentIterator.hasNext())) { 115 while (lastItem == null && currentIterator.hasNext()) { 116 Object temp = currentIterator.next(); 117 if (temp instanceof List ) { 126 iterators.add(0, currentIterator); 127 currentIterator = ((List )temp).iterator(); 128 } 129 else if (temp instanceof TransientElement) { 130 iterators.add(0, currentIterator); 131 currentIterator = ((TransientElement)temp).getChildren().iterator(); 132 } 133 else if (temp == null) { 134 System.out.println("hasNext(): null element"); 135 } 136 else { 137 System.out.println("hasNext(): not transient element"); 138 } 139 140 if (temp instanceof TransientElement) { 141 lastItem = temp; 142 } 143 } 144 if (lastItem == null) { 145 currentIterator = (Iterator ) iterators.remove(0); 146 } 147 } 148 return lastItem != null; 149 } 150 151 public Object next() { 152 try { 154 if (hasNext()) { 155 Object result = lastItem; 156 lastItem = null; 157 return result; 158 } else { 159 throw new NoSuchElementException (); 160 } 161 } finally { 162 } 164 } 165 166 public void remove() { 167 throw new UnsupportedOperationException (); 168 } 169 } 170 171 179 private void printInitialValue(InitialValue iv, String desc) { 180 if (iv == null) { 181 System.out.println(desc+": initial value is null"); 182 return; 183 } 184 185 System.out.println(desc+": "+iv); 186 187 if (!(iv instanceof TransientElement)) { 188 return; 189 } 190 191 for (Iterator it = new It((TransientElement) iv); it.hasNext(); ) { 192 Object o = it.next(); 193 System.out.println(".."+o); 194 } 195 } 196 197 218 219 220 private void printStatementBlock(StatementBlock sb, String desc) { 221 if (sb == null) { 222 System.out.println(desc+": statement block is null"); 223 return; 224 } 225 List statements = sb.getStatements(); 226 if (statements == null) { 227 System.out.println(desc+": statements list is null"); 228 return; 229 } 230 231 System.out.println(desc+":"); 232 for (Iterator it = statements.iterator(); it.hasNext(); ) { 233 Object o = it.next(); 234 System.out.println(" "+o); 235 } 236 237 for (Iterator it = new It(sb); it.hasNext(); ) { 238 Object o = it.next(); 239 System.out.println(".."+o); 240 if (o instanceof LocalVariable) { 241 System.out.println("^^"+((LocalVariable)o).getType()); 242 } 243 } 244 } 245 246 public void testTest() { 247 Utility.beginTrans(false); 248 try { 249 253 List list = new ArrayList (); 254 Type intType = pkg.getType().resolve("int"); 255 list.add(intType); 256 Type stringType = pkg.getType().resolve("String"); 257 list.add(stringType); 258 Method method1_1 = class1.getMethod("method1_1", list, false); 259 StatementBlock method1_1_body = method1_1.getBody(); 260 printStatementBlock(method1_1_body, "method1_1"); 261 262 list = new ArrayList (); 263 list.add(intType); 264 list.add(interface1); 265 Method method1_2 = class1.getMethod("method1_2", list, false); 266 StatementBlock method1_2_body = method1_2.getBody(); 267 printStatementBlock(method1_2_body, "method1_2"); 268 269 list = new ArrayList (); 270 list.add(intType); 271 Method method1_3 = class1.getMethod("method1_3", list, false); 272 StatementBlock method1_3_body = method1_3.getBody(); 273 printStatementBlock(method1_3_body, "method1_3"); 274 } 275 finally { 276 Utility.endTrans(); 277 } 278 } 279 } 280 | Popular Tags |