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