visualweb.insync/test/unit/src/org/netbeans/modules/visualweb/insync/InsyncTestBase.java
author Jesse Glick <jglick@netbeans.org>
Wed, 23 Mar 2011 16:50:27 -0400
changeset 3213 c85dcf71e424
parent 2926 3b669d40369b
permissions -rw-r--r--
Tests at least should compile now.
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  * 
    27  * Contributor(s):
    28  * 
    29  * Portions Copyrighted 2007 Sun Microsystems, Inc.
    30  */
    31 package org.netbeans.modules.visualweb.insync;
    32 
    33 import com.sun.rave.designtime.Constants;
    34 import java.beans.BeanInfo;
    35 import java.io.File;
    36 import java.io.IOException;
    37 import java.lang.reflect.Constructor;
    38 import java.net.URL;
    39 import java.net.URLStreamHandlerFactory;
    40 import java.util.ArrayList;
    41 import java.util.List;
    42 import java.util.regex.Pattern;
    43 import org.netbeans.api.project.Project;
    44 import org.netbeans.api.project.libraries.LibraryManager;
    45 import org.netbeans.junit.MockServices;
    46 import org.netbeans.junit.NbTestCase;
    47 import org.netbeans.modules.visualweb.insync.beans.Bean;
    48 import org.netbeans.modules.visualweb.insync.beans.BeansUnit;
    49 import org.netbeans.modules.visualweb.insync.models.FacesModel;
    50 import org.netbeans.modules.visualweb.insync.models.FacesModelSet;
    51 import org.netbeans.modules.web.project.WebProject;
    52 import org.openide.filesystems.FileObject;
    53 import org.openide.util.Exceptions;
    54 import org.openide.util.Lookup;
    55 
    56 /**
    57  *
    58  * @author sc32560, jdeva
    59  * 
    60  */
    61 public class InsyncTestBase extends NbTestCase {
    62     public InsyncTestBase(String name) {
    63         super(name);
    64         _setUp();
    65     }
    66     
    67     String[] pageBeans, requestBeans, sessionBeans, applicationBeans, facesConfigs;
    68     Project project;
    69     
    70     static {
    71         setupServices();
    72     }
    73     
    74     @Override
    75     protected void setUp() throws Exception {
    76         super.setUp();
    77         //_setUp();
    78     }
    79     
    80     private void _setUp() {
    81         //Set up the system properties
    82         System.setProperty("jdk.home", System.getProperty("java.home"));
    83         try {
    84 
    85             System.setProperty("netbeans.user", new File(getWorkDir().getParentFile(), "user").getAbsolutePath());
    86         } catch (IOException ex) {
    87             Exceptions.printStackTrace(ex);
    88         }
    89         //Load the required libraries. This allows evaluation of project properties
    90         //ex:- ${libs.jsf12-support.classpath}
    91         LibraryManager.getDefault().getLibraries();
    92         openProject();
    93         pageBeans = System.getProperty(
    94                 "visualweb.project.pagebeans").split(Pattern.quote(System.getProperty("path.separator")));
    95         requestBeans = System.getProperty(
    96                 "visualweb.project.requestbeans").split(Pattern.quote(System.getProperty("path.separator")));
    97         sessionBeans = System.getProperty(
    98                 "visualweb.project.sessionbeans").split(Pattern.quote(System.getProperty("path.separator")));
    99         applicationBeans = System.getProperty(
   100                 "visualweb.project.applicationbeans").split(Pattern.quote(System.getProperty("path.separator")));
   101         facesConfigs = System.getProperty(
   102                 "visualweb.project.facesconfig").split(Pattern.quote(System.getProperty("path.separator")));        
   103         
   104     }
   105 
   106     @Override
   107     protected void tearDown() throws Exception {
   108         super.tearDown();
   109         clearWorkDir();
   110     }
   111 
   112     public FacesModelSet createFacesModelSet() {
   113         FileObject file = getProject().getProjectDirectory();
   114         FacesModelSet set = FacesModelSet.getInstance(file);
   115         return set;
   116     }       
   117     
   118     protected int getBeansCount() {
   119         return getPageBeans().length + getNonPageBeansCount();
   120     }
   121     
   122     protected int getNonPageBeansCount() {
   123         return getRequestBeans().length + 
   124                getSessionBeans().length + 
   125                getApplicationBeans().length;
   126     }
   127     
   128     protected String[] getBeanNames() {
   129         String[] str = new String[getBeansCount()];
   130         int i = 0;
   131         for(String s : getPageBeans()) {
   132             str[i++] = s;
   133         }
   134         for (String s : getRequestBeans()) {
   135             str[i++] = s;
   136         }
   137         for (String s : getSessionBeans()) {
   138             str[i++] = s;
   139         }    
   140         for (String s : getApplicationBeans()) {
   141             str[i++] = s;
   142         }         
   143         return str;
   144     }
   145     
   146     protected List<Bean> createBeans(String[] types) {                
   147         FacesModelSet modelSet = createFacesModelSet();
   148         FacesModel model = modelSet.getFacesModel(getJavaFile(getPageBeans()[0]));
   149         model.sync();
   150         BeansUnit bu = model.getBeansUnit();
   151         List<Bean> beans = new ArrayList<Bean>();
   152         for(String type : types) {
   153             beans.add(createBean(bu, type));
   154         }
   155         return beans;
   156     }
   157 
   158     private Bean createBean(BeansUnit bu, String type) {
   159         Constructor ctor = null;
   160         java.lang.reflect.Method m = null;
   161         ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
   162         try {
   163             Class clazz = Class.forName("org.netbeans.modules.visualweb.insync.beans.Bean");
   164             ctor = clazz.getDeclaredConstructor(BeansUnit.class, BeanInfo.class, String.class);
   165             ctor.setAccessible(true);
   166             clazz = Class.forName("org.netbeans.modules.visualweb.insync.beans.BeansUnit");
   167             m = clazz.getDeclaredMethod("nextNameForType", String.class);
   168             m.setAccessible(true);
   169         } catch (Exception ex) {
   170             Exceptions.printStackTrace(ex);
   171         }
   172         try {
   173             Thread.currentThread().setContextClassLoader(bu.getClassLoader());
   174             Class beanClass = bu.getBeanClass(type);
   175             if (beanClass != null) {
   176                 BeanInfo beanInfo = BeansUnit.getBeanInfo(beanClass, bu.getClassLoader());
   177                 String name = (String) beanInfo.getBeanDescriptor().getValue(Constants.BeanDescriptor.INSTANCE_NAME);
   178                 if(name == null) {
   179                     name = (String)m.invoke(bu, type);
   180                 }
   181                 return (Bean) ctor.newInstance(bu, beanInfo, name);
   182             }
   183         } catch (Exception ex) {
   184             Exceptions.printStackTrace(ex);
   185         } finally {
   186             Thread.currentThread().setContextClassLoader(oldContextClassLoader);
   187         }
   188         return null;
   189     }
   190     
   191     protected FileObject getJavaFile(String name) {
   192         FileObject[] roots = ((WebProject)getProject()).getSourceRoots().getRoots();
   193         for(FileObject f : roots) {
   194             FileObject result = findFileObject(f, name);
   195             if(result != null) {
   196                 return result;
   197             }
   198         }
   199         return null;
   200     }
   201     
   202     protected FileObject findFileObject(FileObject f, String name) {
   203         FileObject result = null;
   204         if(f.isFolder()) {
   205             for(FileObject child : f.getChildren()) {
   206                 result = findFileObject(child, name);
   207                 if(result != null) {
   208                     return result;
   209                 }
   210             }
   211         }else {
   212             if(name.equals(f.getName())) {
   213                 result = f;
   214             }
   215         }
   216         return result;
   217     }
   218 
   219     protected String getFQN(String className) {
   220         FileObject[] roots = ((WebProject)getProject()).getSourceRoots().getRoots();
   221         for(FileObject f : roots) {
   222             FileObject result = findFileObject(f, className);
   223             if(result != null) {
   224                 String filePath = result.getPath().substring(0, result.getPath().lastIndexOf("."));
   225                 String relativePath = filePath.replace(f.getPath(), "");
   226                 return relativePath.replace('/', '.').substring(1);
   227             }
   228         }
   229         return null;
   230     }
   231 
   232     protected String getPackageName(String className) {
   233         String relativePath = getFQN(className);
   234         int lastDotIndex = relativePath.lastIndexOf('.');
   235         return relativePath.substring(0, lastDotIndex);
   236     }
   237 
   238     public Project openProject() {
   239         String projectName = System.getProperty("visualweb.project.name");
   240         String relativeProjectPath = "projects" + File.separator + projectName;
   241         //Check for a directory and then a zip file by project name
   242         File f = new File(getDataDir().getAbsolutePath(), relativeProjectPath);
   243         if (!f.exists()) {
   244             f = new File(getDataDir().getAbsolutePath(), relativeProjectPath + ".zip");
   245         }
   246         try {
   247             project = ProjectUtils.openProject(getWorkDir(), f.getAbsolutePath(), projectName);
   248             //Necessary for FacesConfigModel
   249             JSFConfigUtils.setUp(project);
   250         } catch (IOException ex) {
   251             Exceptions.printStackTrace(ex);
   252         }
   253         return project;
   254     }
   255     
   256     public Project getProject() {
   257         return project;
   258     }
   259     
   260     public void destroyProject() throws IOException {
   261         ProjectUtils.destroyProject(project);
   262     }
   263 
   264     public String[] getApplicationBeans() {
   265         return applicationBeans;
   266     }
   267 
   268     public String[] getPageBeans() {
   269         return pageBeans;
   270     }
   271 
   272     public String[] getRequestBeans() {
   273         return requestBeans;
   274     }
   275 
   276     public String[] getSessionBeans() {
   277         return sessionBeans;
   278     }
   279 
   280     public String[] getFacesConfigs() {
   281         return facesConfigs;
   282     }
   283     
   284     private static void setupServices() {
   285         URL.setURLStreamHandlerFactory(Lookup.getDefault().lookup(URLStreamHandlerFactory.class));
   286         MockServices.setServices(RepositoryImpl.class, InsyncMimeResolver.class);
   287     }    
   288 }