Skip to content
Permalink
Browse files

Add a test for DatabaseUrlDataSource

Conflicts:
	common-test-resources/application.conf
	slick-testkit/src/test/scala/slick/test/jdbc/DataSourceTest.scala
  • Loading branch information
szeiger committed Jul 28, 2015
1 parent d7a7d2e commit c277d64ceb4ec012d74c54dd024e5b012a7ea6f8
@@ -13,3 +13,15 @@ tsql {
keepAliveConnection = true
}
}

// HikariCP with DATABASE_URL parsing
databaseUrl {
dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
properties = {
driver = "slick.test.jdbc.MockDriver"
url = "postgres://user:pass@host/dbname"
properties = {
foo = bar
}
}
}
@@ -1,12 +1,49 @@
package slick.test.jdbc

import java.sql.{SQLException, DriverPropertyInfo, Connection, Driver}
import java.util.Properties
import java.util.logging.Logger

import org.junit.Test
import org.junit.Assert._
import slick.backend.DatabaseConfig
import slick.driver.JdbcProfile
import slick.jdbc.JdbcBackend

import scala.concurrent.Await
import scala.concurrent.duration.Duration

class DataSourceTest {
@Test def testDatabaseUrlDataSource: Unit = {
import slick.driver.H2Driver.api.actionBasedSQLInterpolation
MockDriver.reset
val db = JdbcBackend.Database.forConfig("databaseUrl")
try {
try Await.result(db.run(sqlu"dummy"), Duration.Inf) catch { case ex: SQLException => }
val (url, info) = MockDriver.getLast.getOrElse(fail("No connection data recorded").asInstanceOf[Nothing])
assertEquals("jdbc:postgresql://host/dbname", url)
assertEquals("user", info.getProperty("user"))
assertEquals("pass", info.getProperty("password"))
assertEquals("bar", info.getProperty("foo"))
} finally db.close
}
}

object MockDriver {
@volatile private var last: Option[(String, Properties)] = None
def getLast = last
def reset: Unit = last = None
}

class MockDriver extends Driver {
def acceptsURL(url: String): Boolean = true
def jdbcCompliant(): Boolean = false
def getPropertyInfo(url: String, info: Properties): Array[DriverPropertyInfo] = Array()
def getMinorVersion: Int = 0
def getParentLogger: Logger = throw new SQLException("feature not implemented")
def connect(url: String, info: Properties): Connection = {
MockDriver.last = Some((url, info))
throw new SQLException("Connection data has been recorded")
}
def getMajorVersion: Int = 0
}
@@ -2,9 +2,10 @@ package slick.util

import scala.concurrent.duration.Duration
import scala.language.implicitConversions
import com.typesafe.config.{Config, ConfigFactory}
import com.typesafe.config._
import java.util.concurrent.TimeUnit
import java.util.Properties
import scala.collection.mutable

/** Singleton object with Slick's configuration, loaded from the application config.
* This includes configuration for the global driver objects and settings for debug logging. */
@@ -43,13 +44,23 @@ class ConfigExtensionMethods(val c: Config) extends AnyVal {
def getDurationOr(path: String, default: => Duration = Duration.Zero) =
if(c.hasPath(path)) Duration(c.getDuration(path, TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS) else default

def getPropertiesOr(path: String, default: => Properties = null) =
if (!c.hasPath(path)) default
else {
def getPropertiesOr(path: String, default: => Properties = null): Properties =
if(c.hasPath(path)) new ConfigExtensionMethods(c.getConfig(path)).toProperties else default

def toProperties: Properties = {
def toProps(m: mutable.Map[String, ConfigValue]): Properties = {
val props = new Properties(null)
c.getObject(path).asScala.foreach { case (k, v) => props.put(k, v.unwrapped.toString) }
m.foreach { case (k, cv) =>
val v =
if(cv.valueType() == ConfigValueType.OBJECT) toProps(cv.asInstanceOf[ConfigObject].asScala)
else if(cv.unwrapped eq null) null
else cv.unwrapped.toString
if(v ne null) props.put(k, v)
}
props
}
toProps(c.root.asScala)
}

def getBooleanOpt(path: String): Option[Boolean] = if(c.hasPath(path)) Some(c.getBoolean(path)) else None
def getIntOpt(path: String): Option[Int] = if(c.hasPath(path)) Some(c.getInt(path)) else None

0 comments on commit c277d64

Please sign in to comment.
You can’t perform that action at this time.