MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PartitionKeyTest.java
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package testsuite.clusterj;
19 
20 import testsuite.clusterj.model.Employee;
21 import testsuite.clusterj.model.LongIntStringPK;
22 
23 import com.mysql.clusterj.ClusterJUserException;
24 
25 public class PartitionKeyTest extends AbstractClusterJTest {
26 
27  @Override
28  public void localSetUp() {
29  createSessionFactory();
30  addTearDownClasses(Employee.class, LongIntStringPK.class);
31  }
32 
33  public void test() {
34  badClass();
35  wrongKeyTypePrimitive();
36  wrongKeyTypePrimitiveNull();
37  wrongKeyTypeCompound();
38  wrongKeyTypeCompoundNull();
39  wrongKeyTypeCompoundNullPart();
40  setPartitionKeyTwice();
41  goodIntKey();
42  goodCompoundKey();
43  session = sessionFactory.getSession(); // to allow tear down classes to work
44  failOnError();
45  }
46 
47  protected void badClass() {
48  try {
49  session = sessionFactory.getSession();
50  session.setPartitionKey(Integer.class, 0);
51  error("Failed to throw exception on setPartitionKey(Integer.class, 0)");
52  } catch (ClusterJUserException ex){
53  // good catch
54  } finally {
55  session.close();
56  session = null;
57  }
58  }
59 
60  protected void wrongKeyTypePrimitive() {
61  try {
62  session = sessionFactory.getSession();
63  session.setPartitionKey(Employee.class, 0L);
64  error("Failed to throw exception on setPartitionKey(Employee.class, 0L)");
65  } catch (ClusterJUserException ex){
66  // good catch
67  } finally {
68  session.close();
69  session = null;
70  }
71  }
72 
73  protected void wrongKeyTypePrimitiveNull() {
74  try {
75  session = sessionFactory.getSession();
76  session.setPartitionKey(Employee.class, null);
77  error("Failed to throw exception on setPartitionKey(Employee.class, null)");
78  } catch (ClusterJUserException ex){
79  // good catch
80  } finally {
81  session.close();
82  session = null;
83  }
84  }
85 
86  protected void wrongKeyTypeCompound() {
87  try {
88  session = sessionFactory.getSession();
89  session.setPartitionKey(LongIntStringPK.class, 0L);
90  error("Failed to throw exception on setPartitionKey(LongIntStringPK.class, 0L)");
91  } catch (ClusterJUserException ex){
92  // good catch
93  } finally {
94  session.close();
95  session = null;
96  }
97  }
98 
99  protected void wrongKeyTypeCompoundPart() {
100  try {
101  Object[] key = new Object[] {0L, 0L, ""};
102  session = sessionFactory.getSession();
103  session.setPartitionKey(LongIntStringPK.class, key);
104  error("Failed to throw exception on setPartitionKey(LongIntStringPK.class, new Object[] {0L, 0L, \"\"})");
105  } catch (ClusterJUserException ex){
106  // good catch
107  } finally {
108  session.close();
109  session = null;
110  }
111  }
112 
113  protected void wrongKeyTypeCompoundNull() {
114  try {
115  session = sessionFactory.getSession();
116  session.setPartitionKey(LongIntStringPK.class, null);
117  error("Failed to throw exception on setPartitionKey(LongIntStringPK.class, null)");
118  } catch (ClusterJUserException ex){
119  // good catch
120  } finally {
121  session.close();
122  session = null;
123  }
124  }
125 
126  protected void wrongKeyTypeCompoundNullPart() {
127  try {
128  session = sessionFactory.getSession();
129  Object[] key = new Object[] {0L, null, ""};
130  session.setPartitionKey(LongIntStringPK.class, key);
131  error("Failed to throw exception on setPartitionKey(LongIntStringPK.class, new Object[] {0L, null, \"\"})");
132  } catch (ClusterJUserException ex){
133  // good catch
134  } finally {
135  session.close();
136  session = null;
137  }
138  }
139 
140  protected void setPartitionKeyTwice() {
141  try {
142  session = sessionFactory.getSession();
143  // partition key cannot be null
144  Object[] key = new Object[] {0L, 0, ""};
145  session.setPartitionKey(LongIntStringPK.class, key);
146  session.setPartitionKey(LongIntStringPK.class, key);
147  error("Failed to throw exception on second setPartitionKey");
148  } catch (ClusterJUserException ex){
149  // good catch
150  } finally {
151  session.close();
152  session = null;
153  }
154  }
155 
156  protected void goodIntKey() {
157  try {
158  session = sessionFactory.getSession();
159  session.deletePersistentAll(Employee.class);
160  Employee employee = session.newInstance(Employee.class);
161  employee.setId(1000);
162  employee.setAge(1000);
163  employee.setMagic(1000);
164  employee.setName("Employee 1000");
165  session.setPartitionKey(Employee.class, 1000);
166  session.makePersistent(employee);
167  } finally {
168  session.close();
169  session = null;
170  }
171  }
172 
173  protected void goodCompoundKey() {
174  try {
175  session = sessionFactory.getSession();
176  session.deletePersistentAll(LongIntStringPK.class);
177  // key can contain nulls if not part of partition key
178  Object[] key = new Object[] { 1000L, 1000, null};
179  LongIntStringPK instance = session
180  .newInstance(LongIntStringPK.class);
181  instance.setLongpk(1000L);
182  instance.setIntpk(1000);
183  instance.setStringpk("1 Thousand");
184  session.setPartitionKey(LongIntStringPK.class, key);
185  session.makePersistent(instance);
186  } finally {
187  session.close();
188  session = null;
189  }
190  }
191 
192 }