]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
3540e67d2abda0cb2104f7aa70ed7f2685dfdb2e
[packages/trusty/mysql-wsrep-5.6.git] /
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 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;
29
30 /**
31  *
32  */
33 public class LongLongStringPKOneManyTest extends AbstractJPABaseTest {
34
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;
39
40     private LongLongStringPKOneMany a0 = null;
41     private List<LongLongStringPKOneMany> as = new ArrayList<LongLongStringPKOneMany>();
42
43     // set this to true for debug output
44     private boolean print = false;
45
46     @Override
47     public void setUp() {
48         super.setUp();
49     }
50
51     @Override
52     protected String getPersistenceUnitName() {
53         return "ndb";
54     }
55
56     /** This tests delete, insert, find, and update of entities with compound
57      * primary and foreign keys.
58      */
59     public void test() {
60         LongLongStringPKOneMany a;
61         em = emf.createEntityManager();
62         begin();
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 ");
68         commit();
69         em.close();
70
71         em = emf.createEntityManager();
72         begin();
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);
76             em.persist(a0);
77         }
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);
83             bs.add(b);
84             em.persist(b);
85         }
86         a0.setLongLongStringFKRelationships(bs);
87         print("Before commit, " + a0.toString());
88         for (LongLongStringFKManyOne b:bs){print(b.toString());}
89         commit();
90         em.close();
91
92         em = emf.createEntityManager();
93         print("Finding " + NUMBER_OF_A + " instances of LongLongStringPKOneMany.");
94         begin();
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);
98         }
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);
103             print(b.toString());
104         }
105         commit();
106         em.close();
107
108         /** Update every other instance of B to refer to a different A. */
109         em = emf.createEntityManager();
110         print("Finding 1 instance of A.");
111         begin();
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);
120             print(b.toString());
121         }
122         print("After update: " + a0.toString());
123         commit();
124         em.close();
125
126         em = emf.createEntityManager();
127         print("Finding " + NUMBER_OF_A + " instances of A.");
128         begin();
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);
132             as.add(a);
133             print(a.toString());
134         }
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);
139             print(b.toString());
140             if (0 == i%2) {
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));
145             } else {
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));
150             }
151         }
152         commit();
153         em.close();
154         failOnError();
155     }
156
157     private void print(String string) {
158         if (print) {
159             System.out.println(string);
160         }
161     }
162
163     /** Verify that the primary keys match the oid.
164      *
165      * @param oid the oid used to find the instance
166      */
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());
171     }
172
173 }