[PIO-160] Fix JDBCUtils.stringToMap()
Closes #463
diff --git a/storage/jdbc/src/main/scala/org/apache/predictionio/data/storage/jdbc/JDBCUtils.scala b/storage/jdbc/src/main/scala/org/apache/predictionio/data/storage/jdbc/JDBCUtils.scala
index 3eb55ba..98fa257 100644
--- a/storage/jdbc/src/main/scala/org/apache/predictionio/data/storage/jdbc/JDBCUtils.scala
+++ b/storage/jdbc/src/main/scala/org/apache/predictionio/data/storage/jdbc/JDBCUtils.scala
@@ -76,10 +76,14 @@
* @return Map of String to String, e.g. Map("FOO" -> "BAR", "X" -> "Y", ...)
*/
def stringToMap(str: String): Map[String, String] = {
- str.split(",").map { x =>
- val y = x.split("=")
- y(0) -> y(1)
- }.toMap[String, String]
+ if (str.isEmpty) {
+ Map.empty[String, String]
+ } else {
+ str.split(",").map { x =>
+ val y = x.split("=")
+ y(0) -> y(1)
+ }.toMap[String, String]
+ }
}
/** Generate 32-character random ID using UUID with - stripped */
diff --git a/storage/jdbc/src/test/scala/org/apache/predictionio/data/storage/jdbc/JDBCUtilsSpec.scala b/storage/jdbc/src/test/scala/org/apache/predictionio/data/storage/jdbc/JDBCUtilsSpec.scala
new file mode 100644
index 0000000..2282643
--- /dev/null
+++ b/storage/jdbc/src/test/scala/org/apache/predictionio/data/storage/jdbc/JDBCUtilsSpec.scala
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.predictionio.data.storage.jdbc
+
+import org.specs2.Specification
+
+class JDBCUtilsSpec extends Specification {
+ def is = s2"""
+
+ PredictionIO JDBC Utilities Specification
+
+ driverType should extract the correct portion from a JDBC URL ${driverType}
+ mapToString should return an empty string with empty map input ${mapToStringEmptyInput}
+ stringToMap should correctly create mapping ${stringToMap}
+ stringToMap should return an empty map with empty string input ${stringToMapEmptyInput}
+
+ """
+
+ def driverType = {
+ JDBCUtils.driverType("jdbc:postgresql://remotehost:5432/somedbname") must beEqualTo("postgresql")
+ }
+
+ def mapToStringEmptyInput = {
+ JDBCUtils.mapToString(Map.empty[String, String]) must be empty
+ }
+
+ def stringToMap = {
+ val m = JDBCUtils.stringToMap("FOO=BAR,DEAD=BEEF")
+ m must havePairs("FOO" -> "BAR", "DEAD" -> "BEEF")
+ }
+
+ def stringToMapEmptyInput = {
+ JDBCUtils.stringToMap("") must be empty
+ }
+}