001: /*
002: * TestCalendarFields.java
003: *
004: * Created on October 9, 2006, 6:12 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.util.Calendar;
031: import java.util.Date;
032: import java.util.TimeZone;
033:
034: import org.apache.openjpa.persistence.kernel.common.apps.CalendarFields;
035:
036: import org.apache.openjpa.kernel.OpenJPAStateManager;
037: import org.apache.openjpa.lib.util.JavaVersions;
038: import org.apache.openjpa.persistence.OpenJPAEntityManager;
039:
040: public class TestCalendarFields extends BaseKernelTest {
041:
042: /**
043: * Creates a new instance of TestCalendarFields
044: */
045: public TestCalendarFields(String name) {
046: super (name);
047: }
048:
049: public void setUp() {
050: deleteAll(CalendarFields.class);
051: }
052:
053: public void testFieldDefaultTimeZone() {
054: CalendarFields cal = new CalendarFields();
055:
056: OpenJPAEntityManager pm;
057:
058: pm = getPM();
059: startTx(pm);
060: cal.setSingapore(Calendar.getInstance(TimeZone
061: .getTimeZone("America/New_York")));
062: pm.persist(cal);
063: int id = cal.getId();
064: endTx(pm);
065: endEm(pm);
066:
067: pm = getPM();
068: cal = (CalendarFields) pm.find(CalendarFields.class, id);
069: assertEquals(TimeZone.getTimeZone("Asia/Singapore"), cal
070: .getSingapore().getTimeZone());
071: endEm(pm);
072: }
073:
074: public void testTimeZoneEquals() {
075: CalendarFields c1 = new CalendarFields();
076: CalendarFields c2 = new CalendarFields();
077: assertTimeZonesEquals(c1, c2);
078:
079: OpenJPAEntityManager pm;
080:
081: pm = getPM();
082: startTx(pm);
083: pm.persist(c2);
084: int id2 = c2.getId();
085: assertTimeZonesEquals(c1, c2);
086: endTx(pm);
087: assertTimeZonesEquals(c1, c2);
088: endEm(pm);
089:
090: pm = getPM();
091: c2 = (CalendarFields) pm.find(CalendarFields.class, id2);
092: assertTimeZonesEquals(c1, c2);
093: assertTimeZonesEquals(c1, (CalendarFields) pm.detach(c2));
094: endEm(pm);
095: }
096:
097: public void testCalendarQuery() {
098: long time = 1136660560572L;
099:
100: CalendarFields cal = new CalendarFields();
101:
102: OpenJPAEntityManager pm;
103:
104: pm = getPM();
105: startTx(pm);
106: cal.getSingapore().setTime(new Date(time));
107: pm.persist(cal);
108: int id = cal.getId();
109: endTx(pm);
110: endEm(pm);
111:
112: pm = getPM();
113:
114: //FIXME jthomas
115: Date date = new Date(time + 100000);
116: Calendar cals = Calendar.getInstance();
117: cals.setTime(date);
118:
119: String query = "SELECT o FROM CalendarFields o WHERE o.singapore < :cal";
120: int size = pm.createQuery(query).setParameter("cal", cals)
121: .getResultList().size();
122:
123: assertEquals(1, size);
124:
125: // assertSize(0, pm.newQuery(CalendarFields.class, "singapore < :date").execute(new Date(time - 100000)));
126: //
127: // assertSize(0, pm.newQuery(CalendarFields.class, "singapore > :date").execute(new Date(time + 100000)));
128: // assertSize(1, pm.newQuery(CalendarFields.class, "singapore > :date").execute(new Date(time - 100000)));
129: //
130: // assertSize(1, pm.newQuery(CalendarFields.class, "singapore < :date").execute(newCalendar(new Date(time + 100000), null)));
131: // assertSize(0, pm.newQuery(CalendarFields.class, "singapore < :date").execute(newCalendar(new Date(time - 100000), null)));
132: //
133: // assertSize(0, pm.newQuery(CalendarFields.class, "singapore > :date").execute(newCalendar(new Date(time + 100000), null)));
134: // assertSize(1, pm.newQuery(CalendarFields.class, "singapore > :date").execute(newCalendar(new Date(time - 100000), null)));
135:
136: endEm(pm);
137: }
138:
139: private static Calendar newCalendar(Date date, String tz) {
140: Calendar cal = Calendar.getInstance(tz == null ? TimeZone
141: .getDefault() : TimeZone.getTimeZone(tz));
142: cal.setTime(date);
143: return cal;
144: }
145:
146: public void testMutateCalendarDirties() {
147: CalendarFields c1 = new CalendarFields();
148:
149: OpenJPAEntityManager pm;
150:
151: pm = getPM();
152: startTx(pm);
153: pm.persist(c1);
154: int id = c1.getId();
155: endTx(pm);
156: endEm(pm);
157:
158: pm = getPM();
159: c1 = pm.find(CalendarFields.class, id);
160:
161: startTx(pm);
162:
163: OpenJPAStateManager sm = getStateManager(c1, pm);
164:
165: assertFalse(sm.getDirty().get(
166: sm.getMetaData().getField("gmt").getIndex()));
167:
168: // test setting to same value doesn't dirty the field
169: /*
170: setTimeInMillis(c1.getGmtc1.getGmt().getTime().getTime());
171: c1.getGmt().setTime(c1.getGmt().getTime());
172: assertFalse(sm.getDirty().get(sm.getMetaData().
173: getField("gmt").getIndex()));
174: */
175:
176: // test changing time
177: setTimeInMillis(c1.getGmt(), 12345);
178: assertTrue(sm.getDirty().get(
179: sm.getMetaData().getField("gmt").getIndex()));
180:
181: assertFalse(sm.getDirty().get(
182: sm.getMetaData().getField("newYork").getIndex()));
183: // test mutate via "add()" method
184: c1.getNewYork().add(Calendar.SECOND, -1);
185: assertTrue(sm.getDirty().get(
186: sm.getMetaData().getField("newYork").getIndex()));
187:
188: assertFalse(sm.getDirty().get(
189: sm.getMetaData().getField("berlin").getIndex()));
190: // test mutate via "setTimeZone()" method
191: c1.getBerlin().setTimeZone(TimeZone.getTimeZone("GMT"));
192: assertTrue(sm.getDirty().get(
193: sm.getMetaData().getField("berlin").getIndex()));
194:
195: // Calendar.set can only be subclassed in JDK 1.4+ (it is final in
196: // 1.3), so we only run this test in JDK 1.4+
197: if (JavaVersions.VERSION >= 4) {
198: assertFalse(sm.getDirty().get(
199: sm.getMetaData().getField("singapore").getIndex()));
200: // test mutate via "set()" method
201: c1.getSingapore().set(Calendar.YEAR, 1998);
202: assertTrue(sm.getDirty().get(
203: sm.getMetaData().getField("singapore").getIndex()));
204: }
205:
206: assertFalse(sm.getDirty().get(
207: sm.getMetaData().getField("pacific").getIndex()));
208: // test mutate via "roll()" method
209: c1.getPacific().roll(Calendar.YEAR, 5);
210: assertTrue(sm.getDirty().get(
211: sm.getMetaData().getField("pacific").getIndex()));
212:
213: endTx(pm);
214: endEm(pm);
215: }
216:
217: private static void setTimeInMillis(Calendar cal, long millis) {
218: // "setTimeInMillis" is protected in JDK 1.3, put public in 1.4 & 1.5
219: try {
220: // Equivalent to: cal.setTimeInMillis (millis);
221: cal.getClass().getMethod("setTimeInMillis",
222: new Class[] { long.class }).invoke(cal,
223: new Object[] { new Long(millis) });
224: } catch (Exception e) {
225: cal.setTime(new Date(millis));
226: }
227: }
228:
229: private void assertTimeZonesEquals(CalendarFields c1,
230: CalendarFields c2) {
231: for (int i = 0; i < c1.getCalendars().size(); i++) {
232: Calendar cal1 = (Calendar) c1.getCalendars().get(i);
233: Calendar cal2 = (Calendar) c2.getCalendars().get(i);
234:
235: if (cal1 != null && cal2 != null)
236: assertEquals(cal1.getTimeZone().getID(), cal2
237: .getTimeZone().getID());
238: }
239: }
240: }
|