/warp-persist/test/com/wideplay/warp/persist/db4o/Db4oWorkManagerTest.java

http://warp-persist.googlecode.com/ · Java · 131 lines · 82 code · 29 blank · 20 comment · 0 complexity · 3a47b86dd6bea1aad95dd112debb703d MD5 · raw file

  1. /**
  2. * Copyright (C) 2008 Wideplay Interactive.
  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. */
  16. package com.wideplay.warp.persist.db4o;
  17. import com.db4o.Db4o;
  18. import com.db4o.ObjectContainer;
  19. import com.db4o.ObjectServer;
  20. import com.db4o.config.Configuration;
  21. import com.google.inject.AbstractModule;
  22. import com.google.inject.Guice;
  23. import com.google.inject.Inject;
  24. import com.google.inject.Injector;
  25. import com.google.inject.name.Names;
  26. import com.wideplay.warp.persist.*;
  27. import org.testng.annotations.AfterClass;
  28. import org.testng.annotations.BeforeClass;
  29. import org.testng.annotations.Test;
  30. /**
  31. *
  32. * @author Jeffrey Chung (jeffreymchung@gmail.com), Dhanji R. Prasanna (dhanji@gmail.com)
  33. */
  34. @Test(suiteName = "db4o")
  35. public class Db4oWorkManagerTest {
  36. private Injector injector;
  37. @BeforeClass
  38. public void preClass() {
  39. injector = Guice.createInjector(PersistenceService.usingDb4o()
  40. .across(UnitOfWork.REQUEST)
  41. .buildModule(),
  42. new AbstractModule() {
  43. protected void configure() {
  44. bindConstant().annotatedWith(Db4Objects.class).to("TestDatabase.data");
  45. bindConstant().annotatedWith(Names.named(Db4Objects.HOST)).to("localhost");
  46. bindConstant().annotatedWith(Names.named(Db4Objects.PORT)).to(4321);
  47. bindConstant().annotatedWith(Names.named(Db4Objects.USER)).to("autobot");
  48. bindConstant().annotatedWith(Names.named(Db4Objects.PASSWORD)).to("morethanmeetstheeye");
  49. Configuration config = Db4o.newConfiguration();
  50. bind(Configuration.class).toInstance(config);
  51. }
  52. }
  53. );
  54. injector.getInstance(PersistenceService.class).start();
  55. }
  56. @AfterClass
  57. public void postClass() {
  58. injector.getInstance(ObjectServer.class).close();
  59. TestCleanup.clearWorkManagers();
  60. }
  61. @Test
  62. public void verifySingleObjectContainerOverUnitOfWork() {
  63. injector.getInstance(WorkManager.class).beginWork();
  64. ObjectContainer unitOfWorkOC = injector.getInstance(ObjectContainer.class);
  65. Db4oDao dao = injector.getInstance(Db4oDao.class);
  66. assert Db4oDao.oc.equals(unitOfWorkOC)
  67. : "Duplicate object containers";
  68. assert unitOfWorkOC.equals(injector.getInstance(ObjectContainer.class))
  69. : "Duplicate object containers";
  70. Db4oTestObject obj = new Db4oTestObject("more than meets the eye");
  71. assert unitOfWorkOC.equals(injector.getInstance(ObjectContainer.class))
  72. : "Duplicate object containers";
  73. dao.persist(obj);
  74. assert Db4oDao.oc.equals(unitOfWorkOC)
  75. : "Duplicate object containers";
  76. // start a new object container
  77. dao = injector.getInstance(Db4oDao.class);
  78. assert unitOfWorkOC.equals(injector.getInstance(ObjectContainer.class))
  79. : "Duplicate object containers";
  80. assert dao.contains(obj) : "Object container was closed unexpectedly (no persistent behavior)";
  81. assert unitOfWorkOC.equals(injector.getInstance(ObjectContainer.class))
  82. : "Duplicate object containers";
  83. injector.getInstance(WorkManager.class).endWork();
  84. }
  85. public static class Db4oDao {
  86. static ObjectContainer oc;
  87. @Inject
  88. public Db4oDao(ObjectContainer oc) {
  89. Db4oDao.oc = oc;
  90. }
  91. @Transactional
  92. public <T> void persist(T t) {
  93. assert !oc.ext().isClosed() : "oc is not open";
  94. oc.set(t);
  95. assert oc.ext().isStored(t) : "Persisting object failed";
  96. }
  97. @Transactional
  98. public <T> boolean contains(T t) {
  99. return oc.ext().isStored(t);
  100. }
  101. }
  102. }