Skip to content

Commit

Permalink
Fixes setExtension of TipImage.java in org.eclipse.tips.tests eclipse…
Browse files Browse the repository at this point in the history
…-platform#525

In this commit the method setExtension of TipImage.java is corrected to properly change the extension. Now if setExtension is called, not only the extension is updated but also the base64image because the extension is a part of this image. Mind that for this change the field fBase64Image can't be final because it needs to be updated when the extension changes. setExtension is not used in the constructor anymore to set the fExtension field, rather it is directly set. Also the tests setExtension and setExtension2 are reactivated and updated to test the functionality of setExtension. Contributes to eclipse-platform#525.
  • Loading branch information
Michael5601 authored and HeikoKlare committed Nov 30, 2023
1 parent 28d9767 commit 4a50d17
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
17 changes: 8 additions & 9 deletions ua/org.eclipse.tips.core/src/org/eclipse/tips/core/TipImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class TipImage {
private final URL fURL;
private double fAspectRatio = THREE_TO_TWO;

private final String fBase64Image;
private String fBase64Image;

/**
* Creates a new TipImage with the specified URL which gets read into a base 64
Expand Down Expand Up @@ -80,7 +80,6 @@ public boolean isURLSet() {
*
* @param base64Image
* the non-null base64 encoded image according to RFC-2397.
*
* @throws RuntimeException
* if the string is not valid
* @see TipImage
Expand All @@ -94,8 +93,7 @@ public TipImage(String base64Image) {
fBase64Image = base64Image;
int from = base64Image.indexOf('/') + 1;
int to = base64Image.indexOf(';');
setExtension(base64Image.substring(from, to).trim());
setExtension(base64Image.substring(from, to).trim());
fExtension = base64Image.substring(from, to).trim();
} else {
int length = base64Image.length();
throw new RuntimeException(Messages.TipImage_5 + base64Image.substring(0, length < 50 ? length : 50));
Expand Down Expand Up @@ -202,15 +200,16 @@ public TipImage setAspectRatio(double aspectRatio) {

/**
* Changes the default value "null" to the passed value which commonly is "png",
* "gif" and such.
* "gif" and such. It also updates the Base64Image to properly include the new
* extension.
*
* @param extension
* the extension of this file
* @param newExtension the new extension of this file
* @return this
* @see #getExtension()
*/
public TipImage setExtension(String extension) {
fExtension = extension;
public TipImage setExtension(String newExtension) {
fBase64Image = fBase64Image.replaceAll("/.*;", "/" + newExtension + ";"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
fExtension = newExtension;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ public void testAssertWidth() {

@Test
public void testSetExtension() {
// assertTrue(getTipImage().getIMGAttributes(19, 10).contains("png"));
assertTrue(getTipImage().getBase64Image().contains("png"));
}

@Test
public void testSetExtension2() {
// assertTrue(getTipImage().setExtension("bmp").getBase64Image().contains("bmp"));
// assertTrue(getTipImage().getIMGAttributes(19, 10).contains("png"));
assertTrue(getTipImage().setExtension("bmp").getBase64Image().contains("bmp"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ public void testGetIMGAttributes() throws IOException {
}

@Test
public void testSetExtension() {
// assertTrue(getTipImage().getIMGAttributes(19, 10).contains("png"));
public void testSetExtension() throws IOException {
assertTrue(getTipImage().getBase64Image().contains("png"));
}

@Test
public void testSetExtension2() {
// assertTrue(getTipImage().setExtension("bmp").getBase64Image().contains("bmp"));
// assertTrue(getTipImage().getIMGAttributes(19, 10).contains("png"));
public void testSetExtension2() throws IOException {
assertTrue(getTipImage().setExtension("bmp").getBase64Image().contains("bmp"));
}
}

0 comments on commit 4a50d17

Please sign in to comment.