MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LongIntStringPKOneOneTest.java
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
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.
8 
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.
13 
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
17 */
18 
19 package com.mysql.clusterj.openjpatest;
20 
21 import com.mysql.clusterj.jpatest.AbstractJPABaseTest;
22 
23 import com.mysql.clusterj.jpatest.model.LongIntStringFKOneOne;
24 import com.mysql.clusterj.jpatest.model.LongIntStringPKOneOne;
25 import com.mysql.clusterj.jpatest.model.LongIntStringOid;
26 
31 
32  private int NUMBER_OF_INSTANCES = 5;
33  private int OFFSET_A = 100;
34  private int OFFSET_B = 10;
35 
36  private LongIntStringPKOneOne[] as = new LongIntStringPKOneOne[NUMBER_OF_INSTANCES];
37  private LongIntStringFKOneOne[] bs = new LongIntStringFKOneOne[NUMBER_OF_INSTANCES];
38 
39  // set this to true for debug output
40  private boolean print = false;
41 
42  @Override
43  public void setUp() {
44  super.setUp();
45  }
46 
47  @Override
49  return "ndb";
50 // return "jdbc";
51  }
52 
56  public void test() {
59  em = emf.createEntityManager();
60  begin();
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 ");
66  commit();
67  em.close();
68 
69  em = emf.createEntityManager();
70  begin();
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);
77  em.persist(a);
78  print(a.toString());
79  em.persist(b);
80  print(b.toString());
81  }
82  commit();
83  em.close();
84 
85  em = emf.createEntityManager();
86  begin();
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());
98  }
99  }
100  commit();
101  em.close();
102 
103  em = emf.createEntityManager();
104  print("Deleting some instances.");
105  begin();
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);
110  em.remove(a);
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);
114  em.remove(b);
115  commit();
116  em.close();
117 
118  em = emf.createEntityManager();
119  begin();
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());
134  }
135  commit();
136  em.close();
137 
138  em = emf.createEntityManager();
139  begin();
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);
145  commit();
146  em.close();
147 
148  em = emf.createEntityManager();
149  begin();
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());
158  }
159  commit();
160  em.close();
161 
162  em = emf.createEntityManager();
163  begin();
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());
181  commit();
182  em.close();
183 
184  em = emf.createEntityManager();
185  begin();
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());
199  commit();
200  em.close();
201  failOnError();
202  }
203 
204  private void print(String string) {
205  if (print) {
206  System.out.println(string);
207  }
208  }
209 
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());
218  }
219 
220 }