Skip to content

Commit 33196c4

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 33196c4

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,12 @@ 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+
String filePath = IPath.fromOSString(url.getFile()).toOSString();
254+
if (Files.exists(Path.of(filePath))) {
255+
return filePath;
256+
}
257+
}
254258
return null;
255259
}
256260
url = resolvePathVariables(url);

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)