Skip to content

Instantly share code, notes, and snippets.

@b0c1
Last active December 17, 2015 06:19
Show Gist options
  • Save b0c1/5564972 to your computer and use it in GitHub Desktop.
Save b0c1/5564972 to your computer and use it in GitHub Desktop.
Orient DB 1.3 vs scala 2.10
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
import scala.beans.BeanProperty
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) = {
try {
database.begin(TXTYPE.OPTIMISTIC)
val result = f
database.commit
result
} catch {
case e: Throwable =>
database.rollback
throw e
}
}
}
class Test {
@BeanProperty
@Id var id: String = _
@Version var version: String = _
@BeanProperty 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
}
}
after {
OrientDB.database {
implicit db =>
val results = db.queryBySql[Test]("select * from Test")
for (r <- results) {
db.delete(r)
}
}
}
//First test not work, because name is null
test("Orient property naming test 1") {
OrientDB.database {
implicit db =>
OrientDB.transactional {
val test = new Test
test.name = "Boo"
val result = db.attachAndSave[Test](test)
assert(result.name === "Boo")
assert(result.getName === "Boo")
}
}
}
//Second test is work, because the getter fill the name variable
test("Orient property naming test 2") {
OrientDB.database {
implicit db =>
OrientDB.transactional {
val test = new Test
test.name = "Boo"
val result = db.attachAndSave[Test](test)
assert(result.getName === "Boo") //Standard java getter work correctly
assert(result.name === "Boo") //Standard scala getter now work...
}
}
}
//Test fail because name property not set by getter
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")
}
}
}
@tyoc213
Copy link

tyoc213 commented Jan 24, 2014

For reference as starting 2014, the latest orientdb release is 1.6.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment