PageRenderTime 68ms CodeModel.GetById 48ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/com/google/appengine/datanucleus/jpa/JPATemporalTest.java

http://datanucleus-appengine.googlecode.com/
Java | 80 lines | 52 code | 9 blank | 19 comment | 0 complexity | 3bd094ed07d96a0061600e5524765951 MD5 | raw file
 1/**********************************************************************
 2Copyright (c) 2011 Google Inc.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15**********************************************************************/
16package com.google.appengine.datanucleus.jpa;
17
18import java.util.Calendar;
19import java.util.Date;
20
21import javax.persistence.Query;
22
23import junit.framework.Assert;
24
25import com.google.appengine.datanucleus.test.jpa.TemporalHolder;
26
27/**
28 * Tests for storage of temporal types
29 */
30public class JPATemporalTest extends JPATestCase {
31
32  @SuppressWarnings("deprecation")
33public void testInsert_IdGen() {
34    int year = 2010;
35    int month = 5;
36    int day_of_month = 15;
37    int hour_of_day = 5;
38    int minute = 10;
39    int second = 45;
40
41    TemporalHolder holder = new TemporalHolder(1);
42    Calendar cal = Calendar.getInstance();
43    cal.set(Calendar.YEAR, year);
44    cal.set(Calendar.MONTH, month);
45    cal.set(Calendar.DAY_OF_MONTH, day_of_month);
46    cal.set(Calendar.HOUR_OF_DAY, hour_of_day);
47    cal.set(Calendar.MINUTE, minute);
48    cal.set(Calendar.SECOND, second);
49    cal.set(Calendar.MILLISECOND, 0);
50    holder.setDateField(cal.getTime());
51    holder.setTimestampField(cal.getTime());
52    holder.setTimeField(cal.getTime());
53
54    beginTxn();
55    em.persist(holder);
56    commitTxn();
57    em.clear(); // Clean L1 cache to enforce load from datastore
58
59    // Retrieve and check fields
60    beginTxn();
61    Query q = em.createQuery("SELECT h FROM TemporalHolder h WHERE id = 1");
62    TemporalHolder h = (TemporalHolder)q.getSingleResult();
63    Date dateField = h.getDateField();
64    Assert.assertEquals("Year of Date incorrect", year, dateField.getYear()+1900);
65    Assert.assertEquals("Month of Date incorrect", month, dateField.getMonth());
66    Assert.assertEquals("Day of Date incorrect", day_of_month, dateField.getDate());
67    Date timeField = h.getTimeField();
68    Assert.assertEquals("Hour of Time incorrect", hour_of_day, timeField.getHours());
69    Assert.assertEquals("Minute of Time incorrect", minute, timeField.getMinutes());
70    Assert.assertEquals("Second of Time incorrect", second, timeField.getSeconds());
71    Date timestampField = h.getTimestampField();
72    Assert.assertEquals("Year of Timestamp incorrect", year, timestampField.getYear()+1900);
73    Assert.assertEquals("Month of Timestamp incorrect", month, timestampField.getMonth());
74    Assert.assertEquals("Day of Timestamp incorrect", day_of_month, timestampField.getDate());
75    Assert.assertEquals("Hour of Timestamp incorrect", hour_of_day, timestampField.getHours());
76    Assert.assertEquals("Minute of Timestamp incorrect", minute, timestampField.getMinutes());
77    Assert.assertEquals("Second of Timestamp incorrect", second, timestampField.getSeconds());
78    commitTxn();
79  }
80}