Skip to content

Commit cee4631

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 85ab042 commit cee4631

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
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: 16 additions & 14 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;
@@ -246,24 +246,16 @@ private static String getxPath(String name, int zoom) {
246246
*
247247
* @return {@link String} or <code>null</code> if the file cannot be found
248248
*/
249-
private static String getFilePath(URL url, boolean logIOException) {
249+
private static String getFilePath(URL url, boolean logException) {
250250
try {
251251
if (!InternalPolicy.OSGI_AVAILABLE) {
252-
if (FILE_PROTOCOL.equalsIgnoreCase(url.getProtocol()))
253-
return IPath.fromOSString(url.getFile()).toOSString();
254-
return null;
252+
return getFilePath(url);
255253
}
256254
url = resolvePathVariables(url);
257255
URL locatedURL = FileLocator.toFileURL(url);
258-
if (FILE_PROTOCOL.equalsIgnoreCase(locatedURL.getProtocol())) {
259-
String filePath = IPath.fromOSString(locatedURL.getPath()).toOSString();
260-
if (Files.exists(Path.of(filePath))) {
261-
return filePath;
262-
}
263-
}
264-
return null;
265-
} catch (IOException e) {
266-
if (logIOException) {
256+
return getFilePath(locatedURL);
257+
} catch (IOException | URISyntaxException e) {
258+
if (logException) {
267259
Policy.logException(e);
268260
} else if (InternalPolicy.DEBUG_LOG_URL_IMAGE_DESCRIPTOR_MISSING_2x) {
269261
String path = url.getPath();
@@ -275,6 +267,16 @@ private static String getFilePath(URL url, boolean logIOException) {
275267
}
276268
}
277269

270+
private static String getFilePath(URL url) throws URISyntaxException {
271+
if (FILE_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
272+
Path filePath = Path.of(url.toURI());
273+
if (Files.exists(filePath)) {
274+
return filePath.toString();
275+
}
276+
}
277+
return null;
278+
}
279+
278280
private static URL resolvePathVariables(URL url) {
279281
URL platformURL = FileLocator.find(url); // Resolve variables within URL's path
280282
if (platformURL != null) {

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)