From dcfc788dd8788be011bf0a8a54e6e78f26fe7b21 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Thu, 6 Jun 2019 19:24:07 +0200 Subject: [PATCH] when empty or null color is provided throw proper exception --- .../lcsb/mapviewer/common/geometry/ColorParser.java | 4 ++++ .../mapviewer/common/geometry/ColorParserTest.java | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/commons/src/main/java/lcsb/mapviewer/common/geometry/ColorParser.java b/commons/src/main/java/lcsb/mapviewer/common/geometry/ColorParser.java index 2af7761b62..d9bc3ab0bb 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/geometry/ColorParser.java +++ b/commons/src/main/java/lcsb/mapviewer/common/geometry/ColorParser.java @@ -47,6 +47,10 @@ public class ColorParser { * @return {@link Color} obtained from input text */ public Color parse(String string) { + if (string==null || string.isEmpty()) { + throw new InvalidArgumentException( + "Invalid color value: " + string + ". Correct format: #xxxxxx (where x is a hex value)"); + } if (string.charAt(0) != '#') { string = "#" + string; } diff --git a/commons/src/test/java/lcsb/mapviewer/common/geometry/ColorParserTest.java b/commons/src/test/java/lcsb/mapviewer/common/geometry/ColorParserTest.java index 710cfa1511..4b927108fb 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/geometry/ColorParserTest.java +++ b/commons/src/test/java/lcsb/mapviewer/common/geometry/ColorParserTest.java @@ -64,6 +64,18 @@ public class ColorParserTest { } } + @Test(expected=InvalidArgumentException.class) + public void testParseNull() throws Exception { + ColorParser parser = new ColorParser(); + parser.parse(null); + } + + @Test(expected=InvalidArgumentException.class) + public void testParseEmpty() throws Exception { + ColorParser parser = new ColorParser(); + parser.parse(""); + } + @Test public void testSetColorToHtmlString() throws Exception { try { -- GitLab