killbill-aplcache

account: start/stop MySQL for the whole test suite Signed-off-by:

7/7/2012 3:22:33 PM

Details

diff --git a/account/src/test/java/com/ning/billing/account/AccountTestSuite.java b/account/src/test/java/com/ning/billing/account/AccountTestSuite.java
new file mode 100644
index 0000000..02c04a7
--- /dev/null
+++ b/account/src/test/java/com/ning/billing/account/AccountTestSuite.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.account;
+
+import com.ning.billing.KillbillTestSuite;
+
+public class AccountTestSuite extends KillbillTestSuite {
+}
diff --git a/account/src/test/java/com/ning/billing/account/AccountTestSuiteWithEmbeddedDB.java b/account/src/test/java/com/ning/billing/account/AccountTestSuiteWithEmbeddedDB.java
new file mode 100644
index 0000000..24fb47f
--- /dev/null
+++ b/account/src/test/java/com/ning/billing/account/AccountTestSuiteWithEmbeddedDB.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.account;
+
+import com.ning.billing.KillbillTestSuiteWithEmbeddedDB;
+
+public abstract class AccountTestSuiteWithEmbeddedDB extends KillbillTestSuiteWithEmbeddedDB {
+}
diff --git a/account/src/test/java/com/ning/billing/account/api/user/TestDefaultAccountUserApi.java b/account/src/test/java/com/ning/billing/account/api/user/TestDefaultAccountUserApi.java
index 5d4657b..6de32f6 100644
--- a/account/src/test/java/com/ning/billing/account/api/user/TestDefaultAccountUserApi.java
+++ b/account/src/test/java/com/ning/billing/account/api/user/TestDefaultAccountUserApi.java
@@ -24,6 +24,7 @@ import org.testng.Assert;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.ning.billing.account.AccountTestSuite;
 import com.ning.billing.account.api.Account;
 import com.ning.billing.account.api.AccountData;
 import com.ning.billing.account.api.DefaultAccount;
@@ -37,7 +38,7 @@ import com.ning.billing.util.bus.Bus;
 import com.ning.billing.util.callcontext.CallContext;
 import com.ning.billing.util.callcontext.CallContextFactory;
 
