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

http://datanucleus-appengine.googlecode.com/ · Java · 139 lines · 104 code · 16 blank · 19 comment · 0 complexity · 7425b26471413069ea3a69c74b7a648c 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.test.jdo.Flight;
  19. import com.google.appengine.datanucleus.test.jdo.HasTableAndColumnsInMappingJDO;
  20. import java.util.List;
  21. import java.util.Properties;
  22. import javax.jdo.JDOHelper;
  23. import javax.jdo.Query;
  24. /**
  25. * @author Max Ross <maxr@google.com>
  26. */
  27. public class JDOTableAndColumnTest extends JDOTestCase {
  28. public void testInsert() throws EntityNotFoundException {
  29. HasTableAndColumnsInMappingJDO htacim = new HasTableAndColumnsInMappingJDO();
  30. htacim.setFoo("foo val");
  31. makePersistentInTxn(htacim, TXN_START_END);
  32. assertNotNull(htacim.getId());
  33. Entity entity = ds.get(KeyFactory.createKey(
  34. HasTableAndColumnsInMappingJDO.TABLE_NAME, htacim.getId()));
  35. assertNotNull(entity);
  36. assertEquals(HasTableAndColumnsInMappingJDO.TABLE_NAME, entity.getKind());
  37. assertEquals("foo val", entity.getProperty(HasTableAndColumnsInMappingJDO.FOO_COLUMN_NAME));
  38. }
  39. private void setupCustomIdentifierPolicy(
  40. String wordSeparator, String tablePrefix, String tableSuffix, String idCase) {
  41. pm.close();
  42. pmf.close();
  43. Properties properties = new Properties();
  44. properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",
  45. "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
  46. properties.setProperty("javax.jdo.option.ConnectionURL","appengine");
  47. properties.setProperty("datanucleus.NontransactionalRead", Boolean.TRUE.toString());
  48. properties.setProperty("datanucleus.NontransactionalWrite", Boolean.TRUE.toString());
  49. properties.setProperty("datanucleus.identifier.wordSeparator", wordSeparator);
  50. properties.setProperty("datanucleus.identifier.case", idCase);
  51. properties.setProperty("datanucleus.identifier.tablePrefix", tablePrefix);
  52. properties.setProperty("datanucleus.identifier.tableSuffix", tableSuffix);
  53. pmf = JDOHelper.getPersistenceManagerFactory(properties);
  54. pm = pmf.getPersistenceManager();
  55. }
  56. public void testInsertWithCustomIdPolicy() throws EntityNotFoundException {
  57. setupCustomIdentifierPolicy("___", "PREFIX", "SUFFIX", "UpperCase");
  58. Flight f1 = new Flight();
  59. f1.setOrigin("BOS");
  60. f1.setDest("MIA");
  61. f1.setMe(2);
  62. f1.setYou(4);
  63. f1.setName("Harold");
  64. f1.setFlightNumber(400);
  65. pm.makePersistent(f1);
  66. Entity entity = ds.get(KeyFactory.stringToKey(f1.getId()));
  67. assertNotNull(entity);
  68. assertEquals("PREFIX" + Flight.class.getSimpleName().toUpperCase() + "SUFFIX", entity.getKind());
  69. assertEquals("Harold", entity.getProperty("NAME"));
  70. // column name isn't generated if explicitly set
  71. assertEquals(400L, entity.getProperty("flight_number"));
  72. }
  73. public void testFetch() {
  74. Entity entity = new Entity(HasTableAndColumnsInMappingJDO.TABLE_NAME);
  75. entity.setProperty(HasTableAndColumnsInMappingJDO.FOO_COLUMN_NAME, "foo val");
  76. Key key = ds.put(entity);
  77. String keyStr = KeyFactory.keyToString(key);
  78. beginTxn();
  79. HasTableAndColumnsInMappingJDO htacim =
  80. pm.getObjectById(HasTableAndColumnsInMappingJDO.class, keyStr);
  81. assertNotNull(htacim);
  82. assertEquals(Long.valueOf(key.getId()), htacim.getId());
  83. assertEquals("foo val", htacim.getFoo());
  84. commitTxn();
  85. }
  86. public void testQueryWithCustomIdPolicy() {
  87. setupCustomIdentifierPolicy("___", "PREFIX", "SUFFIX", "UpperCase");
  88. Flight f1 = new Flight();
  89. f1.setOrigin("BOS");
  90. f1.setDest("MIA");
  91. f1.setMe(2);
  92. f1.setYou(4);
  93. f1.setName("Harold");
  94. f1.setFlightNumber(400);
  95. pm.makePersistent(f1);
  96. Flight f2 = new Flight();
  97. f2.setOrigin("BOS");
  98. f2.setDest("MIA");
  99. f2.setMe(2);
  100. f2.setYou(4);
  101. f2.setName("Harold2");
  102. f2.setFlightNumber(401);
  103. pm.makePersistent(f2);
  104. Query q = pm.newQuery("select from " + Flight.class.getName()
  105. + " where flightNumber > 300 "
  106. + " order by flightNumber desc");
  107. @SuppressWarnings("unchecked")
  108. List<Flight> result = (List<Flight>) q.execute();
  109. assertEquals(2, result.size());
  110. assertEquals(f2, result.get(0));
  111. assertEquals(f1, result.get(1));
  112. q = pm.newQuery("select from " + Flight.class.getName()
  113. + " where name == \"Harold\" "
  114. + " order by name desc");
  115. @SuppressWarnings("unchecked")
  116. List<Flight> result2 = (List<Flight>) q.execute();
  117. assertEquals(1, result2.size());
  118. assertEquals(f1, result2.get(0));
  119. }
  120. }