Created
May 9, 2013 22:26
-
-
Save b0c1/5551084 to your computer and use it in GitHub Desktop.
OrientDB vs Scala 2.10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hu.javaportal.test | |
import org.junit.runner.RunWith | |
import org.scalatest.junit.JUnitRunner | |
import org.scalatest.{BeforeAndAfter, FunSuite} | |
import org.scalatest.mock.MockitoSugar | |
import com.orientechnologies.orient.`object`.db.{OObjectDatabasePool, OObjectDatabaseTx} | |
import com.orientechnologies.orient.core.tx.OTransaction.TXTYPE | |
import javax.persistence.{Version, Id} | |
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery | |
object OrientDB { | |
def database(f: OObjectDatabaseTx => Any) { | |
val db = new OObjectDatabaseTx("memory:test") | |
if (!db.exists()) db.create() | |
implicit val database: OObjectDatabaseTx = OObjectDatabasePool.global().acquire("memory:test", "admin", "admin") | |
database.getEntityManager.registerEntityClasses("hu.javaportal.test") | |
try { | |
f(database) | |
} finally { | |
database.close | |
} | |
} | |
def transactional(f: => Any)(implicit database: OObjectDatabaseTx): Any = { | |
try { | |
database.begin(TXTYPE.OPTIMISTIC) | |
f | |
database.commit | |
} catch { | |
case e: Throwable => | |
database.rollback | |
throw e | |
} | |
} | |
} | |
class Test { | |
@Id var id: String = _ | |
@Version var version: String = _ | |
var name: String = _ | |
} | |
@RunWith(classOf[JUnitRunner]) | |
class OrientDbTest extends FunSuite with MockitoSugar with BeforeAndAfter { | |
import scala.collection.JavaConverters._ | |
implicit def dbWrapper(db: OObjectDatabaseTx) = new { | |
def queryBySql[T](sql: String, params: AnyRef*): List[T] = { | |
val params4java = params.toArray | |
val results: java.util.List[T] = db.query(new OSQLSynchQuery[T](sql), params4java: _*) | |
results.asScala.toList | |
} | |
} | |
test("Simple save test") { | |
OrientDB.database { | |
implicit db => | |
OrientDB.transactional { | |
val test = new Test | |
test.name = "Boo" | |
db.save(test) | |
} | |
} | |
} | |
test("Simple load test test") { | |
OrientDB.database { | |
implicit db => | |
val test = new Test | |
test.name = "Boo" | |
db.save(test) | |
val results = db.queryBySql[Test]("select * from Test") | |
assert(results.length === 1) | |
assert(results.head.name === "Boo") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment