PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/SessionFactoryTestCase.java

#
Java | 163 lines | 102 code | 25 blank | 36 comment | 4 complexity | 601daba9340be74548503fc1f0d6157a MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.test.integration.jpa.hibernate;
  23. import static org.junit.Assert.assertNotNull;
  24. import static org.junit.Assert.assertTrue;
  25. import javax.naming.InitialContext;
  26. import javax.naming.NameClassPair;
  27. import javax.naming.NamingEnumeration;
  28. import javax.naming.NamingException;
  29. import org.hibernate.Session;
  30. import org.hibernate.SessionFactory;
  31. import org.jboss.arquillian.container.test.api.Deployment;
  32. import org.jboss.arquillian.junit.Arquillian;
  33. import org.jboss.arquillian.test.api.ArquillianResource;
  34. import org.jboss.shrinkwrap.api.Archive;
  35. import org.jboss.shrinkwrap.api.ShrinkWrap;
  36. import org.jboss.shrinkwrap.api.asset.StringAsset;
  37. import org.jboss.shrinkwrap.api.spec.JavaArchive;
  38. import org.junit.Test;
  39. import org.junit.runner.RunWith;
  40. /**
  41. * Hibernate session factory tests
  42. *
  43. *
  44. * @author Scott Marlow
  45. */
  46. @RunWith(Arquillian.class)
  47. public class SessionFactoryTestCase {
  48. private static final String ARCHIVE_NAME = "jpa_sessionfactory";
  49. private static final String persistence_xml =
  50. "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " +
  51. "<persistence xmlns=\"http://java.sun.com/xml/ns/persistence\" version=\"1.0\">" +
  52. " <persistence-unit name=\"mypc\">" +
  53. " <description>Persistence Unit." +
  54. " </description>" +
  55. " <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>" +
  56. "<properties> <property name=\"hibernate.hbm2ddl.auto\" value=\"create-drop\"/>" +
  57. "<property name=\"hibernate.session_factory_name\" value=\"modelSessionFactory\" />" +
  58. "</properties>" +
  59. " </persistence-unit>" +
  60. "</persistence>";
  61. @Deployment
  62. public static Archive<?> deploy() {
  63. JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar");
  64. jar.addClasses(SessionFactoryTestCase.class,
  65. Employee.class,
  66. SFSB1.class,
  67. SFSBHibernateSession.class,
  68. SFSBHibernateSessionFactory.class
  69. );
  70. jar.addAsResource(new StringAsset(persistence_xml), "META-INF/persistence.xml");
  71. return jar;
  72. }
  73. @ArquillianResource
  74. private InitialContext iniCtx;
  75. protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException {
  76. return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName()));
  77. }
  78. protected <T> T rawLookup(String name, Class<T> interfaceType) throws NamingException {
  79. try {
  80. return interfaceType.cast(iniCtx.lookup(name));
  81. } catch (NamingException e) {
  82. dumpJndi("");
  83. throw e;
  84. }
  85. }
  86. private void dumpJndi(String s) {
  87. try {
  88. dumpTreeEntry(iniCtx.list(s), s);
  89. } catch (NamingException ignore) {
  90. }
  91. }
  92. private void dumpTreeEntry(NamingEnumeration<NameClassPair> list, String s) throws NamingException {
  93. System.out.println("\ndump " + s);
  94. while (list.hasMore()) {
  95. NameClassPair ncp = list.next();
  96. System.out.println(ncp.toString());
  97. if (s.length() == 0) {
  98. dumpJndi(ncp.getName());
  99. } else {
  100. dumpJndi(s + "/" + ncp.getName());
  101. }
  102. }
  103. }
  104. // test that we didn't break the Hibernate hibernate.session_factory_name (bind Hibernate session factory to
  105. // specified jndi name) functionality.
  106. @Test
  107. public void testHibernateSessionFactoryName() throws Exception {
  108. SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class);
  109. sfsb1.createEmployee("Sally","1 home street", 1);
  110. // check if we can look up the Hibernate session factory that should of been bound because of
  111. // the hibernate.session_factory_name was specified in the properties (in peristence.xml above).
  112. SessionFactory hibernateSessionFactory = rawLookup("modelSessionFactory",SessionFactory.class);
  113. assertNotNull("jndi lookup of hibernate.session_factory_name should return HibernateSessionFactory", hibernateSessionFactory);
  114. Session session = hibernateSessionFactory.openSession();
  115. Employee emp = (Employee)session.get(Employee.class,1);
  116. assertTrue("name read from hibernate session is Sally", "Sally".equals(emp.getName()));
  117. }
  118. // Test that an extended Persistence context can be injected into a Hibernate Session
  119. // We use extended persistence context, otherwise the Hibernate session will be closed after each transaction and
  120. // the assert test would fail (due to lazy loading of the Employee entity.
  121. // Using extended persistence context allows the hibernate session to stay open long enough for the lazy fetch.
  122. @Test
  123. public void testInjectPCIntoHibernateSession() throws Exception {
  124. SFSBHibernateSession sfsbHibernateSession = lookup("SFSBHibernateSession",SFSBHibernateSession.class);
  125. sfsbHibernateSession.createEmployee("Molly", "2 apple way", 2);
  126. Employee emp = sfsbHibernateSession.getEmployee(2);
  127. assertTrue("name read from hibernate session is Molly", "Molly".equals(emp.getName()));
  128. }
  129. // Test that a Persistence unit can be injected into a Hibernate Session factory
  130. @Test
  131. public void testInjectPUIntoHibernateSessionFactory() throws Exception {
  132. SFSBHibernateSessionFactory sfsbHibernateSessionFactory =
  133. lookup("SFSBHibernateSessionFactory",SFSBHibernateSessionFactory.class);
  134. sfsbHibernateSessionFactory.createEmployee("Sharon", "3 beach ave", 3);
  135. Employee emp = sfsbHibernateSessionFactory.getEmployee(3);
  136. assertTrue("name read from hibernate session is Sharon", "Sharon".equals(emp.getName()));
  137. }
  138. }