/src/test/java/com/google/ie/test/DatastoreTest.java

http://thoughtsite.googlecode.com/ · Java · 64 lines · 42 code · 11 blank · 11 comment · 0 complexity · 7c43a9745bb43733f4e2fe902b9fc2c2 MD5 · raw file

  1. package com.google.ie.test;
  2. import com.google.appengine.api.datastore.dev.LocalDatastoreService;
  3. import com.google.appengine.tools.development.ApiProxyLocalImpl;
  4. import com.google.apphosting.api.ApiProxy;
  5. import com.google.ie.dto.RetrievalInfo;
  6. import org.junit.After;
  7. import org.junit.Before;
  8. import org.springframework.test.context.ContextConfiguration;
  9. import org.springframework.test.context.TestExecutionListeners;
  10. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
  11. import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import java.io.File;
  14. import javax.jdo.JDOHelper;
  15. /**
  16. * Base class for testing all data access classes.
  17. *
  18. * @author abraina
  19. *
  20. */
  21. @ContextConfiguration(locations = { "test-app-context.xml" })
  22. @TestExecutionListeners(TransactionalTestExecutionListener.class)
  23. @Transactional
  24. public class DatastoreTest extends AbstractJUnit4SpringContextTests {
  25. /** Persistence manager factory to be used in tests */
  26. protected static javax.jdo.PersistenceManagerFactory pmf = JDOHelper
  27. .getPersistenceManagerFactory("transactions-optional");
  28. @Before
  29. public void setUp() {
  30. // Set test environment
  31. ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
  32. ApiProxy.setDelegate(new ApiProxyLocalImpl(new File(".")) {
  33. });
  34. ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
  35. // Local datastore should not save test objects to file
  36. proxy.setProperty(LocalDatastoreService.NO_STORAGE_PROPERTY, Boolean.TRUE.toString());
  37. }
  38. @After
  39. public void tearDown() {
  40. // Set environment as test environment
  41. ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
  42. LocalDatastoreService datastoreService =
  43. (LocalDatastoreService) proxy.getService(LocalDatastoreService.PACKAGE);
  44. datastoreService.clearProfiles();
  45. // Setting api proxy to null. Not necessary but a good practice.
  46. ApiProxy.setDelegate(null);
  47. ApiProxy.setEnvironmentForCurrentThread(null);
  48. }
  49. protected RetrievalInfo createDummyRetrievalParam(int startindex, int noOfRecords,
  50. String orderOn, String orderBy) {
  51. return new RetrievalInfo(startindex, noOfRecords, orderOn, orderBy);
  52. }
  53. }