]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
895f8c8d01e65721ae78de4a43b1c7c3bf2a0e60
[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.A;
23 import com.mysql.clusterj.jpatest.model.B0;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.List;
28
29 /**
30  *
31  */
32 public class OneToManyRelationshipTest extends AbstractJPABaseTest {
33
34     private int NUMBER_OF_A = 2;
35     private int OFFSET_A = 100;
36     private int NUMBER_OF_B = 4;
37     private int OFFSET_B = 10;
38     private A a0;
39     // set this to true for debug output
40     private boolean print = false;
41     private List<A> as = new ArrayList<A>();
42
43     @Override
44     public void setUp() {
45         super.setUp();
46     }
47
48     public void test() {
49         em = emf.createEntityManager();
50         print("Removing " + NUMBER_OF_A + " instances of A.");
51         begin();
52         for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
53             A a = em.find(A.class, i);
54             em.remove(a);
55         }
56         print("Removing " + NUMBER_OF_B + " instances of B.");
57         for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; ++i) {
58             B0 b = em.find(B0.class, i);
59             em.remove(b);
60         }
61         commit();
62         em.close();
63
64         em = emf.createEntityManager();
65         begin();
66         Collection<B0> bs = new HashSet<B0>();
67         print("Creating " + NUMBER_OF_A + " instances of A.");
68         for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
69             a0 = A.create(i);
70             em.persist(a0);
71         }
72         print("Creating " + NUMBER_OF_B + " instances of B.");
73         for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; ++i) {
74             B0 b = B0.create(i);
75             b.setA(a0);
76             bs.add(b);
77             em.persist(b);
78         }
79         a0.setB0s(bs);
80         print("Before commit, " + a0.toString());
81         for (B0 b:bs){print(b.toString());}
82         commit();
83         em.close();
84
85         em = emf.createEntityManager();
86         print("Finding " + NUMBER_OF_A + " instances of A.");
87         begin();
88         for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
89             a0 = em.find(A.class, i);
90             print(a0.toString());
91         }
92         print("Finding " + NUMBER_OF_B + " instances of B.");
93         for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; ++i) {
94             B0 b = em.find(B0.class, i);
95             print(b.toString());
96         }
97         commit();
98         em.close();
99
100         em = emf.createEntityManager();
101         print("Finding 1 instance of A.");
102         begin();
103         A a = em.find(A.class, OFFSET_A);
104         print("Finding 2 instances of B.");
105         for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; i += 2) {
106             B0 b = em.find(B0.class, i);
107             // update every other one
108             b.setA(a);
109             print(b.toString());
110         }
111         print("After update: " + a0.toString());
112         commit();
113         em.close();
114
115         em = emf.createEntityManager();
116         print("Finding " + NUMBER_OF_A + " instances of A.");
117         begin();
118         for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
119             a = em.find(A.class, i);
120             as.add(a);
121             print(a.toString());
122         }
123         print("Finding " + NUMBER_OF_B + " instances of B.");
124         for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; ++i) {
125             B0 b = em.find(B0.class, i);
126             print(b.toString());
127             if (0 == i%2) {
128                 assertEquals("Mismatch in relationship a", as.get(0), b.getA());
129                 assertTrue("A.b0s should contain b", as.get(0).getB0s().contains(b));
130             } else {
131                 assertEquals("Mismatch in relationship a", as.get(1), b.getA());
132                 assertTrue("A.b0s should contain b", as.get(1).getB0s().contains(b));
133             }
134         }
135         commit();
136         em.close();
137
138     }
139
140     private void print(String string) {
141         if (print) {
142             System.out.println(string);
143         }
144     }
145
146 }