/tests/com/google/appengine/datanucleus/jdo/JDOUpdateTest.java

http://datanucleus-appengine.googlecode.com/ · Java · 859 lines · 723 code · 86 blank · 50 comment · 0 complexity · fcf1f74bae4b317d0ab9c511e815bae1 MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2009 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.jdo;
  14. import com.google.appengine.api.datastore.Entity;
  15. import com.google.appengine.api.datastore.EntityNotFoundException;
  16. import com.google.appengine.api.datastore.Key;
  17. import com.google.appengine.api.datastore.KeyFactory;
  18. import com.google.appengine.datanucleus.TestUtils;
  19. import com.google.appengine.datanucleus.Utils;
  20. import com.google.appengine.datanucleus.test.jdo.Flight;
  21. import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorStringPkJDO;
  22. import com.google.appengine.datanucleus.test.jdo.HasKeyPkJDO;
  23. import com.google.appengine.datanucleus.test.jdo.HasMultiValuePropsJDO;
  24. import com.google.appengine.datanucleus.test.jdo.HasStringAncestorStringPkJDO;
  25. import com.google.appengine.datanucleus.test.jdo.HasVersionWithFieldJDO;
  26. import com.google.appengine.datanucleus.test.jdo.Name;
  27. import com.google.appengine.datanucleus.test.jdo.NullDataJDO;
  28. import com.google.appengine.datanucleus.test.jdo.Person;
  29. import java.util.ArrayList;
  30. import java.util.HashSet;
  31. import java.util.LinkedHashSet;
  32. import java.util.LinkedList;
  33. import java.util.List;
  34. import java.util.Set;
  35. import java.util.SortedSet;
  36. import java.util.TreeSet;
  37. import javax.jdo.JDOFatalUserException;
  38. import javax.jdo.JDOHelper;
  39. import javax.jdo.JDOOptimisticVerificationException;
  40. /**
  41. * @author Erick Armbrust <earmbrust@google.com>
  42. * @author Max Ross <maxr@google.com>
  43. */
  44. public class JDOUpdateTest extends JDOTestCase {
  45. private static final String DEFAULT_VERSION_PROPERTY_NAME = "VERSION";
  46. public void testSimpleUpdate() throws EntityNotFoundException {
  47. Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
  48. String keyStr = KeyFactory.keyToString(key);
  49. beginTxn();
  50. Flight flight = pm.getObjectById(Flight.class, keyStr);
  51. assertEquals(keyStr, flight.getId());
  52. assertEquals("yam", flight.getOrigin());
  53. assertEquals("bam", flight.getDest());
  54. assertEquals("1", flight.getName());
  55. assertEquals(1, flight.getYou());
  56. assertEquals(2, flight.getMe());
  57. flight.setName("2");
  58. commitTxn();
  59. Entity flightCheck = ds.get(key);
  60. assertEquals("yam", flightCheck.getProperty("origin"));
  61. assertEquals("bam", flightCheck.getProperty("dest"));
  62. assertEquals("2", flightCheck.getProperty("name"));
  63. assertEquals(1L, flightCheck.getProperty("you"));
  64. assertEquals(2L, flightCheck.getProperty("me"));
  65. // verify that the version got bumped
  66. assertEquals(2L,
  67. flightCheck.getProperty(DEFAULT_VERSION_PROPERTY_NAME));
  68. }
  69. public void testSimpleUpdateWithNamedKey() throws EntityNotFoundException {
  70. Key key = ds.put(Flight.newFlightEntity("named key", "1", "yam", "bam", 1, 2));
  71. String keyStr = KeyFactory.keyToString(key);
  72. beginTxn();
  73. Flight flight = pm.getObjectById(Flight.class, keyStr);
  74. assertEquals(keyStr, flight.getId());
  75. assertEquals("yam", flight.getOrigin());
  76. assertEquals("bam", flight.getDest());
  77. assertEquals("1", flight.getName());
  78. assertEquals(1, flight.getYou());
  79. assertEquals(2, flight.getMe());
  80. flight.setName("2");
  81. commitTxn();
  82. Entity flightCheck = ds.get(key);
  83. assertEquals("yam", flightCheck.getProperty("origin"));
  84. assertEquals("bam", flightCheck.getProperty("dest"));
  85. assertEquals("2", flightCheck.getProperty("name"));
  86. assertEquals(1L, flightCheck.getProperty("you"));
  87. assertEquals(2L, flightCheck.getProperty("me"));
  88. // verify that the version got bumped
  89. assertEquals(2L,
  90. flightCheck.getProperty(DEFAULT_VERSION_PROPERTY_NAME));
  91. assertEquals("named key", flightCheck.getKey().getName());
  92. }
  93. public void testOptimisticLocking_Update_NoField() {
  94. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  95. Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
  96. Key key = ds.put(flightEntity);
  97. String keyStr = KeyFactory.keyToString(key);
  98. beginTxn();
  99. Flight flight = pm.getObjectById(Flight.class, keyStr);
  100. flight.setName("2");
  101. flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
  102. // we update the flight directly in the datastore right before commit
  103. ds.put(flightEntity);
  104. try {
  105. commitTxn();
  106. fail("expected optimistic exception");
  107. } catch (JDOOptimisticVerificationException jove) {
  108. // good
  109. }
  110. }
  111. public void testOptimisticLocking_Attach_NoField() {
  112. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  113. Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
  114. Key key = ds.put(flightEntity);
  115. String keyStr = KeyFactory.keyToString(key);
  116. beginTxn();
  117. Flight flight = pm.detachCopy(pm.getObjectById(Flight.class, keyStr));
  118. commitTxn();
  119. beginTxn();
  120. flight.setName("2");
  121. pm.makePersistent(flight);
  122. flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
  123. // we update the flight directly in the datastore right before commit
  124. ds.put(flightEntity);
  125. try {
  126. commitTxn();
  127. fail("expected optimistic exception");
  128. } catch (JDOOptimisticVerificationException jove) {
  129. // good
  130. }
  131. }
  132. public void testOptimisticLocking_Delete_NoField() {
  133. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  134. Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
  135. Key key = ds.put(flightEntity);
  136. String keyStr = KeyFactory.keyToString(key);
  137. beginTxn();
  138. Flight flight = pm.getObjectById(Flight.class, keyStr);
  139. flight.setName("2");
  140. flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
  141. // we remove the flight from the datastore right before commit
  142. ds.delete(key);
  143. try {
  144. commitTxn();
  145. fail("expected optimistic exception");
  146. } catch (JDOOptimisticVerificationException jove) {
  147. // good
  148. }
  149. }
  150. public void testOptimisticLocking_Update_HasVersionField() {
  151. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  152. Entity entity = new Entity(HasVersionWithFieldJDO.class.getSimpleName());
  153. entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 1L);
  154. Key key = ds.put(entity);
  155. String keyStr = KeyFactory.keyToString(key);
  156. beginTxn();
  157. HasVersionWithFieldJDO hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
  158. hvwf.setValue("value");
  159. commitTxn();
  160. beginTxn();
  161. hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
  162. assertEquals(2L, hvwf.getVersion());
  163. // make sure the version gets bumped
  164. entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 3L);
  165. hvwf.setValue("another value");
  166. // we update the entity directly in the datastore right before commit
  167. entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 7L);
  168. ds.put(entity);
  169. try {
  170. commitTxn();
  171. fail("expected optimistic exception");
  172. } catch (JDOOptimisticVerificationException jove) {
  173. // good
  174. }
  175. // make sure the version didn't change on the model object
  176. assertEquals(2L, JDOHelper.getVersion(hvwf));
  177. }
  178. public void testOptimisticLocking_Delete_HasVersionField() {
  179. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  180. Entity entity = new Entity(HasVersionWithFieldJDO.class.getSimpleName());
  181. entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 1L);
  182. Key key = ds.put(entity);
  183. String keyStr = KeyFactory.keyToString(key);
  184. beginTxn();
  185. HasVersionWithFieldJDO hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
  186. // delete the entity in the datastore right before we commit
  187. ds.delete(key);
  188. hvwf.setValue("value");
  189. try {
  190. commitTxn();
  191. fail("expected optimistic exception");
  192. } catch (JDOOptimisticVerificationException jove) {
  193. // good
  194. }
  195. // make sure the version didn't change on the model object
  196. assertEquals(1L, JDOHelper.getVersion(hvwf));
  197. }
  198. public void testNonTransactionalUpdate() throws EntityNotFoundException {
  199. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  200. Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
  201. Flight f = pm.getObjectById(Flight.class, KeyFactory.keyToString(key));
  202. f.setYou(77);
  203. pm.close();
  204. Entity flightEntity = ds.get(key);
  205. assertEquals(77L, flightEntity.getProperty("you"));
  206. pm = pmf.getPersistenceManager();
  207. }
  208. public void testChangeStringPK_SetNonKeyString() throws EntityNotFoundException {
  209. Key key = ds.put(Flight.newFlightEntity("named key", "1", "yam", "bam", 1, 2));
  210. String keyStr = KeyFactory.keyToString(key);
  211. beginTxn();
  212. Flight flight = pm.getObjectById(Flight.class, keyStr);
  213. assertEquals(keyStr, flight.getId());
  214. assertEquals("yam", flight.getOrigin());
  215. assertEquals("bam", flight.getDest());
  216. assertEquals("1", flight.getName());
  217. assertEquals(1, flight.getYou());
  218. assertEquals(2, flight.getMe());
  219. flight.setName("2");
  220. flight.setId("foo");
  221. try {
  222. commitTxn();
  223. fail("expected exception");
  224. } catch (JDOFatalUserException e) {
  225. // good
  226. rollbackTxn();
  227. }
  228. Entity flightCheck = ds.get(key);
  229. assertEquals("yam", flightCheck.getProperty("origin"));
  230. assertEquals("bam", flightCheck.getProperty("dest"));
  231. assertEquals("1", flightCheck.getProperty("name"));
  232. assertEquals(1L, flightCheck.getProperty("you"));
  233. assertEquals(2L, flightCheck.getProperty("me"));
  234. // verify that the version got bumped
  235. assertEquals(1L,
  236. flightCheck.getProperty(DEFAULT_VERSION_PROPERTY_NAME));
  237. assertEquals("named key", flightCheck.getKey().getName());
  238. }
  239. public void testChangeStringPK_SetNull() throws EntityNotFoundException {
  240. Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
  241. beginTxn();
  242. Flight f = pm.getObjectById(Flight.class, KeyFactory.keyToString(key));
  243. f.setId(null);
  244. pm.makePersistent(f);
  245. try {
  246. commitTxn();
  247. fail("expected exception");
  248. } catch (JDOFatalUserException e) {
  249. // good
  250. rollbackTxn();
  251. }
  252. assertEquals(1, countForClass(Flight.class));
  253. }
  254. public void testChangePK_SetKeyString() throws EntityNotFoundException {
  255. Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
  256. beginTxn();
  257. Flight f = pm.getObjectById(Flight.class, KeyFactory.keyToString(key));
  258. f.setId(KeyFactory.keyToString(KeyFactory.createKey(Flight.class.getSimpleName(), "yar")));
  259. f.setYou(77);
  260. pm.makePersistent(f);
  261. try {
  262. commitTxn();
  263. fail("expected exception");
  264. } catch (JDOFatalUserException e) {
  265. // good
  266. rollbackTxn();
  267. }
  268. assertEquals(1, countForClass(Flight.class));
  269. Entity e = ds.get(key);
  270. assertEquals(1L, e.getProperty("you"));
  271. assertEquals(1, countForClass(Flight.class));
  272. }
  273. public void testChangeKeyPK_SetDifferentKey() throws EntityNotFoundException {
  274. Key key = ds.put(new Entity(HasKeyPkJDO.class.getSimpleName()));
  275. beginTxn();
  276. HasKeyPkJDO pojo = pm.getObjectById(HasKeyPkJDO.class, key);
  277. pojo.setKey(KeyFactory.createKey(HasKeyPkJDO.class.getSimpleName(), 33));
  278. try {
  279. commitTxn();
  280. fail("expected exception");
  281. } catch (JDOFatalUserException e) {
  282. // good
  283. rollbackTxn();
  284. }
  285. assertEquals(1, countForClass(HasKeyPkJDO.class));
  286. }
  287. public void testChangeKeyPK_SetNull() throws EntityNotFoundException {
  288. Key key = ds.put(new Entity(HasKeyPkJDO.class.getSimpleName()));
  289. beginTxn();
  290. HasKeyPkJDO pojo = pm.getObjectById(HasKeyPkJDO.class, key);
  291. pojo.setKey(null);
  292. try {
  293. commitTxn();
  294. fail("expected exception");
  295. } catch (JDOFatalUserException e) {
  296. // good
  297. rollbackTxn();
  298. }
  299. assertEquals(1, countForClass(HasKeyPkJDO.class));
  300. }
  301. public void testUpdateList_Add() throws EntityNotFoundException {
  302. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  303. List<String> list = Utils.newArrayList("a", "b");
  304. pojo.setStrList(list);
  305. beginTxn();
  306. pm.makePersistent(pojo);
  307. commitTxn();
  308. pm.close();
  309. pm = pmf.getPersistenceManager();
  310. beginTxn();
  311. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  312. pojo.getStrList().add("zoom");
  313. commitTxn();
  314. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  315. assertEquals(3, ((List<?>)e.getProperty("strList")).size());
  316. }
  317. public void testUpdateList_Reset() throws EntityNotFoundException {
  318. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  319. List<String> list = Utils.newArrayList("a", "b");
  320. pojo.setStrList(list);
  321. beginTxn();
  322. pm.makePersistent(pojo);
  323. commitTxn();
  324. pm.close();
  325. pm = pmf.getPersistenceManager();
  326. beginTxn();
  327. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  328. list = Utils.newArrayList("a", "b", "zoom");
  329. pojo.setStrList(list);
  330. commitTxn();
  331. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  332. assertEquals(3, ((List<?>)e.getProperty("strList")).size());
  333. }
  334. public void testUpdateCollection_Add() throws EntityNotFoundException {
  335. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  336. List<Integer> list = Utils.newArrayList(2, 3);
  337. pojo.setIntColl(list);
  338. beginTxn();
  339. pm.makePersistent(pojo);
  340. commitTxn();
  341. pm.close();
  342. pm = pmf.getPersistenceManager();
  343. beginTxn();
  344. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  345. pojo.getIntColl().add(4);
  346. commitTxn();
  347. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  348. assertEquals(3, ((List<?>)e.getProperty("intColl")).size());
  349. }
  350. public void testUpdateCollection_Reset() throws EntityNotFoundException {
  351. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  352. List<Integer> list = Utils.newArrayList(2, 3);
  353. pojo.setIntColl(list);
  354. beginTxn();
  355. pm.makePersistent(pojo);
  356. commitTxn();
  357. pm.close();
  358. pm = pmf.getPersistenceManager();
  359. beginTxn();
  360. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  361. list = Utils.newArrayList(2, 3, 4);
  362. pojo.setIntColl(list);
  363. commitTxn();
  364. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  365. assertEquals(3, ((List<?>)e.getProperty("intColl")).size());
  366. }
  367. public void testUpdateArrayList_Add() throws EntityNotFoundException {
  368. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  369. ArrayList<String> list = Utils.newArrayList("a", "b");
  370. pojo.setStrArrayList(list);
  371. beginTxn();
  372. pm.makePersistent(pojo);
  373. commitTxn();
  374. pm.close();
  375. pm = pmf.getPersistenceManager();
  376. beginTxn();
  377. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  378. pojo.getStrArrayList().add("zoom");
  379. commitTxn();
  380. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  381. assertEquals(3, ((List<?>)e.getProperty("strArrayList")).size());
  382. }
  383. public void testUpdateArrayList_Reset() throws EntityNotFoundException {
  384. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  385. ArrayList<String> list = Utils.newArrayList("a", "b");
  386. pojo.setStrArrayList(list);
  387. beginTxn();
  388. pm.makePersistent(pojo);
  389. commitTxn();
  390. pm.close();
  391. pm = pmf.getPersistenceManager();
  392. beginTxn();
  393. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  394. list = Utils.newArrayList("a", "b", "zoom");
  395. pojo.setStrArrayList(list);
  396. commitTxn();
  397. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  398. assertEquals(3, ((List<?>)e.getProperty("strArrayList")).size());
  399. }
  400. public void testUpdateLinkedList_Add() throws EntityNotFoundException {
  401. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  402. LinkedList<String> list = Utils.newLinkedList("a", "b");
  403. pojo.setStrLinkedList(list);
  404. beginTxn();
  405. pm.makePersistent(pojo);
  406. commitTxn();
  407. pm.close();
  408. pm = pmf.getPersistenceManager();
  409. beginTxn();
  410. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  411. pojo.getStrLinkedList().add("zoom");
  412. commitTxn();
  413. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  414. assertEquals(3, ((List<?>)e.getProperty("strLinkedList")).size());
  415. }
  416. public void testUpdateLinkedList_Reset() throws EntityNotFoundException {
  417. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  418. LinkedList<String> list = Utils.newLinkedList("a", "b");
  419. pojo.setStrLinkedList(list);
  420. beginTxn();
  421. pm.makePersistent(pojo);
  422. commitTxn();
  423. pm.close();
  424. pm = pmf.getPersistenceManager();
  425. beginTxn();
  426. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  427. list = Utils.newLinkedList("a", "b", "zoom");
  428. pojo.setStrLinkedList(list);
  429. commitTxn();
  430. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  431. assertEquals(3, ((List<?>)e.getProperty("strLinkedList")).size());
  432. }
  433. public void testUpdateSet_Add() throws EntityNotFoundException {
  434. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  435. Set<String> set = Utils.newHashSet("a", "b");
  436. pojo.setStrSet(set);
  437. beginTxn();
  438. pm.makePersistent(pojo);
  439. commitTxn();
  440. pm.close();
  441. pm = pmf.getPersistenceManager();
  442. beginTxn();
  443. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  444. pojo.getStrSet().add("zoom");
  445. commitTxn();
  446. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  447. assertEquals(3, ((List<?>)e.getProperty("strSet")).size());
  448. }
  449. public void testUpdateSet_Reset() throws EntityNotFoundException {
  450. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  451. Set<String> set = Utils.newHashSet("a", "b");
  452. pojo.setStrSet(set);
  453. beginTxn();
  454. pm.makePersistent(pojo);
  455. commitTxn();
  456. pm.close();
  457. pm = pmf.getPersistenceManager();
  458. beginTxn();
  459. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  460. set = Utils.newHashSet("a", "b", "zoom");
  461. pojo.setStrSet(set);
  462. commitTxn();
  463. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  464. assertEquals(3, ((List<?>)e.getProperty("strSet")).size());
  465. }
  466. public void testUpdateHashSet_Add() throws EntityNotFoundException {
  467. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  468. HashSet<String> set = Utils.newHashSet("a", "b");
  469. pojo.setStrHashSet(set);
  470. beginTxn();
  471. pm.makePersistent(pojo);
  472. commitTxn();
  473. pm.close();
  474. pm = pmf.getPersistenceManager();
  475. beginTxn();
  476. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  477. pojo.getStrHashSet().add("zoom");
  478. commitTxn();
  479. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  480. assertEquals(3, ((List<?>)e.getProperty("strHashSet")).size());
  481. }
  482. public void testUpdateHashSet_Reset() throws EntityNotFoundException {
  483. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  484. HashSet<String> set = Utils.newHashSet("a", "b");
  485. pojo.setStrHashSet(set);
  486. beginTxn();
  487. pm.makePersistent(pojo);
  488. commitTxn();
  489. pm.close();
  490. pm = pmf.getPersistenceManager();
  491. beginTxn();
  492. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  493. set = Utils.newHashSet("a", "b", "zoom");
  494. pojo.setStrHashSet(set);
  495. commitTxn();
  496. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  497. assertEquals(3, ((List<?>)e.getProperty("strHashSet")).size());
  498. }
  499. public void testUpdateLinkedHashSet_Add() throws EntityNotFoundException {
  500. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  501. LinkedHashSet<String> set = Utils.newLinkedHashSet("a", "b");
  502. pojo.setStrLinkedHashSet(set);
  503. beginTxn();
  504. pm.makePersistent(pojo);
  505. commitTxn();
  506. pm.close();
  507. pm = pmf.getPersistenceManager();
  508. beginTxn();
  509. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  510. pojo.getStrLinkedHashSet().add("zoom");
  511. commitTxn();
  512. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  513. assertEquals(3, ((List<?>)e.getProperty("strLinkedHashSet")).size());
  514. }
  515. public void testUpdateLinkedHashSet_Reset() throws EntityNotFoundException {
  516. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  517. LinkedHashSet<String> set = Utils.newLinkedHashSet("a", "b");
  518. pojo.setStrLinkedHashSet(set);
  519. beginTxn();
  520. pm.makePersistent(pojo);
  521. commitTxn();
  522. pm.close();
  523. pm = pmf.getPersistenceManager();
  524. beginTxn();
  525. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  526. set = Utils.newLinkedHashSet("a", "b", "zoom");
  527. pojo.setStrLinkedHashSet(set);
  528. commitTxn();
  529. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  530. assertEquals(3, ((List<?>)e.getProperty("strLinkedHashSet")).size());
  531. }
  532. public void testUpdateTreeSet_Add() throws EntityNotFoundException {
  533. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  534. TreeSet<String> set = Utils.newTreeSet("a", "b");
  535. pojo.setStrTreeSet(set);
  536. beginTxn();
  537. pm.makePersistent(pojo);
  538. commitTxn();
  539. pm.close();
  540. pm = pmf.getPersistenceManager();
  541. beginTxn();
  542. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  543. pojo.getStrTreeSet().add("zoom");
  544. commitTxn();
  545. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  546. assertEquals(3, ((List<?>)e.getProperty("strTreeSet")).size());
  547. }
  548. public void testUpdateTreeSet_Reset() throws EntityNotFoundException {
  549. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  550. TreeSet<String> set = Utils.newTreeSet("a", "b");
  551. pojo.setStrTreeSet(set);
  552. beginTxn();
  553. pm.makePersistent(pojo);
  554. commitTxn();
  555. pm.close();
  556. pm = pmf.getPersistenceManager();
  557. beginTxn();
  558. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  559. set = Utils.newTreeSet("a", "b", "zoom");
  560. pojo.setStrTreeSet(set);
  561. commitTxn();
  562. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  563. assertEquals(3, ((List<?>)e.getProperty("strTreeSet")).size());
  564. }
  565. public void testUpdateSortedSet_Add() throws EntityNotFoundException {
  566. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  567. SortedSet<String> set = Utils.newTreeSet("a", "b");
  568. pojo.setStrSortedSet(set);
  569. beginTxn();
  570. pm.makePersistent(pojo);
  571. commitTxn();
  572. pm.close();
  573. pm = pmf.getPersistenceManager();
  574. beginTxn();
  575. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  576. pojo.getStrSortedSet().add("zoom");
  577. commitTxn();
  578. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  579. assertEquals(3, ((List<?>)e.getProperty("strSortedSet")).size());
  580. }
  581. public void testUpdateSortedSet_Reset() throws EntityNotFoundException {
  582. HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
  583. SortedSet<String> set = Utils.newTreeSet("a", "b");
  584. pojo.setStrSortedSet(set);
  585. beginTxn();
  586. pm.makePersistent(pojo);
  587. commitTxn();
  588. pm.close();
  589. pm = pmf.getPersistenceManager();
  590. beginTxn();
  591. pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
  592. set = Utils.newTreeSet("a", "b", "zoom");
  593. pojo.setStrSortedSet(set);
  594. commitTxn();
  595. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  596. assertEquals(3, ((List<?>)e.getProperty("strSortedSet")).size());
  597. }
  598. public void testUpdateArray_Reset() throws EntityNotFoundException {
  599. NullDataJDO pojo = new NullDataJDO();
  600. String[] array = new String[] {"a", "b"};
  601. pojo.setArray(array);
  602. beginTxn();
  603. pm.makePersistent(pojo);
  604. commitTxn();
  605. pm.close();
  606. pm = pmf.getPersistenceManager();
  607. beginTxn();
  608. pojo = pm.getObjectById(NullDataJDO.class, pojo.getId());
  609. array = new String[] {"a", "b", "c"};
  610. pojo.setArray(array);
  611. commitTxn();
  612. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  613. assertEquals(3, ((List<?>)e.getProperty("array")).size());
  614. }
  615. // There's really no way to make this pass since we can't intercept
  616. // calls that modify array elements.
  617. public void xxxtestUpdateArray_ModifyExistingElement() throws EntityNotFoundException {
  618. NullDataJDO pojo = new NullDataJDO();
  619. String[] array = new String[] {"a", "b"};
  620. pojo.setArray(array);
  621. beginTxn();
  622. pm.makePersistent(pojo);
  623. commitTxn();
  624. pm.close();
  625. pm = pmf.getPersistenceManager();
  626. beginTxn();
  627. pojo = pm.getObjectById(NullDataJDO.class, pojo.getId());
  628. pojo.getArray()[0] = "c";
  629. pojo.setArray(array);
  630. commitTxn();
  631. Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
  632. assertEquals("c", ((List<?>)e.getProperty("array")).get(0));
  633. }
  634. public void testEmbeddable() throws EntityNotFoundException {
  635. Person p = new Person();
  636. p.setName(new Name());
  637. p.getName().setFirst("jimmy");
  638. p.getName().setLast("jam");
  639. p.setAnotherName(new Name());
  640. p.getAnotherName().setFirst("anotherjimmy");
  641. p.getAnotherName().setLast("anotherjam");
  642. makePersistentInTxn(p, TXN_START_END);
  643. assertNotNull(p.getId());
  644. beginTxn();
  645. p = pm.getObjectById(Person.class, p.getId());
  646. p.getName().setLast("not jam");
  647. p.getName().setFirst("not jimmy");
  648. commitTxn();
  649. Entity entity = ds.get(TestUtils.createKey(p, p.getId()));
  650. assertNotNull(entity);
  651. assertEquals("not jimmy", entity.getProperty("first"));
  652. assertEquals("not jam", entity.getProperty("last"));
  653. assertEquals("anotherjimmy", entity.getProperty("anotherFirst"));
  654. assertEquals("anotherjam", entity.getProperty("anotherLast"));
  655. }
  656. public void testUpdateStrPrimaryKey_SetNewName() {
  657. Key key = ds.put(Flight.newFlightEntity("name", "bos", "mia", 3, 4, 44));
  658. beginTxn();
  659. Flight f = pm.getObjectById(Flight.class, key.getId());
  660. f.setId("other");
  661. pm.makePersistent(f);
  662. try {
  663. commitTxn();
  664. fail("expected exception");
  665. } catch (JDOFatalUserException e) {
  666. // good
  667. rollbackTxn();
  668. }
  669. }
  670. public void testUpdateStrPrimaryKey_SetNewKey() {
  671. Key key = ds.put(Flight.newFlightEntity("name", "bos", "mia", 3, 4, 44));
  672. beginTxn();
  673. Flight f = pm.getObjectById(Flight.class, key.getId());
  674. f.setId(KeyFactory.keyToString(KeyFactory.createKey(key.getKind(), "jimmy")));
  675. pm.makePersistent(f);
  676. try {
  677. commitTxn();
  678. fail("expected exception");
  679. } catch (JDOFatalUserException e) {
  680. // good
  681. rollbackTxn();
  682. }
  683. }
  684. public void testUpdateStrPrimaryKey_NullKey() {
  685. Key key = ds.put(Flight.newFlightEntity("name", "bos", "mia", 3, 4, 44));
  686. beginTxn();
  687. Flight f = pm.getObjectById(Flight.class, key.getId());
  688. f.setId(null);
  689. pm.makePersistent(f);
  690. try {
  691. commitTxn();
  692. fail("expected exception");
  693. } catch (JDOFatalUserException e) {
  694. // good
  695. rollbackTxn();
  696. }
  697. }
  698. public void testUpdateStrAncestor_SetNewName() {
  699. Key parentKey = ds.put(new Entity(String.class.getSimpleName()));
  700. Key key = ds.put(new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentKey));
  701. beginTxn();
  702. HasStringAncestorStringPkJDO pojo = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(key));
  703. pojo.setAncestorId("other");
  704. try {
  705. commitTxn();
  706. fail("expected exception");
  707. } catch (JDOFatalUserException e) {
  708. // good
  709. rollbackTxn();
  710. }
  711. }
  712. public void testUpdateStrAncestor_SetNewKey() {
  713. Key parentKey = ds.put(new Entity(Flight.class.getSimpleName()));
  714. Key key = ds.put(new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentKey));
  715. beginTxn();
  716. HasStringAncestorStringPkJDO pojo = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(key));
  717. pojo.setAncestorId(KeyFactory.keyToString(KeyFactory.createKey(key.getKind(), 33)));
  718. try {
  719. commitTxn();
  720. fail("expected exception");
  721. } catch (JDOFatalUserException e) {
  722. // good
  723. rollbackTxn();
  724. }
  725. }
  726. public void testUpdateStrAncestor_NullKey() {
  727. Key parentKey = ds.put(new Entity(Flight.class.getSimpleName()));
  728. Key key = ds.put(new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentKey));
  729. beginTxn();
  730. HasStringAncestorStringPkJDO pojo = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(key));
  731. pojo.setAncestorId(null);
  732. try {
  733. commitTxn();
  734. fail("expected exception");
  735. } catch (JDOFatalUserException e) {
  736. // good
  737. rollbackTxn();
  738. }
  739. }
  740. public void testUpdateKeyAncestor_SetNewKey() {
  741. Key parentKey = ds.put(new Entity(Flight.class.getSimpleName()));
  742. Key key = ds.put(new Entity(HasKeyAncestorStringPkJDO.class.getSimpleName(), parentKey));
  743. beginTxn();
  744. HasKeyAncestorStringPkJDO pojo = pm.getObjectById(
  745. HasKeyAncestorStringPkJDO.class, KeyFactory.keyToString(key));
  746. pojo.setAncestorKey(KeyFactory.createKey(key.getKind(), 33));
  747. try {
  748. commitTxn();
  749. fail("expected exception");
  750. } catch (JDOFatalUserException e) {
  751. // good
  752. rollbackTxn();
  753. }
  754. }
  755. public void testUpdateKeyAncestor_NullKey() {
  756. Key parentKey = ds.put(new Entity(Flight.class.getSimpleName()));
  757. Key key = ds.put(new Entity(HasKeyAncestorStringPkJDO.class.getSimpleName(), parentKey));
  758. beginTxn();
  759. HasKeyAncestorStringPkJDO pojo = pm.getObjectById(
  760. HasKeyAncestorStringPkJDO.class, KeyFactory.keyToString(key));
  761. pojo.setAncestorKey(null);
  762. try {
  763. commitTxn();
  764. fail("expected exception");
  765. } catch (JDOFatalUserException e) {
  766. // good
  767. rollbackTxn();
  768. }
  769. }
  770. }