1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activespace.query;
19
20 import org.codehaus.activespace.cache.TransactionalCacheTestSupport;
21
22 import javax.cache.Cache;
23 import java.util.List;
24
25 /***
26 * @version $Revision: 1.2 $
27 */
28 public class QueryTest extends TransactionalCacheTestSupport {
29
30 private Cache cache;
31 private int personCounter;
32
33 public void testQuery() throws Exception {
34
35
36 assertSelect("people.values().findAll { it.country == 'USA' }", 3);
37 assertSelect("people.values().findAll { it.country == 'USA' }.collect { it.name }", 3);
38 assertSelect("people.values().findAll { it.country == 'USA' && it.name != 'Brian'}.collect { it.name }", 2);
39 assertSelect("people.values().findAll { it.country == 'USA' }.collect { it.name }.sort()", 3);
40
41
42 assertSelect("people.findAll { it.key < 4 && it.value.country == 'UK' }.collect { it.value }", 1);
43 }
44
45 protected void assertSelect(String query, int count) {
46 System.out.println("About to execute query: " + query);
47 List answer = cacheManager.select(query);
48 assertEquals("size: " + answer, count, answer.size());
49
50 System.out.println("Found results: " + answer);
51 System.out.println();
52 }
53
54 protected void setUp() throws Exception {
55 super.setUp();
56
57
58 cache = cacheManager.createTransactionalCache("people");
59 addPerson("James", "London", "UK");
60 addPerson("Hiram", "Florida", "USA");
61 addPerson("Jeremy", "LA", "USA");
62 addPerson("Brian", "Dunno", "USA");
63
64 cacheManager.commit();
65 }
66
67 private void addPerson(String name, String city, String country) {
68 Customer customer = new Customer(++personCounter, name, city, country);
69 cache.put(new Integer(customer.getId()), customer);
70 }
71 }