PageRenderTime 39ms CodeModel.GetById 23ms app.highlight 14ms 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
 23package org.jboss.as.test.integration.jpa.hibernate;
 24
 25import static org.junit.Assert.assertNotNull;
 26import static org.junit.Assert.assertTrue;
 27
 28import javax.naming.InitialContext;
 29import javax.naming.NameClassPair;
 30import javax.naming.NamingEnumeration;
 31import javax.naming.NamingException;
 32
 33import org.hibernate.Session;
 34import org.hibernate.SessionFactory;
 35import org.jboss.arquillian.container.test.api.Deployment;
 36import org.jboss.arquillian.junit.Arquillian;
 37import org.jboss.arquillian.test.api.ArquillianResource;
 38import org.jboss.shrinkwrap.api.Archive;
 39import org.jboss.shrinkwrap.api.ShrinkWrap;
 40import org.jboss.shrinkwrap.api.asset.StringAsset;
 41import org.jboss.shrinkwrap.api.spec.JavaArchive;
 42import org.junit.Test;
 43import org.junit.runner.RunWith;
 44
 45/**
 46 * Hibernate session factory tests
 47 *
 48 *
 49 * @author Scott Marlow
 50 */
 51@RunWith(Arquillian.class)
 52public class SessionFactoryTestCase {
 53
 54    private static final String ARCHIVE_NAME = "jpa_sessionfactory";
 55
 56    private static final String persistence_xml =
 57        "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " +
 58            "<persistence xmlns=\"http://java.sun.com/xml/ns/persistence\" version=\"1.0\">" +
 59            "  <persistence-unit name=\"mypc\">" +
 60            "    <description>Persistence Unit." +
 61            "    </description>" +
 62            "  <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>" +
 63            "<properties> <property name=\"hibernate.hbm2ddl.auto\" value=\"create-drop\"/>" +
 64            "<property name=\"hibernate.session_factory_name\" value=\"modelSessionFactory\" />" +
 65            "</properties>" +
 66            "  </persistence-unit>" +
 67            "</persistence>";
 68
 69    @Deployment
 70    public static Archive<?> deploy() {
 71
 72        JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar");
 73        jar.addClasses(SessionFactoryTestCase.class,
 74            Employee.class,
 75            SFSB1.class,
 76            SFSBHibernateSession.class,
 77            SFSBHibernateSessionFactory.class
 78        );
 79
 80        jar.addAsResource(new StringAsset(persistence_xml), "META-INF/persistence.xml");
 81        return jar;
 82    }
 83
 84    @ArquillianResource
 85    private InitialContext iniCtx;
 86
 87    protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException {
 88        return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName()));
 89    }
 90
 91    protected <T> T rawLookup(String name, Class<T> interfaceType) throws NamingException {
 92        try {
 93            return interfaceType.cast(iniCtx.lookup(name));
 94
 95        } catch (NamingException e) {
 96            dumpJndi("");
 97            throw e;
 98        }
 99    }
100
101    private void dumpJndi(String s) {
102        try {
103            dumpTreeEntry(iniCtx.list(s), s);
104        } catch (NamingException ignore) {
105        }
106    }
107
108    private void dumpTreeEntry(NamingEnumeration<NameClassPair> list, String s) throws NamingException {
109        System.out.println("\ndump " + s);
110        while (list.hasMore()) {
111            NameClassPair ncp = list.next();
112            System.out.println(ncp.toString());
113            if (s.length() == 0) {
114                dumpJndi(ncp.getName());
115            } else {
116                dumpJndi(s + "/" + ncp.getName());
117            }
118        }
119    }
120
121
122    // test that we didn't break the Hibernate hibernate.session_factory_name (bind Hibernate session factory to
123    // specified jndi name) functionality.
124    @Test
125    public void testHibernateSessionFactoryName() throws Exception {
126        SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class);
127        sfsb1.createEmployee("Sally","1 home street", 1);
128
129        // check if we can look up the Hibernate session factory that should of been bound because of
130        // the hibernate.session_factory_name was specified in the properties (in peristence.xml above).
131        SessionFactory hibernateSessionFactory = rawLookup("modelSessionFactory",SessionFactory.class);
132        assertNotNull("jndi lookup of hibernate.session_factory_name should return HibernateSessionFactory", hibernateSessionFactory);
133
134        Session session = hibernateSessionFactory.openSession();
135        Employee emp = (Employee)session.get(Employee.class,1);
136        assertTrue("name read from hibernate session is Sally", "Sally".equals(emp.getName()));
137    }
138
139    // Test that an extended Persistence context can be injected into a Hibernate Session
140    // We use extended persistence context, otherwise the Hibernate session will be closed after each transaction and
141    // the assert test would fail (due to lazy loading of the Employee entity.
142    // Using extended persistence context allows the hibernate session to stay open long enough for the lazy fetch.
143    @Test
144    public void testInjectPCIntoHibernateSession() throws Exception {
145        SFSBHibernateSession sfsbHibernateSession = lookup("SFSBHibernateSession",SFSBHibernateSession.class);
146        sfsbHibernateSession.createEmployee("Molly", "2 apple way", 2);
147
148        Employee emp = sfsbHibernateSession.getEmployee(2);
149        assertTrue("name read from hibernate session is Molly", "Molly".equals(emp.getName()));
150    }
151
152    // Test that a Persistence unit can be injected into a Hibernate Session factory
153    @Test
154    public void testInjectPUIntoHibernateSessionFactory() throws Exception {
155        SFSBHibernateSessionFactory sfsbHibernateSessionFactory =
156            lookup("SFSBHibernateSessionFactory",SFSBHibernateSessionFactory.class);
157        sfsbHibernateSessionFactory.createEmployee("Sharon", "3 beach ave", 3);
158
159        Employee emp = sfsbHibernateSessionFactory.getEmployee(3);
160        assertTrue("name read from hibernate session is Sharon", "Sharon".equals(emp.getName()));
161    }
162
163}