quynguyen@1759
|
1 |
/*
|
quynguyen@1759
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jglick@2927
|
3 |
*
|
jglick@2927
|
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
|
jglick@2927
|
5 |
*
|
jglick@2927
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jglick@2927
|
7 |
* Other names may be trademarks of their respective owners.
|
jglick@2927
|
8 |
*
|
quynguyen@1759
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
quynguyen@1759
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
quynguyen@1759
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
quynguyen@1759
|
12 |
* "License"). You may not use this file except in compliance with the
|
quynguyen@1759
|
13 |
* License. You can obtain a copy of the License at
|
quynguyen@1759
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
quynguyen@1759
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
quynguyen@1759
|
16 |
* specific language governing permissions and limitations under the
|
quynguyen@1759
|
17 |
* License. When distributing the software, include this License Header
|
quynguyen@1759
|
18 |
* Notice in each file and include the License file at
|
jglick@2927
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
quynguyen@1759
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jglick@2927
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
quynguyen@1759
|
22 |
* accompanied this code. If applicable, add the following below the
|
quynguyen@1759
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
quynguyen@1759
|
24 |
* your own identifying information:
|
quynguyen@1759
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
quynguyen@1759
|
26 |
*
|
quynguyen@1759
|
27 |
* If you wish your version of this file to be governed by only the CDDL
|
quynguyen@1759
|
28 |
* or only the GPL Version 2, indicate your decision by adding
|
quynguyen@1759
|
29 |
* "[Contributor] elects to include this software in this distribution
|
quynguyen@1759
|
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
quynguyen@1759
|
31 |
* single choice of license, a recipient has the option to distribute
|
quynguyen@1759
|
32 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
quynguyen@1759
|
33 |
* to extend the choice of license to its licensees as provided above.
|
quynguyen@1759
|
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
quynguyen@1759
|
35 |
* Version 2 license, then the option applies only if the new code is
|
quynguyen@1759
|
36 |
* made subject to such option by the copyright holder.
|
quynguyen@1759
|
37 |
*
|
quynguyen@1759
|
38 |
* Contributor(s):
|
quynguyen@1759
|
39 |
*
|
quynguyen@1759
|
40 |
* Portions Copyrighted 2007 Sun Microsystems, Inc.
|
quynguyen@1759
|
41 |
*/
|
quynguyen@1759
|
42 |
|
quynguyen@1759
|
43 |
package org.netbeans.modules.visualweb.project.jsfloader.test;
|
quynguyen@1759
|
44 |
|
quynguyen@1759
|
45 |
import java.io.File;
|
quynguyen@1759
|
46 |
import java.io.FileInputStream;
|
quynguyen@1759
|
47 |
import java.io.IOException;
|
quynguyen@1759
|
48 |
import java.io.OutputStream;
|
quynguyen@1759
|
49 |
import java.util.Arrays;
|
quynguyen@1759
|
50 |
import java.util.Collections;
|
quynguyen@1759
|
51 |
import java.util.Enumeration;
|
quynguyen@1759
|
52 |
import java.util.zip.ZipEntry;
|
quynguyen@1759
|
53 |
import java.util.zip.ZipInputStream;
|
quynguyen@1759
|
54 |
import javax.swing.event.ChangeEvent;
|
quynguyen@1759
|
55 |
import org.netbeans.api.project.Project;
|
quynguyen@1759
|
56 |
import org.netbeans.api.project.ProjectManager;
|
quynguyen@1759
|
57 |
import org.netbeans.api.project.ui.OpenProjects;
|
jglick@3214
|
58 |
import org.netbeans.junit.MockServices;
|
quynguyen@1759
|
59 |
import org.netbeans.modules.visualweb.project.jsfloader.JsfJavaDataLoader;
|
quynguyen@1759
|
60 |
import org.netbeans.modules.visualweb.project.jsfloader.JsfJspDataLoader;
|
quynguyen@1759
|
61 |
import org.openide.filesystems.FileLock;
|
quynguyen@1759
|
62 |
import org.openide.filesystems.FileObject;
|
quynguyen@1759
|
63 |
import org.openide.filesystems.FileUtil;
|
quynguyen@1759
|
64 |
import org.openide.loaders.DataLoader;
|
quynguyen@1759
|
65 |
import org.openide.loaders.DataLoaderPool;
|
quynguyen@1759
|
66 |
import org.openide.util.Lookup;
|
quynguyen@1759
|
67 |
import org.openide.util.LookupEvent;
|
quynguyen@1759
|
68 |
import org.openide.util.LookupListener;
|
quynguyen@1759
|
69 |
|
quynguyen@1759
|
70 |
/**
|
quynguyen@1759
|
71 |
*
|
quynguyen@1759
|
72 |
* @author quynguyen
|
quynguyen@1759
|
73 |
*/
|
quynguyen@1759
|
74 |
public class SetupUtils {
|
quynguyen@1759
|
75 |
public static Project setup(File workDir) throws IOException {
|
quynguyen@1759
|
76 |
File userDir = new File(workDir, "userdir");
|
quynguyen@1759
|
77 |
userDir.mkdir();
|
quynguyen@1759
|
78 |
System.getProperties().put("netbeans.user", userDir.getAbsolutePath());
|
jglick@3214
|
79 |
|
jglick@3214
|
80 |
MockServices.setServices(SetupUtils.DefaultPool.class, JsfJspDataLoader.class, JsfJavaDataLoader.class);
|
quynguyen@1759
|
81 |
|
quynguyen@1759
|
82 |
String zipResource = "VWJavaEE5.zip";
|
quynguyen@1759
|
83 |
String zipPath = SetupUtils.class.getResource(zipResource).getPath();
|
quynguyen@1759
|
84 |
if (zipPath == null) {
|
quynguyen@1759
|
85 |
throw new IOException("Could not load zip resource: " + zipResource);
|
quynguyen@1759
|
86 |
}
|
quynguyen@1759
|
87 |
|
quynguyen@1759
|
88 |
File archiveFile = new File(zipPath);
|
quynguyen@1759
|
89 |
|
quynguyen@1759
|
90 |
FileObject destFileObj = FileUtil.toFileObject(workDir);
|
quynguyen@1759
|
91 |
unZipFile(archiveFile, destFileObj);
|
quynguyen@1759
|
92 |
|
quynguyen@1759
|
93 |
if (!destFileObj.isValid()) {
|
quynguyen@1759
|
94 |
throw new IOException("FileObject for project directory not valid");
|
quynguyen@1759
|
95 |
}
|
quynguyen@1759
|
96 |
|
quynguyen@1759
|
97 |
FileObject testApp = destFileObj.getFileObject("VWJavaEE5");
|
quynguyen@1759
|
98 |
System.out.println("Children of VWJavaEE5:" + Arrays.toString(testApp.getChildren()));
|
quynguyen@1759
|
99 |
|
quynguyen@1759
|
100 |
Project project = ProjectManager.getDefault().findProject(testApp);
|
quynguyen@1759
|
101 |
|
quynguyen@1759
|
102 |
if (project == null) {
|
quynguyen@1759
|
103 |
throw new IOException("Could not load project");
|
quynguyen@1759
|
104 |
}
|
quynguyen@1759
|
105 |
|
quynguyen@1759
|
106 |
OpenProjects.getDefault().open(new Project[]{project}, false);
|
quynguyen@1759
|
107 |
return project;
|
quynguyen@1759
|
108 |
}
|
quynguyen@1759
|
109 |
|
quynguyen@1816
|
110 |
public static JsfJavaDataLoader getJavaLoader() {
|
jglick@3214
|
111 |
return JsfJavaDataLoader.findObject(JsfJavaDataLoader.class, true);
|
quynguyen@1816
|
112 |
}
|
quynguyen@1816
|
113 |
|
quynguyen@1816
|
114 |
public static JsfJspDataLoader getJspLoader() {
|
jglick@3214
|
115 |
return JsfJspDataLoader.findObject(JsfJspDataLoader.class, true);
|
quynguyen@1816
|
116 |
}
|
quynguyen@1816
|
117 |
|
quynguyen@1759
|
118 |
private static void unZipFile(File archiveFile, FileObject destDir) throws IOException {
|
quynguyen@1759
|
119 |
FileInputStream fis = new FileInputStream(archiveFile);
|
quynguyen@1759
|
120 |
try {
|
quynguyen@1759
|
121 |
ZipInputStream str = new ZipInputStream(fis);
|
quynguyen@1759
|
122 |
ZipEntry entry;
|
quynguyen@1759
|
123 |
while ((entry = str.getNextEntry()) != null) {
|
quynguyen@1759
|
124 |
if (entry.isDirectory()) {
|
quynguyen@1759
|
125 |
FileUtil.createFolder(destDir, entry.getName());
|
quynguyen@1759
|
126 |
} else {
|
quynguyen@1759
|
127 |
FileObject fo = FileUtil.createData(destDir, entry.getName());
|
quynguyen@1759
|
128 |
FileLock lock = fo.lock();
|
quynguyen@1759
|
129 |
try {
|
quynguyen@1759
|
130 |
OutputStream out = fo.getOutputStream(lock);
|
quynguyen@1759
|
131 |
try {
|
quynguyen@1759
|
132 |
FileUtil.copy(str, out);
|
quynguyen@1759
|
133 |
} finally {
|
quynguyen@1759
|
134 |
out.close();
|
quynguyen@1759
|
135 |
}
|
quynguyen@1759
|
136 |
} finally {
|
quynguyen@1759
|
137 |
lock.releaseLock();
|
quynguyen@1759
|
138 |
}
|
quynguyen@1759
|
139 |
}
|
quynguyen@1759
|
140 |
}
|
quynguyen@1759
|
141 |
} finally {
|
quynguyen@1759
|
142 |
fis.close();
|
quynguyen@1759
|
143 |
}
|
quynguyen@1759
|
144 |
}
|
quynguyen@1759
|
145 |
|
quynguyen@1759
|
146 |
/**
|
quynguyen@1759
|
147 |
* Taken from DataLoaderPool.DefaultPool to override NbLoaderPool
|
quynguyen@1759
|
148 |
*
|
quynguyen@1759
|
149 |
* Special pool for unit testing etc.
|
quynguyen@1759
|
150 |
* Finds all relevant data loaders in default lookup.
|
quynguyen@1759
|
151 |
*
|
quynguyen@1759
|
152 |
*/
|
quynguyen@1759
|
153 |
public static final class DefaultPool extends DataLoaderPool implements LookupListener {
|
quynguyen@1759
|
154 |
|
quynguyen@1759
|
155 |
private final Lookup.Result<DataLoader> result;
|
quynguyen@1759
|
156 |
|
quynguyen@1759
|
157 |
public DefaultPool() {
|
quynguyen@1759
|
158 |
result = Lookup.getDefault().lookupResult(DataLoader.class);
|
quynguyen@1759
|
159 |
result.addLookupListener(this);
|
quynguyen@1759
|
160 |
}
|
quynguyen@1759
|
161 |
|
quynguyen@1759
|
162 |
protected Enumeration<? extends DataLoader> loaders() {
|
quynguyen@1759
|
163 |
return Collections.enumeration(result.allInstances());
|
quynguyen@1759
|
164 |
}
|
quynguyen@1759
|
165 |
|
quynguyen@1759
|
166 |
public void resultChanged(LookupEvent e) {
|
quynguyen@1759
|
167 |
fireChangeEvent(new ChangeEvent(this));
|
quynguyen@1759
|
168 |
}
|
quynguyen@1759
|
169 |
|
quynguyen@1759
|
170 |
}
|
quynguyen@1759
|
171 |
}
|