Skip to content

Commit abd678c

Browse files
committed
Extend existence check in URLImageDescriptor when running without OSGi
This extends the check added in cff785d to also consider the case when OSGi isn't running. Otherwise a path to a non-existent file is returned, thus breaking the contract specified in the JavaDoc.
1 parent 789b373 commit abd678c

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

bundles/org.eclipse.jface/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Export-Package: org.eclipse.jface,
1818
org.eclipse.jface.fieldassist,
1919
org.eclipse.jface.fieldassist.images,
2020
org.eclipse.jface.images,
21-
org.eclipse.jface.internal;x-friends:="org.eclipse.ui.workbench,org.eclipse.e4.ui.workbench.renderers.swt",
21+
org.eclipse.jface.internal;x-friends:="org.eclipse.ui.workbench,org.eclipse.e4.ui.workbench.renderers.swt,org.eclipse.jface.tests",
2222
org.eclipse.jface.internal.provisional.action;x-friends:="org.eclipse.ui.workbench,org.eclipse.ui.ide",
2323
org.eclipse.jface.layout,
2424
org.eclipse.jface.menus,

bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.net.MalformedURLException;
25+
import java.net.URISyntaxException;
2526
import java.net.URL;
2627
import java.nio.file.Files;
2728
import java.nio.file.Path;
@@ -31,7 +32,6 @@
3132

3233
import org.eclipse.core.runtime.FileLocator;
3334
import org.eclipse.core.runtime.IAdaptable;
34-
import org.eclipse.core.runtime.IPath;
3535
import org.eclipse.core.runtime.Status;
3636
import org.eclipse.jface.internal.InternalPolicy;
3737
import org.eclipse.jface.util.Policy;
@@ -249,16 +249,20 @@ private static String getxPath(String name, int zoom) {
249249
private static String getFilePath(URL url, boolean logIOException) {
250250
try {
251251
if (!InternalPolicy.OSGI_AVAILABLE) {
252-
if (FILE_PROTOCOL.equalsIgnoreCase(url.getProtocol()))
253-
return IPath.fromOSString(url.getFile()).toOSString();
252+
if (FILE_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
253+
Path filePath = Path.of(url.toURI());
254+
if (Files.exists(filePath)) {
255+
return filePath.toString();
256+
}
257+
}
254258
return null;
255259
}
256260
url = resolvePathVariables(url);
257261
URL locatedURL = FileLocator.toFileURL(url);
258262
if (FILE_PROTOCOL.equalsIgnoreCase(locatedURL.getProtocol())) {
259-
String filePath = IPath.fromOSString(locatedURL.getPath()).toOSString();
260-
if (Files.exists(Path.of(filePath))) {
261-
return filePath;
263+
Path filePath = Path.of(locatedURL.toURI());
264+
if (Files.exists(filePath)) {
265+
return filePath.toString();
262266
}
263267
}
264268
return null;
@@ -272,6 +276,9 @@ private static String getFilePath(URL url, boolean logIOException) {
272276
}
273277
}
274278
return null;
279+
} catch (URISyntaxException e) {
280+
Policy.logException(e);
281+
return null;
275282
}
276283
}
277284

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/images/UrlImageDescriptorTest.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.eclipse.core.runtime.Adapters;
2828
import org.eclipse.core.runtime.IPath;
29+
import org.eclipse.jface.internal.InternalPolicy;
2930
import org.eclipse.jface.resource.ImageDescriptor;
3031
import org.eclipse.swt.graphics.Image;
3132
import org.eclipse.swt.graphics.ImageData;
@@ -122,20 +123,35 @@ public void testImageFileNameProviderGetxName() {
122123

123124
@Test
124125
public void testImageFileNameProviderGetxName_forFileURL() throws IOException {
125-
URL imageFileURL = tempFolder.newFile("image.png").toURI().toURL();
126-
tempFolder.newFile("[email protected]");
127-
ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageFileURL);
126+
testImageFileNameProviderGetxName_forFileURL(true);
127+
}
128128

129-
ImageFileNameProvider fileNameProvider = Adapters.adapt(descriptor, ImageFileNameProvider.class);
130-
assertNotNull("URLImageDescriptor does not adapt to ImageFileNameProvider", fileNameProvider);
131-
String imagePath100 = fileNameProvider.getImagePath(100);
132-
assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the 100% path", imagePath100);
133-
assertEquals(IPath.fromOSString(imagePath100).lastSegment(), "image.png");
134-
String imagePath200 = fileNameProvider.getImagePath(200);
135-
assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the @2x path", imagePath200);
136-
assertEquals(IPath.fromOSString(imagePath200).lastSegment(), "[email protected]");
137-
String imagePath150 = fileNameProvider.getImagePath(150);
138-
assertNull("URLImageDescriptor's ImageFileNameProvider does return a @1.5x path", imagePath150);
129+
@Test
130+
public void testImageFileNameProviderGetxName_forFileURL_noOSGi() throws IOException {
131+
testImageFileNameProviderGetxName_forFileURL(false);
132+
}
133+
134+
private void testImageFileNameProviderGetxName_forFileURL(boolean osgiAvailable) throws IOException {
135+
boolean oldOsgiAvailable = InternalPolicy.OSGI_AVAILABLE;
136+
InternalPolicy.OSGI_AVAILABLE = osgiAvailable;
137+
try {
138+
URL imageFileURL = tempFolder.newFile("image.png").toURI().toURL();
139+
tempFolder.newFile("[email protected]");
140+
ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageFileURL);
141+
142+
ImageFileNameProvider fileNameProvider = Adapters.adapt(descriptor, ImageFileNameProvider.class);
143+
assertNotNull("URLImageDescriptor does not adapt to ImageFileNameProvider", fileNameProvider);
144+
String imagePath100 = fileNameProvider.getImagePath(100);
145+
assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the 100% path", imagePath100);
146+
assertEquals(IPath.fromOSString(imagePath100).lastSegment(), "image.png");
147+
String imagePath200 = fileNameProvider.getImagePath(200);
148+
assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the @2x path", imagePath200);
149+
assertEquals(IPath.fromOSString(imagePath200).lastSegment(), "[email protected]");
150+
String imagePath150 = fileNameProvider.getImagePath(150);
151+
assertNull("URLImageDescriptor's ImageFileNameProvider does return a @1.5x path", imagePath150);
152+
} finally {
153+
InternalPolicy.OSGI_AVAILABLE = oldOsgiAvailable;
154+
}
139155
}
140156

141157
@Test

0 commit comments

Comments
 (0)