Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.pde.ui.tests.build.properties; |
12 |
|
13 |
import org.eclipse.core.runtime.CoreException; |
14 |
|
15 |
import org.eclipse.core.resources.IMarker; |
16 |
|
17 |
import java.io.*; |
18 |
import java.net.URL; |
19 |
import java.util.Enumeration; |
20 |
import java.util.PropertyResourceBundle; |
21 |
import java.util.zip.ZipEntry; |
22 |
import java.util.zip.ZipFile; |
23 |
import junit.framework.*; |
24 |
import org.eclipse.core.resources.*; |
25 |
import org.eclipse.core.runtime.*; |
26 |
import org.eclipse.core.runtime.jobs.Job; |
27 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
28 |
import org.eclipse.pde.core.plugin.IPluginModelBase; |
29 |
import org.eclipse.pde.core.plugin.PluginRegistry; |
30 |
import org.eclipse.pde.internal.core.builders.CompilerFlags; |
31 |
import org.eclipse.pde.internal.core.builders.PDEMarkerFactory; |
32 |
import org.eclipse.pde.internal.core.natures.PDE; |
33 |
import org.eclipse.pde.internal.ui.PDEPlugin; |
34 |
import org.eclipse.pde.internal.ui.correction.ResolutionGenerator; |
35 |
import org.eclipse.pde.internal.ui.tests.macro.MacroPlugin; |
36 |
import org.eclipse.pde.ui.tests.target.LocalTargetDefinitionTests; |
37 |
import org.eclipse.ui.IMarkerResolution; |
38 |
import org.osgi.service.prefs.BackingStoreException; |
39 |
|
40 |
public abstract class BaseValidator extends TestCase { |
41 |
|
42 |
private static final String MARKER = "marker"; |
43 |
|
44 |
/* (non-Javadoc) |
45 |
* @see junit.framework.TestCase#setUp() |
46 |
*/ |
47 |
protected void setUp() throws Exception { |
48 |
IPath projectsLocation = new Path(MacroPlugin.getDefault().getBundle().getLocation() + "/tests/build.properties"); |
49 |
projectsLocation = projectsLocation.setDevice(null).removeFirstSegments(1); |
50 |
File[] zipFiles = projectsLocation.toFile().listFiles(); |
51 |
for (int i = 0; i < zipFiles.length; i++) { |
52 |
IProject project = findProject(zipFiles[i].getName().substring(0, zipFiles[i].getName().lastIndexOf('.'))); |
53 |
if (project.exists()) { |
54 |
project.delete(true, new NullProgressMonitor()); |
55 |
} |
56 |
project.create(new NullProgressMonitor()); |
57 |
doUnZip(MacroPlugin.getDefault().getStateLocation().removeLastSegments(2), "/tests/build.properties/" + zipFiles[i].getName()); |
58 |
} |
59 |
|
60 |
} |
61 |
|
62 |
/** |
63 |
* Runs the quick fix and verifies that the marker is now gone. |
64 |
* |
65 |
* @param buildProperty build.properties file (on which markers will looked for) |
66 |
* @param expectedValues properties file from which expected values will be read |
67 |
* @throws CoreException |
68 |
*/ |
69 |
protected void verifyQuickFixes(IResource buildProperty, PropertyResourceBundle expectedValues) throws CoreException { |
70 |
IMarker[] markers = buildProperty.findMarkers(PDEMarkerFactory.MARKER_ID, true, IResource.DEPTH_INFINITE); |
71 |
ResolutionGenerator resGen = new ResolutionGenerator(); |
72 |
for (int i = 0; i < markers.length; i++) { |
73 |
if (resGen.hasResolutions(markers[i])) { |
74 |
String markerEntry = (String) markers[i].getAttribute(PDEMarkerFactory.BK_BUILD_ENTRY); |
75 |
IMarkerResolution[] resolutions = resGen.getResolutions(markers[i]); |
76 |
String quickFixindex = getProperty(expectedValues, markerEntry , "quickfix"); |
77 |
resolutions[new Integer(quickFixindex.trim()).intValue()].run(markers[i]); |
78 |
buildProject(markers[i].getResource().getProject(), 0); |
79 |
assertFalse(markers[i].exists()); |
80 |
} |
81 |
} |
82 |
} |
83 |
|
84 |
/** |
85 |
* Verify the problem markers on the build.properties |
86 |
* @param buildProperty build.properties file (on which markers will looked for) |
87 |
* @param expectedValues properties file from which expected values will be read |
88 |
* @param severity expected severity of the problem markers |
89 |
* @throws CoreException |
90 |
*/ |
91 |
protected void verifyBuildPropertiesMarkers(IResource buildProperty, PropertyResourceBundle expectedValues, int severity) throws CoreException { |
92 |
IMarker[] markers = buildProperty.findMarkers(PDEMarkerFactory.MARKER_ID, true, IResource.DEPTH_INFINITE); |
93 |
|
94 |
//TODO |
95 |
String markercount = getProperty(expectedValues, "count"); |
96 |
for (int i = 0; i < markers.length; i++) { |
97 |
System.out.println(markers[i].getType()); |
98 |
System.out.println(markers[i].getAttributes().values()); |
99 |
} |
100 |
assertEquals(markercount, String.valueOf(markers.length)); |
101 |
|
102 |
int markerSeverity; |
103 |
switch (severity) { |
104 |
case CompilerFlags.ERROR : |
105 |
markerSeverity = IMarker.SEVERITY_ERROR; |
106 |
break; |
107 |
case CompilerFlags.WARNING : |
108 |
markerSeverity = IMarker.SEVERITY_WARNING; |
109 |
break; |
110 |
default : |
111 |
markerSeverity = IMarker.SEVERITY_INFO; |
112 |
} |
113 |
for (int i = 0; i < markers.length; i++) { |
114 |
String markerEntry = (String) markers[i].getAttribute(PDEMarkerFactory.BK_BUILD_ENTRY); |
115 |
assertEquals(markerSeverity, getIntAttribute(markers[i],IMarker.SEVERITY)); |
116 |
|
117 |
String markerType = getProperty(expectedValues, markerEntry, PDEMarkerFactory.CAT_ID); |
118 |
assertEquals(markerType, getStringAttribute(markers[i],PDEMarkerFactory.CAT_ID)); |
119 |
|
120 |
String tokenValue = getProperty(expectedValues, markerEntry, PDEMarkerFactory.BK_BUILD_TOKEN); |
121 |
assertEquals(tokenValue, getStringAttribute(markers[i],PDEMarkerFactory.BK_BUILD_TOKEN)); |
122 |
} |
123 |
|
124 |
} |
125 |
|
126 |
private int getIntAttribute(IMarker marker, String property) { |
127 |
Integer value; |
128 |
try { |
129 |
value = (Integer) marker.getAttribute(property); |
130 |
} catch (CoreException e) { |
131 |
return 0; |
132 |
} |
133 |
|
134 |
if (value == null ) |
135 |
return 0; |
136 |
return value.intValue(); |
137 |
} |
138 |
|
139 |
private String getStringAttribute(IMarker marker, String property) { |
140 |
String value; |
141 |
try { |
142 |
value = (String) marker.getAttribute(property); |
143 |
} catch (CoreException e) { |
144 |
value = ""; |
145 |
} |
146 |
|
147 |
if (value == null || value.equalsIgnoreCase("\"\"")) |
148 |
value = ""; |
149 |
return value.trim(); |
150 |
} |
151 |
|
152 |
private String getProperty(PropertyResourceBundle propertyBundle, String property) { |
153 |
String value; |
154 |
try { |
155 |
value = propertyBundle.getString(MARKER + '.' + property); |
156 |
} catch (Exception e) { |
157 |
value = ""; |
158 |
} |
159 |
if (value == null || value.equalsIgnoreCase("\"\"")) |
160 |
value = ""; |
161 |
return value.trim(); |
162 |
} |
163 |
|
164 |
private String getProperty(PropertyResourceBundle propertyBundle, String entry, String property) { |
165 |
return getProperty(propertyBundle, entry + '.' + property); |
166 |
} |
167 |
|
168 |
/** |
169 |
* Unzips the given archive to the specified location. |
170 |
* |
171 |
* @param location path in the local file system |
172 |
* @param archivePath path to archive relative to the test plug-in |
173 |
* @throws IOException |
174 |
*/ |
175 |
protected IPath doUnZip(IPath location, String archivePath) throws IOException { |
176 |
URL zipURL = MacroPlugin.getBundleContext().getBundle().getEntry(archivePath); |
177 |
Path zipPath = new Path(new File(FileLocator.toFileURL(zipURL).getFile()).getAbsolutePath()); |
178 |
ZipFile zipFile = new ZipFile(zipPath.toFile()); |
179 |
Enumeration entries = zipFile.entries(); |
180 |
IPath parent = location.removeLastSegments(1); |
181 |
while (entries.hasMoreElements()) { |
182 |
ZipEntry entry = (ZipEntry) entries.nextElement(); |
183 |
if (!entry.isDirectory()) { |
184 |
IPath entryPath = parent.append(entry.getName()); |
185 |
File dir = entryPath.removeLastSegments(1).toFile(); |
186 |
dir.mkdirs(); |
187 |
File file = entryPath.toFile(); |
188 |
file.createNewFile(); |
189 |
InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); |
190 |
byte[] bytes = LocalTargetDefinitionTests.getInputStreamAsByteArray(inputStream, -1); |
191 |
inputStream.close(); |
192 |
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file)); |
193 |
outputStream.write(bytes); |
194 |
outputStream.close(); |
195 |
} |
196 |
} |
197 |
zipFile.close(); |
198 |
return parent; |
199 |
} |
200 |
|
201 |
/** |
202 |
* Build the given project with the specified build.properties file and wait till the build the complete |
203 |
* @param project project to be build |
204 |
* @param index suffix to the build.properties that shall be used to build the project |
205 |
* @return <code>true</code> if the project got build successfully. <code>false</code> otherwise. |
206 |
* @throws CoreException |
207 |
*/ |
208 |
protected boolean buildProject(IProject project, int index) throws CoreException { |
209 |
IResource buildProp = project.findMember("build.properties"); |
210 |
if (index > 0) { |
211 |
if (buildProp != null && buildProp.exists()) |
212 |
buildProp.delete(true, new NullProgressMonitor()); |
213 |
buildProp = project.findMember("build.properties" + index); |
214 |
if (buildProp == null) { |
215 |
fail("build.properties" + index + "is missing. Can not build the project '" + project.getName() + "'"); |
216 |
return false; |
217 |
} |
218 |
buildProp.copy(buildProp.getProjectRelativePath().removeFileExtension().addFileExtension("properties"), true, new NullProgressMonitor()); |
219 |
} |
220 |
project.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor()); |
221 |
boolean wasInterrupted = false; |
222 |
do { |
223 |
try { |
224 |
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null); |
225 |
Job.getJobManager().join(ResourcesPlugin.FAMILY_MANUAL_BUILD, null); |
226 |
wasInterrupted = false; |
227 |
} catch (OperationCanceledException e) { |
228 |
return false; |
229 |
} catch (InterruptedException e) { |
230 |
wasInterrupted = true; |
231 |
} |
232 |
} while (wasInterrupted); |
233 |
return true; |
234 |
} |
235 |
|
236 |
/** |
237 |
* Set the project specific preferences on build.properties |
238 |
* |
239 |
* @param project project for which the preferences are to be set |
240 |
* @param severity severity level |
241 |
* @throws BackingStoreException |
242 |
*/ |
243 |
protected void setPreferences(IProject project, int severity) throws BackingStoreException { |
244 |
ProjectScope scope = new ProjectScope(project); |
245 |
IEclipsePreferences projectPrefs = scope.getNode(PDE.PLUGIN_ID); |
246 |
projectPrefs.putInt(CompilerFlags.P_BUILD, severity); |
247 |
projectPrefs.putInt(CompilerFlags.P_BUILD_MISSING_OUTPUT, severity); |
248 |
projectPrefs.putInt(CompilerFlags.P_BUILD_SOURCE_LIBRARY, severity); |
249 |
projectPrefs.putInt(CompilerFlags.P_BUILD_OUTPUT_LIBRARY, severity); |
250 |
projectPrefs.putInt(CompilerFlags.P_BUILD_SRC_INCLUDES, severity); |
251 |
projectPrefs.putInt(CompilerFlags.P_BUILD_BIN_INCLUDES, severity); |
252 |
projectPrefs.putInt(CompilerFlags.P_BUILD_JAVA_COMPLIANCE, severity); |
253 |
projectPrefs.flush(); |
254 |
projectPrefs.sync(); |
255 |
} |
256 |
|
257 |
/** |
258 |
* Find the project in workspace with the given id |
259 |
* @param id project id |
260 |
* @return project |
261 |
*/ |
262 |
protected IProject findProject(String id) { |
263 |
IPluginModelBase model = PluginRegistry.findModel(id); |
264 |
if (model != null) { |
265 |
IResource resource = model.getUnderlyingResource(); |
266 |
if (resource != null && resource.exists()) { |
267 |
return resource.getProject(); |
268 |
} |
269 |
} |
270 |
return PDEPlugin.getWorkspace().getRoot().getProject(id); |
271 |
} |
272 |
|
273 |
} |