001: /*
002: * TestDateQueries.java
003: *
004: * Created on October 10, 2006, 1:28 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: /*
011: * Licensed to the Apache Software Foundation (ASF) under one
012: * or more contributor license agreements. See the NOTICE file
013: * distributed with this work for additional information
014: * regarding copyright ownership. The ASF licenses this file
015: * to you under the Apache License, Version 2.0 (the
016: * "License"); you may not use this file except in compliance
017: * with the License. You may obtain a copy of the License at
018: *
019: * http://www.apache.org/licenses/LICENSE-2.0
020: *
021: * Unless required by applicable law or agreed to in writing,
022: * software distributed under the License is distributed on an
023: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
024: * KIND, either express or implied. See the License for the
025: * specific language governing permissions and limitations
026: * under the License.
027: */
028: package org.apache.openjpa.persistence.kernel;
029:
030: import java.text.SimpleDateFormat;
031: import java.util.Collection;
032: import java.util.Date;
033: import java.util.Iterator;
034: import java.util.List;
035:
036: import org.apache.openjpa.persistence.kernel.common.apps.AllFieldTypesTest;
037:
038: import org.apache.openjpa.persistence.OpenJPAEntityManager;
039: import org.apache.openjpa.persistence.OpenJPAQuery;
040:
041: public class TestDateQueries extends BaseKernelTest {
042:
043: private OpenJPAEntityManager _pm = null;
044: private Date _date = null;
045: private Date _before = null;
046: private Date _after = null;
047:
048: /**
049: * Creates a new instance of TestDateQueries
050: */
051: public TestDateQueries() {
052: }
053:
054: public TestDateQueries(String name) {
055: super (name);
056: }
057:
058: public void setUp() throws Exception {
059: super .setUp();
060:
061: SimpleDateFormat sdf = new SimpleDateFormat("MMMMM dd, yyyy");
062: _date = sdf.parse("April 26, 1978");
063: _before = sdf.parse("April 25, 1978");
064: _after = sdf.parse("April 27, 1978");
065: _pm = getPM();
066:
067: // delete all existing instances
068: deleteAll(AllFieldTypesTest.class);
069:
070: // create some instances to query on
071: startTx(_pm);
072: AllFieldTypesTest test = new AllFieldTypesTest();
073: test.setTestDate(_date);
074: _pm.persist(test);
075:
076: test = new AllFieldTypesTest();
077: test.setTestDate(_before);
078: _pm.persist(test);
079:
080: test = new AllFieldTypesTest();
081: test.setTestDate(_after);
082: _pm.persist(test);
083: endTx(_pm);
084: }
085:
086: public void testEquals() {
087: Collection vals = executeQuery("testDate = :date");
088: assertEquals(1, vals.size());
089: assertEquals(_date,
090: ((AllFieldTypesTest) vals.iterator().next())
091: .getTestDate());
092: }
093:
094: public void testNotEquals() {
095: Collection vals = executeQuery("testDate <> :date");
096: assertEquals(2, vals.size());
097: }
098:
099: public void testBefore() {
100: Collection vals = executeQuery("testDate < :date");
101: assertEquals(1, vals.size());
102: assertEquals(_before, ((AllFieldTypesTest) vals.iterator()
103: .next()).getTestDate());
104: }
105:
106: public void testAfter() {
107: Collection vals = executeQuery("testDate > :date");
108: assertEquals(1, vals.size());
109: assertEquals(_after, ((AllFieldTypesTest) vals.iterator()
110: .next()).getTestDate());
111: }
112:
113: public void testOrderBy() {
114: String query = "SELECT o FROM AllFieldTypesTest o ORDER BY o.testDate ASC";
115: OpenJPAQuery q = _pm.createQuery(query);
116: List vals = q.getResultList();
117: assertEquals(3, vals.size());
118:
119: Iterator i = vals.iterator();
120: assertEquals(_before, ((AllFieldTypesTest) i.next())
121: .getTestDate());
122: assertEquals(_date, ((AllFieldTypesTest) i.next())
123: .getTestDate());
124: assertEquals(_after, ((AllFieldTypesTest) i.next())
125: .getTestDate());
126: }
127:
128: private List executeQuery(String filter) {
129: String query = "SELECT o FROM AllFieldTypesTest o WHERE o."
130: + filter;
131: OpenJPAQuery q = _pm.createQuery(query);
132: q.setParameter("date", _date);
133: return q.getResultList();
134: }
135: }
|