-public class TestDefaultAccountUserApi {
+public class TestDefaultAccountUserApi extends AccountTestSuite {
     private final CallContextFactory factory = Mockito.mock(CallContextFactory.class);
     private final CallContext callContext = Mockito.mock(CallContext.class);
 
diff --git a/account/src/test/java/com/ning/billing/account/api/user/TestEventJson.java b/account/src/test/java/com/ning/billing/account/api/user/TestEventJson.java
index 8853ad6..a15d101 100644
--- a/account/src/test/java/com/ning/billing/account/api/user/TestEventJson.java
+++ b/account/src/test/java/com/ning/billing/account/api/user/TestEventJson.java
@@ -13,6 +13,7 @@
  * License for the specific language governing permissions and limitations
  * under the License.
  */
+
 package com.ning.billing.account.api.user;
 
 import java.util.ArrayList;
@@ -22,18 +23,18 @@ import java.util.UUID;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
+import com.ning.billing.account.AccountTestSuite;
 import com.ning.billing.account.api.AccountChangeEvent;
 import com.ning.billing.account.api.ChangedField;
 import com.ning.billing.account.api.DefaultChangedField;
 import com.ning.billing.account.api.user.DefaultAccountCreationEvent.DefaultAccountData;
 import com.ning.billing.util.jackson.ObjectMapper;
 
-public class TestEventJson {
+public class TestEventJson extends AccountTestSuite {
     private final ObjectMapper mapper = new ObjectMapper();
 
-    @Test(groups = {"fast"})
+    @Test(groups = "fast")
     public void testDefaultAccountChangeEvent() throws Exception {
-
         final List<ChangedField> changes = new ArrayList<ChangedField>();
         changes.add(new DefaultChangedField("fieldXX", "valueX", "valueXXX"));
         changes.add(new DefaultChangedField("fieldYY", "valueY", "valueYYY"));
@@ -46,17 +47,15 @@ public class TestEventJson {
         Assert.assertTrue(obj.equals(e));
     }
 
-    @Test(groups = {"fast"})
+    @Test(groups = "fast")
     public void testAccountCreationEvent() throws Exception {
-
         final DefaultAccountData data = new DefaultAccountData("dsfdsf", "bobo", 3, "bobo@yahoo.com", 12, "USD", UUID.randomUUID(),
-                                                         "UTC", "US", "21 avenue", "", "Gling", "San Franciso", "CA", "94110", "USA", "4126789887", false, false);
+                                                               "UTC", "US", "21 avenue", "", "Gling", "San Franciso", "CA", "94110", "USA", "4126789887", false, false);
         final DefaultAccountCreationEvent e = new DefaultAccountCreationEvent(data, UUID.randomUUID(), UUID.randomUUID());
 
         final String json = mapper.writeValueAsString(e);
         final Class<?> claz = Class.forName(DefaultAccountCreationEvent.class.getName());
         final Object obj = mapper.readValue(json, claz);
         Assert.assertTrue(obj.equals(e));
-
     }
 }
diff --git a/account/src/test/java/com/ning/billing/account/dao/AccountDaoTestBase.java b/account/src/test/java/com/ning/billing/account/dao/AccountDaoTestBase.java
index 0635c4e..463afe7 100644
--- a/account/src/test/java/com/ning/billing/account/dao/AccountDaoTestBase.java
+++ b/account/src/test/java/com/ning/billing/account/dao/AccountDaoTestBase.java
@@ -18,15 +18,10 @@ package com.ning.billing.account.dao;
 
 import java.io.IOException;
 
-import org.skife.jdbi.v2.Handle;
 import org.skife.jdbi.v2.IDBI;
-import org.skife.jdbi.v2.TransactionCallback;
-import org.skife.jdbi.v2.TransactionStatus;
-import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
-import org.testng.annotations.BeforeMethod;
 
-import com.ning.billing.dbi.MysqlTestingHelper;
+import com.ning.billing.account.AccountTestSuiteWithEmbeddedDB;
 import com.ning.billing.util.bus.Bus;
 import com.ning.billing.util.bus.BusService;
 import com.ning.billing.util.bus.DefaultBusService;
@@ -37,14 +32,11 @@ import com.ning.billing.util.callcontext.DefaultCallContextFactory;
 import com.ning.billing.util.callcontext.UserType;
 import com.ning.billing.util.clock.Clock;
 import com.ning.billing.util.clock.ClockMock;
-import com.ning.billing.util.io.IOUtils;
 import com.ning.billing.util.tag.api.user.TagEventBuilder;
 
 import static org.testng.Assert.fail;
 
-public abstract class AccountDaoTestBase {
-    private final MysqlTestingHelper helper = new MysqlTestingHelper();
-
+public abstract class AccountDaoTestBase extends AccountTestSuiteWithEmbeddedDB {
     protected final TagEventBuilder tagEventBuilder = new TagEventBuilder();
 
     protected AccountDao accountDao;
@@ -53,17 +45,9 @@ public abstract class AccountDaoTestBase {
     protected Bus bus;
     protected CallContext context;
 
-    @BeforeClass(alwaysRun = true)
+    @BeforeClass(groups = "slow")
     protected void setup() throws IOException {
-        // Health check test to make sure MySQL is setup properly
         try {
-            final String accountDdl = IOUtils.toString(AccountSqlDao.class.getResourceAsStream("/com/ning/billing/account/ddl.sql"));
-            final String utilDdl = IOUtils.toString(AccountSqlDao.class.getResourceAsStream("/com/ning/billing/util/ddl.sql"));
-
-            helper.startMysql();
-            helper.initDb(accountDdl);
-            helper.initDb(utilDdl);
-
             dbi = helper.getDBI();
 
             bus = new InMemoryBus();
@@ -71,9 +55,11 @@ public abstract class AccountDaoTestBase {
             ((DefaultBusService) busService).startBus();
 
             accountDao = new AuditedAccountDao(dbi, bus);
+            // Health check test to make sure MySQL is setup properly
             accountDao.test();
 
             accountEmailDao = new AuditedAccountEmailDao(dbi);
+            // Health check test to make sure MySQL is setup properly
             accountEmailDao.test();
 
             final Clock clock = new ClockMock();
@@ -82,27 +68,4 @@ public abstract class AccountDaoTestBase {
             fail(t.toString());
         }
     }
-
-    @AfterClass(alwaysRun = true)
-    public void stopMysql() {
-        helper.stopMysql();
-    }
-
-    @BeforeMethod(alwaysRun = true)
-    public void cleanupData() {
-        dbi.inTransaction(new TransactionCallback<Void>() {
-            @Override
-            public Void inTransaction(final Handle h, final TransactionStatus status) throws Exception {
-                h.execute("truncate table accounts");
-                h.execute("truncate table notifications");
-                h.execute("truncate table bus_events");
-                h.execute("truncate table claimed_bus_events");
-                h.execute("truncate table claimed_notifications");
-                h.execute("truncate table tag_definitions");
-                h.execute("truncate table tags");
-                h.execute("truncate table custom_fields");
-                return null;
-            }
-        });
-    }
 }
diff --git a/account/src/test/java/com/ning/billing/account/dao/TestAccountDao.java b/account/src/test/java/com/ning/billing/account/dao/TestAccountDao.java
index b3105c3..5d457e2 100644
--- a/account/src/test/java/com/ning/billing/account/dao/TestAccountDao.java
+++ b/account/src/test/java/com/ning/billing/account/dao/TestAccountDao.java
@@ -126,7 +126,6 @@ public class TestAccountDao extends AccountDaoTestBase {
         assertEquals(account.getExternalKey(), key);
         assertEquals(account.getName(), name);
         assertEquals(account.getFirstNameLength(), firstNameLength);
-
     }
 
     @Test
@@ -359,9 +358,9 @@ public class TestAccountDao extends AccountDaoTestBase {
 
         final String buggyKey = "extKey1338";
         final DefaultAccount updatedAccountData = new DefaultAccount(accountId, buggyKey, "myemail1337@glam.com",
-                                                                 "John Smith", 4, Currency.USD, 15, null,
-                                                                 null, null, null, null, null, null, null, null, null, null,
-                                                                 false, false);
+                                                                     "John Smith", 4, Currency.USD, 15, null,
+                                                                     null, null, null, null, null, null, null, null, null, null,
+                                                                     false, false);
         accountDao.update(updatedAccountData, context);
         Assert.assertNull(accountDao.getAccountByKey(buggyKey));
     }
diff --git a/util/src/test/java/com/ning/billing/dbi/MysqlTestingHelper.java b/util/src/test/java/com/ning/billing/dbi/MysqlTestingHelper.java
index f1bfc60..6371fe1 100644
--- a/util/src/test/java/com/ning/billing/dbi/MysqlTestingHelper.java
+++ b/util/src/test/java/com/ning/billing/dbi/MysqlTestingHelper.java
@@ -33,8 +33,10 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.Assert;
 
+import com.google.common.io.Resources;
 import com.mysql.management.MysqldResource;
 import com.mysql.management.MysqldResourceI;
+import com.ning.billing.util.io.IOUtils;
 
 /**
  * Utility class to embed MySQL for testing purposes
@@ -173,6 +175,19 @@ public class MysqlTestingHelper {
         return new DBI(dbiString, USERNAME, PASSWORD);
     }
 
+    public void initDb() throws IOException {
+        for (final String pack : new String[]{"account", "analytics", "entitlement", "util", "payment", "invoice", "junction"}) {
+            final String ddl;
+            try {
+                ddl = IOUtils.toString(Resources.getResource("com/ning/billing/" + pack + "/ddl.sql").openStream());
+            } catch (IllegalArgumentException ignored) {
+                // The test doesn't have this module ddl in the classpath - that's fine
+                continue;
+            }
+            initDb(ddl);
+        }
+    }
+
     public void initDb(final String ddl) throws IOException {
         if (isUsingLocalInstance()) {
             return;
diff --git a/util/src/test/java/com/ning/billing/KillbillTestSuite.java b/util/src/test/java/com/ning/billing/KillbillTestSuite.java
new file mode 100644
index 0000000..b3bc530
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/KillbillTestSuite.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing;
+
+import java.lang.reflect.Method;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+
+public class KillbillTestSuite {
+    private static final Logger log = LoggerFactory.getLogger(KillbillTestSuite.class);
+
+    @BeforeMethod(alwaysRun = true)
+    public void startTest(final Method method) throws Exception {
+        log.info("***************************************************************************************************");
+        log.info("*** Starting test {}:{}", method.getDeclaringClass().getName(), method.getName());
+        log.info("***************************************************************************************************");
+    }
+
+    @AfterMethod(alwaysRun = true)
+    public void endTest(final Method method) throws Exception {
+        log.info("***************************************************************************************************");
+        log.info("***   Ending test {}:{}", method.getDeclaringClass().getName(), method.getName());
+        log.info("***************************************************************************************************");
+    }
+}
diff --git a/util/src/test/java/com/ning/billing/KillbillTestSuiteWithEmbeddedDB.java b/util/src/test/java/com/ning/billing/KillbillTestSuiteWithEmbeddedDB.java
new file mode 100644
index 0000000..163164e
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/KillbillTestSuiteWithEmbeddedDB.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.sql.SQLException;
+
+import org.testng.annotations.AfterSuite;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.BeforeSuite;
+
+import com.ning.billing.dbi.MysqlTestingHelper;
+
+public class KillbillTestSuiteWithEmbeddedDB extends KillbillTestSuite {
+    protected static final MysqlTestingHelper helper = new MysqlTestingHelper();
+
+    public static MysqlTestingHelper getMysqlTestingHelper() {
+        return helper;
+    }
+
+    @BeforeSuite(groups = "slow")
+    public void startMysql() throws IOException, ClassNotFoundException, SQLException, URISyntaxException {
+        helper.startMysql();
+        helper.initDb();
+        helper.cleanupAllTables();
+    }
+
+    @BeforeMethod(groups = "slow")
+    public void cleanup() {
+        try {
+            helper.cleanupAllTables();
+        } catch (Exception ignored) {
+        }
+    }
+
+    @AfterSuite(groups = "slow")
+    public void shutdownMysql() throws IOException, ClassNotFoundException, SQLException, URISyntaxException {
+        try {
+            helper.cleanupAllTables();
+            helper.stopMysql();
+        } catch (Exception ignored) {
+        }
+    }
+}