Use a separate test db
authorChristos KK Loverdos <loverdos@gmail.com>
Thu, 10 Nov 2011 10:55:53 +0000 (12:55 +0200)
committerChristos KK Loverdos <loverdos@gmail.com>
Thu, 10 Nov 2011 10:55:53 +0000 (12:55 +0200)
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSL.scala
logic/src/test/scala/gr/grnet/aquarium/logic/test/AccountsTest.scala
logic/src/test/scala/gr/grnet/aquarium/logic/test/BillingTest.scala
logic/src/test/scala/gr/grnet/aquarium/logic/test/FixtureLoader.scala
logic/src/test/scala/gr/grnet/aquarium/logic/test/TestDB.scala [new file with mode: 0644]
model/src/test/scala/gr/grnet/aquarium/model/test/BasicModelTest.scala
model/src/test/scala/gr/grnet/aquarium/model/test/TestDB.scala [new file with mode: 0644]
shared/src/main/resources/META-INF/persistence.xml

index a2216d5..18fb61f 100644 (file)
@@ -478,8 +478,8 @@ object DSL extends Loggable {
 }
 
 sealed abstract class DSLTreeNode {
-  abstract def toYaml() : String
-  abstract def children() : List[DSLTreeNode]
+  def toYaml() : String
+  def children() : List[DSLTreeNode]
 }
 
 case class DSLCreditPolicy (
index 330c618..de7410b 100644 (file)
 
 package gr.grnet.aquarium.logic.test
 
-import gr.grnet.aquarium.model.DB
-import gr.grnet.aquarium.logic.test._
 import org.junit.{After, Before, Test}
 
-
 class AccountsTest extends FixtureLoader {
-
   @Before
   def before() = {
-    if (!DB.getTransaction.isActive)
-      DB.getTransaction.begin
+    if (!TestDB.getTransaction.isActive)
+      TestDB.getTransaction.begin
     //loadFixture("data.json")
   }
 
@@ -55,6 +51,6 @@ class AccountsTest extends FixtureLoader {
 
   @After
   def after() = {
-    DB.getTransaction.rollback()
+    TestDB.getTransaction.rollback()
   }
 }
\ No newline at end of file
index 34a6b3c..e4db294 100644 (file)
@@ -38,7 +38,7 @@ package gr.grnet.aquarium.logic.test
 import org.junit._
 import org.junit.Assert._
 import gr.grnet.aquarium.logic.Bills
-import gr.grnet.aquarium.model.{User, DB}
+import gr.grnet.aquarium.model.{User}
 import gr.grnet.aquarium.logic.accounting.policies.DefaultRatePolicy
 import gr.grnet.aquarium.logic.accounting.{Agreement, AccountingEvent, AccountingEntryType, AccountingEventType}
 import gr.grnet.aquarium.logic.accounting.agreements.AgreementRegistry
@@ -49,8 +49,8 @@ class BillingTest
 
   @Before
   def before() = {
-    if (!DB.getTransaction.isActive)
-      DB.getTransaction.begin
+    if (!TestDB.getTransaction.isActive)
+      TestDB.getTransaction.begin
     //loadFixture("data.json")
   }
 
@@ -82,7 +82,7 @@ class BillingTest
     u.agreement = TestAgreement.id
     u.credits = 100
 
-    DB.persistAndFlush(u)
+    TestDB.persistAndFlush(u)
 
     // Try with a basic event
     /*var evt = new AccountingEvent(AccountingEventType.VMTime,
@@ -99,6 +99,6 @@ class BillingTest
 
   @After
   def after() = {
-    DB.getTransaction.rollback
+    TestDB.getTransaction.rollback
   }
 }
\ No newline at end of file
index 6cef9a1..bf1b629 100644 (file)
@@ -139,7 +139,7 @@ trait FixtureLoader {
     //Create an object instance
     val tmpObj = loadClass[AnyRef](model).newInstance()
     //Save the object
-    DB.persistAndFlush(tmpObj)
+    TestDB.persistAndFlush(tmpObj)
 
     /* Special treatment for the ID field: we allow JPA to set on persist, but
      * we reset it here to what the fixture specifies. This is to bypass
@@ -147,7 +147,7 @@ trait FixtureLoader {
      */
     updatePK(tmpObj.getClass.getSimpleName, tmpObj.getV("id").asInstanceOf[Long], id)
 
-    val obj = DB.find(tmpObj.getClass, id).getOrElse(
+    val obj = TestDB.find(tmpObj.getClass, id).getOrElse(
       throw new Exception("Cannot find")
     ).asInstanceOf[AnyRef]
 
@@ -191,11 +191,11 @@ trait FixtureLoader {
         }
     }
 
-    DB.flush()
+    TestDB.flush()
   }
 
   private def updatePK(entity: String, oldid: Long, newid: Long) = {
-    val q = DB.createQuery(
+    val q = TestDB.createQuery(
       "update " + entity +
         " set id=:newid where id = :oldid")
     q.setParameter("newid", newid)
@@ -205,7 +205,7 @@ trait FixtureLoader {
 
   /** Set the referenced object in a many to one relationship*/
   private def setManyToOne(dao: AnyRef, fieldName: String, fieldValue: Double) = {
-    val other = DB.find(dao.getT(fieldName), fieldValue.longValue()).getOrElse(
+    val other = TestDB.find(dao.getT(fieldName), fieldValue.longValue()).getOrElse(
       throw new Exception("Cannot find related object for " + dao.getClass +
         ", field: " + fieldName + " value: " + fieldValue)
     )
@@ -246,7 +246,7 @@ trait FixtureLoader {
     //Add all values specified
     fieldValues.foreach {
       v =>
-        val other = DB.find(targetType, v.longValue).getOrElse(
+        val other = TestDB.find(targetType, v.longValue).getOrElse(
           throw new Exception("Cannot find entry of type "+ targetType +
             " with id=" + v.longValue)
         )
diff --git a/logic/src/test/scala/gr/grnet/aquarium/logic/test/TestDB.scala b/logic/src/test/scala/gr/grnet/aquarium/logic/test/TestDB.scala
new file mode 100644 (file)
index 0000000..68d52a4
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+package gr.grnet.aquarium.logic.test
+
+import org.scala_libs.jpa.{ThreadLocalEM, LocalEMF}
+
+/**
+ * 
+ * @author Christos KK Loverdos <loverdos@gmail.com>.
+ */
+object TestDB extends LocalEMF("aquarium-test", true) with ThreadLocalEM {}
\ No newline at end of file
index 9974a6f..4c0a93a 100644 (file)
@@ -1,8 +1,6 @@
 package gr.grnet.aquarium.model.test
 
 import gr.grnet.aquarium.model._
-import scala.collection.JavaConversions._
-import org.scala_libs.jpa.{LocalEMF, ThreadLocalEM}
 import org.junit._
 import Assert._
 
@@ -10,8 +8,8 @@ class BasicModelTest {
 
   @Before
   def before() = {
-    if (!DB.getTransaction.isActive)
-      DB.getTransaction.begin
+    if (!TestDB.getTransaction.isActive)
+      TestDB.getTransaction.begin
   }
 
   @Test
@@ -19,10 +17,10 @@ class BasicModelTest {
 
     val user = new User
     user.name = "foobar"
-    DB.persist(user)
-    DB.flush()
+    TestDB.persist(user)
+    TestDB.flush()
 
-    val a = DB.find(classOf[User], user.id)
+    val a = TestDB.find(classOf[User], user.id)
     assert(a.exists(u => u.id == user.id))
   }
 
@@ -33,29 +31,29 @@ class BasicModelTest {
     val org1 = new Organization
     org1.name = "EDET"
 
-    DB.persist(org1)
+    TestDB.persist(org1)
 
     val org2 = new Organization
     org2.name = "AUEB"
     org2.parent = org1
 
-    DB.persist(org2)
-    DB.flush()
+    TestDB.persist(org2)
+    TestDB.flush()
 
     assertTrue(org1.id != org2.id)
 
-    val results = DB.find(classOf[Organization], org1.id)
+    val results = TestDB.find(classOf[Organization], org1.id)
     assert(results.exists(o => o.id == org1.id))
 
     //Add to entity with composite key
     val srv1 = new ServiceItem
     srv1.url = "http://foo.bar/"
-    DB.persist(srv1)
+    TestDB.persist(srv1)
 
   }
 
   @After
   def after() = {
-    DB.getTransaction.rollback()
+    TestDB.getTransaction.rollback()
   }
 }
diff --git a/model/src/test/scala/gr/grnet/aquarium/model/test/TestDB.scala b/model/src/test/scala/gr/grnet/aquarium/model/test/TestDB.scala
new file mode 100644 (file)
index 0000000..d283853
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+package gr.grnet.aquarium.model.test
+
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+import org.scala_libs.jpa.{ThreadLocalEM, LocalEMF}
+
+/**
+ * 
+ * @author Christos KK Loverdos <loverdos@gmail.com>.
+ */
+object TestDB extends LocalEMF("aquarium-test", true) with ThreadLocalEM {}
\ No newline at end of file
index a3955fd..7ca01fc 100644 (file)
     </properties>
   </persistence-unit>
 
+  <persistence-unit name="aquarium-test" transaction-type="RESOURCE_LOCAL">
+
+    <class>gr.grnet.aquarium.model.Entity</class>
+    <class>gr.grnet.aquarium.model.Group</class>
+    <class>gr.grnet.aquarium.model.User</class>
+    <class>gr.grnet.aquarium.model.Organization</class>
+    <class>gr.grnet.aquarium.model.ServiceItem</class>
+    <class>gr.grnet.aquarium.model.RuntimeData</class>
+    <class>gr.grnet.aquarium.model.Bill</class>
+    <class>gr.grnet.aquarium.model.Action</class>
+    <class>gr.grnet.aquarium.model.DefaultPermission</class>
+    <class>gr.grnet.aquarium.model.Permission</class>
+    <class>gr.grnet.aquarium.model.ServiceTemplate</class>
+    <class>gr.grnet.aquarium.model.ResourceType</class>
+    <class>gr.grnet.aquarium.model.EntityType</class>
+    <class>gr.grnet.aquarium.model.Agreement</class>
+    <class>gr.grnet.aquarium.model.PriceList</class>
+
+    <properties>
+
+      <!-- Common configuration -->
+      <property name="javax.persistence.jdbc.driver"
+                value="org.h2.Driver"/>
+      <property name="javax.persistence.jdbc.url"
+                value="jdbc:h2:aquarium-test"/>
+      <property name="javax.persistence.jdbc.password" value=""/>
+      <property name="javax.persistence.jdbc.user" value=""/>
+
+      <!-- EclipseLink configuration -->
+      <property name="eclipselink.target-database"
+                value="org.eclipse.persistence.platform.database.H2Platform"/>
+      <property name="eclipselink.logging.level" value="FINE"/>
+      <property name="eclipselink.logging.timestamp" value="false"/>
+      <property name="eclipselink.logging.session" value="false"/>
+      <property name="eclipselink.logging.thread" value="false"/>
+      <property name="eclipselink.logging.level.sql" value="FINEST"/>
+      <property name="eclipselink.ddl-generation" value="create-tables"/>
+      <property name="eclipselink.ddl-generation.output-mode" value="database"/>
+
+      <!-- Hibernate configuration -->
+      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
+      <property name="hibernate.hbm2ddl.auto" value="update"/>
+      <property name="hibernate.show_sql" value="true"/>
+
+      <!-- OpenJPA configuration -->
+      <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.H2Dictionary"/>
+      <property name="openjpa.Log" value="DefaultLevel=TRACE, SQL=TRACE"/>
+      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+
+    </properties>
+  </persistence-unit>
 </persistence>
\ No newline at end of file