Skip to content

Commit

Permalink
Fixes TipImageTests in org.eclipse.tips.tests eclipse-platform#525
Browse files Browse the repository at this point in the history
In this commit the two Tests setExtension and setExtension2 are reactivated and fixed. For this the class TipImage and especially its method setExtension have been changed. Now if setExtension is called, not only the instance variable fExtension is updated but also the instance variables fBase64Image or, if the URL constructor was used, fURL. These two instance variable are no final variables anymore, so they can be changed and tested. Contributes to eclipse-platform#525.
  • Loading branch information
Michael5601 committed Sep 25, 2023
1 parent efe0dce commit 3ad4430
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
34 changes: 25 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 @@ -15,6 +15,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;

Expand All @@ -39,10 +40,10 @@ public class TipImage {
private String fExtension = null;
private int fMaxWidth = UNDEFINED;
private int fMaxHeight = UNDEFINED;
private final URL fURL;
private 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 @@ -82,7 +83,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 @@ -98,7 +98,6 @@ public TipImage(String base64Image) {
int from = base64Image.indexOf('/') + 1;
int to = base64Image.indexOf(';');
setExtension(base64Image.substring(from, to).trim());
setExtension(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 @@ -205,15 +204,28 @@ public TipImage setAspectRatio(double aspectRatio) {

/**
* Changes the default value "null" to the passed value which commonly is "png",
* "gif" and such.
* "gif" and updates the URL if the URL constructor was used or updates the
* Base64Image if the base64 constructor was used.
*
* @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) {
if (isURLSet()) {
String oldExtension = getExtension();
try {
fURL = new URL(fURL.toString().replace(oldExtension, newExtension));
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else {
int from = fBase64Image.indexOf('/') + 1;
int to = fBase64Image.indexOf(';');
String oldExtension = fBase64Image.substring(from, to).trim();
fBase64Image = fBase64Image.replace(oldExtension, newExtension);
}
fExtension = newExtension;
return this;
}

Expand Down Expand Up @@ -292,4 +304,8 @@ private String getExtension() {
}
return fExtension;
}

public URL getURL() {
return fURL;
}
}
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().setExtension("png").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().setExtension("png").getURL().toString().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").getURL().toString().contains("bmp"));
}
}

0 comments on commit 3ad4430

Please sign in to comment.