KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
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 /**
36  * @author Vladimir Hudec
37  *
38  * [TODO]: anonymous classes should be also considered
39  */

40 public class TestTest extends NbTestCase {
41    
42     /** Need to be defined because of JUnit */
43     public TestTest(String JavaDoc 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     /** Use for execution inside IDE */
55     public static void main(java.lang.String JavaDoc[] 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 JavaDoc 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 JavaDoc {
97         private final LinkedList JavaDoc iterators = new LinkedList JavaDoc();
98         private Iterator JavaDoc currentIterator;
99         private Object JavaDoc lastItem = null;
100             
101         public It(TransientElement te) {
102             currentIterator = te.getChildren().iterator();
103         }
104             
105         public It(List JavaDoc 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 JavaDoc temp = currentIterator.next();
117 // if (temp instanceof List) {
118
// iterators.add(((List)temp).iterator());
119
// iterators.add(currentIterator);
120
// currentIterator = (Iterator) iterators.remove(0);
121
// }
122
// else if (temp instanceof TransientElement) {
123
// iterators.add(((TransientElement)temp).getChildren().iterator());
124
// }
125
if (temp instanceof List JavaDoc) {
126                         iterators.add(0, currentIterator);
127                         currentIterator = ((List JavaDoc)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 JavaDoc) iterators.remove(0);
146                 }
147             }
148             return lastItem != null;
149         }
150             
151         public Object JavaDoc next() {
152             //lock();
153
try {
154                 if (hasNext()) {
155                     Object JavaDoc result = lastItem;
156                     lastItem = null;
157                     return result;
158                 } else {
159                     throw new NoSuchElementException JavaDoc();
160                 }
161             } finally {
162                 //unlock();
163
}
164         }
165             
166         public void remove() {
167             throw new UnsupportedOperationException JavaDoc();
168         }
169     }
170
171 // variable_initializer: expression
172
// | array_initializer -> ArrayInitializationImpl
173
//
174
// expression: assignment_expression
175
//
176
// assignment_expression: conditional_expression -> ConditionImpl
177
// | assignment -> AssignmentImpl
178

179     private void printInitialValue(InitialValue iv, String JavaDoc 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 JavaDoc it = new It((TransientElement) iv); it.hasNext(); ) {
192             Object JavaDoc o = it.next();
193             System.out.println(".."+o);
194         }
195     }
196
197 // block_statement: local_variable_declaration_statement
198
// | class_declaration
199
// | statement
200
//
201
// statement: labeled_statement
202
// | if_statement
203
// | while_statement
204
// | for_statement
205
// | block
206
// | empty_statement
207
// | expression_statement
208
// | switch_statement
209
// | do_statement
210
// | break_statement
211
// | continue_statement
212
// | return_statement
213
// | synchronized_statement
214
// | throw_statement
215
// | try_statement
216
// | assert_statement
217

218
219     
220     private void printStatementBlock(StatementBlock sb, String JavaDoc desc) {
221         if (sb == null) {
222             System.out.println(desc+": statement block is null");
223             return;
224         }
225         List JavaDoc 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 JavaDoc it = statements.iterator(); it.hasNext(); ) {
233             Object JavaDoc o = it.next();
234             System.out.println(" "+o);
235         }
236
237         for (Iterator JavaDoc it = new It(sb); it.hasNext(); ) {
238             Object JavaDoc 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 // Field field1_3 = class1.getField("field1_3", false);
250
// InitialValue field1_3_init = field1_3.getInitialValue();
251
// printInitialValue(field1_3_init, "field1_3");
252

253             List JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc();
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 JavaDoc();
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