View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
19   */
20  package org.jumpmind.symmetric.test;
21  
22  import javax.sql.DataSource;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.jumpmind.symmetric.SymmetricEngine;
27  import org.jumpmind.symmetric.common.Constants;
28  import org.jumpmind.symmetric.db.IDbDialect;
29  import org.jumpmind.symmetric.util.AppUtils;
30  import org.junit.AfterClass;
31  import org.junit.Before;
32  import org.springframework.jdbc.core.JdbcTemplate;
33  
34  public class AbstractIntegrationTest {
35  
36      static final Log logger = LogFactory.getLog(AbstractIntegrationTest.class);
37  
38      protected String client;
39      protected String root;
40      protected JdbcTemplate rootJdbcTemplate;
41      protected JdbcTemplate clientJdbcTemplate;
42      protected static boolean standalone = false;
43  
44      public AbstractIntegrationTest(String client, String root) {
45          this.client = client;
46          this.root = root;
47      }
48  
49      public AbstractIntegrationTest() throws Exception {
50          if (!standalone) {
51              logger.info("Running test in standalone mode");
52              standalone = true;
53              String[] databases = TestSetupUtil.lookupDatabasePairs(DatabaseTestSuite.DEFAULT_TEST_PREFIX).iterator()
54                      .next();
55              TestSetupUtil.setup(DatabaseTestSuite.DEFAULT_TEST_PREFIX, TestConstants.TEST_ROOT_DOMAIN_SETUP_SCRIPT, databases[0], databases[1]);
56          }
57      }
58  
59      protected SymmetricEngine getRootEngine() {
60          return TestSetupUtil.getRootEngine();
61      }
62  
63      protected SymmetricEngine getClientEngine() {
64          return TestSetupUtil.getClientEngine();
65      }
66  
67      protected IDbDialect getRootDbDialect() {
68          return AppUtils.find(Constants.DB_DIALECT, getRootEngine());
69      }
70  
71      protected IDbDialect getClientDbDialect() {
72          return AppUtils.find(Constants.DB_DIALECT, getClientEngine());
73      }
74  
75      protected String printRootAndClientDatabases() {
76          return " The root database is " + root + " and the client database is " + client + ".";
77      }
78      
79      @SuppressWarnings("unchecked")
80      protected <T> T findOnClient(String name) {
81          return (T) AppUtils.find(name, getClientEngine());
82      }
83      
84      @SuppressWarnings("unchecked")
85      protected <T> T findOnRoot(String name) {
86          return (T) AppUtils.find(name, getRootEngine());
87      }
88  
89      @Before
90      public void setupTemplates() {
91          rootJdbcTemplate = new JdbcTemplate((DataSource) AppUtils.find(Constants.DATA_SOURCE, getRootEngine()));
92          clientJdbcTemplate = new JdbcTemplate((DataSource) AppUtils.find(Constants.DATA_SOURCE, getClientEngine()));
93      }
94  
95      @AfterClass
96      public static void cleanup() throws Exception {
97          if (standalone) {
98              standalone = false;
99              logger.info("Cleaning up after test in standalone mode");
100             TestSetupUtil.cleanup();
101         }
102     }
103 
104 }