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: package org.netbeans.modules.visualweb.insync;
029:
030: import com.sun.rave.designtime.Constants;
031: import java.beans.BeanInfo;
032: import java.io.File;
033: import java.io.IOException;
034: import java.lang.reflect.Constructor;
035: import java.net.URL;
036: import java.net.URLStreamHandlerFactory;
037: import java.util.ArrayList;
038: import java.util.List;
039: import java.util.regex.Pattern;
040: import junit.framework.*;
041: import org.netbeans.api.project.Project;
042: import org.netbeans.api.project.libraries.LibraryManager;
043: import org.netbeans.core.startup.layers.NbinstURLStreamHandlerFactory;
044: import org.netbeans.junit.NbTestCase;
045: import org.netbeans.modules.visualweb.insync.beans.Bean;
046: import org.netbeans.modules.visualweb.insync.beans.BeansUnit;
047: import org.netbeans.modules.visualweb.insync.models.FacesModel;
048: import org.netbeans.modules.visualweb.insync.models.FacesModelSet;
049: import org.netbeans.modules.web.project.WebProject;
050: import org.openide.filesystems.FileObject;
051: import org.openide.util.Exceptions;
052: import org.openide.util.test.MockLookup;
053:
054: /**
055: *
056: * @author sc32560, jdeva
057: *
058: */
059: public class InsyncTestBase extends NbTestCase {
060: public InsyncTestBase(String name) {
061: super (name);
062: _setUp();
063: }
064:
065: String[] pageBeans, requestBeans, sessionBeans, applicationBeans,
066: facesConfigs;
067: Project project;
068:
069: static {
070: setupServices();
071: }
072:
073: @Override
074: protected void setUp() throws Exception {
075: super .setUp();
076: //_setUp();
077: }
078:
079: private void _setUp() {
080: //Set up the system properties
081: System.setProperty("jdk.home", System.getProperty("java.home"));
082: try {
083:
084: System.setProperty("netbeans.user", new File(getWorkDir()
085: .getParentFile(), "user").getAbsolutePath());
086: } catch (IOException ex) {
087: Exceptions.printStackTrace(ex);
088: }
089: //Load the required libraries. This allows evaluation of project properties
090: //ex:- ${libs.jsf12-support.classpath}
091: LibraryManager.getDefault().getLibraries();
092: openProject();
093: pageBeans = System.getProperty("visualweb.project.pagebeans")
094: .split(
095: Pattern.quote(System
096: .getProperty("path.separator")));
097: requestBeans = System.getProperty(
098: "visualweb.project.requestbeans").split(
099: Pattern.quote(System.getProperty("path.separator")));
100: sessionBeans = System.getProperty(
101: "visualweb.project.sessionbeans").split(
102: Pattern.quote(System.getProperty("path.separator")));
103: applicationBeans = System.getProperty(
104: "visualweb.project.applicationbeans").split(
105: Pattern.quote(System.getProperty("path.separator")));
106: facesConfigs = System.getProperty(
107: "visualweb.project.facesconfig").split(
108: Pattern.quote(System.getProperty("path.separator")));
109:
110: }
111:
112: @Override
113: protected void tearDown() throws Exception {
114: super .tearDown();
115: clearWorkDir();
116: }
117:
118: public FacesModelSet createFacesModelSet() {
119: FileObject file = getProject().getProjectDirectory();
120: FacesModelSet set = FacesModelSet.getInstance(file);
121: return set;
122: }
123:
124: protected int getBeansCount() {
125: return getPageBeans().length + getNonPageBeansCount();
126: }
127:
128: protected int getNonPageBeansCount() {
129: return getRequestBeans().length + getSessionBeans().length
130: + getApplicationBeans().length;
131: }
132:
133: protected String[] getBeanNames() {
134: String[] str = new String[getBeansCount()];
135: int i = 0;
136: for (String s : getPageBeans()) {
137: str[i++] = s;
138: }
139: for (String s : getRequestBeans()) {
140: str[i++] = s;
141: }
142: for (String s : getSessionBeans()) {
143: str[i++] = s;
144: }
145: for (String s : getApplicationBeans()) {
146: str[i++] = s;
147: }
148: return str;
149: }
150:
151: protected List<Bean> createBeans(String[] types) {
152: FacesModelSet modelSet = createFacesModelSet();
153: FacesModel model = modelSet
154: .getFacesModel(getJavaFile(getPageBeans()[0]));
155: model.sync();
156: BeansUnit bu = model.getBeansUnit();
157: List<Bean> beans = new ArrayList<Bean>();
158: for (String type : types) {
159: beans.add(createBean(bu, type));
160: }
161: return beans;
162: }
163:
164: private Bean createBean(BeansUnit bu, String type) {
165: Constructor ctor = null;
166: java.lang.reflect.Method m = null;
167: ClassLoader oldContextClassLoader = Thread.currentThread()
168: .getContextClassLoader();
169: try {
170: Class clazz = Class
171: .forName("org.netbeans.modules.visualweb.insync.beans.Bean");
172: ctor = clazz.getDeclaredConstructor(BeansUnit.class,
173: BeanInfo.class, String.class);
174: ctor.setAccessible(true);
175: clazz = Class
176: .forName("org.netbeans.modules.visualweb.insync.beans.BeansUnit");
177: m = clazz
178: .getDeclaredMethod("nextNameForType", String.class);
179: m.setAccessible(true);
180: } catch (Exception ex) {
181: Exceptions.printStackTrace(ex);
182: }
183: try {
184: Thread.currentThread().setContextClassLoader(
185: bu.getClassLoader());
186: Class beanClass = bu.getBeanClass(type);
187: if (beanClass != null) {
188: BeanInfo beanInfo = BeansUnit.getBeanInfo(beanClass, bu
189: .getClassLoader());
190: String name = (String) beanInfo.getBeanDescriptor()
191: .getValue(
192: Constants.BeanDescriptor.INSTANCE_NAME);
193: if (name == null) {
194: name = (String) m.invoke(bu, type);
195: }
196: return (Bean) ctor.newInstance(bu, beanInfo, name);
197: }
198: } catch (Exception ex) {
199: Exceptions.printStackTrace(ex);
200: } finally {
201: Thread.currentThread().setContextClassLoader(
202: oldContextClassLoader);
203: }
204: return null;
205: }
206:
207: protected FileObject getJavaFile(String name) {
208: FileObject[] roots = ((WebProject) getProject())
209: .getSourceRoots().getRoots();
210: for (FileObject f : roots) {
211: FileObject result = findFileObject(f, name);
212: if (result != null) {
213: return result;
214: }
215: }
216: return null;
217: }
218:
219: protected FileObject findFileObject(FileObject f, String name) {
220: FileObject result = null;
221: if (f.isFolder()) {
222: for (FileObject child : f.getChildren()) {
223: result = findFileObject(child, name);
224: if (result != null) {
225: return result;
226: }
227: }
228: } else {
229: if (name.equals(f.getName())) {
230: result = f;
231: }
232: }
233: return result;
234: }
235:
236: protected String getFQN(String className) {
237: FileObject[] roots = ((WebProject) getProject())
238: .getSourceRoots().getRoots();
239: for (FileObject f : roots) {
240: FileObject result = findFileObject(f, className);
241: if (result != null) {
242: String filePath = result.getPath().substring(0,
243: result.getPath().lastIndexOf("."));
244: String relativePath = filePath.replace(f.getPath(), "");
245: return relativePath.replace('/', '.').substring(1);
246: }
247: }
248: return null;
249: }
250:
251: protected String getPackageName(String className) {
252: String relativePath = getFQN(className);
253: int lastDotIndex = relativePath.lastIndexOf('.');
254: return relativePath.substring(0, lastDotIndex);
255: }
256:
257: public Project openProject() {
258: String projectName = System
259: .getProperty("visualweb.project.name");
260: String relativeProjectPath = "projects" + File.separator
261: + projectName;
262: //Check for a directory and then a zip file by project name
263: File f = new File(getDataDir().getAbsolutePath(),
264: relativeProjectPath);
265: if (!f.exists()) {
266: f = new File(getDataDir().getAbsolutePath(),
267: relativeProjectPath + ".zip");
268: }
269: try {
270: project = ProjectUtils.openProject(getWorkDir(), f
271: .getAbsolutePath(), projectName);
272: //Necessary for FacesConfigModel
273: JSFConfigUtils.setUp(project);
274: } catch (IOException ex) {
275: Exceptions.printStackTrace(ex);
276: }
277: return project;
278: }
279:
280: public Project getProject() {
281: return project;
282: }
283:
284: public void destroyProject() throws IOException {
285: ProjectUtils.destroyProject(project);
286: }
287:
288: public String[] getApplicationBeans() {
289: return applicationBeans;
290: }
291:
292: public String[] getPageBeans() {
293: return pageBeans;
294: }
295:
296: public String[] getRequestBeans() {
297: return requestBeans;
298: }
299:
300: public String[] getSessionBeans() {
301: return sessionBeans;
302: }
303:
304: public String[] getFacesConfigs() {
305: return facesConfigs;
306: }
307:
308: private static void setupServices() {
309: URLStreamHandlerFactory urlStreamHandlerFactory = new NbinstURLStreamHandlerFactory();
310: URL.setURLStreamHandlerFactory(urlStreamHandlerFactory);
311: MockLookup.setInstances(new RepositoryImpl(),
312: new InsyncMimeResolver(), urlStreamHandlerFactory);
313: }
314: }
|