PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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