2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package com.mysql.clusterj.openjpatest;
21 import com.mysql.clusterj.jpatest.AbstractJPABaseTest;
23 import com.mysql.clusterj.jpatest.model.LongIntStringFKOneOne;
24 import com.mysql.clusterj.jpatest.model.LongIntStringPKOneOne;
25 import com.mysql.clusterj.jpatest.model.LongIntStringOid;
30 public class LongIntStringPKOneOneTest extends AbstractJPABaseTest {
32 private int NUMBER_OF_INSTANCES = 5;
33 private int OFFSET_A = 100;
34 private int OFFSET_B = 10;
36 private LongIntStringPKOneOne[] as = new LongIntStringPKOneOne[NUMBER_OF_INSTANCES];
37 private LongIntStringFKOneOne[] bs = new LongIntStringFKOneOne[NUMBER_OF_INSTANCES];
39 // set this to true for debug output
40 private boolean print = false;
48 protected String getPersistenceUnitName() {
53 /** This tests delete, insert, find, and update of entities with compound
54 * primary and foreign keys.
57 LongIntStringPKOneOne a;
58 LongIntStringFKOneOne b;
59 em = emf.createEntityManager();
61 // bulk remove all LongLongStringFKManyOne
62 int countB = em.createQuery("DELETE FROM LongIntStringFKOneOne").executeUpdate();
63 int countA = em.createQuery("DELETE FROM LongIntStringPKOneOne").executeUpdate();
64 print ("Deleted " + countB + " instances of LongLongStringFKManyOne " +
65 countA + " instances of LongLongStringFKManyOne ");
69 em = emf.createEntityManager();
71 print("Creating " + NUMBER_OF_INSTANCES + " instances of LongIntStringPKOneOne and LongIntStringFKOneOne.");
72 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
73 a = as[i] = LongIntStringPKOneOne.create(OFFSET_A + i);
74 b = bs[i] = LongIntStringFKOneOne.create(OFFSET_B + i);
75 a.setLongIntStringFKOneOne(b);
76 b.setLongIntStringPKOneOne(a);
85 em = emf.createEntityManager();
87 print("Checking " + NUMBER_OF_INSTANCES + " instances.");
88 for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
89 b = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + i));
90 a = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + i));
91 errorIfNull("Instance of LongIntStringPKOneOne for value " + (OFFSET_A + i) + " was not found.", a);
92 errorIfNull("Instance of LongIntStringFKOneOne for value " + (OFFSET_B + i) + " was not found.", b);
93 if (b != null && a != null) {
94 errorIfNotEqual("Mismatch in longIntStringFKOneOne.getLongIntStringPKOneOne",
95 a, b.getLongIntStringPKOneOne());
96 errorIfNotEqual("Mismatch in longIntStringPKOneOne.getLongIntStringFKOneOne",
97 b, a.getLongIntStringFKOneOne());
103 em = emf.createEntityManager();
104 print("Deleting some instances.");
106 // delete a0; set b0.a = null
107 a = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 0));
108 b = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 0));
109 b.setLongIntStringPKOneOne(null);
111 a = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 1));
112 b = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 1));
113 a.setLongIntStringFKOneOne(null);
118 em = emf.createEntityManager();
120 print("Checking deleted instances.");
121 a = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 0));
122 errorIfNotEqual("finding deleted instance of LongIntStringPK should return null", null, a);
123 b = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 1));
124 errorIfNotEqual("finding deleted instance of LongIntStringFK should return null", null, b);
125 a = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 1));
126 b = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 0));
127 errorIfNull("Instance of LongIntStringPKOneOne for value " + (OFFSET_A + 1) + " was not found.", a);
128 errorIfNull("Instance of LongIntStringFKOneOne for value " + (OFFSET_B + 0) + " was not found.", b);
129 if (b != null && a != null) {
130 errorIfNotEqual("field longIntStringFKOneOne should be null",
131 null, a.getLongIntStringFKOneOne());
132 errorIfNotEqual("field longIntStringPKOneOne should be null",
133 null, b.getLongIntStringPKOneOne());
138 em = emf.createEntityManager();
140 print("Setting fields to null");
141 a = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 2));
142 b = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 2));
143 b.setLongIntStringPKOneOne(null);
144 a.setLongIntStringFKOneOne(null);
148 em = emf.createEntityManager();
150 print("Checking fields set to null.");
151 a = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 2));
152 b = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 2));
153 if (b != null && a != null) {
154 errorIfNotEqual("field longIntStringFKOneOne should be null",
155 null, a.getLongIntStringFKOneOne());
156 errorIfNotEqual("field longIntStringPKOneOne should be null",
157 null, b.getLongIntStringPKOneOne());
162 em = emf.createEntityManager();
164 print("Swapping references.");
165 LongIntStringPKOneOne a3 = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 3));
166 LongIntStringPKOneOne a4 = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 4));
167 LongIntStringFKOneOne b3 = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 3));
168 LongIntStringFKOneOne b4 = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 4));
169 a3.setLongIntStringFKOneOne(b4);
170 b4.setLongIntStringPKOneOne(a3);
171 a4.setLongIntStringFKOneOne(b3);
172 b3.setLongIntStringPKOneOne(a4);
173 errorIfNotEqual("Swapped field b4.longIntStringPKOneOne should be a3",
174 a3, b4.getLongIntStringPKOneOne());
175 errorIfNotEqual("Swapped field a3.longIntStringFKOneOne should be b4",
176 b4, a3.getLongIntStringFKOneOne());
177 errorIfNotEqual("Swapped field b3.longIntStringPKOneOne should be a4",
178 a4, b3.getLongIntStringPKOneOne());
179 errorIfNotEqual("Swapped field a4.longIntStringFKOneOne should be b3",
180 b3, a4.getLongIntStringFKOneOne());
184 em = emf.createEntityManager();
186 print("Checking swapped references.");
187 a3 = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 3));
188 a4 = em.find(LongIntStringPKOneOne.class, new LongIntStringOid(OFFSET_A + 4));
189 b3 = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 3));
190 b4 = em.find(LongIntStringFKOneOne.class, new LongIntStringOid(OFFSET_B + 4));
191 errorIfNotEqual("Swapped field b4.longIntStringPKOneOne should be a3",
192 a3, b4.getLongIntStringPKOneOne());
193 errorIfNotEqual("Swapped field a3.longIntStringFKOneOne should be b4",
194 b4, a3.getLongIntStringFKOneOne());
195 errorIfNotEqual("Swapped field b3.longIntStringPKOneOne should be a4",
196 a4, b3.getLongIntStringPKOneOne());
197 errorIfNotEqual("Swapped field a4.longIntStringFKOneOne should be b3",
198 b3, a4.getLongIntStringFKOneOne());
204 private void print(String string) {
206 System.out.println(string);
210 /** Verify that the primary keys match the oid.
212 * @param oid the oid used to find the instance
214 public void verify(LongIntStringOid oid, LongIntStringPKOneOne instance) {
215 errorIfNotEqual("Mismatch longpk", oid.longpk, instance.getLongpk());
216 errorIfNotEqual("Mismatch intpk", oid.intpk, instance.getIntpk());
217 errorIfNotEqual("Mismatch stringpk", oid.stringpk, instance.getStringpk());