From 4a50d17d0aa1358d31f0c31e636689c8cc420db0 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Wed, 9 Aug 2023 15:34:30 +0200 Subject: [PATCH] Fixes setExtension of TipImage.java in org.eclipse.tips.tests #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 #525. --- .../src/org/eclipse/tips/core/TipImage.java | 17 ++++++++--------- .../eclipse/tips/core/TipImageBas64Test.java | 5 ++--- .../org/eclipse/tips/core/TipImageURLTest.java | 9 ++++----- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/ua/org.eclipse.tips.core/src/org/eclipse/tips/core/TipImage.java b/ua/org.eclipse.tips.core/src/org/eclipse/tips/core/TipImage.java index 7e3dceb5610..a90bb55786d 100644 --- a/ua/org.eclipse.tips.core/src/org/eclipse/tips/core/TipImage.java +++ b/ua/org.eclipse.tips.core/src/org/eclipse/tips/core/TipImage.java @@ -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 @@ -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 @@ -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)); @@ -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; } diff --git a/ua/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageBas64Test.java b/ua/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageBas64Test.java index c19e566d0bc..f013c5a061e 100644 --- a/ua/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageBas64Test.java +++ b/ua/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageBas64Test.java @@ -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 diff --git a/ua/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageURLTest.java b/ua/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageURLTest.java index 3988683086b..9764d843af9 100644 --- a/ua/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageURLTest.java +++ b/ua/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageURLTest.java @@ -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")); } }