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.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;
32
public class OneToManyRelationshipTest extends AbstractJPABaseTest {
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;
39
// set this to true for debug output
40
private boolean print = false;
41
private List<A> as = new ArrayList<A>();
49
em = emf.createEntityManager();
50
print("Removing " + NUMBER_OF_A + " instances of A.");
52
for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
53
A a = em.find(A.class, i);
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);
64
em = emf.createEntityManager();
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) {
72
print("Creating " + NUMBER_OF_B + " instances of B.");
73
for (int i = OFFSET_B; i < OFFSET_B + NUMBER_OF_B; ++i) {
80
print("Before commit, " + a0.toString());
81
for (B0 b:bs){print(b.toString());}
85
em = emf.createEntityManager();
86
print("Finding " + NUMBER_OF_A + " instances of A.");
88
for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
89
a0 = em.find(A.class, i);
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);
100
em = emf.createEntityManager();
101
print("Finding 1 instance of A.");
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
111
print("After update: " + a0.toString());
115
em = emf.createEntityManager();
116
print("Finding " + NUMBER_OF_A + " instances of A.");
118
for (int i = OFFSET_A; i < OFFSET_A + NUMBER_OF_A; ++i) {
119
a = em.find(A.class, i);
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);
128
assertEquals("Mismatch in relationship a", as.get(0), b.getA());
129
assertTrue("A.b0s should contain b", as.get(0).getB0s().contains(b));
131
assertEquals("Mismatch in relationship a", as.get(1), b.getA());
132
assertTrue("A.b0s should contain b", as.get(1).getB0s().contains(b));
140
private void print(String string) {
142
System.out.println(string);