PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/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/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.jdo;
 17
 18import com.google.appengine.api.datastore.Entity;
 19import com.google.appengine.api.datastore.EntityNotFoundException;
 20import com.google.appengine.api.datastore.Key;
 21import com.google.appengine.api.datastore.KeyFactory;
 22import com.google.appengine.datanucleus.test.jdo.Flight;
 23import com.google.appengine.datanucleus.test.jdo.HasTableAndColumnsInMappingJDO;
 24
 25
 26import java.util.List;
 27import java.util.Properties;
 28
 29import javax.jdo.JDOHelper;
 30import javax.jdo.Query;
 31
 32/**
 33 * @author Max Ross <maxr@google.com>
 34 */
 35public class JDOTableAndColumnTest extends JDOTestCase {
 36
 37  public void testInsert() throws EntityNotFoundException {
 38    HasTableAndColumnsInMappingJDO htacim = new HasTableAndColumnsInMappingJDO();
 39    htacim.setFoo("foo val");
 40    makePersistentInTxn(htacim, TXN_START_END);
 41    assertNotNull(htacim.getId());
 42    Entity entity = ds.get(KeyFactory.createKey(
 43        HasTableAndColumnsInMappingJDO.TABLE_NAME, htacim.getId()));
 44    assertNotNull(entity);
 45    assertEquals(HasTableAndColumnsInMappingJDO.TABLE_NAME, entity.getKind());
 46    assertEquals("foo val", entity.getProperty(HasTableAndColumnsInMappingJDO.FOO_COLUMN_NAME));
 47  }
 48
 49  private void setupCustomIdentifierPolicy(
 50      String wordSeparator, String tablePrefix, String tableSuffix, String idCase) {
 51    pm.close();
 52    pmf.close();
 53    Properties properties = new Properties();
 54    properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",
 55                    "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
 56    properties.setProperty("javax.jdo.option.ConnectionURL","appengine");
 57    properties.setProperty("datanucleus.NontransactionalRead", Boolean.TRUE.toString());
 58    properties.setProperty("datanucleus.NontransactionalWrite", Boolean.TRUE.toString());
 59    properties.setProperty("datanucleus.identifier.wordSeparator", wordSeparator);
 60    properties.setProperty("datanucleus.identifier.case", idCase);
 61    properties.setProperty("datanucleus.identifier.tablePrefix", tablePrefix);
 62    properties.setProperty("datanucleus.identifier.tableSuffix", tableSuffix);
 63    pmf = JDOHelper.getPersistenceManagerFactory(properties);
 64    pm = pmf.getPersistenceManager();
 65  }
 66
 67  public void testInsertWithCustomIdPolicy() throws EntityNotFoundException {
 68    setupCustomIdentifierPolicy("___", "PREFIX", "SUFFIX", "UpperCase");
 69
 70    Flight f1 = new Flight();
 71    f1.setOrigin("BOS");
 72    f1.setDest("MIA");
 73    f1.setMe(2);
 74    f1.setYou(4);
 75    f1.setName("Harold");
 76    f1.setFlightNumber(400);
 77    pm.makePersistent(f1);
 78    Entity entity = ds.get(KeyFactory.stringToKey(f1.getId()));
 79    assertNotNull(entity);
 80    assertEquals("PREFIX" + Flight.class.getSimpleName().toUpperCase() + "SUFFIX", entity.getKind());
 81    assertEquals("Harold", entity.getProperty("NAME"));
 82    // column name isn't generated if explicitly set
 83    assertEquals(400L, entity.getProperty("flight_number"));
 84  }
 85
 86  public void testFetch() {
 87    Entity entity = new Entity(HasTableAndColumnsInMappingJDO.TABLE_NAME);
 88    entity.setProperty(HasTableAndColumnsInMappingJDO.FOO_COLUMN_NAME, "foo val");
 89    Key key = ds.put(entity);
 90
 91    String keyStr = KeyFactory.keyToString(key);
 92    beginTxn();
 93    HasTableAndColumnsInMappingJDO htacim =
 94        pm.getObjectById(HasTableAndColumnsInMappingJDO.class, keyStr);
 95    assertNotNull(htacim);
 96    assertEquals(Long.valueOf(key.getId()), htacim.getId());
 97    assertEquals("foo val", htacim.getFoo());
 98    commitTxn();
 99  }
100
101  public void testQueryWithCustomIdPolicy() {
102    setupCustomIdentifierPolicy("___", "PREFIX", "SUFFIX", "UpperCase");
103
104    Flight f1 = new Flight();
105    f1.setOrigin("BOS");
106    f1.setDest("MIA");
107    f1.setMe(2);
108    f1.setYou(4);
109    f1.setName("Harold");
110    f1.setFlightNumber(400);
111    pm.makePersistent(f1);
112
113    Flight f2 = new Flight();
114    f2.setOrigin("BOS");
115    f2.setDest("MIA");
116    f2.setMe(2);
117    f2.setYou(4);
118    f2.setName("Harold2");
119    f2.setFlightNumber(401);
120    pm.makePersistent(f2);
121
122    Query q = pm.newQuery("select from " + Flight.class.getName()
123        + " where flightNumber > 300 "
124        + " order by flightNumber desc");
125    @SuppressWarnings("unchecked")
126    List<Flight> result = (List<Flight>) q.execute();
127    assertEquals(2, result.size());
128    assertEquals(f2, result.get(0));
129    assertEquals(f1, result.get(1));
130
131    q = pm.newQuery("select from " + Flight.class.getName()
132        + " where name == \"Harold\" "
133        + " order by name desc");
134    @SuppressWarnings("unchecked")
135    List<Flight> result2 = (List<Flight>) q.execute();
136    assertEquals(1, result2.size());
137    assertEquals(f1, result2.get(0));
138  }
139}