blob: e6bba34e0aaabbad6d269effcfbdcb39789b7e0b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/*-
* ============LICENSE_START=======================================================
* APPC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* Copyright (C) 2017 Amdocs
* ================================================================================
* Licensed 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.
* ============LICENSE_END=========================================================
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
*/
package org.openecomp.appc.lockmanager.impl.sql;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.openecomp.appc.dao.util.DefaultJdbcConnectionFactory;
import org.openecomp.appc.lockmanager.api.LockManager;
import org.openecomp.appc.lockmanager.api.LockManagerBaseTests;
import org.openecomp.appc.lockmanager.impl.sql.JdbcLockManager;
import org.openecomp.appc.lockmanager.impl.sql.MySqlConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public abstract class MySqlLockManagerBaseTests extends LockManagerBaseTests {
private static final boolean USE_REAL_DB = Boolean.getBoolean("lockmanager.tests.useRealDb");
private static final String TABLE_LOCK_MANAGEMENT = "TEST_LOCK_MANAGEMENT";
private static final String JDBC_URL = System.getProperty("lockmanager.tests.jdbcUrl", "jdbc:mysql://192.168.1.2/test");
private static final String JDBC_USERNAME = System.getProperty("lockmanager.tests.jdbcUsername", "test");
private static final String JDBC_PASSWORD = System.getProperty("lockmanager.tests.jdbcPassword", "123456");
protected static final int CONCURRENT_TEST_WAIT_TIME = 10; // secs
@Rule
public TestName testName = new TestName();
@Override
protected LockManager createLockManager() {
JdbcLockManager jdbcLockManager = createJdbcLockManager(USE_REAL_DB);
DefaultJdbcConnectionFactory connectionFactory = new MySqlConnectionFactory();
connectionFactory.setJdbcURL(JDBC_URL);
connectionFactory.setJdbcUserName(JDBC_USERNAME);
connectionFactory.setJdbcPassword(JDBC_PASSWORD);
jdbcLockManager.setConnectionFactory(connectionFactory);
jdbcLockManager.setTableName(TABLE_LOCK_MANAGEMENT);
System.out.println("=> Running LockManager test [" + jdbcLockManager.getClass().getName() + "." + testName.getMethodName() + "]" + (USE_REAL_DB ? ". JDBC URL is [" + JDBC_URL + "]" : ""));
clearTestLocks(jdbcLockManager);
return jdbcLockManager;
}
protected abstract JdbcLockManager createJdbcLockManager(boolean useRealDb);
protected boolean setSynchronizer(Synchronizer synchronizer) {
if(!(lockManager instanceof SynchronizerReceiver)) {
System.err.println("Skipping concurrency test [" + testName.getMethodName() + "] for LockManager of type " + lockManager.getClass());
return false;
}
((SynchronizerReceiver)lockManager).setSynchronizer(synchronizer);
return true;
}
private static final String SQL_DELETE_LOCK_RECORD = String.format("DELETE FROM %s WHERE RESOURCE_ID=?", TABLE_LOCK_MANAGEMENT);
private void clearTestLocks(JdbcLockManager jdbcLockManager) {
Connection connection = jdbcLockManager.openDbConnection();
if(connection == null) {
return;
}
try {
for(Resource resource: Resource.values()) {
try(PreparedStatement statement = connection.prepareStatement(SQL_DELETE_LOCK_RECORD)) {
statement.setString(1, resource.name());
statement.executeUpdate();
}
}
} catch(SQLException e) {
throw new RuntimeException("Cannot clear test resources in table", e);
} finally {
jdbcLockManager.closeDbConnection(connection);
}
}
}
|