Skip to content

Commit

Permalink
Merge pull request #858 from knowm/testAddFileExtension-fix-uppercase
Browse files Browse the repository at this point in the history
Fixing BitmapEncoder.addFileExtension handling of uppercase extensions
  • Loading branch information
mccartney authored Jun 5, 2024
2 parents 6766f1a + 89ba686 commit a9f75d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 6 additions & 3 deletions xchart/src/main/java/org/knowm/xchart/BitmapEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ private BitmapEncoder() {}
*/
public static String addFileExtension(String fileName, BitmapFormat bitmapFormat) {

String fileNameWithFileExtension = fileName;
final String newFileExtension = "." + bitmapFormat.toString().toLowerCase();
if (fileName.length() <= newFileExtension.length()
final String fileNameWithFileExtension;
if (fileName.length() < newFileExtension.length()
|| !fileName
.substring(fileName.length() - newFileExtension.length(), fileName.length())
.substring(fileName.length() - newFileExtension.length())
.equalsIgnoreCase(newFileExtension)) {
fileNameWithFileExtension = fileName + newFileExtension;
} else {
// This is to ensure the lower-case for the extension
fileNameWithFileExtension = fileName.substring(0, fileName.length() - newFileExtension.length()) + newFileExtension;
}
return fileNameWithFileExtension;
}
Expand Down
26 changes: 26 additions & 0 deletions xchart/src/test/java/org/knowm/xchart/BitmapEncoderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.knowm.xchart;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;


public class BitmapEncoderTest {

@Test
public void testAddFileExtension() {
String fileName1 = "image";
String fileName2 = "image.png";
String fileName3 = "image.PNG";

for (String s : Arrays.asList(fileName1, fileName2, fileName3)) {
assertEquals("image.png", BitmapEncoder.addFileExtension(s, BitmapEncoder.BitmapFormat.PNG));
}
assertEquals("z.bmp", BitmapEncoder.addFileExtension("z", BitmapEncoder.BitmapFormat.BMP));
assertEquals("asdf.bmp", BitmapEncoder.addFileExtension("asdf", BitmapEncoder.BitmapFormat.BMP));
assertEquals(".bmp", BitmapEncoder.addFileExtension(".bmp", BitmapEncoder.BitmapFormat.BMP));
assertEquals(".bmp", BitmapEncoder.addFileExtension(".BmP", BitmapEncoder.BitmapFormat.BMP));
}
}

0 comments on commit a9f75d4

Please sign in to comment.