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.crypto.provider.AESCipher;
043: import com.sun.source.tree.ExpressionTree;
044: import java.util.List;
045: import junit.framework.Test;
046: import junit.framework.TestSuite;
047: import org.netbeans.api.java.source.CompilationInfo;
048: import org.netbeans.junit.NbTestSuite;
049: import org.netbeans.modules.visualweb.insync.InsyncTestBase;
050: import org.netbeans.modules.visualweb.insync.beans.Bean;
051: import org.openide.filesystems.FileObject;
052: import org.openide.util.Exceptions;
053:
054: /**
055: *
056: * @author jdeva
057: */
058: public class ExpressionUtilsTest extends InsyncTestBase {
059: public ExpressionUtilsTest(String testName) {
060: super (testName);
061: }
062:
063: public static void main(java.lang.String[] args) {
064: junit.textui.TestRunner.run(suite());
065: }
066:
067: public static Test suite() {
068: TestSuite suite = new NbTestSuite(ExpressionUtilsTest.class);
069: return suite;
070: }
071:
072: @Override
073: protected void setUp() throws Exception {
074: super .setUp();
075: }
076:
077: @Override
078: protected void tearDown() throws Exception {
079: super .tearDown();
080: }
081:
082: Method getInitMethod() {
083: FileObject fObj = getJavaFile(getPageBeans()[0]);
084: JavaClass javaClass = JavaClass.getJavaClass(fObj);
085: return javaClass.getMethod("_init", new Class[] {});
086: }
087:
088: /**
089: * Test of getValue method, of class ExpressionUtils.
090: */
091: public void testGetValue() {
092: System.out.println("getValue");
093: String[] types = {
094: "com.sun.rave.faces.converter.SqlDateConverter",
095: "com.sun.webui.jsf.model.SingleSelectOptionsList" };
096: createBeans(types);
097: Method initMethod = getInitMethod();
098: String body = "sqlDateConverter1.setTimeStyle(\"medium\");\n"
099: + "sqlDateConverter1.setTimeZone(java.util.TimeZone.getTimeZone(\"America/Mendoza\"))"
100: + "singleSelectOptionsList1.setSelectedValue(\"item1\");"
101: + "singleSelectOptionsList1.setOptions(new com.sun.webui.jsf.model.Option[]{new com.sun.webui.jsf.model.Option(\"item1\", \"Item 1\"),"
102: + "new com.sun.webui.jsf.model.Option(\"item2\", \"Item 2\")});\n";
103: initMethod.replaceBody(body);
104: Object[] results = {
105: "medium",
106: java.util.TimeZone.getTimeZone("America/Mendoza"),
107: "item1",
108: new com.sun.webui.jsf.model.Option[] {
109: new com.sun.webui.jsf.model.Option("item1",
110: "Item 1"),
111: new com.sun.webui.jsf.model.Option("item2",
112: "Item 2") } };
113:
114: for (int i = 0; i < results.length - 1; i++) {
115: Object result = testGetValueImpl(initMethod
116: .getPropertySetStatements().get(i), initMethod
117: .getJavaClass().getFileObject());
118: assertEquals(result, results[i]);
119: }
120: Object result = testGetValueImpl(initMethod
121: .getPropertySetStatements().get(results.length - 1),
122: initMethod.getJavaClass().getFileObject());
123: assert (result.getClass().isArray());
124: }
125:
126: private Object testGetValueImpl(final Statement stmt,
127: FileObject fileObject) {
128: return ReadTaskWrapper.execute(new ReadTaskWrapper.Read() {
129: public Object run(CompilationInfo cinfo) {
130: java.lang.reflect.Method m = null;
131: ExpressionTree exprTree = null;
132: try {
133: m = stmt.getClass().getDeclaredMethod(
134: "getArgument",
135: new Class[] { CompilationInfo.class });
136: m.setAccessible(true);
137: exprTree = (ExpressionTree) m.invoke(stmt,
138: new Object[] { cinfo });
139: } catch (Exception ex) {
140: Exceptions.printStackTrace(ex);
141: }
142: return ExpressionUtils.getValue(cinfo, exprTree);
143: }
144: }, fileObject);
145: }
146:
147: /**
148: * Test of getArgumentSource method, of class ExpressionUtils.
149: */
150: public void testGetArgumentSource() {
151: System.out.println("getArgumentSource");
152:
153: String[] types = { "com.sun.rave.faces.converter.SqlDateConverter" };
154: String[] results = { "\"medium\"",
155: "java.util.TimeZone.getTimeZone(\"America/Mendoza\")" };
156: createBeans(types);
157: Method initMethod = getInitMethod();
158: String body = "sqlDateConverter1.setTimeStyle(\"medium\");\n"
159: + "sqlDateConverter1.setTimeZone(java.util.TimeZone.getTimeZone(\"America/Mendoza\"))";
160:
161: initMethod.replaceBody(body);
162:
163: for (int i = 0; i < results.length; i++) {
164: Object result = testGetArgumentSourceImpl(initMethod
165: .getPropertySetStatements().get(i), initMethod
166: .getJavaClass().getFileObject());
167: assertEquals(result, results[i]);
168: }
169: }
170:
171: private Object testGetArgumentSourceImpl(final Statement stmt,
172: FileObject fileObject) {
173: return ReadTaskWrapper.execute(new ReadTaskWrapper.Read() {
174: public Object run(CompilationInfo cinfo) {
175: java.lang.reflect.Method m = null;
176: ExpressionTree exprTree = null;
177: try {
178: m = stmt.getClass().getDeclaredMethod(
179: "getArgument",
180: new Class[] { CompilationInfo.class });
181: m.setAccessible(true);
182: exprTree = (ExpressionTree) m.invoke(stmt,
183: new Object[] { cinfo });
184: } catch (Exception ex) {
185: Exceptions.printStackTrace(ex);
186: }
187: return ExpressionUtils.getArgumentSource(cinfo,
188: exprTree);
189: }
190: }, fileObject);
191: }
192: }
|