My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Copyright (C) 2008 Eric Bottard
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.code.liquidform.itest.jsr220doc;

import static com.google.code.liquidform.Aggregates.*;
import static com.google.code.liquidform.LiquidForm.*;
import static com.google.code.liquidform.Parameters.*;
import static org.junit.Assert.*;

import org.junit.Test;

import com.google.code.liquidform.conditions.ConditionalExpression;
import com.google.code.liquidform.itest.LiquidFormIntegrationTest;

/**
* This set of integration tests are adapted from the inline examples of the
* JSR. As such, they're considered as more "real world" examples as the ones
* found in {@link LiquidFormIntegrationTest}.
*/
public class JSR220ExemplesTest {

@Test
public void testPara432() {
Order o = alias(Order.class, "o");
LineItem l = alias(o.getLineItems(), "l");
assertEquals(
"select distinct o from Order as o inner join o.lineItems as l "
+ "where l.shipped = false", select(distinct(o)).from(
Order.class).as(o).innerJoin(o.getLineItems()).as(l)
.where(eq(l.getShipped(), false)).toString());

Product p = alias(l.getProduct(), "p");
assertEquals(
"select distinct o from Order as o inner join o.lineItems as l "
+ "inner join l.product as p where p.productType = 'office_supplies'",
select(distinct(o)).from(Order.class).as(o).innerJoin(
o.getLineItems()).as(l).innerJoin(l.getProduct()).as(p)
.where(eq(p.getProductType(), "office_supplies"))
.toString());
}

@Test
public void testPara443() {
Order o1 = alias(Order.class, "o1");
Order o2 = alias(Order.class, "o2");
assertEquals(
"select distinct o1 from Order as o1, Order as o2 where o1.quantity"
+ " > o2.quantity and o2.customer.lastname = 'Smith'"
+ " and o2.customer.firstname = 'John'", select(
distinct(o1)).from(Order.class).as(o1).andFrom(
Order.class).as(o2).where(
gt(o1.getQuantity(), o2.getQuantity()).and(
eq(o2.getCustomer().getLastname(), "Smith"))
.and(
eq(o2.getCustomer().getFirstname(),
"John"))).toString());
}

@Test
public void testPara444() {
Order o = alias(Order.class, "o");
LineItem l = alias(in(o.getLineItems()), "l");
assertEquals(
"select distinct l.product from Order as o, in(o.lineItems) as l",
select(distinct(l.getProduct())).from(Order.class).as(o)
.andFrom(in(o.getLineItems())).as(l).toString());

}

@Test
public void testPara445() {
Customer c = alias(Customer.class, "c");
Employee e = alias(Employee.class, "e");
assertEquals(
"select c from Customer as c, Employee as e where c.hatsize = e.shoesize",
select(c).from(Customer.class).as(c).andFrom(Employee.class)
.as(e).where(eq(c.getHatsize(), e.getShoesize()))
.toString());
}

@Test
public void testPara4451() {
Customer c = alias(Customer.class, "c");
Order o = alias(c.getOrders(), "o");
assertEquals(
"select c from Customer as c inner join c.orders as o where c.status = 1",
select(c).from(Customer.class).as(c).innerJoin(c.getOrders())
.as(o).where(eq(c.getStatus(), 1)).toString());

o = alias(in(c.getOrders()), "o");
assertEquals(
"select object(c) from Customer as c, in(c.orders) as o where c.status = 1",
select(object(c)).from(Customer.class).as(c).andFrom(
in(c.getOrders())).as(o).where(eq(c.getStatus(), 1))
.toString());

}

@Test
public void testPara4452() {
Customer c = alias(Customer.class, "c");
Order o = alias(c.getOrders(), "o");
assertEquals(
"select c from Customer as c left outer join c.orders as o where c.status = 1",
select(c).from(Customer.class).as(c).leftOuterJoin(
c.getOrders()).as(o).where(eq(c.getStatus(), 1))
.toString());

}

@Test
// TODO FromClause:254
public void testPara4453() {
Department d = alias(Department.class, "d");
assertEquals("select d from Department as d left outer join fetch "
+ "d.employees where d.deptno = 1", select(d).from(
Department.class).as(d).leftOuterJoinFetch(d.getEmployees())
.where(eq(d.getDeptno(), 1)).toString());
}

@Test
public void testParam447() {
Order o = alias(Order.class, "o");
LineItem l = alias(in(o.getLineItems()), "l");
Product p = alias(Product.class, "p");
assertEquals(
"select o from Order as o, in(o.lineItems) as l, Product as p",
select(o).from(Order.class).as(o).andFrom(in(o.getLineItems()))
.as(l).andFrom(Product.class).as(p).toString());
}

@Test
public void testParam4642() {
Customer c = alias(Customer.class, "c");
assertEquals("select c from Customer as c where c.status = :stat",
select(c).from(Customer.class).as(c).where(
eq(c.getStatus(), param("stat", Integer.class)))
.toString());
}

@Test
public void testPara46eleven() {
Order o = alias(Order.class, "o");
assertEquals("select o from Order as o where o.lineItems is empty",
select(o).from(Order.class).as(o).where(
isEmpty(o.getLineItems())).toString());
}

@Test
public void test46thirteen() {
Employee emp = alias(Employee.class, "emp");
Employee spouseEmp = alias(Employee.class, "spouseEmp");
String exp = "select distinct emp from Employee as emp where exists "
+ "(select spouseEmp from Employee as spouseEmp "
+ "where spouseEmp = emp.spouse)";
assertEquals(exp, select(distinct(emp)).from(Employee.class).as(emp)
.where(
exists(select(spouseEmp).from(Employee.class).as(
spouseEmp)
.where(eq(spouseEmp, emp.getSpouse()))))
.toString());
}

@Test
public void test46fourteen() {
Employee emp = alias(Employee.class, "emp");
Manager m = alias(Manager.class, "m");
String exp = "select emp from Employee as emp where emp.salary > "
+ "all (select m.salary from Manager as m where m.department = emp.department)";
assertEquals(exp, select(emp).from(Employee.class).as(emp).where(
gt(emp.getSalary(), all(select(m.getSalary()).from(
Manager.class).as(m).where(
eq(m.getDepartment(), emp.getDepartment())))))
.toString());
}

@Test
public void testPara46fifteen() {
Customer c = alias(Customer.class, "c");
Order o = alias(c.getOrders(), "o");
assertEquals("select c from Customer as c where "
+ "(select count(o) from c.orders as o) > 10", select(c).from(
Customer.class).as(c).where(
gt(select(count(o)).from(c.getOrders()).as(o), 10)).toString());

Customer goodCustomer = alias(Customer.class, "goodCustomer");
assertEquals(
"select goodCustomer from Customer as goodCustomer"
+ " where goodCustomer.balanceOwed < (select avg(c.balanceOwed) from Customer as c)",
select(goodCustomer).from(Customer.class).as(goodCustomer)
.where(
lt(goodCustomer.getBalanceOwed(), select(
avg(c.getBalanceOwed())).from(
Customer.class).as(c))).toString());

}

@Test
public void testPara47() {
Customer c = alias(Customer.class, "c");
String exp = "select c.status, avg(c.filledOrderCount), count(c)"
+ " from Customer as c group by c.status having c.status in (1, 2)";

assertEquals(exp, select(c.getStatus(), avg(c.getFilledOrderCount()),
count(c)).from(Customer.class).as(c).groupBy(c.getStatus())
.having(in(c.getStatus(), 1, 2)).toString());

exp = "select c.country, count(c) from Customer as c "
+ "group by c.country having count(c.country) > 3";
assertEquals(exp, select(c.getCountry(), count(c)).from(Customer.class)
.as(c).groupBy(c.getCountry()).having(
gt(count(c.getCountry()), 3)).toString());

}

@Test
public void testPara48() {
Customer c = alias(Customer.class, "c");
Order o = alias(c.getOrders(), "o");
assertEquals(
"select c.id, c.status from Customer as c inner join c.orders as o "
+ "where o.quantity > 100", select(c.getId(),
c.getStatus()).from(Customer.class).as(c).innerJoin(
c.getOrders()).as(o).where(gt(o.getQuantity(), 100))
.toString());
}

// TODO
@Test
public void testPara482() {
}

@Test
public void testPara4841() {
Order o = alias(Order.class, "o");
assertEquals("select avg(o.quantity) from Order as o", select(
avg(o.getQuantity())).from(Order.class).as(o).toString());

LineItem l = alias(o.getLineItems(), "l");
Customer c = alias(o.getCustomer(), "c");
String exp = "select sum(l.price) from Order as o"
+ " inner join o.lineItems as l"
+ " inner join o.customer as c"
+ " where c.lastname = 'Smith' and c.firstname = 'John'";

assertEquals(exp, select(sum(l.getPrice())).from(Order.class).as(o)
.innerJoin(o.getLineItems()).as(l).innerJoin(o.getCustomer())
.as(c).where(
eq(c.getLastname(), "Smith").and(
eq(c.getFirstname(), "John"))).toString());

assertEquals("select count(o) from Order as o", select(count(o)).from(
Order.class).as(o).toString());

exp = "select count(l.price) from Order as o"
+ " inner join o.lineItems as l"
+ " inner join o.customer as c"
+ " where c.lastname = 'Smith' and c.firstname = 'John'";

assertEquals(exp, select(count(l.getPrice())).from(Order.class).as(o)
.innerJoin(o.getLineItems()).as(l).innerJoin(o.getCustomer())
.as(c).where(
eq(c.getLastname(), "Smith").and(
eq(c.getFirstname(), "John"))).toString());

exp = "select count(l) from Order as o"
+ " inner join o.lineItems as l"
+ " inner join o.customer as c"
+ " where c.lastname = 'Smith' and c.firstname = 'John'"
+ " and l.price is not null";

assertEquals(exp, select(count(l)).from(Order.class).as(o).innerJoin(
o.getLineItems()).as(l).innerJoin(o.getCustomer()).as(c).where(
eq(c.getLastname(), "Smith").and(
eq(c.getFirstname(), "John").and(
isNotNull(l.getPrice())))).toString());
}

@Test
public void testPara49() {
// SELECT o.quantity, a.zipcode
// FROM Customer c JOIN c.orders o JOIN c.address a
// WHERE a.state = ‘CA’
// ORDER BY o.quantity, a.zipcode

Customer c = alias(Customer.class, "c");
Order o = alias(c.getOrders(), "o");
Address a = alias(c.getAddress(), "a");

String exp = "select o from Customer as c inner join c.orders as o "
+ "inner join c.address as a" + " where a.state = 'CA'"
+ " order by o.quantity asc, o.totalcost asc";
assertEquals(exp, select(o).from(Customer.class).as(c).innerJoin(
c.getOrders()).as(o).innerJoin(c.getAddress()).as(a).where(
eq(a.getState(), "CA")).orderBy(o.getQuantity()).andThenBy(
o.getTotalcost()).toString());

exp = "select o.quantity, a.zipcode"
+ " from Customer as c inner join c.orders as o"
+ " inner join c.address as a" + " where a.state = 'CA'"
+ " order by o.quantity asc, a.zipcode asc";
assertEquals(exp, select(o.getQuantity(), a.getZipcode()).from(
Customer.class).as(c).innerJoin(c.getOrders()).as(o).innerJoin(
c.getAddress()).as(a).where(eq(a.getState(), "CA")).orderBy(
o.getQuantity()).andThenBy(a.getZipcode()).toString());

}

@Test
public void testPara4thirteen1() {
Order o = alias(Order.class, "o");
assertEquals("select o from Order as o", select(o).from(Order.class)
.as(o).toString());

assertEquals(
"select o from Order as o where o.shippingAddress.state = 'CA'",
select(o).from(Order.class).as(o).where(
eq(o.getShippingAddress().getState(), "CA")).toString());

assertEquals("select distinct o.shippingAddress.state from Order as o",
select(distinct(o.getShippingAddress().getState())).from(
Order.class).as(o).toString());
}

@Test
public void testPara4thirteen2() {
Order o = alias(Order.class, "o");
LineItem l = alias(in(o.getLineItems()), "l");
assertEquals("select distinct o from Order as o, in(o.lineItems) as l",
select(distinct(o)).from(Order.class).as(o).andFrom(
in(o.getLineItems())).as(l).toString());

assertEquals("select o from Order as o where o.lineItems is not empty",
select(o).from(Order.class).as(o).where(
isNotEmpty(o.getLineItems())).toString());

assertEquals("select o from Order as o where o.lineItems is empty",
select(o).from(Order.class).as(o).where(
isEmpty(o.getLineItems())).toString());

l = alias(o.getLineItems(), "l");
assertEquals(
"select distinct o from Order as o inner join o.lineItems as l where l.shipped = false",
select(distinct(o)).from(Order.class).as(o).innerJoin(
o.getLineItems()).as(l)
.where(eq(l.getShipped(), false)).toString());

String exp = "select o from Order as o where not "
+ "(o.shippingAddress.state = o.billingAddress.state and "
+ "o.shippingAddress.city = o.billingAddress.city and "
+ "o.shippingAddress.street = o.billingAddress.street)";

ConditionalExpression cond = not(paren(eq(
o.getShippingAddress().getState(),
o.getBillingAddress().getState()).and(
eq(o.getShippingAddress().getCity(), o.getBillingAddress()
.getCity())).and(
eq(o.getShippingAddress().getStreet(), o.getBillingAddress()
.getStreet()))));

assertEquals(exp, select(o).from(Order.class).as(o).where(cond)
.toString());

exp = "select distinct o from Order as o inner join o.lineItems as l "
+ "where l.product.productType = 'book' and l.product.name = "
+ "'Applying Enterprise JavaBeans: Component-Based Development for the J2EE Platform'";

String title = "Applying Enterprise JavaBeans: "
+ "Component-Based Development for the J2EE Platform";
assertEquals(exp, select(distinct(o)).from(Order.class).as(o)
.innerJoin(o.getLineItems()).as(l).where(
eq(l.getProduct().getProductType(), "book").and(
eq(l.getProduct().getName(), title)))
.toString());
}

@Test
public void testPara4thirteen3() {
Order o = alias(Order.class, "o");
LineItem l = alias(in(o.getLineItems()), "l");

assertEquals("select distinct o from Order as o, in(o.lineItems) as l "
+ "where l.product.name = ?1", select(distinct(o)).from(
Order.class).as(o).andFrom(in(o.getLineItems())).as(l).where(
eq(l.getProduct().getName(), param(1, String.class)))
.toString());
}
}
Show details Hide details

Change log

r90 by eric.bottard on Nov 27, 2008   Diff
[maven-release-plugin]  copy for tag
liquidform.jpa-1.0.0-rc1
Go to: 
Sign in to write a code review

Older revisions

r84 by eric.bottard on Nov 24, 2008   Diff
Add support for fecth joins on
Collection<T>
r80 by eric.bottard on Nov 19, 2008   Diff
doco++
r74 by eric.bottard on Nov 10, 2008   Diff
Renamed use() to alias()
All revisions of this file

File info

Size: 14982 bytes, 402 lines

File properties

svn:mime-type
text/plain
Hosted by Google Code