PageRenderTime 4385ms CodeModel.GetById 32ms RepoModel.GetById 5ms app.codeStats 0ms

/warp-persist/test/com/wideplay/warp/persist/hibernate/SessionFactoryProvisionTest.java

http://warp-persist.googlecode.com/
Java | 84 lines | 43 code | 11 blank | 30 comment | 0 complexity | e7964ff26cddf1e526061eac5e9bd075 MD5 | raw file
Possible License(s): Apache-2.0
  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.hibernate;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Injector;
  20. import com.google.inject.matcher.Matchers;
  21. import com.wideplay.codemonkey.web.startup.Initializer;
  22. import com.wideplay.warp.persist.PersistenceService;
  23. import com.wideplay.warp.persist.UnitOfWork;
  24. import org.hibernate.Session;
  25. import org.hibernate.SessionFactory;
  26. import org.hibernate.cfg.AnnotationConfiguration;
  27. import org.hibernate.cfg.Configuration;
  28. import org.testng.annotations.AfterClass;
  29. import org.testng.annotations.BeforeTest;
  30. import org.testng.annotations.Test;
  31. /**
  32. * Created by IntelliJ IDEA.
  33. * User: Dhanji R. Prasanna (dhanji@gmail.com)
  34. * Date: 1/06/2007
  35. * Time: 11:40:36
  36. * <p/>
  37. * TODO: Describe me!
  38. *
  39. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  40. * @since 1.0
  41. */
  42. public class SessionFactoryProvisionTest {
  43. private Injector injector;
  44. @BeforeTest
  45. public void pre() {
  46. injector = Guice.createInjector(PersistenceService.usingHibernate()
  47. .across(UnitOfWork.TRANSACTION)
  48. .forAll(Matchers.any())
  49. .buildModule(),
  50. new AbstractModule() {
  51. protected void configure() {
  52. bind(Configuration.class).toInstance(new AnnotationConfiguration()
  53. .addAnnotatedClass(HibernateTestEntity.class)
  54. .setProperties(Initializer.loadProperties("spt-persistence.properties")));
  55. }
  56. });
  57. }
  58. @AfterClass
  59. void post() {
  60. injector.getInstance(SessionFactory.class).close();
  61. }
  62. @Test
  63. public void testSessionCreateOnInjection() {
  64. // TODO (Robbie) review
  65. //assert injector.getInstance(SessionFactoryHolder.class).equals(injector.getInstance(SessionFactoryHolder.class));
  66. assert injector.getInstance(PersistenceService.class).equals(injector.getInstance(PersistenceService.class)) : "SINGLETON VIOLATION " + PersistenceService.class.getName() ;
  67. //startup persistence
  68. injector.getInstance(PersistenceService.class)
  69. .start();
  70. //obtain session
  71. assert injector.getInstance(Session.class).isOpen() : "session is not open!";
  72. }
  73. }