2
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
* Deniz TURAN
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.carbide.cpp.sysdoc.hover;
|
|
18 |
|
|
19 |
import java.io.BufferedInputStream;
|
|
20 |
import java.io.File;
|
|
21 |
import java.io.FileOutputStream;
|
|
22 |
import java.io.InputStream;
|
|
23 |
import java.net.URL;
|
|
24 |
import java.util.Iterator;
|
|
25 |
import java.util.Set;
|
|
26 |
import java.util.concurrent.Callable;
|
|
27 |
import java.util.concurrent.FutureTask;
|
|
28 |
import java.util.zip.ZipEntry;
|
|
29 |
import java.util.zip.ZipInputStream;
|
|
30 |
|
|
31 |
import org.eclipse.cdt.core.model.CModelException;
|
|
32 |
import org.eclipse.cdt.core.model.CoreModel;
|
|
33 |
import org.eclipse.cdt.core.model.ICProject;
|
|
34 |
import org.eclipse.cdt.core.model.ITranslationUnit;
|
|
35 |
import org.eclipse.cdt.internal.core.model.CModelManager;
|
|
36 |
import org.eclipse.cdt.internal.ui.util.EditorUtility;
|
|
37 |
import org.eclipse.cdt.ui.CUIPlugin;
|
|
38 |
import org.eclipse.core.resources.IFile;
|
|
39 |
import org.eclipse.core.resources.IProject;
|
|
40 |
import org.eclipse.core.resources.IWorkspaceRoot;
|
|
41 |
import org.eclipse.core.runtime.FileLocator;
|
|
42 |
import org.eclipse.core.runtime.IPath;
|
|
43 |
import org.eclipse.core.runtime.Path;
|
|
44 |
import org.eclipse.swt.widgets.Display;
|
|
45 |
import org.eclipse.ui.IEditorPart;
|
|
46 |
import org.eclipse.ui.PartInitException;
|
|
47 |
import org.eclipse.ui.internal.WorkbenchPage;
|
|
48 |
import org.eclipse.ui.part.FileEditorInput;
|
|
49 |
|
|
50 |
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.AsynchronousLookup;
|
|
51 |
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.devlib.DevLibPluginController;
|
|
52 |
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.devlib.DevLibProperties;
|
|
53 |
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.interX.InterXIndexController;
|
|
54 |
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.interX.InterXProperties;
|
|
55 |
import com.nokia.carbide.cpp.sysdoc.internal.hover.dao.DevLibContent;
|
|
56 |
import com.nokia.carbide.cpp.sysdoc.internal.hover.uitlis.Logger;
|
|
57 |
|
|
58 |
public class TestHelper {
|
|
59 |
|
|
60 |
private static String projectName="HW";
|
|
61 |
|
|
62 |
public static Set<DevLibProperties> getAvailableDevLibProps()
|
|
63 |
throws Exception {
|
|
64 |
|
|
65 |
Set<DevLibProperties> devLibProps = DevLibPluginController
|
|
66 |
.getInstance().getDevLibPropertiesSet();
|
|
67 |
|
|
68 |
return devLibProps;
|
|
69 |
}
|
|
70 |
|
|
71 |
public static InterXProperties getAnInterXProps() {
|
|
72 |
Set<DevLibProperties> devPropSet = null;
|
|
73 |
try {
|
|
74 |
devPropSet = getAvailableDevLibProps();
|
|
75 |
} catch (Exception e) {
|
|
76 |
return null;
|
|
77 |
}
|
|
78 |
if (devPropSet.isEmpty()) {
|
|
79 |
return null;
|
|
80 |
} else {
|
|
81 |
Iterator<DevLibProperties> it = devPropSet.iterator();
|
|
82 |
DevLibProperties devLib = it.next();
|
|
83 |
return devLib.getInterXProperties();
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
public static void waitIndexingComplete(long timeout) throws Exception {
|
|
88 |
long sleep = 100;
|
|
89 |
Display display = Activator.getDefault().getWorkbench().getDisplay();
|
|
90 |
int totalSleep = 0;
|
|
91 |
Thread.sleep(2000);
|
|
92 |
Logger.logDebug("Waiting to timeout spend" + timeout);
|
|
93 |
while (true) {
|
|
94 |
if (totalSleep > timeout) {
|
|
95 |
throw new Exception(
|
|
96 |
"Timeout. Indexing has not finished in timout period:"
|
|
97 |
+ timeout);
|
|
98 |
}
|
|
99 |
boolean indexingCompleted = InterXIndexController
|
|
100 |
.isIndexingCompleted();
|
|
101 |
if (indexingCompleted) {
|
|
102 |
break;
|
|
103 |
} else {
|
|
104 |
display.readAndDispatch();
|
|
105 |
Thread.sleep(sleep);
|
|
106 |
}
|
|
107 |
totalSleep += sleep;
|
|
108 |
}
|
|
109 |
Logger.logDebug("Waiting has finished " + timeout);
|
|
110 |
}
|
|
111 |
|
|
112 |
public static FutureTask<DevLibContent> createAsynchronousLookup(String fqn) {
|
|
113 |
Callable<DevLibContent> callable = new AsynchronousLookup(fqn);
|
|
114 |
FutureTask<DevLibContent> ft = new FutureTask<DevLibContent>(callable);
|
|
115 |
new Thread(ft).start();
|
|
116 |
return ft;
|
|
117 |
}
|
|
118 |
|
|
119 |
// this method creates a feature which simulates waiting
|
|
120 |
public static FutureTask<DevLibContent> createAsynchronousLookupFuture(
|
|
121 |
final String fqn) {
|
|
122 |
|
|
123 |
Callable<DevLibContent> waitCallable = new Callable<DevLibContent>() {
|
|
124 |
@Override
|
|
125 |
public DevLibContent call() throws Exception {
|
|
126 |
Thread.sleep(2000);
|
|
127 |
Callable<DevLibContent> callable = new AsynchronousLookup(fqn);
|
|
128 |
FutureTask<DevLibContent> ft1 = new FutureTask<DevLibContent>(
|
|
129 |
callable);
|
|
130 |
new Thread(ft1).start();
|
|
131 |
Display display = Activator.getDefault().getWorkbench()
|
|
132 |
.getDisplay();
|
|
133 |
int sleep = 100;
|
|
134 |
while (true) {
|
|
135 |
if (ft1.isDone()) {
|
|
136 |
return ft1.get();
|
|
137 |
}
|
|
138 |
|
|
139 |
if (ft1.isCancelled()) {
|
|
140 |
return null;
|
|
141 |
}
|
|
142 |
|
|
143 |
display.readAndDispatch();
|
|
144 |
Thread.sleep(sleep);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
};
|
|
148 |
|
|
149 |
FutureTask<DevLibContent> ft = new FutureTask<DevLibContent>(
|
|
150 |
waitCallable);
|
|
151 |
new Thread(ft).start();
|
|
152 |
return ft;
|
|
153 |
}
|
|
154 |
|
|
155 |
public static IEditorPart getIEditorPart() throws Exception{
|
|
156 |
IProject project=getProject();
|
|
157 |
IFile file = project.getFile("/src/HW.cpp");
|
|
158 |
IEditorPart editorPart =EditorUtility.openInEditor(file);
|
|
159 |
return editorPart;
|
|
160 |
// IEditorPart findEditor = WorkbenchPage.this.getEditorManager().findEditor(new FileEditorInput(file));
|
|
161 |
//WorkbenchHelper
|
|
162 |
}
|
|
163 |
|
|
164 |
|
|
165 |
private static IProject getProject() throws Exception{
|
|
166 |
IProject project;
|
|
167 |
IWorkspaceRoot root = CUIPlugin.getWorkspace().getRoot();
|
|
168 |
IPath location = root.getLocation();
|
|
169 |
URL src = FileLocator.find(ActivatorTest.getDefault().getBundle(),
|
|
170 |
new Path("resource/HW.zip"), null);
|
|
171 |
String dest = location.toOSString();
|
|
172 |
uncompressedZip(src, dest);
|
|
173 |
root.refreshLocal(0, null);
|
|
174 |
Thread.sleep(2000);
|
|
175 |
project = CUIPlugin.getWorkspace().getRoot().getProject(projectName);
|
|
176 |
if (!project.isOpen()){
|
|
177 |
project.create(null);
|
|
178 |
project.open(null);
|
|
179 |
}
|
|
180 |
return project;
|
|
181 |
}
|
|
182 |
|
|
183 |
public static ITranslationUnit getTranslationUnit()
|
|
184 |
throws Exception {
|
|
185 |
|
|
186 |
IProject project=getProject();
|
|
187 |
IFile file = project.getFile("/src/HW.cpp");
|
|
188 |
ITranslationUnit tUnit = (ITranslationUnit) CoreModel.getDefault()
|
|
189 |
.create(file);
|
|
190 |
return tUnit;
|
|
191 |
}
|
|
192 |
|
|
193 |
private static void uncompressedZip(URL jarSrc, String dest)
|
|
194 |
throws Exception {
|
|
195 |
InputStream os = jarSrc.openStream();
|
|
196 |
BufferedInputStream bis = new BufferedInputStream(os);
|
|
197 |
ZipInputStream zis = new ZipInputStream(bis);
|
|
198 |
int i = 0;
|
|
199 |
while (true) {
|
|
200 |
ZipEntry zipEntry = zis.getNextEntry();
|
|
201 |
|
|
202 |
if (zis.available() == 0 || zipEntry == null)
|
|
203 |
break;
|
|
204 |
|
|
205 |
uncompressZipEntry(zis, zipEntry, dest);
|
|
206 |
i++;
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
private static long uncompressZipEntry(ZipInputStream zis,
|
|
211 |
ZipEntry zipEntry, String dest) throws Exception {
|
|
212 |
byte[] buf = new byte[1024];
|
|
213 |
String entryName = zipEntry.getName();
|
|
214 |
int n;
|
|
215 |
File rootDir = new File(dest);
|
|
216 |
FileOutputStream fileoutputstream;
|
|
217 |
File newFile = new File(rootDir, entryName);
|
|
218 |
long totalLen = 0;
|
|
219 |
|
|
220 |
if (zipEntry.isDirectory()) {
|
|
221 |
if (!newFile.exists()) {
|
|
222 |
newFile.mkdir();
|
|
223 |
}
|
|
224 |
return 0;
|
|
225 |
}
|
|
226 |
fileoutputstream = new FileOutputStream(newFile);
|
|
227 |
|
|
228 |
while ((n = zis.read(buf, 0, 1024)) > -1) {
|
|
229 |
fileoutputstream.write(buf, 0, n);
|
|
230 |
totalLen += n;
|
|
231 |
}
|
|
232 |
|
|
233 |
fileoutputstream.close();
|
|
234 |
zis.closeEntry();
|
|
235 |
return totalLen;
|
|
236 |
}
|
|
237 |
}
|