Repository: knowm/XChart (java)

Issue:
Bug: BitmapEncoder not accepting uppercase format of extension
**Describe the bug**
File extensions with uppercase, eg."image.PNG", are not supported by addFileExtension() method  of BitmapEncoder class. 

**To Reproduce**

```
public class BitmapEncoderTest {

    @Test
    public void testAddFileExtension() {
        String fileName1 = "image";
        String fileName2 = "image.png";
        String fileName3 = "image.PNG";

        Assert.assertEquals(BitmapEncoder.addFileExtension(fileName1, BitmapEncoder.BitmapFormat.PNG), "image.png");
        Assert.assertEquals(BitmapEncoder.addFileExtension(fileName2, BitmapEncoder.BitmapFormat.PNG), "image.png");
        Assert.assertEquals(BitmapEncoder.addFileExtension(fileName3, BitmapEncoder.BitmapFormat.PNG), "image.png");
    }
}
```

**Screenshots**
![image](https://github.com/knowm/XChart/assets/42827734/e2d5d12a-0e62-4a3f-b1ed-e4950f5eee56)



**Expected behavior**
The method should accept both lowercase and uppercase formats of a file extension. Eg: "PNG" == "png"


Pull request description:
Fixing BitmapEncoder.addFileExtension handling of uppercase extensions
**What**:
Fixes #852 by ensuring lowercase file extension is enforced, even if the original file name provided had the extension.

Also by the chance, changing how the artificial edge case of `.png` or `.bmp` as the input file name is handled.

**Why**:
- To satisfy the #852 issue.
- It looks like it was the intent of the code in the first place.
 

Patch:
diff --git a/xchart/src/main/java/org/knowm/xchart/BitmapEncoder.java b/xchart/src/main/java/org/knowm/xchart/BitmapEncoder.java
index f100ac27..0e958b45 100644
--- a/xchart/src/main/java/org/knowm/xchart/BitmapEncoder.java
+++ b/xchart/src/main/java/org/knowm/xchart/BitmapEncoder.java
@@ -39,13 +39,16 @@ public final class 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;
   }


Test patch:
diff --git a/xchart/src/test/java/org/knowm/xchart/BitmapEncoderTest.java b/xchart/src/test/java/org/knowm/xchart/BitmapEncoderTest.java
new file mode 100644
index 00000000..68d74f21
--- /dev/null
+++ b/xchart/src/test/java/org/knowm/xchart/BitmapEncoderTest.java
@@ -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));
+    }
+}