[PIO-160] Fix JDBCUtils.stringToMap() Closes #463
Project: http://git-wip-us.apache.org/repos/asf/predictionio/repo Commit: http://git-wip-us.apache.org/repos/asf/predictionio/commit/18fa9b7a Tree: http://git-wip-us.apache.org/repos/asf/predictionio/tree/18fa9b7a Diff: http://git-wip-us.apache.org/repos/asf/predictionio/diff/18fa9b7a Branch: refs/heads/master Commit: 18fa9b7af1c968a60b5a78e532a4499726cccfca Parents: eac83c5 Author: Donald Szeto <dsz...@salesforce.com> Authored: Mon Sep 10 20:35:18 2018 +0900 Committer: Naoki Takezoe <take...@apache.org> Committed: Mon Sep 10 20:36:37 2018 +0900 ---------------------------------------------------------------------- .../data/storage/jdbc/JDBCUtils.scala | 12 +++-- .../data/storage/jdbc/JDBCUtilsSpec.scala | 50 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/predictionio/blob/18fa9b7a/storage/jdbc/src/main/scala/org/apache/predictionio/data/storage/jdbc/JDBCUtils.scala ---------------------------------------------------------------------- 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 @@ object JDBCUtils { * @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 */ http://git-wip-us.apache.org/repos/asf/predictionio/blob/18fa9b7a/storage/jdbc/src/test/scala/org/apache/predictionio/data/storage/jdbc/JDBCUtilsSpec.scala ---------------------------------------------------------------------- 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 + } +}