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;
22 import com.mysql.clusterj.jpatest.model.LongLongStringFKManyOne;
23 import com.mysql.clusterj.jpatest.model.LongLongStringPKOneMany;
24 import com.mysql.clusterj.jpatest.model.LongLongStringOid;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.List;
33 public class LongLongStringPKOneManyTest extends AbstractJPABaseTest {
35 private int NUMBER_OF_A = 2;
36 private int OFFSET_A = 100;
37 private int NUMBER_OF_B = 4;
38 private int OFFSET_B = 10;
40 private LongLongStringPKOneMany a0 = null;
41 private List<LongLongStringPKOneMany> as = new ArrayList<LongLongStringPKOneMany>();
43 // set this to true for debug output
44 private boolean print = false;
52 protected String getPersistenceUnitName() {
56 /** This tests delete, insert, find, and update of entities with compound
57 * primary and foreign keys.
60 LongLongStringPKOneMany a;
61 em = emf.createEntityManager();
63 // bulk remove all LongLongStringFKManyOne
64 int countB = em.createQuery("DELETE FROM LongLongStringFKManyOne").executeUpdate();
65 int countA = em.createQuery("DELETE FROM LongLongStringPKOneMany").executeUpdate();
66 print ("Deleted " + countB + " instances of LongLongStringFKManyOne " +
67 countA + " instances of LongLongStringFKManyOne ");
71 em = emf.createEntityManager();
73 print("Creating " + NUMBER_OF_A + " instances of LongLongStringPKOneMany.");
74 for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
75 a0 = LongLongStringPKOneMany.create(i);
78 Collection<LongLongStringFKManyOne> bs = new HashSet<LongLongStringFKManyOne>();
79 print("Creating " + NUMBER_OF_B + " instances of LongLongStringFKManyOne.");
80 for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; ++i) {
81 LongLongStringFKManyOne b = LongLongStringFKManyOne.create(i);
82 b.setLongLongStringPKRelationship(a0);
86 a0.setLongLongStringFKRelationships(bs);
87 print("Before commit, " + a0.toString());
88 for (LongLongStringFKManyOne b:bs){print(b.toString());}
92 em = emf.createEntityManager();
93 print("Finding " + NUMBER_OF_A + " instances of LongLongStringPKOneMany.");
95 for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
96 LongLongStringOid oid = new LongLongStringOid(i);
97 a = em.find(LongLongStringPKOneMany.class, oid);
99 print("Finding " + NUMBER_OF_B + " instances of LongLongStringFKManyOne.");
100 for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; ++i) {
101 LongLongStringOid oid = new LongLongStringOid(i);
102 LongLongStringFKManyOne b = em.find(LongLongStringFKManyOne.class, oid);
108 /** Update every other instance of B to refer to a different A. */
109 em = emf.createEntityManager();
110 print("Finding 1 instance of A.");
112 LongLongStringOid oid = new LongLongStringOid(OFFSET_A);
113 a = em.find(LongLongStringPKOneMany.class, oid);
114 print("Finding 2 instances of B.");
115 for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; i += 2) {
116 oid = new LongLongStringOid(i);
117 LongLongStringFKManyOne b = em.find(LongLongStringFKManyOne.class, oid);
118 // update every other one
119 b.setLongLongStringPKRelationship(a);
122 print("After update: " + a0.toString());
126 em = emf.createEntityManager();
127 print("Finding " + NUMBER_OF_A + " instances of A.");
129 for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
130 oid = new LongLongStringOid(i);
131 a = em.find(LongLongStringPKOneMany.class, oid);
135 print("Finding " + NUMBER_OF_B + " instances of B.");
136 for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; ++i) {
137 oid = new LongLongStringOid(i);
138 LongLongStringFKManyOne b = em.find(LongLongStringFKManyOne.class, oid);
141 errorIfNotEqual("Mismatch in relationship longLongStringPKRelationship",
142 as.get(0), b.getLongLongStringPKOneMany());
143 errorIfNotEqual("A.longLongStringFKRelationships should contain longLongStringFKRelationship",
144 true, as.get(0).getLongLongStringFKRelationships().contains(b));
146 errorIfNotEqual("Mismatch in relationship longLongStringPKRelationship",
147 as.get(1), b.getLongLongStringPKOneMany());
148 errorIfNotEqual("A.longLongStringFKRelationships should contain longLongStringFKRelationship",
149 true, as.get(1).getLongLongStringFKRelationships().contains(b));
157 private void print(String string) {
159 System.out.println(string);
163 /** Verify that the primary keys match the oid.
165 * @param oid the oid used to find the instance
167 public void verify(LongLongStringOid oid, LongLongStringPKOneMany instance) {
168 errorIfNotEqual("Mismatch longpk1", oid.longpk1, instance.getLongpk1());
169 errorIfNotEqual("Mismatch longpk2", oid.longpk2, instance.getLongpk2());
170 errorIfNotEqual("Mismatch stringpk", oid.stringpk, instance.getStringpk());