16
16
*/
17
17
package org .apache .jdo .tck .query .api ;
18
18
19
+ import java .util .HashMap ;
19
20
import java .util .List ;
21
+ import java .util .Map ;
22
+ import javax .jdo .JDOQLTypedQuery ;
20
23
import javax .jdo .Query ;
21
24
import javax .jdo .Transaction ;
25
+ import javax .jdo .query .NumericExpression ;
22
26
import org .apache .jdo .tck .pc .company .CompanyModelReader ;
23
27
import org .apache .jdo .tck .pc .company .FullTimeEmployee ;
28
+ import org .apache .jdo .tck .pc .company .QFullTimeEmployee ;
24
29
import org .apache .jdo .tck .query .QueryTest ;
25
30
import org .junit .jupiter .api .Test ;
26
31
30
35
* <B>Assertion IDs:</B> <br>
31
36
* <B>Assertion Description: </B> This test class runs the example modify queries from the JDO
32
37
* 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
33
49
*/
34
50
public class SampleModifyQueries extends QueryTest {
35
51
@@ -46,7 +62,7 @@ public class SampleModifyQueries extends QueryTest {
46
62
* <p>This query deletes all Employees who make more than the parameter salary.
47
63
*/
48
64
@ Test
49
- public void testQuery21 () {
65
+ public void testQuery21a () {
50
66
Transaction tx = pm .currentTransaction ();
51
67
try {
52
68
tx .begin ();
@@ -74,6 +90,118 @@ public void testQuery21() {
74
90
}
75
91
}
76
92
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
+
77
205
/**
78
206
* @see org.apache.jdo.tck.JDO_Test#localSetUp()
79
207
*/
0 commit comments