2 Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
18 package testsuite.clusterj;
20 import testsuite.clusterj.model.Employee;
21 import testsuite.clusterj.model.LongIntStringPK;
23 import com.mysql.clusterj.ClusterJUserException;
25 public class PartitionKeyTest extends AbstractClusterJTest {
28 public void localSetUp() {
29 createSessionFactory();
30 addTearDownClasses(Employee.class, LongIntStringPK.class);
35 wrongKeyTypePrimitive();
36 wrongKeyTypePrimitiveNull();
37 wrongKeyTypeCompound();
38 wrongKeyTypeCompoundNull();
39 wrongKeyTypeCompoundNullPart();
40 setPartitionKeyTwice();
43 session = sessionFactory.getSession(); // to allow tear down classes to work
47 protected void badClass() {
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){
60 protected void wrongKeyTypePrimitive() {
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){
73 protected void wrongKeyTypePrimitiveNull() {
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){
86 protected void wrongKeyTypeCompound() {
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){
99 protected void wrongKeyTypeCompoundPart() {
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){
113 protected void wrongKeyTypeCompoundNull() {
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){
126 protected void wrongKeyTypeCompoundNullPart() {
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){
140 protected void setPartitionKeyTwice() {
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){
156 protected void goodIntKey() {
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);
173 protected void goodCompoundKey() {
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);