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