From 3c0e8022a97c155e7da56ad3fb5e58b40ff60b08 Mon Sep 17 00:00:00 2001 From: Freddy Tuxworth Date: Wed, 23 Jan 2019 18:15:15 +0000 Subject: [PATCH] Added tests for common utilities CollectionUtil & HtmlUtils. These tests were written using Diffblue Cover. --- .../common/util/CollectionUtilTest.java | 43 +++++++++++++++++++ .../keycloak/common/util/HtmlUtilsTest.java | 17 ++++++++ 2 files changed, 60 insertions(+) create mode 100644 common/src/test/java/org/keycloak/common/util/CollectionUtilTest.java create mode 100644 common/src/test/java/org/keycloak/common/util/HtmlUtilsTest.java diff --git a/common/src/test/java/org/keycloak/common/util/CollectionUtilTest.java b/common/src/test/java/org/keycloak/common/util/CollectionUtilTest.java new file mode 100644 index 0000000000..4b650fb238 --- /dev/null +++ b/common/src/test/java/org/keycloak/common/util/CollectionUtilTest.java @@ -0,0 +1,43 @@ +package org.keycloak.common.util; + +import org.junit.Assert; +import org.junit.Test; +import org.keycloak.common.util.CollectionUtil; + +import java.util.ArrayList; + +public class CollectionUtilTest { + + @Test + public void joinInputNoneOutputEmpty() { + final ArrayList strings = new ArrayList(); + final String retval = CollectionUtil.join(strings, ","); + Assert.assertEquals("", retval); + } + + @Test + public void joinInput2SeparatorNull() { + final ArrayList strings = new ArrayList(); + strings.add("foo"); + strings.add("bar"); + final String retval = CollectionUtil.join(strings, null); + Assert.assertEquals("foonullbar", retval); + } + + @Test + public void joinInput1SeparatorNotNull() { + final ArrayList strings = new ArrayList(); + strings.add("foo"); + final String retval = CollectionUtil.join(strings, ","); + Assert.assertEquals("foo", retval); + } + + @Test + public void joinInput2SeparatorNotNull() { + final ArrayList strings = new ArrayList(); + strings.add("foo"); + strings.add("bar"); + final String retval = CollectionUtil.join(strings, ","); + Assert.assertEquals("foo,bar", retval); + } +} diff --git a/common/src/test/java/org/keycloak/common/util/HtmlUtilsTest.java b/common/src/test/java/org/keycloak/common/util/HtmlUtilsTest.java new file mode 100644 index 0000000000..b2c6c5a4fc --- /dev/null +++ b/common/src/test/java/org/keycloak/common/util/HtmlUtilsTest.java @@ -0,0 +1,17 @@ +package org.keycloak.common.util; + +import org.junit.Assert; +import org.junit.Test; +import org.keycloak.common.util.HtmlUtils; + +public class HtmlUtilsTest { + + @Test + public void escapeAttribute() { + Assert.assertEquals(HtmlUtils.escapeAttribute("1<2"), "1<2"); + Assert.assertEquals(HtmlUtils.escapeAttribute("2<3&&3>2"), "2<3&&3>2"); + Assert.assertEquals(HtmlUtils.escapeAttribute("test"), "test"); + Assert.assertEquals(HtmlUtils.escapeAttribute("\'test\'"), "'test'"); + Assert.assertEquals(HtmlUtils.escapeAttribute("\"test\""), ""test""); + } +}