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