/
TestDB.scala
211 lines (190 loc) · 8.32 KB
/
TestDB.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.typesafe.slick.testkit.util
import java.util.Properties
import scala.slick.jdbc.{StaticQuery => Q, ResultSetInvoker}
import scala.slick.jdbc.GetResult._
import java.util.zip.GZIPInputStream
import java.io._
import org.junit.Assert
import scala.slick.driver._
import java.net.{URL, URLClassLoader}
import java.sql.Driver
import scala.collection.mutable
import scala.slick.profile.Capability
object TestDB {
type TestDBSpec = (String => TestDB)
/** Marks a driver which is specially supported by the test kit for plain SQL queries */
val plainSql = new Capability("test.plainSql")
/** Marks a driver which is specially supported by the test kit for plain SQL wide result set queries */
val plainSqlWide = new Capability("test.plainSqlWide")
val testDBDir = dbProps.getProperty("testDir", "test-dbs")
def testDBPath = {
val f = new File(testDBDir)
val s = f.getPath().replace('\\', '/')
if(f.isAbsolute) s else "./" + s
}
private lazy val dbProps = {
val p = new Properties
val f = new File(sys.props.getOrElse("slick.testkit.dbprops", "test-dbs/databases.properties"))
if(f.isFile) {
val in = new FileInputStream(f)
try { p.load(in) } finally { in.close() }
}
p
}
private lazy val testDBs = Option(dbProps.getProperty("testDBs")).map(_.split(',').map(_.trim).toSet)
def isInternalEnabled(db: String) = testDBs.map(_.contains(db)).getOrElse(true)
def isExternalEnabled(db: String) = isInternalEnabled(db) && "true" == dbProps.getProperty(db+".enabled")
def get(db: String, o: String) = Option(dbProps.getProperty(db+"."+o))
def getMulti(db: String, key: String): Seq[String] = get(db, key) match {
case Some(s) => Seq(s)
case None =>
Iterator.from(1).map(i => get(db, key+"."+i)).takeWhile(_.isDefined).toSeq.flatten
}
/** Copy a file, expanding it if the source name ends with .gz */
def copy(src: File, dest: File) {
dest.createNewFile()
val out = new FileOutputStream(dest)
try {
var in: InputStream = new FileInputStream(src)
try {
if(src.getName.endsWith(".gz")) in = new GZIPInputStream(in)
val buf = new Array[Byte](4096)
var cont = true
while(cont) {
val len = in.read(buf)
if(len < 0) cont = false
else out.write(buf, 0, len)
}
} finally in.close()
} finally out.close()
}
/** Delete files in the testDB directory */
def deleteDBFiles(prefix: String) {
assert(!prefix.isEmpty, "prefix must not be empty")
def deleteRec(f: File): Boolean = {
if(f.isDirectory()) f.listFiles.forall(deleteRec _) && f.delete()
else f.delete()
}
val dir = new File(TestDB.testDBDir)
if(!dir.isDirectory) throw new IOException("Directory "+TestDB.testDBDir+" not found")
for(f <- dir.listFiles if f.getName startsWith prefix) {
val p = TestDB.testDBDir+"/"+f.getName
if(deleteRec(f)) println("[Deleted database file "+p+"]")
else throw new IOException("Couldn't delete database file "+p)
}
}
def mapToProps(m: Map[String, String]) = {
val p = new Properties
if(m ne null)
for((k,v) <- m) if(k.ne(null) && v.ne(null)) p.setProperty(k, v)
p
}
}
/**
* Describes a database against which you can run TestKit tests. It includes
* features such as reading the configuration file, setting up a DB connection,
* removing DB files left over by a test run, etc.
*/
abstract class TestDB(final val confName: String, final val driver: JdbcDriver) {
final val profile: JdbcProfile = driver
protected val Database = profile.backend.Database
override def toString = url
val url: String
val jdbcDriver: String
def createDB() = Database.forURL(url, driver = jdbcDriver)
def cleanUpBefore() {}
def cleanUpAfter() = cleanUpBefore()
def isEnabled = TestDB.isInternalEnabled(confName)
def isPersistent = true
def isShared = true
def getLocalTables(implicit session: profile.Backend#Session) = {
val tables = ResultSetInvoker[(String,String,String, String)](_.conn.getMetaData().getTables("", "", null, null))
tables.list.filter(_._4.toUpperCase == "TABLE").map(_._3).sorted
}
def getLocalSequences(implicit session: profile.Backend#Session) = {
val tables = ResultSetInvoker[(String,String,String, String)](_.conn.getMetaData().getTables("", "", null, null))
tables.list.filter(_._4.toUpperCase == "SEQUENCE").map(_._3).sorted
}
def dropUserArtifacts(implicit session: profile.Backend#Session) = {
for(t <- getLocalTables)
(Q.u+"drop table if exists "+driver.quoteIdentifier(t)+" cascade").execute()
for(t <- getLocalSequences)
(Q.u+"drop sequence if exists "+driver.quoteIdentifier(t)+" cascade").execute()
}
def assertTablesExist(tables: String*)(implicit session: profile.Backend#Session) {
for(t <- tables) {
try ((Q[Int]+"select 1 from "+driver.quoteIdentifier(t)+" where 1 < 0").list) catch { case _: Exception =>
Assert.fail("Table "+t+" should exist")
}
}
}
def assertNotTablesExist(tables: String*)(implicit session: profile.Backend#Session) {
for(t <- tables) {
try {
(Q[Int]+"select 1 from "+driver.quoteIdentifier(t)+" where 1 < 0").list
Assert.fail("Table "+t+" should not exist")
} catch { case _: Exception => }
}
}
def canGetLocalTables = true
lazy val capabilities = driver.capabilities
}
class ExternalTestDB(confName: String, driver: JdbcDriver) extends TestDB(confName, driver) {
val jdbcDriver = TestDB.get(confName, "driver").orNull
val urlTemplate = TestDB.get(confName, "url").getOrElse("")
val dbPath = new File(TestDB.testDBDir).getAbsolutePath
val dbName = TestDB.get(confName, "testDB").getOrElse("").replace("[DBPATH]", dbPath)
val password = TestDB.get(confName, "password").orNull
val user = TestDB.get(confName, "user").orNull
val adminUser = TestDB.get(confName, "adminUser").getOrElse(user)
val adminPassword = TestDB.get(confName, "adminPassword").getOrElse(password)
lazy val url = replaceVars(urlTemplate)
lazy val adminDB = TestDB.get(confName, "adminDB").getOrElse("").replace("[DBPATH]", dbPath)
lazy val adminDBURL = replaceVars(urlTemplate.replace("[DB]", adminDB))
lazy val create = TestDB.getMulti(confName, "create").map(replaceVars)
lazy val postCreate = TestDB.getMulti(confName, "postCreate").map(replaceVars)
lazy val drop = TestDB.getMulti(confName, "drop").map(replaceVars)
def replaceVars(s: String): String =
s.replace("[DB]", dbName).replace("[DBPATH]", dbPath).
replace("[USER]", user).replace("[PASSWORD]", password)
override def isEnabled = TestDB.isExternalEnabled(confName)
def databaseFor(url: String, user: String, password: String, prop: Map[String, String] = null) = loadCustomDriver() match {
case Some(dr) => Database.forDriver(dr, url, user = user, password = password, prop = TestDB.mapToProps(prop))
case None => Database.forURL(url, user = user, password = password, driver = jdbcDriver, prop = TestDB.mapToProps(prop))
}
override def createDB() = databaseFor(url, user, password)
override def cleanUpBefore() {
if(!drop.isEmpty || !create.isEmpty) {
println("[Creating test database "+this+"]")
databaseFor(adminDBURL, adminUser, adminPassword) withSession { implicit session: profile.Backend#Session =>
for(s <- drop) (Q.u + s).execute
for(s <- create) (Q.u + s).execute
}
}
if(!postCreate.isEmpty) {
createDB() withSession { implicit session: profile.Backend#Session =>
for(s <- postCreate) (Q.u + s).execute
}
}
}
override def cleanUpAfter() {
if(!drop.isEmpty) {
println("[Dropping test database "+this+"]")
databaseFor(adminDBURL, adminUser, adminPassword) withSession { implicit session: profile.Backend#Session =>
for(s <- drop) (Q.u + s).execute
}
}
}
def loadCustomDriver() = TestDB.get(confName, "driverJar").map { jar =>
ExternalTestDB.getCustomDriver(jar, jdbcDriver)
}
}
object ExternalTestDB {
// A cache for custom drivers to avoid excessive reloading and memory leaks
private[this] val driverCache = new mutable.HashMap[(String, String), Driver]()
def getCustomDriver(url: String, driverClass: String): Driver = synchronized {
driverCache.getOrElseUpdate((url, driverClass),
new URLClassLoader(Array(new URL(url)), getClass.getClassLoader).loadClass(driverClass).newInstance.asInstanceOf[Driver]
)
}
}