/jboss-as-7.1.1.Final/testsuite/compat/src/test/java/org/jboss/as/test/compat/jpa/hibernate/Hibernate3SharedModuleProviderTestCase.java
Java | 140 lines | 92 code | 19 blank | 29 comment | 4 complexity | 5b2365cbd5f609d54e7c64ec5422616a 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.compat.jpa.hibernate;
24
25import org.jboss.arquillian.container.test.api.Deployment;
26import org.jboss.arquillian.junit.Arquillian;
27import org.jboss.arquillian.test.api.ArquillianResource;
28import org.jboss.shrinkwrap.api.Archive;
29import org.jboss.shrinkwrap.api.ShrinkWrap;
30import org.jboss.shrinkwrap.api.asset.StringAsset;
31import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
32import org.jboss.shrinkwrap.api.spec.JavaArchive;
33import org.jboss.shrinkwrap.api.spec.WebArchive;
34import org.junit.BeforeClass;
35import org.junit.Ignore;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
39import javax.naming.InitialContext;
40import javax.naming.NameClassPair;
41import javax.naming.NamingEnumeration;
42import javax.naming.NamingException;
43
44/**
45 * @author Scott Marlow
46 */
47@RunWith(Arquillian.class)
48@Ignore // until we hack the test to populate the org.hibernate module
49public class Hibernate3SharedModuleProviderTestCase {
50
51 private static final String ARCHIVE_NAME = "hibernate3module_test";
52
53 private static final String persistence_xml =
54 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " +
55 "<persistence xmlns=\"http://java.sun.com/xml/ns/persistence\" version=\"1.0\">" +
56 " <persistence-unit name=\"hibernate3_pc\">" +
57 " <description>Persistence Unit." +
58 " </description>" +
59 " <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>" +
60 "<class>org.jboss.as.test.compat.jpa.hibernate.Employee</class>" + // currently hibernate 3.3.x cannot discover entities
61 "<properties> <property name=\"hibernate.hbm2ddl.auto\" value=\"create-drop\"/>" +
62 "<property name=\"hibernate.show_sql\" value=\"true\"/>" +
63// set the providerModule to the AS org.hibernate3 module (should be in as/modules/org/hibernate/3 folder
64// Hibernate 3 jars will need to be in the module folder and each jar name
65// should be added to module.xml in same folder.
66// not configuring the org.hibernate:3 module will give errors like this: http://pastie.org/2280769
67 "<property name=\"jboss.as.jpa.providerModule\" value=\"org.hibernate:3\"/>" +
68 "</properties>" +
69 " </persistence-unit>" +
70 "</persistence>";
71
72 @ArquillianResource
73 private static InitialContext iniCtx;
74
75 @BeforeClass
76 public static void beforeClass() throws NamingException {
77 iniCtx = new InitialContext();
78 }
79
80 @Deployment
81 public static Archive<?> deploy() throws Exception {
82
83 EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, ARCHIVE_NAME + ".ear");
84
85 JavaArchive lib = ShrinkWrap.create(JavaArchive.class, "beans.jar");
86 lib.addClasses(SFSB1.class);
87 ear.addAsModule(lib);
88
89 lib = ShrinkWrap.create(JavaArchive.class, "entities.jar");
90 lib.addClasses(Employee.class);
91 lib.addAsManifestResource(new StringAsset(persistence_xml), "persistence.xml");
92 ear.addAsLibraries(lib);
93
94 final WebArchive main = ShrinkWrap.create(WebArchive.class, "main.war");
95 main.addClasses(Hibernate3SharedModuleProviderTestCase.class);
96 ear.addAsModule(main);
97
98 return ear;
99 }
100
101 protected static <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException {
102 try {
103 return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + "beans/" + beanName + "!" + interfaceType.getName()));
104 } catch (NamingException e) {
105 dumpJndi("");
106 throw e;
107 }
108 }
109
110 // TODO: move this logic to a common base class (might be helpful for writing new tests)
111 private static void dumpJndi(String s) {
112 try {
113 dumpTreeEntry(iniCtx.list(s), s);
114 } catch (NamingException ignore) {
115 }
116 }
117
118 private static void dumpTreeEntry(NamingEnumeration<NameClassPair> list, String s) throws NamingException {
119 System.out.println("\ndump " + s);
120 while (list.hasMore()) {
121 NameClassPair ncp = list.next();
122 System.out.println(ncp.toString());
123 if (s.length() == 0) {
124 dumpJndi(ncp.getName());
125 } else {
126 dumpJndi(s + "/" + ncp.getName());
127 }
128 }
129 }
130
131 @Test
132 public void testSimpleCreateAndLoadEntities() throws Exception {
133 SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class);
134 sfsb1.createEmployee("Kelly Smith", "Watford, England", 10);
135 sfsb1.createEmployee("Alex Scott", "London, England", 20);
136 sfsb1.getEmployeeNoTX(10);
137 sfsb1.getEmployeeNoTX(20);
138 }
139
140}