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

http://datanucleus-appengine.googlecode.com/ · Java · 90 lines · 65 code · 7 blank · 18 comment · 6 complexity · 9b710a839c57b27f606aefb3ddfd7f39 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.appengine.datanucleus.jpa;
  17. import com.google.appengine.api.datastore.Entity;
  18. import com.google.appengine.api.datastore.EntityNotFoundException;
  19. import com.google.appengine.api.datastore.KeyFactory;
  20. import com.google.appengine.datanucleus.test.jpa.HasNonWritableFieldsJPA;
  21. /**
  22. * @author Max Ross <max.ross@gmail.com>
  23. */
  24. public class JPANonWritableFieldsTest extends JPATestCase {
  25. public void testInsert() throws EntityNotFoundException {
  26. HasNonWritableFieldsJPA pojo = new HasNonWritableFieldsJPA();
  27. pojo.setNotInsertable("insert");
  28. pojo.setNotUpdatable("update");
  29. pojo.setNotWritable("write");
  30. beginTxn();
  31. em.persist(pojo);
  32. commitTxn();
  33. em.clear();
  34. if (em.getEntityManagerFactory().getCache() != null)
  35. {
  36. em.getEntityManagerFactory().getCache().evictAll();
  37. }
  38. beginTxn();
  39. pojo = em.find(pojo.getClass(), pojo.getId());
  40. assertNull(pojo.getNotInsertable());
  41. assertEquals("update", pojo.getNotUpdatable());
  42. assertNull(pojo.getNotWritable());
  43. commitTxn();
  44. Entity entity = ds.get(KeyFactory.createKey(kindForObject(pojo), pojo.getId()));
  45. assertEquals(1, entity.getProperties().size());
  46. assertEquals("update", entity.getProperty("notUpdatable"));
  47. }
  48. public void testUpdate() throws EntityNotFoundException {
  49. HasNonWritableFieldsJPA pojo = new HasNonWritableFieldsJPA();
  50. pojo.setNotInsertable("insert");
  51. pojo.setNotUpdatable("update");
  52. pojo.setNotWritable("write");
  53. beginTxn();
  54. em.persist(pojo);
  55. commitTxn();
  56. em.clear();
  57. if (em.getEntityManagerFactory().getCache() != null)
  58. {
  59. em.getEntityManagerFactory().getCache().evictAll();
  60. }
  61. beginTxn();
  62. pojo = em.find(pojo.getClass(), pojo.getId());
  63. pojo.setNotInsertable("insert2");
  64. pojo.setNotUpdatable("update2");
  65. pojo.setNotWritable("write2");
  66. commitTxn();
  67. Entity entity = ds.get(KeyFactory.createKey(kindForObject(pojo), pojo.getId()));
  68. assertEquals(2, entity.getProperties().size());
  69. assertEquals("insert2", entity.getProperty("notInsertable"));
  70. assertEquals("update", entity.getProperty("notUpdatable"));
  71. em.clear();
  72. if (em.getEntityManagerFactory().getCache() != null)
  73. {
  74. em.getEntityManagerFactory().getCache().evictAll();
  75. }
  76. beginTxn();
  77. pojo = em.find(pojo.getClass(), pojo.getId());
  78. assertEquals("insert2", pojo.getNotInsertable());
  79. assertEquals("update", pojo.getNotUpdatable());
  80. assertNull(pojo.getNotWritable());
  81. commitTxn();
  82. }
  83. }