Unit tests for ExpressionUtils. Also removed unnecessary unit test library dependenices editor_settings_90403_base_4
authordeva@netbeans.org
Tue, 11 Dec 2007 21:44:08 +0000
changeset 19218f59bdaacf00
parent 1920 cc3d76185723
child 1922 1e5d40cbe8a6
Unit tests for ExpressionUtils. Also removed unnecessary unit test library dependenices
visualweb.insync/nbproject/project.xml
visualweb.insync/src/org/netbeans/modules/visualweb/insync/java/ExpressionUtils.java
visualweb.insync/test/unit/src/org/netbeans/modules/visualweb/insync/java/ExpressionUtilsTest.java
     1.1 --- a/visualweb.insync/nbproject/project.xml	Mon Dec 10 17:50:19 2007 +0000
     1.2 +++ b/visualweb.insync/nbproject/project.xml	Tue Dec 11 21:44:08 2007 +0000
     1.3 @@ -473,10 +473,6 @@
     1.4                          <compile-dependency/>
     1.5                      </test-dependency>
     1.6                      <test-dependency>
     1.7 -                        <code-name-base>org.netbeans.modules.j2eeapis</code-name-base>
     1.8 -                        <recursive/>
     1.9 -                    </test-dependency>
    1.10 -                    <test-dependency>
    1.11                          <code-name-base>org.netbeans.modules.java.j2seplatform</code-name-base>
    1.12                          <recursive/>
    1.13                          <compile-dependency/>
    1.14 @@ -486,23 +482,12 @@
    1.15                          <recursive/>
    1.16                      </test-dependency>
    1.17                      <test-dependency>
    1.18 -                        <code-name-base>org.netbeans.modules.project.libraries</code-name-base>
    1.19 -                        <recursive/>
    1.20 -                        <compile-dependency/>
    1.21 -                    </test-dependency>
    1.22 -                    <test-dependency>
    1.23                          <code-name-base>org.netbeans.modules.projectapi</code-name-base>
    1.24                          <recursive/>
    1.25                          <compile-dependency/>
    1.26                      </test-dependency>
    1.27                      <test-dependency>
    1.28                          <code-name-base>org.netbeans.modules.projectui</code-name-base>
    1.29 -                        <recursive/>
    1.30 -                        <compile-dependency/>
    1.31 -                    </test-dependency>
    1.32 -                    <test-dependency>
    1.33 -                        <code-name-base>org.netbeans.modules.projectuiapi</code-name-base>
    1.34 -                        <recursive/>
    1.35                          <compile-dependency/>
    1.36                      </test-dependency>
    1.37                      <test-dependency>
    1.38 @@ -521,20 +506,6 @@
    1.39                          <compile-dependency/>
    1.40                      </test-dependency>
    1.41                      <test-dependency>
    1.42 -                        <code-name-base>org.netbeans.modules.visualweb.project.jsf</code-name-base>
    1.43 -                        <recursive/>
    1.44 -                        <compile-dependency/>
    1.45 -                    </test-dependency>
    1.46 -                    <test-dependency>
    1.47 -                        <code-name-base>org.netbeans.modules.visualweb.woodstock.webui.jsf</code-name-base>
    1.48 -                        <compile-dependency/>
    1.49 -                    </test-dependency>
    1.50 -                    <test-dependency>
    1.51 -                        <code-name-base>org.netbeans.modules.web.jsf</code-name-base>
    1.52 -                        <recursive/>
    1.53 -                        <compile-dependency/>
    1.54 -                    </test-dependency>
    1.55 -                    <test-dependency>
    1.56                          <code-name-base>org.netbeans.modules.web.project</code-name-base>
    1.57                          <recursive/>
    1.58                          <compile-dependency/>
     2.1 --- a/visualweb.insync/src/org/netbeans/modules/visualweb/insync/java/ExpressionUtils.java	Mon Dec 10 17:50:19 2007 +0000
     2.2 +++ b/visualweb.insync/src/org/netbeans/modules/visualweb/insync/java/ExpressionUtils.java	Tue Dec 11 21:44:08 2007 +0000
     2.3 @@ -42,15 +42,12 @@
     2.4  package org.netbeans.modules.visualweb.insync.java;
     2.5  
     2.6  import com.sun.source.tree.AssignmentTree;
     2.7 -import com.sun.source.tree.ExpressionStatementTree;
     2.8  import com.sun.source.tree.ExpressionTree;
     2.9 -import com.sun.source.tree.IdentifierTree;
    2.10  import com.sun.source.tree.LiteralTree;
    2.11  import com.sun.source.tree.MemberSelectTree;
    2.12  import com.sun.source.tree.MethodInvocationTree;
    2.13  import com.sun.source.tree.NewArrayTree;
    2.14  import com.sun.source.tree.NewClassTree;
    2.15 -import com.sun.source.tree.StatementTree;
    2.16  import com.sun.source.tree.Tree;
    2.17  import com.sun.source.tree.TypeCastTree;
    2.18  import java.lang.reflect.Array;
    2.19 @@ -225,6 +222,6 @@
    2.20      public static String getArgumentSource(CompilationInfo cinfo, ExpressionTree exprTree) {
    2.21          long end = cinfo.getTrees().getSourcePositions().getEndPosition(cinfo.getCompilationUnit(), exprTree);
    2.22          long start = cinfo.getTrees().getSourcePositions().getStartPosition(cinfo.getCompilationUnit(), exprTree);
    2.23 -        return cinfo.getText().substring((int)start, (int)end-1);
    2.24 +        return cinfo.getText().substring((int)start, (int)end);
    2.25      }    
    2.26  }
    2.27 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/visualweb.insync/test/unit/src/org/netbeans/modules/visualweb/insync/java/ExpressionUtilsTest.java	Tue Dec 11 21:44:08 2007 +0000
     3.3 @@ -0,0 +1,185 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + * 
     3.7 + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
     3.8 + * 
     3.9 + * The contents of this file are subject to the terms of either the GNU
    3.10 + * General Public License Version 2 only ("GPL") or the Common
    3.11 + * Development and Distribution License("CDDL") (collectively, the
    3.12 + * "License"). You may not use this file except in compliance with the
    3.13 + * License. You can obtain a copy of the License at
    3.14 + * http://www.netbeans.org/cddl-gplv2.html
    3.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.16 + * specific language governing permissions and limitations under the
    3.17 + * License.  When distributing the software, include this License Header
    3.18 + * Notice in each file and include the License file at
    3.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    3.20 + * particular file as subject to the "Classpath" exception as provided
    3.21 + * by Sun in the GPL Version 2 section of the License file that
    3.22 + * accompanied this code. If applicable, add the following below the
    3.23 + * License Header, with the fields enclosed by brackets [] replaced by
    3.24 + * your own identifying information:
    3.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.26 + * 
    3.27 + * If you wish your version of this file to be governed by only the CDDL
    3.28 + * or only the GPL Version 2, indicate your decision by adding
    3.29 + * "[Contributor] elects to include this software in this distribution
    3.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.31 + * single choice of license, a recipient has the option to distribute
    3.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.33 + * to extend the choice of license to its licensees as provided above.
    3.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.35 + * Version 2 license, then the option applies only if the new code is
    3.36 + * made subject to such option by the copyright holder.
    3.37 + * 
    3.38 + * Contributor(s):
    3.39 + * 
    3.40 + * Portions Copyrighted 2007 Sun Microsystems, Inc.
    3.41 + */
    3.42 +
    3.43 +package org.netbeans.modules.visualweb.insync.java;
    3.44 +
    3.45 +import com.sun.crypto.provider.AESCipher;
    3.46 +import com.sun.source.tree.ExpressionTree;
    3.47 +import java.util.List;
    3.48 +import junit.framework.Test;
    3.49 +import junit.framework.TestSuite;
    3.50 +import org.netbeans.api.java.source.CompilationInfo;
    3.51 +import org.netbeans.junit.NbTestSuite;
    3.52 +import org.netbeans.modules.visualweb.insync.InsyncTestBase;
    3.53 +import org.netbeans.modules.visualweb.insync.beans.Bean;
    3.54 +import org.openide.filesystems.FileObject;
    3.55 +import org.openide.util.Exceptions;
    3.56 +
    3.57 +/**
    3.58 + *
    3.59 + * @author jdeva
    3.60 + */
    3.61 +public class ExpressionUtilsTest extends InsyncTestBase {
    3.62 +    public ExpressionUtilsTest(String testName) {
    3.63 +        super(testName);
    3.64 +    }
    3.65 +
    3.66 +    public static void main(java.lang.String[] args) {
    3.67 +        junit.textui.TestRunner.run(suite());
    3.68 +    }
    3.69 +
    3.70 +    public static Test suite() {
    3.71 +        TestSuite suite = new NbTestSuite(ExpressionUtilsTest.class);
    3.72 +        return suite;
    3.73 +    }
    3.74 +
    3.75 +    @Override
    3.76 +    protected void setUp() throws Exception {
    3.77 +        super.setUp();
    3.78 +    }
    3.79 +    
    3.80 +    @Override
    3.81 +    protected void tearDown() throws Exception {
    3.82 +        super.tearDown();
    3.83 +    }   
    3.84 +
    3.85 +    Method getInitMethod() {
    3.86 +        FileObject fObj = getJavaFile(getPageBeans()[0]);
    3.87 +        JavaClass javaClass = JavaClass.getJavaClass(fObj);
    3.88 +        return javaClass.getMethod("_init", new Class[]{});        
    3.89 +    }
    3.90 +    
    3.91 +    /**
    3.92 +     * Test of getValue method, of class ExpressionUtils.
    3.93 +     */
    3.94 +    public void testGetValue() {
    3.95 +        System.out.println("getValue");
    3.96 +        String[] types = {
    3.97 +            "com.sun.rave.faces.converter.SqlDateConverter",
    3.98 +            "com.sun.webui.jsf.model.SingleSelectOptionsList"
    3.99 +        };
   3.100 +        createBeans(types);     
   3.101 +        Method initMethod = getInitMethod();
   3.102 +        String body = 
   3.103 +        "sqlDateConverter1.setTimeStyle(\"medium\");\n" +
   3.104 +        "sqlDateConverter1.setTimeZone(java.util.TimeZone.getTimeZone(\"America/Mendoza\"))" +
   3.105 +        "singleSelectOptionsList1.setSelectedValue(\"item1\");" +
   3.106 +        "singleSelectOptionsList1.setOptions(new com.sun.webui.jsf.model.Option[]{new com.sun.webui.jsf.model.Option(\"item1\", \"Item 1\")," + 
   3.107 +        "new com.sun.webui.jsf.model.Option(\"item2\", \"Item 2\")});\n";        
   3.108 +        initMethod.replaceBody(body);
   3.109 +        Object[] results = {
   3.110 +            "medium",
   3.111 +            java.util.TimeZone.getTimeZone("America/Mendoza"),
   3.112 +            "item1",
   3.113 +            new com.sun.webui.jsf.model.Option[]{new com.sun.webui.jsf.model.Option("item1", "Item 1"), 
   3.114 +            new com.sun.webui.jsf.model.Option("item2", "Item 2")}
   3.115 +        };        
   3.116 +        
   3.117 +        for(int i = 0;i < results.length-1; i++) {
   3.118 +            Object result = testGetValueImpl(initMethod.getPropertySetStatements().get(i), 
   3.119 +                    initMethod.getJavaClass().getFileObject());
   3.120 +            assertEquals(result, results[i]);
   3.121 +        }
   3.122 +        Object result = testGetValueImpl(initMethod.getPropertySetStatements().get(results.length-1), 
   3.123 +                initMethod.getJavaClass().getFileObject());
   3.124 +        assert(result.getClass().isArray());
   3.125 +    }
   3.126 +    
   3.127 +    private Object testGetValueImpl(final Statement stmt, FileObject fileObject) {
   3.128 +        return ReadTaskWrapper.execute(new ReadTaskWrapper.Read() {
   3.129 +            public Object run(CompilationInfo cinfo) {
   3.130 +                java.lang.reflect.Method m = null;
   3.131 +                ExpressionTree exprTree = null;
   3.132 +                try {
   3.133 +                    m = stmt.getClass().getDeclaredMethod("getArgument", new Class[]{CompilationInfo.class});
   3.134 +                    m.setAccessible(true);
   3.135 +                    exprTree = (ExpressionTree) m.invoke(stmt, new Object[]{cinfo});
   3.136 +                } catch (Exception ex) {
   3.137 +                    Exceptions.printStackTrace(ex);
   3.138 +                }
   3.139 +                return ExpressionUtils.getValue(cinfo, exprTree);
   3.140 +            }
   3.141 +        }, fileObject);
   3.142 +    }
   3.143 +
   3.144 +    /**
   3.145 +     * Test of getArgumentSource method, of class ExpressionUtils.
   3.146 +     */
   3.147 +    public void testGetArgumentSource() {
   3.148 +        System.out.println("getArgumentSource");
   3.149 +
   3.150 +        String[] types = {
   3.151 +            "com.sun.rave.faces.converter.SqlDateConverter"
   3.152 +        };
   3.153 +        String[] results = {
   3.154 +            "\"medium\"",
   3.155 +            "java.util.TimeZone.getTimeZone(\"America/Mendoza\")"
   3.156 +        };
   3.157 +        createBeans(types);     
   3.158 +        Method initMethod = getInitMethod();
   3.159 +        String body = 
   3.160 +        "sqlDateConverter1.setTimeStyle(\"medium\");\n" +
   3.161 +        "sqlDateConverter1.setTimeZone(java.util.TimeZone.getTimeZone(\"America/Mendoza\"))";
   3.162 +        
   3.163 +        initMethod.replaceBody(body);
   3.164 +        
   3.165 +        for(int i = 0;i < results.length; i++) {
   3.166 +            Object result = testGetArgumentSourceImpl(initMethod.getPropertySetStatements().get(i), 
   3.167 +                    initMethod.getJavaClass().getFileObject());
   3.168 +            assertEquals(result, results[i]);
   3.169 +        }
   3.170 +    }
   3.171 +    
   3.172 +    private Object testGetArgumentSourceImpl(final Statement stmt, FileObject fileObject) {
   3.173 +        return ReadTaskWrapper.execute(new ReadTaskWrapper.Read() {
   3.174 +            public Object run(CompilationInfo cinfo) {
   3.175 +                java.lang.reflect.Method m = null;
   3.176 +                ExpressionTree exprTree = null;
   3.177 +                try {
   3.178 +                    m = stmt.getClass().getDeclaredMethod("getArgument", new Class[]{CompilationInfo.class});
   3.179 +                    m.setAccessible(true);
   3.180 +                    exprTree = (ExpressionTree) m.invoke(stmt, new Object[]{cinfo});
   3.181 +                } catch (Exception ex) {
   3.182 +                    Exceptions.printStackTrace(ex);
   3.183 +                }
   3.184 +                return ExpressionUtils.getArgumentSource(cinfo, exprTree);
   3.185 +            }
   3.186 +        }, fileObject);
   3.187 +    }    
   3.188 +}