Skip to content

Commit f3b4df0

Browse files
authored
JDO-850: Add SampleModifyQuery test method using typesafe query API (#103)
1 parent 11d676c commit f3b4df0

File tree

1 file changed

+129
-1
lines changed

1 file changed

+129
-1
lines changed

tck/src/main/java/org/apache/jdo/tck/query/api/SampleModifyQueries.java

+129-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
*/
1717
package org.apache.jdo.tck.query.api;
1818

19+
import java.util.HashMap;
1920
import java.util.List;
21+
import java.util.Map;
22+
import javax.jdo.JDOQLTypedQuery;
2023
import javax.jdo.Query;
2124
import javax.jdo.Transaction;
25+
import javax.jdo.query.NumericExpression;
2226
import org.apache.jdo.tck.pc.company.CompanyModelReader;
2327
import org.apache.jdo.tck.pc.company.FullTimeEmployee;
28+
import org.apache.jdo.tck.pc.company.QFullTimeEmployee;
2429
import org.apache.jdo.tck.query.QueryTest;
2530
import org.junit.jupiter.api.Test;
2631

@@ -30,6 +35,17 @@
3035
* <B>Assertion IDs:</B> <br>
3136
* <B>Assertion Description: </B> This test class runs the example modify queries from the JDO
3237
* specification.
38+
*
39+
* <p>There are up to four test methods per test case:
40+
*
41+
* <ul>
42+
* <li>testQueryxxa: runtime constructed JDO query using deletePersistentAll taking parameters to
43+
* run the query
44+
* <li>testQueryxxb: runtime constructed JDO query using setNamedParameters to specify the
45+
* parameter values and no-arg deletePersistentAll to run the query
46+
* <li>testQueryxxc: runtime constructed JDO query using setParameters to specify the parameter
47+
* values and no-arg deletePersistentAll to run the query
48+
* <li>testQueryxxf: JDOQLTypedQuery version
3349
*/
3450
public class SampleModifyQueries extends QueryTest {
3551

@@ -46,7 +62,7 @@ public class SampleModifyQueries extends QueryTest {
4662
* <p>This query deletes all Employees who make more than the parameter salary.
4763
*/
4864
@Test
49-
public void testQuery21() {
65+
public void testQuery21a() {
5066
Transaction tx = pm.currentTransaction();
5167
try {
5268
tx.begin();
@@ -74,6 +90,118 @@ public void testQuery21() {
7490
}
7591
}
7692

93+
/**
94+
* Deleting Multiple Instances.
95+
*
96+
* <p>This query deletes all Employees who make more than the parameter salary.
97+
*/
98+
@Test
99+
public void testQuery21b() {
100+
Transaction tx = pm.currentTransaction();
101+
try {
102+
tx.begin();
103+
Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal");
104+
q.declareParameters("Double sal");
105+
Map<String, Object> paramValues = new HashMap<>();
106+
paramValues.put("sal", 30000.);
107+
q.setNamedParameters(paramValues);
108+
q.deletePersistentAll();
109+
tx.commit();
110+
111+
tx.begin();
112+
Query<FullTimeEmployee> allQuery = pm.newQuery(FullTimeEmployee.class);
113+
List<FullTimeEmployee> allFTE = allQuery.executeList();
114+
if (!allFTE.isEmpty()) {
115+
fail(
116+
ASSERTION_FAILED,
117+
"All FullTimeEmployee instances should have been deleted,"
118+
+ " there are still "
119+
+ allFTE.size()
120+
+ " instances left.");
121+
}
122+
tx.commit();
123+
} finally {
124+
if (tx.isActive()) {
125+
tx.rollback();
126+
}
127+
}
128+
}
129+
130+
/**
131+
* Deleting Multiple Instances.
132+
*
133+
* <p>This query deletes all Employees who make more than the parameter salary.
134+
*/
135+
@Test
136+
public void testQuery21c() {
137+
Transaction tx = pm.currentTransaction();
138+
try {
139+
tx.begin();
140+
Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal");
141+
q.declareParameters("Double sal");
142+
q.setParameters(30000.);
143+
q.deletePersistentAll();
144+
tx.commit();
145+
146+
tx.begin();
147+
Query<FullTimeEmployee> allQuery = pm.newQuery(FullTimeEmployee.class);
148+
List<FullTimeEmployee> allFTE = allQuery.executeList();
149+
if (!allFTE.isEmpty()) {
150+
fail(
151+
ASSERTION_FAILED,
152+
"All FullTimeEmployee instances should have been deleted,"
153+
+ " there are still "
154+
+ allFTE.size()
155+
+ " instances left.");
156+
}
157+
tx.commit();
158+
} finally {
159+
if (tx.isActive()) {
160+
tx.rollback();
161+
}
162+
}
163+
}
164+
165+
/**
166+
* Deleting Multiple Instances.
167+
*
168+
* <p>This query deletes all Employees who make more than the parameter salary.
169+
*/
170+
@Test
171+
public void testQuery21f() {
172+
Transaction tx = pm.currentTransaction();
173+
try {
174+
tx.begin();
175+
try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
176+
QFullTimeEmployee cand = QFullTimeEmployee.candidate("this");
177+
NumericExpression<Double> sal = q.numericParameter("sal", Double.class);
178+
q.filter(cand.salary.gt(sal));
179+
q.setParameter("sal", 30000.);
180+
q.deletePersistentAll();
181+
} catch (Exception ex) {
182+
fail(ASSERTION_FAILED, ex.getLocalizedMessage());
183+
}
184+
tx.commit();
185+
186+
tx.begin();
187+
Query<FullTimeEmployee> allQuery = pm.newQuery(FullTimeEmployee.class);
188+
List<FullTimeEmployee> allFTE = allQuery.executeList();
189+
if (!allFTE.isEmpty()) {
190+
fail(
191+
ASSERTION_FAILED,
192+
"All FullTimeEmployee instances should have been deleted,"
193+
+ " there are still "
194+
+ allFTE.size()
195+
+ " instances left.");
196+
}
197+
tx.commit();
198+
} finally {
199+
if (tx.isActive()) {
200+
tx.rollback();
201+
}
202+
}
203+
}
204+
77205
/**
78206
* @see org.apache.jdo.tck.JDO_Test#localSetUp()
79207
*/

0 commit comments

Comments
 (0)