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: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028:
029: package org.netbeans.modules.visualweb.insync.java;
030:
031: import com.sun.rave.designtime.Constants;
032: import com.sun.rave.designtime.ContextMethod;
033: import java.beans.BeanInfo;
034: import java.lang.reflect.Constructor;
035: import java.lang.reflect.Modifier;
036: import java.util.ArrayList;
037: import java.util.HashMap;
038: import java.util.List;
039: import junit.framework.Test;
040: import junit.framework.TestSuite;
041: import org.netbeans.api.java.source.ElementHandle;
042: import org.netbeans.junit.NbTestSuite;
043: import org.netbeans.modules.visualweb.insync.InsyncTestBase;
044: import org.netbeans.modules.visualweb.insync.beans.Bean;
045: import org.netbeans.modules.visualweb.insync.beans.BeansUnit;
046: import org.netbeans.modules.visualweb.insync.beans.Naming;
047: import org.netbeans.modules.visualweb.insync.models.FacesModel;
048: import org.netbeans.modules.visualweb.insync.models.FacesModelSet;
049: import org.openide.filesystems.FileObject;
050: import org.openide.util.Exceptions;
051:
052: /**
053: *
054: * @author jdeva
055: */
056: public class JavaClassTest extends InsyncTestBase {
057: public JavaClassTest(String testName) {
058: super (testName);
059: }
060:
061: public static void main(java.lang.String[] args) {
062: junit.textui.TestRunner.run(suite());
063: }
064:
065: public static Test suite() {
066: TestSuite suite = new NbTestSuite(JavaClassTest.class);
067: return suite;
068: }
069:
070: @Override
071: protected void setUp() throws Exception {
072: super .setUp();
073: }
074:
075: @Override
076: protected void tearDown() throws Exception {
077: super .tearDown();
078: }
079:
080: JavaClass createJavaClass() {
081: FileObject fObj = getJavaFile(getPageBeans()[0]);
082: return JavaClass.getJavaClass(fObj);
083: }
084:
085: /* Test of getShortName method, of class JavaClass. */
086: public void testGetShortName() {
087: System.out.println("getShortName");
088: JavaClass instance = createJavaClass();
089: String result = instance.getShortName();
090: assertNotNull(result);
091: assertEquals(result, getPageBeans()[0]);
092: }
093:
094: /* Test of getName method, of class JavaClass. */
095: public void testGetName() {
096: System.out.println("getName");
097: JavaClass instance = createJavaClass();
098: String result = instance.getName();
099: assertNotNull(result);
100: String expResult = getFQN(getPageBeans()[0]);
101: assertEquals(result, expResult);
102: }
103:
104: /* Test of getPackageName method, of class JavaClass. */
105: public void testGetPackageName() {
106: System.out.println("getPackageName");
107: JavaClass instance = createJavaClass();
108: String result = instance.getPackageName();
109: assertNotNull(result);
110: String expResult = getPackageName(getPageBeans()[0]);
111: assertEquals(expResult, result);
112: }
113:
114: /* Test of getFileObject method, of class JavaClass. */
115: public void testGetFileObject() {
116: System.out.println("getFileObject");
117: FileObject fObj = getJavaFile(getPageBeans()[0]);
118: JavaClass instance = JavaClass.getJavaClass(fObj);
119: FileObject result = instance.getFileObject();
120: assertEquals(fObj, result);
121: }
122:
123: /* Test of isSubTypeOf method, of class JavaClass. */
124: public void testIsSubTypeOf() {
125: System.out.println("isSubTypeOf");
126: JavaClass instance = createJavaClass();
127: boolean result = false;
128: for (String baseTypeName : FacesModel.managedBeanNames) {
129: if (instance.isSubTypeOf(baseTypeName)) {
130: result = true;
131: }
132: }
133: assertEquals(result, true);
134: }
135:
136: /* Test of getMethods method, of class JavaClass. */
137: public void testGetMethods() {
138: System.out.println("getMethods");
139: JavaClass instance = createJavaClass();
140: List<Method> result = instance.getMethods();
141: assertNotNull(result);
142: assert (result.size() > 0);
143: }
144:
145: /* Test of getMethodNames method, of class JavaClass. */
146: public void testGetMethodNames() {
147: System.out.println("getMethodNames");
148: JavaClass instance = createJavaClass();
149: List<Bean> beans = createBeans();
150: instance.addBeans(beans);
151: Class[] params = null;
152: Class retType = null;
153: //Try setter for first bean
154: params = new Class[] { beans.get(0).getBeanInfo()
155: .getBeanDescriptor().getBeanClass() };
156: List result = instance.getMethodNames(params, retType);
157: assertNotNull(result);
158: assertEquals(result.size(), 1);
159:
160: //Try getter for second bean
161: params = new Class[] {};
162: retType = beans.get(1).getBeanInfo().getBeanDescriptor()
163: .getBeanClass();
164: result = instance.getMethodNames(params, retType);
165: assertNotNull(result);
166: assertEquals(result.size(), 1);
167: }
168:
169: /* Test of getPropertiesNameAndTypes method, of class JavaClass. */
170: public void testGetPropertiesNameAndTypes() {
171: System.out.println("getPropertiesNameAndTypes");
172: JavaClass instance = createJavaClass();
173: HashMap result = instance.getPropertiesNameAndTypes();
174: assertNotNull(result);
175: assert (result.size() > 0);
176: List<Bean> beans = createBeans();
177: instance.addBeans(beans);
178: HashMap resultAfterAdding = instance
179: .getPropertiesNameAndTypes();
180: assertNotNull(resultAfterAdding);
181: assertEquals(resultAfterAdding.size(), beans.size()
182: + result.size());
183: }
184:
185: /* Test of getField method, of class JavaClass. */
186: public void testGetField() {
187: System.out.println("getField");
188: JavaClass instance = createJavaClass();
189: List<Bean> beans = createBeans();
190: instance.addBeans(beans);
191: ElementHandle result = instance
192: .getField(beans.get(0).getName());
193: assertNotNull(result);
194: }
195:
196: private List<Bean> createBeans() {
197: String[] types = { "com.sun.webui.jsf.component.Button",
198: "com.sun.webui.jsf.component.TextField" };
199: return createBeans(types);
200: }
201:
202: /* Test of addBeans method, of class JavaClass. */
203: public void testAddBeans() {
204: System.out.println("addBeans");
205: List<Bean> beans = createBeans();
206: JavaClass instance = createJavaClass();
207: instance.addBeans(beans);
208: }
209:
210: /* Test of removeBeans method, of class JavaClass. */
211: public void testRemoveBeans() {
212: System.out.println("removeBeans");
213: List<Bean> beans = createBeans();
214: JavaClass instance = createJavaClass();
215: instance.removeBeans(beans);
216: }
217:
218: /* Test of renameProperty method, of class JavaClass. */
219: public void testRenameProperty() {
220: System.out.println("renameProperty");
221: JavaClass instance = createJavaClass();
222: List<Bean> beans = createBeans();
223: instance.addBeans(beans);
224: String name = beans.get(0).getName();
225: ElementHandle field = instance.getField(name);
226: assertNotNull(field);
227: String newName = name + "1";
228: List<FileObject> fObjs = new ArrayList<FileObject>();
229: for (String beanName : getBeanNames()) {
230: fObjs.add(getJavaFile(beanName));
231: }
232: instance.renameProperty(name, newName, fObjs);
233:
234: field = instance.getField(name);
235: assertNull(field);
236: field = instance.getField(newName);
237: assertNotNull(field);
238: }
239:
240: /* Test of addMethod method, of class JavaClass. */
241: public void testAddMethod() {
242: System.out.println("addMethod");
243: JavaClass instance = createJavaClass();
244: String name = "foo";
245: Class[] params = new Class[] { String.class };
246: ContextMethod cm = new ContextMethod(null, name,
247: Modifier.PUBLIC, Void.TYPE, params,
248: new String[] { "name" });
249: Method result = instance.addMethod(cm);
250: assertNotNull(result);
251: Method method = instance.getMethod(name, params);
252: assertNotNull(method);
253: assertEquals(name, method.getName());
254: }
255:
256: /* Test of getMethod method, of class JavaClass. */
257: public void testGetMethod() {
258: System.out.println("getMethod");
259: JavaClass instance = createJavaClass();
260: List<Bean> beans = createBeans();
261: instance.addBeans(beans);
262: Class[] params = null;
263: //Try setter for first bean
264: params = new Class[] { beans.get(0).getBeanInfo()
265: .getBeanDescriptor().getBeanClass() };
266: String methodName = Naming.setterName(beans.get(0).getName());
267: Method result = instance.getMethod(methodName, params);
268: assertNotNull(result);
269:
270: //Try getter for second bean
271: params = new Class[] {};
272: methodName = Naming.getterName(beans.get(1).getName());
273: result = instance.getMethod(methodName, params);
274: assertNotNull(result);
275: }
276:
277: // public void testAddDelegatorMethod() {
278: // System.out.println("addDelegatorMethod");
279: // MethodInfo mInfo = null;
280: // JavaClass instance = null;
281: // DelegatorMethod expResult = null;
282: // DelegatorMethod result = instance.addDelegatorMethod(mInfo);
283: // assertEquals(expResult, result);
284: // fail("The test case is a prototype.");
285: // } /* Test of addDelegatorMethod method, of class JavaClass. */
286: //
287: // public void testAddEventMethod() {
288: // System.out.println("addEventMethod");
289: // MethodInfo mInfo = null;
290: // JavaClass instance = null;
291: // EventMethod expResult = null;
292: // EventMethod result = instance.addEventMethod(mInfo);
293: // assertEquals(expResult, result);
294: // fail("The test case is a prototype.");
295: // } /* Test of addEventMethod method, of class JavaClass. */
296: //
297: //
298: // public void testGetEventMethod() {
299: // System.out.println("getEventMethod");
300: // String name = "";
301: // Class[] params = null;
302: // JavaClass instance = null;
303: // EventMethod expResult = null;
304: // EventMethod result = instance.getEventMethod(name, params);
305: // assertEquals(expResult, result);
306: // fail("The test case is a prototype.");
307: // } /* Test of getEventMethod method, of class JavaClass. */
308: //
309: // public void testGetDelegatorMethod() {
310: // System.out.println("getDelegatorMethod");
311: // String name = "";
312: // Class[] params = null;
313: // JavaClass instance = null;
314: // DelegatorMethod expResult = null;
315: // DelegatorMethod result = instance.getDelegatorMethod(name, params);
316: // assertEquals(expResult, result);
317: // fail("The test case is a prototype.");
318: // } /* Test of getDelegatorMethod method, of class JavaClass. */
319: //
320:
321: /* Test of getPublicMethod method, of class JavaClass. */
322: public void testGetPublicMethod() {
323: System.out.println("getPublicMethod");
324: JavaClass instance = createJavaClass();
325: String name = "foo";
326: Class[] params = new Class[] { String.class };
327: ContextMethod cm = new ContextMethod(null, "foo",
328: Modifier.PUBLIC, Void.TYPE, params,
329: new String[] { "name" });
330: Method method = instance.addMethod(cm);
331: assertNotNull(method);
332: Method result = instance.getPublicMethod(name, params);
333: assertNotNull(result);
334: assertEquals(name, result.getName());
335:
336: //remove the public method, and add private method and do the test
337: method.remove();
338: cm = new ContextMethod(null, "foo", Modifier.PRIVATE,
339: Void.TYPE, params, new String[] { "name" });
340: method = instance.addMethod(cm);
341: assertNotNull(method);
342: result = instance.getPublicMethod(name, params);
343: assertNull(result);
344: }
345:
346: /* Test of getJavaClass method, of class JavaClass. */
347: public void testGetJavaClass() {
348: System.out.println("getJavaClass");
349: FileObject fObj = getJavaFile(getPageBeans()[0]);
350: JavaClass result = JavaClass.getJavaClass(fObj);
351: assertNotNull(result);
352: assertEquals(result.getShortName(), getPageBeans()[0]);
353: }
354: }
|