visualweb.insync/test/unit/src/org/netbeans/modules/visualweb/insync/InsyncTestBase.java
Tests at least should compile now.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
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]"
29 * Portions Copyrighted 2007 Sun Microsystems, Inc.
31 package org.netbeans.modules.visualweb.insync;
33 import com.sun.rave.designtime.Constants;
34 import java.beans.BeanInfo;
36 import java.io.IOException;
37 import java.lang.reflect.Constructor;
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;
58 * @author sc32560, jdeva
61 public class InsyncTestBase extends NbTestCase {
62 public InsyncTestBase(String name) {
67 String[] pageBeans, requestBeans, sessionBeans, applicationBeans, facesConfigs;
75 protected void setUp() throws Exception {
80 private void _setUp() {
81 //Set up the system properties
82 System.setProperty("jdk.home", System.getProperty("java.home"));
85 System.setProperty("netbeans.user", new File(getWorkDir().getParentFile(), "user").getAbsolutePath());
86 } catch (IOException ex) {
87 Exceptions.printStackTrace(ex);
89 //Load the required libraries. This allows evaluation of project properties
90 //ex:- ${libs.jsf12-support.classpath}
91 LibraryManager.getDefault().getLibraries();
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")));
107 protected void tearDown() throws Exception {
112 public FacesModelSet createFacesModelSet() {
113 FileObject file = getProject().getProjectDirectory();
114 FacesModelSet set = FacesModelSet.getInstance(file);
118 protected int getBeansCount() {
119 return getPageBeans().length + getNonPageBeansCount();
122 protected int getNonPageBeansCount() {
123 return getRequestBeans().length +
124 getSessionBeans().length +
125 getApplicationBeans().length;
128 protected String[] getBeanNames() {
129 String[] str = new String[getBeansCount()];
131 for(String s : getPageBeans()) {
134 for (String s : getRequestBeans()) {
137 for (String s : getSessionBeans()) {
140 for (String s : getApplicationBeans()) {
146 protected List<Bean> createBeans(String[] types) {
147 FacesModelSet modelSet = createFacesModelSet();
148 FacesModel model = modelSet.getFacesModel(getJavaFile(getPageBeans()[0]));
150 BeansUnit bu = model.getBeansUnit();
151 List<Bean> beans = new ArrayList<Bean>();
152 for(String type : types) {
153 beans.add(createBean(bu, type));
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();
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);
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);
179 name = (String)m.invoke(bu, type);
181 return (Bean) ctor.newInstance(bu, beanInfo, name);
183 } catch (Exception ex) {
184 Exceptions.printStackTrace(ex);
186 Thread.currentThread().setContextClassLoader(oldContextClassLoader);
191 protected FileObject getJavaFile(String name) {
192 FileObject[] roots = ((WebProject)getProject()).getSourceRoots().getRoots();
193 for(FileObject f : roots) {
194 FileObject result = findFileObject(f, name);
202 protected FileObject findFileObject(FileObject f, String name) {
203 FileObject result = null;
205 for(FileObject child : f.getChildren()) {
206 result = findFileObject(child, name);
212 if(name.equals(f.getName())) {
219 protected String getFQN(String className) {
220 FileObject[] roots = ((WebProject)getProject()).getSourceRoots().getRoots();
221 for(FileObject f : roots) {
222 FileObject result = findFileObject(f, className);
224 String filePath = result.getPath().substring(0, result.getPath().lastIndexOf("."));
225 String relativePath = filePath.replace(f.getPath(), "");
226 return relativePath.replace('/', '.').substring(1);
232 protected String getPackageName(String className) {
233 String relativePath = getFQN(className);
234 int lastDotIndex = relativePath.lastIndexOf('.');
235 return relativePath.substring(0, lastDotIndex);
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);
244 f = new File(getDataDir().getAbsolutePath(), relativeProjectPath + ".zip");
247 project = ProjectUtils.openProject(getWorkDir(), f.getAbsolutePath(), projectName);
248 //Necessary for FacesConfigModel
249 JSFConfigUtils.setUp(project);
250 } catch (IOException ex) {
251 Exceptions.printStackTrace(ex);
256 public Project getProject() {
260 public void destroyProject() throws IOException {
261 ProjectUtils.destroyProject(project);
264 public String[] getApplicationBeans() {
265 return applicationBeans;
268 public String[] getPageBeans() {
272 public String[] getRequestBeans() {
276 public String[] getSessionBeans() {
280 public String[] getFacesConfigs() {
284 private static void setupServices() {
285 URL.setURLStreamHandlerFactory(Lookup.getDefault().lookup(URLStreamHandlerFactory.class));
286 MockServices.setServices(RepositoryImpl.class, InsyncMimeResolver.class);