001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.visualweb.insync.java;
041:
042: import com.sun.rave.designtime.ContextMethod;
043: import java.lang.reflect.Modifier;
044: import java.util.List;
045: import javax.lang.model.element.ExecutableElement;
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048: import org.netbeans.api.java.source.CompilationInfo;
049: import org.netbeans.api.java.source.ElementHandle;
050: import org.netbeans.junit.NbTestSuite;
051: import org.netbeans.modules.visualweb.insync.InsyncTestBase;
052: import org.netbeans.modules.visualweb.insync.beans.Bean;
053: import org.netbeans.modules.visualweb.insync.beans.EventSet;
054: import org.netbeans.modules.visualweb.insync.beans.Property;
055: import org.openide.filesystems.FileObject;
056: import org.openide.util.Exceptions;
057:
058: /**
059: *
060: * @author jdeva
061: */
062: public class MethodTest extends InsyncTestBase {
063: public MethodTest(String testName) {
064: super (testName);
065: }
066:
067: public static void main(java.lang.String[] args) {
068: junit.textui.TestRunner.run(suite());
069: }
070:
071: public static Test suite() {
072: TestSuite suite = new NbTestSuite(MethodTest.class);
073: return suite;
074: }
075:
076: @Override
077: protected void setUp() throws Exception {
078: super .setUp();
079: }
080:
081: @Override
082: protected void tearDown() throws Exception {
083: super .tearDown();
084: }
085:
086: Method getMethod() {
087: return getMethod("_init");
088: }
089:
090: Method getMethod(String name) {
091: FileObject fObj = getJavaFile(getPageBeans()[0]);
092: JavaClass javaClass = JavaClass.getJavaClass(fObj);
093: return javaClass.getMethod(name, new Class[] {});
094: }
095:
096: /**
097: * Test of getJavaClass method, of class Method.
098: */
099: public void testGetJavaClass() {
100: System.out.println("getJavaClass");
101: Method instance = getMethod();
102: JavaClass result = instance.getJavaClass();
103: assertNotNull(result);
104: }
105:
106: /**
107: * Test of getName method, of class Method.
108: */
109: public void testGetName() {
110: System.out.println("getName");
111: String name = "_init";
112: Method instance = getMethod(name);
113: String result = instance.getName();
114: assertEquals(name, result);
115: }
116:
117: /**
118: * Test of getElementHandle method, of class Method.
119: */
120: public void testGetElementHandle() {
121: System.out.println("getElementHandle");
122: Method instance = getMethod();
123: ElementHandle<ExecutableElement> result = instance
124: .getElementHandle();
125: assertNotNull(result);
126: }
127:
128: private List<Bean> addBeans() {
129: String[] types = {
130: "com.sun.data.provider.impl.CachedRowSetDataProvider",
131: "com.sun.sql.rowset.CachedRowSetXImpl" };
132: return createBeans(types);
133: }
134:
135: // /**
136: // * Test of findPropertyStatement method, of class Method.
137: // */
138: // public void testFindPropertyStatement() {
139: // System.out.println("findPropertyStatement");
140: // testAddPropertySetStatements();
141: // }
142:
143: /**
144: * Test of addEventSetStatement method, of class Method.
145: */
146: public void testAddEventSetStatement() {
147: try {
148: System.out.println("addEventSetStatement");
149: String[] types = { "com.sun.data.provider.impl.CachedRowSetDataProvider" };
150:
151: List<Bean> beans = createBeans(types);
152: Method instance = getMethod();
153: Bean dpBean = beans.get(0);
154: String eventSetName = "dataListener";
155: EventSet eventSet = dpBean.setEventSet(eventSetName);
156:
157: java.lang.reflect.Method m = null;
158: Class clazz = Class
159: .forName("org.netbeans.modules.visualweb.insync.beans.EventSet");
160: m = clazz.getDeclaredMethod("getAdapterType",
161: new Class[] {});
162: m.setAccessible(true);
163: Class aType = (Class) m.invoke(eventSet, new Object[] {});
164: String adapterClassName;
165: if (aType != null) {
166: adapterClassName = aType.getName();
167: } else {
168: adapterClassName = eventSet.getListenerType().getName();
169: }
170:
171: Statement result = instance.addEventSetStatement(dpBean
172: .getName(), eventSet.getAddListenerMethodName(),
173: adapterClassName);
174: assertNotNull(result);
175: } catch (Exception ex) {
176: Exceptions.printStackTrace(ex);
177: }
178: }
179:
180: /**
181: * Test of addPropertySetStatements method, of class Method.
182: */
183: public void testAddPropertySetStatements() {
184: System.out.println("addPropertySetStatements");
185: String[] types = {
186: "com.sun.data.provider.impl.ObjectDataProvider",
187: "com.sun.webui.jsf.component.Button" };
188:
189: List<Bean> beans = createBeans(types);
190: Method instance = getMethod();
191: Bean dpBean = beans.get(0);
192: String propName = "object";
193: String propValSrc = beans.get(1).getName();
194: dpBean.setProperty(propName, null, propValSrc);
195: Property prop = dpBean.getProperty(propName);
196: instance.addPropertySetStatements(dpBean);
197:
198: //Test if the property set statement is added
199: Statement stmt = instance.findPropertyStatement(dpBean
200: .getName(), prop.getWriteMethodName());
201: assertNotNull(stmt);
202: List<Statement> stmts = instance.getPropertySetStatements();
203:
204: //addPropertySetStatements() should ignore markup properties
205: dpBean = beans.get(1);
206: propName = "immediate";
207: propValSrc = "true";
208: dpBean.setProperty(propName, null, propValSrc);
209: instance.addPropertySetStatements(dpBean);
210: assertEquals(instance.getPropertySetStatements().size(), stmts
211: .size() + 1);
212: }
213:
214: /**
215: * Test of addEventSetStatements method, of class Method.
216: */
217: public void testAddEventSetStatements() {
218: System.out.println("addEventSetStatements");
219: String[] types = { "com.sun.data.provider.impl.CachedRowSetDataProvider" };
220: List<Bean> beans = createBeans(types);
221: Method instance = getMethod();
222: Bean dpBean = beans.get(0);
223: String eventSetName = "data";
224: EventSet eventSet = dpBean.setEventSet(eventSetName);
225: List<Statement> stmts = instance.getPropertySetStatements();
226:
227: instance.addEventSetStatements(dpBean);
228:
229: //Test if the property set statement is indeed added
230: Statement stmt = instance.findPropertyStatement(dpBean
231: .getName(), eventSet.getAddListenerMethodName());
232: assertNotNull(stmt);
233: assertEquals(instance.getPropertySetStatements().size(), stmts
234: .size() + 1);
235: }
236:
237: /**
238: * Test of removeStatement method, of class Method.
239: */
240: public void testRemoveStatement() {
241: System.out.println("removeStatement");
242: String[] types = {
243: "com.sun.data.provider.impl.ObjectDataProvider",
244: "com.sun.webui.jsf.component.Button" };
245: List<Bean> beans = createBeans(types);
246: Method instance = getMethod();
247: Bean dpBean = beans.get(0);
248: String propName = "object";
249: String propValSrc = beans.get(1).getName();
250: dpBean.setProperty(propName, null, propValSrc);
251: Property prop = dpBean.getProperty(propName);
252: instance.addPropertySetStatements(dpBean);
253: List<Statement> stmts = instance.getPropertySetStatements();
254:
255: instance.removeStatement(dpBean.getName(), prop
256: .getWriteMethodName());
257:
258: //Test if the property set statement is removed
259: Statement stmt = instance.findPropertyStatement(dpBean
260: .getName(), prop.getWriteMethodName());
261: assertNull(stmt);
262: assertEquals(instance.getPropertySetStatements().size(), stmts
263: .size() - 1);
264: }
265:
266: /**
267: * Test of replaceBody method, of class Method.
268: */
269: public void testReplaceBody() {
270: System.out.println("replaceBody");
271: String bodyText = "String temp = new String();";
272: Method instance = getMethod();
273: instance.replaceBody(bodyText);
274: String bodyTextResult = instance.getBodyText();
275: assertEquals(bodyText, bodyTextResult.substring(1,
276: bodyTextResult.length() - 1).trim());
277: }
278:
279: /**
280: * Test of rename method, of class Method.
281: */
282: public void testRename() {
283: System.out.println("rename");
284: Method instance = getMethod();
285: String name = "_" + instance.getName();
286: instance.rename(name);
287: assertEquals(name, instance.getName());
288: }
289:
290: // /**
291: // * Test of update method, of class Method.
292: // */
293: // public void testUpdate() {
294: // System.out.println("update");
295: // ContextMethod method = null;
296: // Method instance = null;
297: // instance.update(method);
298: // // TODO review the generated test code and remove the default call to fail.
299: // fail("The test case is a prototype.");
300: // }
301:
302: /**
303: * Test of remove method, of class Method.
304: */
305: public void testRemove() {
306: System.out.println("remove");
307: JavaClass javaClass = getMethod().getJavaClass();
308: String name = "foo";
309: Class[] params = new Class[] { String.class };
310: ContextMethod cm = new ContextMethod(null, name,
311: Modifier.PUBLIC, Void.TYPE, params,
312: new String[] { "name" });
313: Method instance = javaClass.addMethod(cm);
314: instance.remove();
315:
316: Method method = javaClass.getMethod(name, params);
317: assertNull(method);
318: }
319:
320: /**
321: * Test of getPropertySetStatements method, of class Method.
322: */
323: public void testGetPropertySetStatements() {
324: System.out.println("getPropertySetStatements");
325: Method instance = getMethod();
326: List<Statement> result = instance.getPropertySetStatements();
327: assertEquals(result.size(), 0);
328:
329: String[] types = { "com.sun.rave.faces.converter.SqlDateConverter", };
330: List<Bean> beans = createBeans(types);
331: Bean dpBean = beans.get(0);
332: String propName = "type";
333: String propValSrc = "both";
334: dpBean.setProperty(propName, null, propValSrc);
335: instance.addPropertySetStatements(dpBean);
336:
337: result = instance.getPropertySetStatements();
338: assertEquals(result.size(), 1);
339: }
340:
341: /**
342: * Test of isConstructor method, of class Method.
343: */
344: public void testIsConstructor() {
345: System.out.println("isConstructor");
346: Method instance = getMethod();
347: boolean result = instance.isConstructor();
348: assertEquals(false, result);
349:
350: //Try with constructor
351: JavaClass javaClass = getMethod().getJavaClass();
352: instance = javaClass.getPublicMethod("<init>", new Class[] {});
353: result = instance.isConstructor();
354: assertEquals(true, result);
355: }
356:
357: /**
358: * Test of hasInitBlock method, of class Method.
359: */
360: public void testHasInitBlock() {
361: System.out.println("hasInitBlock");
362: Method instance = getMethod();
363: boolean result = instance.hasInitBlock();
364: assertEquals(false, result);
365:
366: //Try with constructor
367: JavaClass javaClass = getMethod().getJavaClass();
368: instance = javaClass.getPublicMethod("init", new Class[] {});
369: result = instance.hasInitBlock();
370: assertEquals(true, result);
371: }
372:
373: /**
374: * Test of getBodyText method, of class Method.
375: */
376: public void testGetBodyText() {
377: System.out.println("getBodyText");
378: testReplaceBody();
379: }
380:
381: /**
382: * Test of getElement method, of class Method.
383: */
384: public void testGetElement() {
385: System.out.println("getElement");
386: final Method instance = getMethod();
387: ReadTaskWrapper.execute(new ReadTaskWrapper.Read() {
388: public Object run(CompilationInfo cinfo) {
389: ExecutableElement result = instance.getElement(cinfo);
390: assertNotNull(result);
391: assertEquals(result.getSimpleName().toString(),
392: instance.getName());
393: return null;
394: }
395: }, instance.getJavaClass().getFileObject());
396: }
397:
398: // /**
399: // * Test of getCommentText method, of class Method.
400: // */
401: // public void testGetCommentText() {
402: // System.out.println("getCommentText");
403: // JavaClass javaClass = getMethod().getJavaClass();
404: // final String comment = "Just a comment";
405: // ContextMethod cm = new ContextMethod(null, "foo", Modifier.PUBLIC, Void.TYPE,
406: // new Class[] {String.class}, new String[] {"name"}, null, comment);
407: // final Method instance = javaClass.addMethod(cm);
408: //
409: // ReadTaskWrapper.execute(new ReadTaskWrapper.Read() {
410: // public Object run(CompilationInfo cinfo) {
411: // ExecutableElement element = instance.execElementHandle.resolve(cinfo);
412: // String result = instance.getCommentText(cinfo, cinfo.getTrees().getTree(element));
413: // assertNotNull(result);
414: // assertEquals(result, comment);
415: // return null;
416: // }
417: // }, instance.getJavaClass().getFileObject());
418: // }
419:
420: /**
421: * Test of getModifierFlags method, of class Method.
422: */
423: public void testGetModifierFlags() {
424: System.out.println("getModifierFlags");
425: JavaClass javaClass = getMethod().getJavaClass();
426: ContextMethod cm = new ContextMethod(null, "foo",
427: Modifier.PROTECTED, Void.TYPE,
428: new Class[] { String.class }, new String[] { "name" });
429: final Method instance = javaClass.addMethod(cm);
430:
431: ReadTaskWrapper.execute(new ReadTaskWrapper.Read() {
432: public Object run(CompilationInfo cinfo) {
433: ExecutableElement element = instance.execElementHandle
434: .resolve(cinfo);
435: int result = instance.getModifierFlags(cinfo.getTrees()
436: .getTree(element));
437: assertNotNull(result);
438: assertEquals(result, Modifier.PROTECTED);
439: return null;
440: }
441: }, instance.getJavaClass().getFileObject());
442: }
443:
444: // /**
445: // * Test of getCursorPosition method, of class Method.
446: // */
447: // public void testGetCursorPosition() {
448: // System.out.println("getCursorPosition");
449: // boolean inserted = false;
450: // Method instance = null;
451: // int[] expResult = null;
452: // int[] result = instance.getCursorPosition(inserted);
453: // assertEquals(expResult, result);
454: // // TODO review the generated test code and remove the default call to fail.
455: // fail("The test case is a prototype.");
456: // }
457:
458: }
|