Skip to content

Commit d8fb044

Browse files
committed
Issue 1815: Add testing
Signed-off-by: Will Dazey <[email protected]>
1 parent c6e5bb3 commit d8fb044

File tree

2 files changed

+240
-0
lines changed

2 files changed

+240
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright (c) 2023 IBM Corporation. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
// Contributors:
14+
package org.eclipse.persistence.jpa.test.collection;
15+
16+
import jakarta.persistence.EntityManager;
17+
import jakarta.persistence.EntityManagerFactory;
18+
import jakarta.persistence.Query;
19+
import jakarta.persistence.TypedQuery;
20+
21+
import java.util.List;
22+
import java.util.Set;
23+
24+
import org.eclipse.persistence.jpa.test.framework.DDLGen;
25+
import org.eclipse.persistence.jpa.test.framework.Emf;
26+
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
27+
import org.eclipse.persistence.jpa.test.framework.Property;
28+
import org.eclipse.persistence.jpa.test.collection.model.CityEntity;
29+
import org.junit.Assert;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
33+
@RunWith(EmfRunner.class)
34+
public class TestBasicCollection {
35+
36+
@Emf(createTables = DDLGen.DROP_CREATE,
37+
classes = { CityEntity.class },
38+
properties = { @Property(name = "eclipselink.logging.level", value = "ALL"),
39+
@Property(name = "eclipselink.logging.level.sql", value = "FINE"),
40+
@Property(name = "eclipselink.logging.parameters", value = "true")})
41+
private EntityManagerFactory emf;
42+
43+
@Test
44+
public void testUpdateBasicCollectionWithQuery() {
45+
Set<Integer> origSet = Set.of(608);
46+
47+
EntityManager em = emf.createEntityManager();
48+
try {
49+
em.getTransaction().begin();
50+
em.persist(new CityEntity("Minneapolis", origSet));
51+
em.getTransaction().commit();
52+
} finally {
53+
if (em.getTransaction().isActive()) {
54+
em.getTransaction().rollback();
55+
}
56+
if(em.isOpen()) {
57+
em.clear();
58+
em.close();
59+
}
60+
}
61+
62+
em = emf.createEntityManager();
63+
try {
64+
em.getTransaction().begin();
65+
CityEntity find1 = em.find(CityEntity.class, "Minneapolis");
66+
67+
Assert.assertNotNull(find1);
68+
Assert.assertNotNull(find1.getAreaCodes());
69+
Assert.assertEquals(origSet.size(), find1.getAreaCodes().size());
70+
Assert.assertTrue(origSet.containsAll(find1.getAreaCodes()));
71+
em.getTransaction().commit();
72+
} finally {
73+
if (em.getTransaction().isActive()) {
74+
em.getTransaction().rollback();
75+
}
76+
if(em.isOpen()) {
77+
em.clear();
78+
em.close();
79+
}
80+
}
81+
82+
Set<Integer> updatedSet = Set.of(563, 456);
83+
84+
em = emf.createEntityManager();
85+
try {
86+
em.getTransaction().begin();
87+
Query q = em.createQuery("UPDATE CityEntity o SET o.areaCodes=?1 WHERE o.name=?2");
88+
q.setParameter(1, updatedSet);
89+
q.setParameter(2, "Minneapolis");
90+
q.executeUpdate();
91+
em.getTransaction().commit();
92+
} finally {
93+
if (em.getTransaction().isActive()) {
94+
em.getTransaction().rollback();
95+
}
96+
if(em.isOpen()) {
97+
em.clear();
98+
em.close();
99+
}
100+
}
101+
102+
em = emf.createEntityManager();
103+
try {
104+
em.getTransaction().begin();
105+
CityEntity find2 = em.find(CityEntity.class, "Minneapolis");
106+
107+
Assert.assertNotNull(find2);
108+
Assert.assertNotNull(find2.getAreaCodes());
109+
Assert.assertEquals(updatedSet.size(), find2.getAreaCodes().size());
110+
Assert.assertTrue(updatedSet.containsAll(find2.getAreaCodes()));
111+
em.getTransaction().commit();
112+
} finally {
113+
if (em.getTransaction().isActive()) {
114+
em.getTransaction().rollback();
115+
}
116+
if(em.isOpen()) {
117+
em.clear();
118+
em.close();
119+
}
120+
}
121+
}
122+
123+
@Test
124+
public void testINBasicCollectionWithQuery() {
125+
Set<Integer> origSet = Set.of(234, 789);
126+
127+
EntityManager em = emf.createEntityManager();
128+
try {
129+
em.getTransaction().begin();
130+
em.persist(new CityEntity("Los Angeles", origSet));
131+
em.getTransaction().commit();
132+
} finally {
133+
if (em.getTransaction().isActive()) {
134+
em.getTransaction().rollback();
135+
}
136+
if(em.isOpen()) {
137+
em.clear();
138+
em.close();
139+
}
140+
}
141+
142+
em = emf.createEntityManager();
143+
try {
144+
em.getTransaction().begin();
145+
CityEntity find1 = em.find(CityEntity.class, "Los Angeles");
146+
147+
Assert.assertNotNull(find1);
148+
Assert.assertNotNull(find1.getAreaCodes());
149+
Assert.assertEquals(origSet.size(), find1.getAreaCodes().size());
150+
Assert.assertTrue("Expected " + origSet + " / actual " + find1.getAreaCodes(), origSet.containsAll(find1.getAreaCodes()));
151+
em.getTransaction().commit();
152+
} finally {
153+
if (em.getTransaction().isActive()) {
154+
em.getTransaction().rollback();
155+
}
156+
if(em.isOpen()) {
157+
em.clear();
158+
em.close();
159+
}
160+
}
161+
162+
Set<Integer> set1 = Set.of(123, 234);
163+
Set<Integer> set2 = Set.of(345);
164+
Set<Integer> set3 = Set.of(234, 789);
165+
Set<Integer> set4 = Set.of(678);
166+
Set<Set<Integer>> searchSet = Set.of(set1, set2, set3, set4);
167+
168+
em = emf.createEntityManager();
169+
try {
170+
em.getTransaction().begin();
171+
TypedQuery<CityEntity> q = em.createQuery("SELECT e FROM CityEntity e WHERE e.areaCodes IN ?1", CityEntity.class);
172+
q.setParameter(1, searchSet);
173+
List<CityEntity> res = q.getResultList();
174+
175+
Assert.assertEquals(1, res.size());
176+
CityEntity find2 = res.get(1);
177+
Assert.assertTrue(origSet.containsAll(find2.getAreaCodes()));
178+
em.getTransaction().commit();
179+
} finally {
180+
if (em.getTransaction().isActive()) {
181+
em.getTransaction().rollback();
182+
}
183+
if(em.isOpen()) {
184+
em.clear();
185+
em.close();
186+
}
187+
}
188+
}
189+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2023 IBM Corporation. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
// Contributors:
14+
package org.eclipse.persistence.jpa.test.collection.model;
15+
16+
import java.util.Set;
17+
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.Id;
20+
21+
@Entity
22+
public class CityEntity {
23+
24+
@Id
25+
public String name;
26+
27+
public Set<Integer> areaCodes;
28+
29+
public CityEntity() {}
30+
31+
public CityEntity(String name, Set<Integer> areaCodes) {
32+
this.name = name;
33+
this.areaCodes = areaCodes;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
44+
public Set<Integer> getAreaCodes() {
45+
return areaCodes;
46+
}
47+
48+
public void setAreaCodes(Set<Integer> areaCodes) {
49+
this.areaCodes = areaCodes;
50+
}
51+
}

0 commit comments

Comments
 (0)