18 package testsuite.clusterj;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Properties;
24 import testsuite.clusterj.model.Employee;
26 import com.mysql.clusterj.ClusterJFatalUserException;
27 import com.mysql.clusterj.ClusterJHelper;
28 import com.mysql.clusterj.Constants;
29 import com.mysql.clusterj.Session;
30 import com.mysql.clusterj.SessionFactory;
31 import com.mysql.clusterj.core.SessionFactoryImpl;
41 protected boolean runSpecificNodeIdTests() {
49 if (sessionFactory != null) {
50 sessionFactory.
close();
51 sessionFactory = null;
55 public void testNoPooling() {
56 Properties modifiedProperties =
new Properties();
57 modifiedProperties.putAll(props);
65 sessionFactory1.
close();
66 sessionFactory2.
close();
67 errorIfNotEqual(
"With connection pooling, SessionFactory1 should be the same object as SessionFactory2",
68 true, sessionFactory1 == sessionFactory2);
76 sessionFactory3.
close();
77 }
catch (ClusterJFatalUserException ex) {
80 sessionFactory1.
close();
81 sessionFactory2.
close();
82 errorIfNotEqual(
"With no connection pooling, SessionFactory1 should not be the same object as SessionFactory2",
83 false, sessionFactory1 == sessionFactory2);
88 public void testConnectionPoolSize() {
90 modifiedProperties.putAll(props);
91 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 2);
92 checkConnectionPoolSize2(
"testConnectionPoolSize", modifiedProperties);
96 public void testConnectionPoolSizeAndNodeIds() {
97 if (!runSpecificNodeIdTests()) {
101 modifiedProperties.putAll(props);
102 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 2);
103 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS,
"4;5");
104 checkConnectionPoolSize2(
"testConnectionPoolSizeAndNodeIds", modifiedProperties);
108 public void testConnectionNodeIds() {
109 if (!runSpecificNodeIdTests()) {
113 modifiedProperties.putAll(props);
114 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS,
"4,5");
115 checkConnectionPoolSize2(
"testConnectionNodeIds", modifiedProperties);
119 public void testConnectionSingleNodeIdAndConnectionPoolSize() {
120 if (!runSpecificNodeIdTests()) {
124 modifiedProperties.putAll(props);
125 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 2);
126 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS,
"4");
127 checkConnectionPoolSize2(
"testConnectionNodeIds", modifiedProperties);
131 private void checkConnectionPoolSize2(
String where,
Properties modifiedProperties) {
132 SessionFactory sessionFactory1 = null;
133 SessionFactory sessionFactory2 = null;
134 SessionFactory sessionFactory3 = null;
135 sessionFactory1 = ClusterJHelper.getSessionFactory(modifiedProperties);
136 sessionFactory2 = ClusterJHelper.getSessionFactory(modifiedProperties);
137 sessionFactory3 = ClusterJHelper.getSessionFactory(modifiedProperties);
138 errorIfNotEqual(where +
" SessionFactory1 should be the same object as SessionFactory2",
true,
139 sessionFactory1 == sessionFactory2);
140 errorIfNotEqual(where +
" SessionFactory1 should be the same object as SessionFactory3",
true,
141 sessionFactory1 == sessionFactory3);
142 Session session1 = sessionFactory1.getSession();
144 checkSessions(where +
" after get session1", sessionFactory1,
new Integer[] {1, 0});
145 Session session2 = sessionFactory1.getSession();
147 checkSessions(where +
" after get session2", sessionFactory1,
new Integer[] {1, 1});
148 Session session3 = sessionFactory1.getSession();
149 checkSessions(where +
" nafter get session3", sessionFactory1,
new Integer[] {2, 1});
150 Session session4 = sessionFactory1.getSession();
151 checkSessions(where +
" after get session4", sessionFactory1,
new Integer[] {2, 2});
152 Session session5 = sessionFactory1.getSession();
153 checkSessions(where +
" after get session5", sessionFactory1,
new Integer[] {3, 2});
154 Session session6 = sessionFactory1.getSession();
155 checkSessions(where +
" after get session6", sessionFactory1,
new Integer[] {3, 3});
158 checkSessions(where +
" after close session1", sessionFactory1,
new Integer[] {2, 3});
160 checkSessions(where +
" after close session4", sessionFactory1,
new Integer[] {2, 2});
162 checkSessions(where +
" after close session5", sessionFactory1,
new Integer[] {1, 2});
163 Session session7 = sessionFactory1.getSession();
164 checkSessions(where +
" after get session7", sessionFactory1,
new Integer[] {2, 2});
170 sessionFactory1.close();
173 public void testNegativeMismatchConnectionPoolSizeAndConnectionPoolNodeids() {
175 modifiedProperties.putAll(props);
176 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 3);
177 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS,
"4\t5");
179 ClusterJHelper.getSessionFactory(modifiedProperties);
180 }
catch (ClusterJFatalUserException ex) {
181 if (
getDebug()) ex.printStackTrace();
184 if (!ex.getMessage().contains(expected)) {
185 error(
"Mismatch error message should contain " + expected);
191 public void testNegativeConnectionPoolNodeidsFormatError() {
193 modifiedProperties.putAll(props);
194 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_SIZE, 2);
195 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS,
"7 t");
197 ClusterJHelper.getSessionFactory(modifiedProperties);
198 }
catch (ClusterJFatalUserException ex) {
199 if (
getDebug()) ex.printStackTrace();
201 String expected =
"NumberFormatException";
202 if (!ex.getMessage().contains(expected)) {
203 error(
"Mismatch error message '" + ex.getMessage() +
"' should contain '" + expected +
'"');
209 public void testNegativeConnectionPoolIllegalNodeids() {
211 modifiedProperties.putAll(props);
212 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS,
"256");
214 ClusterJHelper.getSessionFactory(modifiedProperties);
215 }
catch (ClusterJFatalUserException ex) {
216 if (
getDebug()) ex.printStackTrace();
218 String expected =
"illegal";
219 if (!ex.getMessage().contains(expected)) {
220 error(
"Mismatch error message '" + ex.getMessage() +
"' should contain '" + expected +
'"');
226 public void testNegativeConnectionPoolNoNodeId() {
227 if (!runSpecificNodeIdTests()) {
231 modifiedProperties.putAll(props);
232 modifiedProperties.
put(Constants.PROPERTY_CONNECTION_POOL_NODEIDS,
"48");
234 ClusterJHelper.getSessionFactory(modifiedProperties);
235 }
catch (ClusterJFatalUserException ex) {
236 if (
getDebug()) ex.printStackTrace();
238 String expected =
"No node defined";
239 if (!ex.getMessage().contains(expected)) {
240 error(
"Mismatch error message '" + ex.getMessage() +
"' should contain '" + expected +
'"');
246 private void checkSessions(
String where, SessionFactory sessionFactory1, Integer[] expected) {
247 SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl)sessionFactory1;
248 List<Integer> connectionCounts = sessionFactoryImpl.getConnectionPoolSessionCounts();
249 if (expected.length != connectionCounts.size()) {
250 error(where +
" wrong number of connections in pool\n"
251 +
"Expected: " + Arrays.toString(expected)
252 +
" Actual: " + connectionCounts);
256 for (Integer connectionCount: connectionCounts) {
257 if (
getDebug()) System.out.println(
"Connection " + i +
" has " + connectionCount +
" sessions.");
258 if (i >= expected.length)
break;
259 errorIfNotEqual(where +
" wrong count on connection " + i, expected[i], connectionCount);
262 if (
getDebug()) System.out.println();