/tests/com/google/appengine/datanucleus/jpa/JPANonWritableFieldsTest.java
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 */ 16package com.google.appengine.datanucleus.jpa; 17 18import com.google.appengine.api.datastore.Entity; 19import com.google.appengine.api.datastore.EntityNotFoundException; 20import com.google.appengine.api.datastore.KeyFactory; 21import com.google.appengine.datanucleus.test.jpa.HasNonWritableFieldsJPA; 22 23/** 24 * @author Max Ross <max.ross@gmail.com> 25 */ 26public class JPANonWritableFieldsTest extends JPATestCase { 27 28 public void testInsert() throws EntityNotFoundException { 29 HasNonWritableFieldsJPA pojo = new HasNonWritableFieldsJPA(); 30 pojo.setNotInsertable("insert"); 31 pojo.setNotUpdatable("update"); 32 pojo.setNotWritable("write"); 33 beginTxn(); 34 em.persist(pojo); 35 commitTxn(); 36 em.clear(); 37 if (em.getEntityManagerFactory().getCache() != null) 38 { 39 em.getEntityManagerFactory().getCache().evictAll(); 40 } 41 42 beginTxn(); 43 pojo = em.find(pojo.getClass(), pojo.getId()); 44 assertNull(pojo.getNotInsertable()); 45 assertEquals("update", pojo.getNotUpdatable()); 46 assertNull(pojo.getNotWritable()); 47 commitTxn(); 48 Entity entity = ds.get(KeyFactory.createKey(kindForObject(pojo), pojo.getId())); 49 assertEquals(1, entity.getProperties().size()); 50 assertEquals("update", entity.getProperty("notUpdatable")); 51 } 52 53 public void testUpdate() throws EntityNotFoundException { 54 HasNonWritableFieldsJPA pojo = new HasNonWritableFieldsJPA(); 55 pojo.setNotInsertable("insert"); 56 pojo.setNotUpdatable("update"); 57 pojo.setNotWritable("write"); 58 beginTxn(); 59 em.persist(pojo); 60 commitTxn(); 61 em.clear(); 62 if (em.getEntityManagerFactory().getCache() != null) 63 { 64 em.getEntityManagerFactory().getCache().evictAll(); 65 } 66 67 beginTxn(); 68 pojo = em.find(pojo.getClass(), pojo.getId()); 69 pojo.setNotInsertable("insert2"); 70 pojo.setNotUpdatable("update2"); 71 pojo.setNotWritable("write2"); 72 commitTxn(); 73 Entity entity = ds.get(KeyFactory.createKey(kindForObject(pojo), pojo.getId())); 74 assertEquals(2, entity.getProperties().size()); 75 assertEquals("insert2", entity.getProperty("notInsertable")); 76 assertEquals("update", entity.getProperty("notUpdatable")); 77 em.clear(); 78 if (em.getEntityManagerFactory().getCache() != null) 79 { 80 em.getEntityManagerFactory().getCache().evictAll(); 81 } 82 83 beginTxn(); 84 pojo = em.find(pojo.getClass(), pojo.getId()); 85 assertEquals("insert2", pojo.getNotInsertable()); 86 assertEquals("update", pojo.getNotUpdatable()); 87 assertNull(pojo.getNotWritable()); 88 commitTxn(); 89 } 90}