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.service.IParameterService;
30  import org.jumpmind.symmetric.util.AppUtils;
31  import org.junit.AfterClass;
32  import org.junit.Assert;
33  import org.springframework.jdbc.core.JdbcTemplate;
34  
35  public class AbstractDatabaseTest {
36  
37      static final Log logger = LogFactory.getLog(AbstractDatabaseTest.class);
38  
39      protected String database;
40      static boolean standalone = false;
41  
42      public AbstractDatabaseTest(String dbName) {
43          this.database = dbName;
44      }
45  
46      public AbstractDatabaseTest() throws Exception {
47          if (!standalone) {
48              logger.info("Running test in standalone mode");
49              standalone = true;
50              TestSetupUtil.setup(DatabaseTestSuite.DEFAULT_TEST_PREFIX, TestConstants.TEST_CONTINUOUS_SETUP_SCRIPT,
51                      null, TestSetupUtil.getRootDbTypes(DatabaseTestSuite.DEFAULT_TEST_PREFIX)[0]);
52          }
53      }
54  
55      protected SymmetricEngine getSymmetricEngine() {
56          return TestSetupUtil.getRootEngine();
57      }
58  
59      protected IParameterService getParameterService() {
60          return AppUtils.find(Constants.PARAMETER_SERVICE, getSymmetricEngine());
61      }
62  
63      protected IDbDialect getDbDialect() {
64          return AppUtils.find(Constants.DB_DIALECT, getSymmetricEngine());
65      }
66  
67      protected DataSource getDataSource() {
68          return AppUtils.find(Constants.DATA_SOURCE, getSymmetricEngine());
69      }
70  
71      protected JdbcTemplate getJdbcTemplate() {
72          return new JdbcTemplate((DataSource) AppUtils.find(Constants.DATA_SOURCE, getSymmetricEngine()));
73      }
74  
75      @SuppressWarnings("unchecked")
76      protected <T> T find(String name) {
77          return (T) AppUtils.find(name, getSymmetricEngine());
78      }
79  
80      @AfterClass
81      public static void cleanup() throws Exception {
82          if (standalone) {
83              logger.info("Cleaning up after test in standalone mode");
84              TestSetupUtil.cleanup();
85              standalone = false;
86          }
87      }
88  
89      protected void cleanSlate(final String... tableName) {
90          for (String table : tableName) {
91              getJdbcTemplate().update("delete from " + table);
92          }
93      }
94  
95      protected void assertTrue(boolean condition, String message) {
96          Assert.assertTrue(message, condition);
97      }
98  
99      protected void assertFalse(boolean condition, String message) {
100         Assert.assertFalse(message, condition);
101     }
102 
103     protected void assertNotNull(Object condition, String message) {
104         Assert.assertNotNull(message, condition);
105     }
106     
107     protected void assertNull(Object condition) {
108         Assert.assertNull(condition);
109     }
110     
111     protected void assertNotNull(Object condition) {
112         Assert.assertNotNull(condition);
113     }
114 
115     protected void assertNull(Object condition, String message) {
116         Assert.assertNull(message, condition);
117     }
118 
119     protected void assertFalse(boolean condition) {
120         Assert.assertFalse(condition);
121     }
122 
123     protected void assertTrue(boolean condition) {
124         Assert.assertTrue(condition);
125     }
126     
127     protected void assertEquals(Object actual, Object expected) {
128         Assert.assertEquals(expected, actual);
129     }
130 
131     protected void assertEquals(Object actual, Object expected, String message) {
132         Assert.assertEquals(message, expected, actual);
133     }
134     
135     protected void assertNotSame(Object actual, Object expected, String message) {
136         Assert.assertNotSame(message, expected, actual);
137     }        
138     
139 }