repo
stringlengths
1
191
file
stringlengths
23
351
code
stringlengths
0
5.32M
file_length
int64
0
5.32M
avg_line_length
float64
0
2.9k
max_line_length
int64
0
288k
extension_type
stringclasses
1 value
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/sibling/SFSBTopLevel.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.sibling; import jakarta.annotation.PostConstruct; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionAttribute; import jakarta.ejb.TransactionAttributeType; import jakarta.persistence.EntityManager; import jakarta.persistence.NoResultException; import jakarta.persistence.PersistenceContext; import jakarta.persistence.Query; import jakarta.transaction.UserTransaction; /** * stateful session bean * * @author Scott Marlow */ @Stateful public class SFSBTopLevel { @PersistenceContext(unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB DAO1 otherBean1; // sibling 1 @EJB DAO2 otherBean2; // sibling 2 public String testfunc() { otherBean1.myFunction(); otherBean2.myFunction(); return "fine"; } /** * The PostConstruct callback invocations occur before the first business method invocation on thebean. * This is at a point after which any dependency injection has been performed by the container. */ @PostConstruct public void postconstruct() { //System.out.println("SFSBTopLevel PostConstruct occurred for " + this.toString() + ", current thread=" + Thread.currentThread().getName() + ", all dependency injection has been performed."); } public void createEmployee(String name, String address, int id) { Employee emp = new Employee(); emp.setId(id); emp.setAddress(address); emp.setName(name); em.persist(emp); } // always throws a TransactionRequiredException @TransactionAttribute(TransactionAttributeType.NEVER) public void createEmployeeNoTx(String name, String address, int id) { Employee emp = new Employee(); emp.setId(id); emp.setAddress(address); emp.setName(name); UserTransaction tx1 = sessionContext.getUserTransaction(); try { tx1.begin(); em.joinTransaction(); em.persist(emp); tx1.commit(); } catch (Exception e) { throw new RuntimeException("couldn't start tx", e); } em.flush(); // should throw TransactionRequiredException } @TransactionAttribute(TransactionAttributeType.NEVER) public Employee getEmployeeNoTX(int id) { return em.find(Employee.class, id); } @TransactionAttribute(TransactionAttributeType.NEVER) public String queryEmployeeNameNoTX(int id) { Query q = em.createQuery("SELECT e.name FROM Employee e"); try { String name = (String) q.getSingleResult(); return name; } catch (NoResultException expected) { return "success"; } catch (Exception unexpected) { return unexpected.getMessage(); } } }
3,981
30.109375
199
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/sibling/DAO2.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.sibling; import java.util.Map; import jakarta.annotation.PostConstruct; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; import jakarta.persistence.PersistenceUnit; /** * @author Scott Marlow */ @Stateful public class DAO2 { @PersistenceContext(unitName = "mypc", type = PersistenceContextType.EXTENDED) EntityManager em; @PersistenceUnit(unitName = "mypc") private EntityManagerFactory emf; public Map<String, Object> getEMInfo() { return emf.getProperties(); } /** * The PostConstruct callback invocations occur before the first business method invocation on thebean. * This is at a point after which any dependency injection has been performed by the container. */ @PostConstruct public void postconstruct() { //System.out.println("DAO2 PostConstruct occurred for " + this.toString() +", current thread=" + Thread.currentThread().getName() +", all dependency injection has been performed."); } public void myFunction() { em.find(Employee.class, 123); } }
2,290
34.796875
189
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/sibling/SiblingXPCInheritanceTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.sibling; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * EntityManagerFactory tests * * @author Zbynek Roubalik */ @RunWith(Arquillian.class) public class SiblingXPCInheritanceTestCase { private static final String ARCHIVE_NAME = "jpa_SiblingXPCInheritanceTestCase"; private static final String persistence_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + "<persistence xmlns=\"http://java.sun.com/xml/ns/persistence\" version=\"1.0\">" + " <persistence-unit name=\"mypc\">" + " <description>Persistence Unit." + " </description>" + " <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>" + "<properties> <property name=\"hibernate.hbm2ddl.auto\" value=\"create-drop\"/>" + "</properties>" + " </persistence-unit>" + "</persistence>"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses(SiblingXPCInheritanceTestCase.class, SFSBTopLevel.class, Employee.class, DAO1.class, DAO2.class); jar.addAsResource(new StringAsset(persistence_xml), "META-INF/persistence.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } protected <T> T rawLookup(String name, Class<T> interfaceType) throws NamingException { try { return interfaceType.cast(iniCtx.lookup(name)); } catch (NamingException e) { throw e; } } /** * Test that EntityManagerFactory can be bind to specified JNDI name */ @Test public void testSibling() throws Exception { SFSBTopLevel sfsb1 = lookup("SFSBTopLevel", SFSBTopLevel.class); sfsb1.createEmployee("SiblingTest", "1 home street", 123); sfsb1.testfunc(); // sibling xpc test } }
3,749
38.0625
129
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/sibling/DAO1.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.sibling; import java.util.Map; import jakarta.annotation.PostConstruct; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; import jakarta.persistence.PersistenceUnit; /** * @author Scott Marlow */ @Stateful public class DAO1 { @PersistenceContext(unitName = "mypc", type = PersistenceContextType.EXTENDED) EntityManager em; @PersistenceUnit(unitName = "mypc") private EntityManagerFactory emf; public Map<String, Object> getEMInfo() { return emf.getProperties(); } /** * The PostConstruct callback invocations occur before the first business method invocation on thebean. * This is at a point after which any dependency injection has been performed by the container. */ @PostConstruct public void postconstruct() { } public void myFunction() { em.find(Employee.class, 123); } }
2,100
32.349206
107
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/jarfile/JpaJarFileTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.jarfile; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * Tests that the <jar-file> element of persistence.xml works as expected. * * @author Stuart Douglas * @author Franck Garcia */ @RunWith(Arquillian.class) public class JpaJarFileTestCase { private static final String ARCHIVE_NAME = "jpajarfile.ear"; @Deployment public static Archive<?> deploy() { EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, ARCHIVE_NAME); JavaArchive ejbModule = ShrinkWrap.create(JavaArchive.class, "my-ejb-module.jar"); ejbModule.addClasses(JpaJarFileTestCase.class, JpaTestSlsb.class); ejbModule.addAsManifestResource(JpaJarFileTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); ear.addAsModule(ejbModule); JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "jarfile.jar"); jar.addClass(JarFileEntity.class); jar.addClass(MainArchiveEntity.class); ear.addAsLibrary(jar); return ear; } @Test public void testEntityInMainArchive() throws NamingException { JpaTestSlsb slsb = (JpaTestSlsb) new InitialContext().lookup("java:module/" + JpaTestSlsb.class.getSimpleName()); slsb.testMainArchiveEntity(); } @Test public void testEntityInJarFileArchive() throws NamingException { JpaTestSlsb slsb = (JpaTestSlsb) new InitialContext().lookup("java:module/" + JpaTestSlsb.class.getSimpleName()); slsb.testJarFileEntity(); } }
2,952
37.350649
121
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/jarfile/MainArchiveEntity.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.jarfile; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * @author Scott Marlow */ @Entity public class MainArchiveEntity { @Id private int id; private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1,693
24.666667
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/jarfile/JpaTestSlsb.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.jarfile; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceUnit; import jakarta.persistence.metamodel.EntityType; import org.junit.Assert; /** * @author Stuart Douglas */ @Stateless public class JpaTestSlsb { @PersistenceUnit private EntityManagerFactory emf; @PersistenceContext private EntityManager em; public void testMainArchiveEntity() { final EntityType<MainArchiveEntity> meta = emf.getMetamodel().entity(MainArchiveEntity.class); Assert.assertNotNull("class must be an entity", meta); MainArchiveEntity entity = new MainArchiveEntity(); entity.setId(1); entity.setName("Bob"); entity.setAddress("123 Fake St"); em.persist(entity); em.flush(); } public void testJarFileEntity() { final EntityType<JarFileEntity> meta = emf.getMetamodel().entity(JarFileEntity.class); Assert.assertNotNull("class must be an entity", meta); JarFileEntity entity = new JarFileEntity(); entity.setId(1); entity.setName("Bob"); entity.setAddress("123 Fake St"); em.persist(entity); em.flush(); } }
2,367
34.343284
102
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/jarfile/JarFileEntity.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.jarfile; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * @author Scott Marlow */ @Entity public class JarFileEntity { @Id private int id; private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1,689
24.606061
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/IntermediateStatefulInterface.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ public interface IntermediateStatefulInterface { boolean execute(Integer id, String name) throws Exception; }
1,281
41.733333
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/StatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import java.util.Locale; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.Local; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionManagement; import jakarta.ejb.TransactionManagementType; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.transaction.UserTransaction; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Stateful @TransactionManagement(TransactionManagementType.BEAN) @Local(StatefulInterface.class) public class StatefulBean extends AbstractStatefulInterface { @PersistenceContext(unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB StatelessInterface cmtBean; public boolean execute(Integer id, String name) throws Exception { try { UserTransaction tx1 = sessionContext.getUserTransaction(); tx1.begin(); em.joinTransaction(); MyEntity entity = em.find(MyEntity.class, id); entity.setName(name.toUpperCase(Locale.ENGLISH)); String propagatedName = cmtBean.updateEntity(id, name.toLowerCase(Locale.ENGLISH)); tx1.commit(); return propagatedName.equals(name.toUpperCase(Locale.ENGLISH)); } catch (Exception e) { try { sessionContext.getUserTransaction().rollback(); } catch (Exception e1) { System.out.println("ROLLBACK: " + e1); } throw e; } } }
2,676
35.175676
95
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/NoTxEPCStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import java.util.Locale; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.Local; import jakarta.ejb.Remove; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionManagement; import jakarta.ejb.TransactionManagementType; import javax.naming.NamingException; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Stateful @TransactionManagement(TransactionManagementType.BEAN) @Local(StatefulInterface.class) public class NoTxEPCStatefulBean extends AbstractStatefulInterface { @PersistenceContext(type = PersistenceContextType.EXTENDED, unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB StatelessInterface cmtBean; public boolean execute(Integer id, String name) throws Exception { MyEntity eb = em.find(MyEntity.class, id); eb.setName(name.toUpperCase(Locale.ENGLISH)); String propagatedName = cmtBean.updateEntity(id, name.toLowerCase(Locale.ENGLISH)); return propagatedName.equals(name.toUpperCase(Locale.ENGLISH)); } public boolean createEntity(Integer id, String name) throws Exception { boolean result = true; cmtBean.createEntity(id, name); // entity is created in XPC (will not be persisted to DB) // first test is that created entity propagated to stateful_2ndSFSBInvocation that inherits XPC from calling SFSB StatefulInterface stateful_2ndSFSBInvocation = lookup("NoTxEPCStatefulBean", StatefulInterface.class); stateful_2ndSFSBInvocation.execute(8, "EntityName"); // NPE Exception will occur if entity isn't found. success is making it the next line // repeat same test once more StatefulInterface stateful_3rdSFSBInvocation = lookup("NoTxEPCStatefulBean", StatefulInterface.class); stateful_3rdSFSBInvocation.execute(8, "EntityName"); // NPE Exception will occur if entity isn't found. success is making it the next line stateful_2ndSFSBInvocation.finishUp(); stateful_3rdSFSBInvocation.finishUp(); // transaction entity manager should still be able to find/update the entity cmtBean.updateEntity(id, name + " and Emma Peel"); MyEntity eb = em.find(MyEntity.class, id); result = (eb != null); // true if we find the entity in our XPC, false otherwise return result; } public StatefulInterface createSFSBOnInvocation() throws Exception { return lookup("NoTxEPCStatefulBean", StatefulInterface.class); } public StatelessInterface createSLSBOnInvocation() throws Exception { return lookup("StatelessBean", StatelessInterface.class); } protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(sessionContext.lookup("java:module/" + beanName + "!" + interfaceType.getName())); } public EntityManager getExtendedPersistenceContext() { return em; } @Remove public void finishUp() { } }
4,321
36.912281
121
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/IntermediateStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import java.util.Locale; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.Local; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Stateful @Local public class IntermediateStatefulBean implements IntermediateStatefulInterface { @PersistenceContext(unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB StatelessInterface cmtBean; public boolean execute(Integer id, String name) throws Exception { MyEntity entity = em.find(MyEntity.class, id); String propagatedName = cmtBean.updateEntity(id, name.toLowerCase(Locale.ENGLISH)); return propagatedName.equals(name.toUpperCase(Locale.ENGLISH)); } }
1,989
33.912281
91
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/StatelessBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import jakarta.annotation.Resource; import jakarta.ejb.Local; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateless; import jakarta.ejb.TransactionAttribute; import jakarta.ejb.TransactionAttributeType; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Stateless @Local public class StatelessBean implements StatelessInterface { @PersistenceContext(unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @TransactionAttribute(TransactionAttributeType.REQUIRED) public void createEntity(Integer id, String name) { MyEntity entity = em.find(MyEntity.class, id); if (entity == null) { entity = new MyEntity(); entity.setId(id); em.persist(entity); } entity.setName(name); } /** * can only be called from a SFSB with an extended persistence context * * @param id * @param name */ @TransactionAttribute(TransactionAttributeType.NEVER) public void createEntityNoTx(Integer id, String name) { } @TransactionAttribute(TransactionAttributeType.REQUIRED) public String updateEntity(Integer id, String name) { MyEntity entity = em.find(MyEntity.class, id); String propagatedName = entity.getName(); entity.setName(name); return propagatedName; } }
2,567
31.923077
74
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/CMTStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import java.util.Locale; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.Local; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Stateful @Local(StatefulInterface.class) public class CMTStatefulBean extends AbstractStatefulInterface { @PersistenceContext(unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB StatelessInterface cmtBean; public boolean execute(Integer id, String name) throws Exception { MyEntity eb = em.find(MyEntity.class, id); eb.setName(name.toUpperCase(Locale.ENGLISH)); String propagatedName = cmtBean.updateEntity(id, name.toLowerCase(Locale.ENGLISH)); return propagatedName.equals(name.toUpperCase(Locale.ENGLISH)); } }
2,047
34.929825
91
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/InitEPCStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import java.util.Locale; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.Local; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionManagement; import jakarta.ejb.TransactionManagementType; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; import jakarta.transaction.UserTransaction; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Stateful @TransactionManagement(TransactionManagementType.BEAN) @Local(StatefulInterface.class) public class InitEPCStatefulBean extends AbstractStatefulInterface { @PersistenceContext(type = PersistenceContextType.EXTENDED, unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB(beanName = "IntermediateStatefulBean") IntermediateStatefulInterface cmtBean; public boolean execute(Integer id, String name) throws Exception { try { UserTransaction tx1 = sessionContext.getUserTransaction(); tx1.begin(); em.joinTransaction(); MyEntity entity = em.find(MyEntity.class, id); entity.setName(name.toUpperCase(Locale.ENGLISH)); boolean result = cmtBean.execute(id, name.toLowerCase(Locale.ENGLISH)); tx1.commit(); return result; } catch (Exception e) { try { sessionContext.getUserTransaction().rollback(); } catch (Exception e1) { System.out.println("ROLLBACK: " + e1); } throw e; } } }
2,764
35.381579
83
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/NoTxStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import java.util.Locale; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.Local; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionManagement; import jakarta.ejb.TransactionManagementType; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Stateful @TransactionManagement(TransactionManagementType.BEAN) @Local(StatefulInterface.class) public class NoTxStatefulBean extends AbstractStatefulInterface { @PersistenceContext(unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB StatelessInterface cmtBean; public boolean execute(Integer id, String name) throws Exception { MyEntity eb = em.find(MyEntity.class, id); eb.setName(name.toUpperCase(Locale.ENGLISH)); String propagatedName = cmtBean.updateEntity(id, name.toLowerCase(Locale.ENGLISH)); return propagatedName.equals(name.toUpperCase(Locale.ENGLISH)); } }
2,191
35.533333
91
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/StatefulInterface.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import jakarta.ejb.Local; import jakarta.persistence.EntityManager; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Local public interface StatefulInterface { boolean execute(Integer id, String name) throws Exception; String getPostConstructErrorMessage() throws Exception; /** * @param id * @param name * @return true for success * @throws Exception */ boolean createEntity(Integer id, String name) throws Exception; StatefulInterface createSFSBOnInvocation() throws Exception; EntityManager getExtendedPersistenceContext(); void finishUp() throws Exception; }
1,741
33.84
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/EPCStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import java.util.Locale; import jakarta.annotation.PostConstruct; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.Local; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionManagement; import jakarta.ejb.TransactionManagementType; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; import jakarta.transaction.UserTransaction; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Stateful @TransactionManagement(TransactionManagementType.BEAN) @Local public class EPCStatefulBean extends AbstractStatefulInterface implements StatefulInterface { @PersistenceContext(type = PersistenceContextType.EXTENDED, unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB StatelessInterface cmtBean; private String constructError = null; @PostConstruct public void init() { try { em.createQuery("select f from MyEntity f"); } catch (Exception e) { constructError = e.getMessage(); } } public boolean execute(Integer id, String name) throws Exception { try { UserTransaction tx1 = sessionContext.getUserTransaction(); tx1.begin(); em.joinTransaction(); MyEntity entity = em.find(MyEntity.class, id); entity.setName(name.toUpperCase(Locale.ENGLISH)); String propagatedName = cmtBean.updateEntity(id, name.toLowerCase(Locale.ENGLISH)); tx1.commit(); return propagatedName.equals(name.toUpperCase(Locale.ENGLISH)); } catch (Exception e) { try { sessionContext.getUserTransaction().rollback(); } catch (Exception e1) { System.out.println("ROLLBACK: " + e1); } throw e; } } public String getPostConstructErrorMessage() throws Exception { return constructError; } @Override public boolean createEntity(Integer id, String name) throws Exception { return false; } @Override public StatefulInterface createSFSBOnInvocation() throws Exception { return null; } }
3,410
31.485714
95
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/CMTEPCStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import java.util.Locale; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.Local; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Stateful @Local(StatefulInterface.class) public class CMTEPCStatefulBean extends AbstractStatefulInterface { @PersistenceContext(type = PersistenceContextType.EXTENDED, unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB StatelessInterface cmtBean; public boolean execute(Integer id, String name) throws Exception { MyEntity eb = em.find(MyEntity.class, id); eb.setName(name.toUpperCase(Locale.ENGLISH)); String propagatedName = cmtBean.updateEntity(id, name.toLowerCase(Locale.ENGLISH)); return propagatedName.equals(name.toUpperCase(Locale.ENGLISH)); } }
2,141
35.931034
91
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/EPCPropagationTestCase.java
/* * JBoss, Home of Professional Open Source * Copyright 2011, Red Hat Middleware LLC, and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import javax.naming.InitialContext; import javax.naming.NamingException; import jakarta.persistence.EntityManager; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * For managed debugging: * -Djboss.options="-Djava.compiler=NONE -agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=y" * <p/> * For embedded debugging: * start AS standalone and attach debugger * mvn install -Premote * * @author <a href="mailto:[email protected]">William DeCoste</a> * @author Scott Marlow */ @RunWith(Arquillian.class) public class EPCPropagationTestCase { @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class); jar.addClasses( CMTEPCStatefulBean.class, CMTStatefulBean.class, EPCStatefulBean.class, InitEPCStatefulBean.class, IntermediateStatefulBean.class, IntermediateStatefulInterface.class, MyEntity.class, NoTxEPCStatefulBean.class, NoTxStatefulBean.class, StatefulBean.class, StatefulInterface.class, StatelessBean.class, StatelessInterface.class, AbstractStatefulInterface.class, EPCPropagationTestCase.class ); jar.addAsManifestResource(EPCPropagationTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:module/" + beanName + "!" + interfaceType.getName())); } @Test public void testBMTPropagation() throws Exception { StatelessInterface stateless = lookup("StatelessBean", StatelessInterface.class); stateless.createEntity(1, "EntityName"); StatefulInterface stateful = lookup("StatefulBean", StatefulInterface.class); boolean equal = stateful.execute(1, "EntityName"); assertTrue("Name changes should propagate", equal); } @Test public void testBMTEPCPropagation() throws Exception { StatelessInterface stateless = lookup("StatelessBean", StatelessInterface.class); stateless.createEntity(2, "EntityName"); StatefulInterface stateful = lookup("EPCStatefulBean", StatefulInterface.class); boolean equal = stateful.execute(2, "EntityName"); assertTrue("Name changes should propagate", equal); } @Test public void testCMTPropagation() throws Exception { StatelessInterface stateless = lookup("StatelessBean", StatelessInterface.class); stateless.createEntity(3, "EntityName"); StatefulInterface stateful = lookup("CMTStatefulBean", StatefulInterface.class); boolean equal = stateful.execute(3, "EntityName"); assertTrue("Name changes should propagate", equal); } @Test public void testCMTEPCPropagation() throws Exception { StatelessInterface stateless = lookup("StatelessBean", StatelessInterface.class); stateless.createEntity(4, "EntityName"); StatefulInterface stateful = lookup("CMTEPCStatefulBean", StatefulInterface.class); boolean equal = stateful.execute(4, "EntityName"); assertTrue("Name changes should propagate", equal); } @Test public void testNoTxPropagation() throws Exception { StatelessInterface stateless = lookup("StatelessBean", StatelessInterface.class); stateless.createEntity(5, "EntityName"); StatefulInterface stateful = lookup("NoTxStatefulBean", StatefulInterface.class); boolean equal = stateful.execute(5, "EntityName"); assertFalse("Name changes should not propagate", equal); } /** * Ensure that AS7-1663 is fixed, extended persistence context should not be propagated if there is no JTA transaction. * * @throws Exception */ @Test public void testNoTxEPCPropagation() throws Exception { StatelessInterface stateless = lookup("StatelessBean", StatelessInterface.class); stateless.createEntity(6, "EntityName"); StatefulInterface stateful = lookup("NoTxEPCStatefulBean", StatefulInterface.class); boolean equal = stateful.execute(6, "EntityName"); assertFalse("Name changes should not propagate (non-tx SFSB XPC shouldn't see SLSB PC changes)", equal); } @Test public void testIntermediateEPCPropagation() throws Exception { StatelessInterface stateless = lookup("StatelessBean", StatelessInterface.class); stateless.createEntity(7, "EntityName"); StatefulInterface stateful = lookup("InitEPCStatefulBean", StatefulInterface.class); boolean equal = stateful.execute(7, "EntityName"); assertTrue("Name changes should propagate", equal); } @Test public void testXPCPostConstruct() throws Exception { StatefulInterface stateful = lookup("EPCStatefulBean", StatefulInterface.class); assertNull("stateful postConstruct operation should success: " + stateful.getPostConstructErrorMessage(), stateful.getPostConstructErrorMessage()); } /** * Jakarta Persistence 7.6.2 XPC is closed when dependent session bean(s) are closed/destroyed. * <p> * During this test, an entity (X) will be created in the XPC but not persisted to the database. * When the last SFSB referencing the XPC is closed, the entity (X) will no longer be found. * * @throws Exception */ @Test public void testNoTxEPCRemoveOperation() throws Exception { StatefulInterface stateful = lookup("NoTxEPCStatefulBean", StatefulInterface.class); boolean equal = stateful.createEntity(8, "EntityName X"); assertTrue("XPC inheritance should copy entity to other SFSB created on SFSB invocation call", equal); // stateful should be the only SFSB left // create another sfsb on the invocation to stateful (should inherit the same XPC used above. StatefulInterface anotherStateful = stateful.createSFSBOnInvocation(); stateful.finishUp(); // destroy SFSB stateful = null; // clear reference to avoid using it by accident // both entities (8,9) should still be in XPC equal = anotherStateful.createEntity(9, "John Steed"); assertTrue("again, XPC inheritance should copy entity to other SFSB created on SFSB invocation call", equal); EntityManager xpc = anotherStateful.getExtendedPersistenceContext(); assertTrue("extended persistence context is still open", xpc.isOpen()); anotherStateful.finishUp(); assertFalse("extended persistence context is closed after last referencing SFSB is destroyed", xpc.isOpen()); } }
8,282
40.415
155
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/StatelessInterface.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ public interface StatelessInterface { void createEntity(Integer id, String name); void createEntityNoTx(Integer id, String name); String updateEntity(Integer id, String name); }
1,359
39
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/MyEntity.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import java.io.Serializable; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * @author <a href="mailto:[email protected]">William DeCoste</a> */ @Entity public class MyEntity implements Serializable { private Integer id; private String name; public MyEntity() { } public MyEntity(String name) { this.name = name; } @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "MyEntity:id=" + id + ",name=" + name; } }
1,820
27.015385
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/AbstractStatefulInterface.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation; import jakarta.persistence.EntityManager; /** * @author Scott Marlow */ public abstract class AbstractStatefulInterface implements StatefulInterface { public String getPostConstructErrorMessage() throws Exception { return null; } public boolean createEntity(Integer id, String name) throws Exception { return true; } public StatefulInterface createSFSBOnInvocation() throws Exception { return null; } public EntityManager getExtendedPersistenceContext() { return null; } public void finishUp() { } }
1,673
31.823529
78
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/hierarchy/EPCPropagationHierarchyTestCase.java
/* * JBoss, Home of Professional Open Source * Copyright 2011, Red Hat Middleware LLC, and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.hierarchy; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * AS7-1121 Tests that Jakarta Persistence interceptors are correctly registered if injection is done into the superclass of a SFSB * <p> * TODO: AS7-1120 Also tests that persistence context inheritance (Jakarta Persistence 7.6.2.1) works correctly when injecting one SFSB into another directly * * @author Stuart Douglas */ @RunWith(Arquillian.class) public class EPCPropagationHierarchyTestCase { private static final String ARCHIVE_NAME = "epc-hierarchy-propagation"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addPackage(EPCPropagationHierarchyTestCase.class.getPackage()); jar.addAsManifestResource(EPCPropagationHierarchyTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } @Test public void testPersistenceContextPropagation() throws Exception { ChildStatefulBean stateful = lookup("ChildStatefulBean", ChildStatefulBean.class); stateful.testPropagation(); } }
2,888
39.690141
157
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/hierarchy/Bus.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.hierarchy; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * @author Stuart Douglas */ @Entity public class Bus { private Integer id; private String name; public Bus() { } public Bus(int id, String name) { this.name = name; this.id = id; } @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "Bus:id=" + id + ",name=" + name; } }
1,730
25.227273
73
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/hierarchy/ChildStatefulBean.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.hierarchy; import jakarta.ejb.EJB; import jakarta.ejb.Stateful; import javax.naming.InitialContext; import org.junit.Assert; /** * @author Stuart Douglas */ @Stateful public class ChildStatefulBean extends BeanParent { @EJB private SimpleStatefulBean injectedSimpleStatefulBean; public void testPropagation() throws Exception { SimpleStatefulBean simpleStatefulBean = (SimpleStatefulBean) new InitialContext().lookup("java:module/" + SimpleStatefulBean.class.getSimpleName()); Bus b = new Bus(1, "My Bus"); entityManager.persist(b); //the XPC should propagate simpleStatefulBean.noop(); injectedSimpleStatefulBean.noop(); Assert.assertTrue(simpleStatefulBean.contains(b)); Assert.assertTrue(injectedSimpleStatefulBean.contains(b)); Assert.assertTrue(entityManager.contains(b)); } }
1,950
37.254902
156
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/hierarchy/SimpleStatefulBean.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.hierarchy; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * @author Stuart Douglas */ @Stateful public class SimpleStatefulBean { @PersistenceContext(type = PersistenceContextType.EXTENDED) private EntityManager entityManager; public void noop() { } public boolean contains(Bus b) { return entityManager.contains(b); } }
1,562
31.5625
73
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/hierarchy/BeanParent.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.hierarchy; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * @author Stuart Douglas */ public class BeanParent { @PersistenceContext(type = PersistenceContextType.EXTENDED) protected EntityManager entityManager; }
1,397
36.783784
73
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/contextduel/Employee.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.contextduel; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * Employee entity class * * @author Scott Marlow */ @Entity public class Employee { @Id private int id; private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1,730
24.835821
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/contextduel/BMTEPCStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.contextduel; import jakarta.annotation.Resource; import jakarta.ejb.EJB; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionManagement; import jakarta.ejb.TransactionManagementType; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; import jakarta.transaction.HeuristicMixedException; import jakarta.transaction.HeuristicRollbackException; import jakarta.transaction.NotSupportedException; import jakarta.transaction.RollbackException; import jakarta.transaction.SystemException; /** * Test JPA 2.0 section 7.6.3.1 * " * If the component is a stateful session bean to which an extended persistence context has been * bound and there is a different persistence context bound to the JTA transaction, * an EJBException is thrown by the container. * " * * @author Scott Marlow */ @Stateful @TransactionManagement(TransactionManagementType.BEAN) public class BMTEPCStatefulBean { @PersistenceContext(type = PersistenceContextType.EXTENDED, unitName = "mypc") EntityManager em; @Resource SessionContext sessionContext; @EJB(beanName = "CMTPCStatefulBean") CMTPCStatefulBean cmtpcStatefulBean; // expect EJBException to be thrown for success, anything else thrown is an error public void shouldThrowError() throws SystemException, NotSupportedException, RollbackException, HeuristicRollbackException, HeuristicMixedException { Employee emp = new Employee(); emp.setAddress("1003 Main Street"); emp.setName("Sharon Sells"); emp.setId(1); em.persist(emp); // XPC has new entity, not yet saved to db sessionContext.getUserTransaction().begin(); // XPC is still isolated from this started TX // start normal persistence context action, which will first be associated with TX Employee ensureIsolation = cmtpcStatefulBean.getEmp(1); if (ensureIsolation != null) { // if XPC was saved to the DB, then the XPC support has a bug throw new RuntimeException("XPC leaked, should of been isolated from new transaction"); } // the XPC is still isolated from the started TX, cmtpcStatefulBean.em should be associated with the TX // if we invoke an operation on the BMTEPCStatefulBean.em, an EJBException should be thrown Employee shouldOfBeenAnError = em.find(Employee.class, 1); throw new RuntimeException("EJBException not thrown when attempting to propagate EPC into TX that already has PC " + ", look at ExtendedEntityManager logic for likely cause, find returned = " + shouldOfBeenAnError ); //sessionContext.getUserTransaction().commit(); } public void shouldNotThrowError() throws SystemException, NotSupportedException, RollbackException, HeuristicRollbackException, HeuristicMixedException { Employee emp = new Employee(); emp.setAddress("1003 Main Street"); emp.setName("Sharon Sells"); emp.setId(1); em.persist(emp); // XPC has new entity, not yet saved to db sessionContext.getUserTransaction().begin(); emp = em.find(Employee.class, 1); // XPC should be associated with TX emp = em.find(Employee.class, 1); // this second call would trigger AS7-1673 if (emp == null) { throw new RuntimeException("expected XPC search to find entity"); } // start normal persistence context action, which should use the XPC emp = cmtpcStatefulBean.getEmp(1); if (emp == null) { throw new RuntimeException("expected PC search to use XPC and find entity"); } sessionContext.getUserTransaction().commit(); emp = cmtpcStatefulBean.getEmp(1); // XPC should be flushed to the db, search in new TX should work if (emp == null) { throw new RuntimeException("expected DB search with new TX, to find entity"); } } }
5,152
40.224
124
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/contextduel/CMTPCStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.contextduel; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * CMT stateful bean * * @author Scott Marlow */ @Stateful public class CMTPCStatefulBean { @PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "mypc") EntityManager em; public Employee getEmp(int id) { return em.find(Employee.class, id); } }
1,562
34.522727
85
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/contextduel/AddEPC2TxAfterPCTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.contextduel; import jakarta.ejb.EJBException; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * Test JPA 2.0 section 7.6.3.1 * " * If the component is a stateful session bean to which an extended persistence context has been * bound and there is a different persistence context bound to the JTA transaction, * an EJBException is thrown by the container. * " * * @author Scott Marlow */ @RunWith(Arquillian.class) public class AddEPC2TxAfterPCTestCase { private static final String ARCHIVE_NAME = "jpa_AddEPC2TxAfterPCTestCase"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses( AddEPC2TxAfterPCTestCase.class, Employee.class, BMTEPCStatefulBean.class, CMTPCStatefulBean.class); jar.addAsManifestResource(AddEPC2TxAfterPCTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { try { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } catch (NamingException e) { throw e; } } @Test public void testShouldGetEjbExceptionBecauseEPCIsAddedToTxAfterPc() throws Exception { BMTEPCStatefulBean stateful = lookup("BMTEPCStatefulBean", BMTEPCStatefulBean.class); try { stateful.shouldThrowError(); } catch (EJBException expected) { // success } } @Test public void testShouldNotGetError() throws Exception { BMTEPCStatefulBean stateful = lookup("BMTEPCStatefulBean", BMTEPCStatefulBean.class); stateful.shouldNotThrowError(); } }
3,408
34.884211
133
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/shallow/TopLevelBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.shallow; import jakarta.ejb.EJB; import jakarta.ejb.Stateful; /** * top-level stateful bean that has no extended persistence context of its own * * @author Scott Marlow */ @Stateful public class TopLevelBean { @EJB FirstDAO firstDAO; @EJB SecondDAO secondDAO; /** * Failure is expected when the SecondAO bean is invoked, which will bring in a * separate extended persistence context */ public void referenceTwoDistinctExtendedPersistenceContextsInSameTX_fail() { firstDAO.noop(); secondDAO.noop(); } /** * not expected to Fail (created bean should use the same XPC) */ public void induceCreationViaJNDILookup() { firstDAO.induceCreationViaJNDILookup(); } /** * not expected to Fail (created bean should use the same XPC) */ public void induceCreationViaTwoLevelJNDILookup() { firstDAO.induceTwoLevelCreationViaJNDILookup(); } }
2,042
29.044118
83
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/shallow/SecondDAO.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.shallow; import jakarta.annotation.Resource; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * DAO stateful bean that has its own isolated extended persistence context * its simbling bean will have its own instance of the extended persistence context * * @author Scott Marlow */ @Stateful public class SecondDAO { @Resource SessionContext ctx; @PersistenceContext(unitName = "shallow_pu", type = PersistenceContextType.EXTENDED) private EntityManager em; public void noop() { } /** * no failures should occur during this test method. If the extended persistence context is not shared as expected, * an EJBException will be thrown (indicating failure). */ public void induceCreationViaJNDILookup() { FirstDAO secondDAO = (FirstDAO) ctx.lookup("java:module/FirstDAO"); // create an instance of FirstDAO that will share // the same extended persistence context secondDAO.noop(); // any failures should of already occurred, calling noop just gives an additional // change to check for other unexpected failures. } }
2,363
34.283582
132
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/shallow/ShallowExtendedPersistenceContextInheritanceTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.shallow; import static org.junit.Assert.assertNotNull; import jakarta.ejb.EJBException; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * Test JPA 2.0 section 7.6.2.1 * <p> * Inheritance of Extended Persistence Context * <p> * " * If a stateful session bean instantiates a stateful session bean (executing in the same EJB container instance) * which also has such an extended persistence context, the extended persistence context of the first stateful * session bean is inherited by the second stateful session bean and bound to it, and this rule recursively * applies—independently of whether transactions are active or not at the point of the creation of the stateful * session beans. * " * <p> * This tests the "shallow" inheritance where the above only applies to parent/child relationships (not recursively * up and vertically which is "deep" inheritance). * * @author Scott Marlow */ @RunWith(Arquillian.class) public class ShallowExtendedPersistenceContextInheritanceTestCase { private static final String ARCHIVE_NAME = "jpa_ShallowExtendedPersistenceContextInheritanceTestCase"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses( ShallowExtendedPersistenceContextInheritanceTestCase.class, SecondDAO.class, FirstDAO.class, TopLevelBean.class); jar.addAsManifestResource(ShallowExtendedPersistenceContextInheritanceTestCase.class.getPackage(), "jboss-all.xml", "jboss-all.xml"); jar.addAsManifestResource(ShallowExtendedPersistenceContextInheritanceTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { try { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } catch (NamingException e) { throw e; } } /** * With shallow extended persistence context inheritance, sibling beans do not inherit the XPC from each other. * If sibling beans are involved in the same transaction, an EJBException will be thrown. * This test ensures that the EJBException is thrown. * * @throws Exception */ @Test public void testShouldGetEjbExceptionBecauseEPCIsAddedToTxAfterPc() throws Exception { TopLevelBean topLevelBean = lookup("TopLevelBean", TopLevelBean.class); Throwable error = null; // excepted error will be something like: // jakarta.ejb.EJBException: WFLYJPA0030: // Found extended persistence context in SFSB invocation call stack but that cannot be used // because the transaction already has a transactional context associated with it... try { topLevelBean.referenceTwoDistinctExtendedPersistenceContextsInSameTX_fail(); } catch (EJBException caught) { error = caught; } assertNotNull("with shallow inheritance, sibling beans do not inherit extended persistence " + "context from each other (unless ExtendedPersistenceInheritance.DEEP is specified)", error); } /** * With both DEEP and SHALLOW extended persistence context inheritance, a bean creating another local bean via * JNDI lookup will also use the extended persistence context inheritance rules. This tests that the JNDI lookup * does use the same XPC. * <p> * See http://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-06/message/28 for more details than * the JPA 2.0 specification includes. * * @throws Exception */ @Test public void testChildInheritanceFromParentExtendedPersistenceContextViaBusinessMethodUsingJndiLookup() throws Exception { TopLevelBean topLevelBean = lookup("TopLevelBean", TopLevelBean.class); topLevelBean.induceCreationViaJNDILookup(); } /** * With both DEEP and SHALLOW extended persistence context inheritance, a bean creating another local bean via * JNDI lookup will also use the extended persistence context inheritance rules. This tests that two levels of JNDI * lookup does use the same XPC. * <p> * See http://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-06/message/28 for more details than * the JPA 2.0 specification includes. * * @throws Exception */ @Test public void testChildInheritanceFromParentExtendedPersistenceContextViaBusinessMethodUsingTwoLevelJndiLookup() throws Exception { TopLevelBean topLevelBean = lookup("TopLevelBean", TopLevelBean.class); topLevelBean.induceCreationViaJNDILookup(); } }
6,377
41.238411
145
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/shallow/FirstDAO.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.shallow; import jakarta.annotation.Resource; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * DAO stateful bean that has its own isolated extended persistence context * its simbling bean will have its own instance of the extended persistence context * * @author Scott Marlow */ @Stateful public class FirstDAO { @Resource SessionContext ctx; @PersistenceContext(unitName = "shallow_pu", type = PersistenceContextType.EXTENDED) private EntityManager em; public void noop() { } /** * no failures should occur during this test method. If the extended persistence context is not shared as expected, * an EJBException will be thrown (indicating failure). */ public void induceCreationViaJNDILookup() { SecondDAO secondDAO = (SecondDAO) ctx.lookup("java:module/SecondDAO"); // create an instance of SecondDAO that will share // the same extended persistence context secondDAO.noop(); // any failures should of already occurred, calling noop just gives an additional // change to check for other unexpected failures. } /** * no failures should occur during this test method. If the extended persistence context is not shared as expected, * an EJBException will be thrown (indicating failure). */ public void induceTwoLevelCreationViaJNDILookup() { SecondDAO secondDAO = (SecondDAO) ctx.lookup("java:module/SecondDAO"); // create an instance of SecondDAO that will share // the same extended persistence context secondDAO.induceCreationViaJNDILookup(); // second level of creation via JNDI lookup } }
2,909
33.642857
133
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/requiresnew/EPCPropagationNewTransactionTestCase.java
/* * JBoss, Home of Professional Open Source * Copyright 2011, Red Hat Middleware LLC, and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.requiresnew; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.junit.InSequence; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * Tests that the propagation rules for Extended Persistence Contexts respect nestled transactions. * <p> * As a transaction scoped EM has already been bound to the transaction, if REQUIRES_NEW is not used then * an exception will be thrown by the SFSB with the EPC. * * @author Stuart Douglas */ @RunWith(Arquillian.class) public class EPCPropagationNewTransactionTestCase { private static final String ARCHIVE_NAME = "epc-hierarchy-propagation"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addPackage(EPCPropagationNewTransactionTestCase.class.getPackage()); jar.addAsManifestResource(EPCPropagationNewTransactionTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } @Test @InSequence(1) public void testRequiresNewXPCPropagation() throws Exception { BikeManagerBean stateful = lookup(BikeManagerBean.class.getSimpleName(), BikeManagerBean.class); stateful.runTest(); } @Test @InSequence(2) public void testXPCIsAssociatedWithTX() throws Exception { BikeManagerBean stateful = lookup(BikeManagerBean.class.getSimpleName(), BikeManagerBean.class); stateful.runTest2(); } }
3,177
38.725
129
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/requiresnew/BikeManagerBean.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.requiresnew; import jakarta.ejb.EJB; import jakarta.ejb.Stateless; import org.junit.Assert; /** * @author Stuart Douglas */ @Stateless public class BikeManagerBean { @EJB private BikeRace bikeRace; public void runTest() { //this enlists the transaction scoped em into the transaction Assert.assertTrue(bikeRace.allMotorBikes().isEmpty()); Motorbike bike = bikeRace.createNewBike(1, "Bike1"); Assert.assertFalse(bikeRace.contains(bike)); } @EJB private BikeShop bikeShop; /** * When a XPC with pending DB changes should be associated with Jakarta Transactions TX when SFSB enters the SFSB transactional method. * as per JPA 7.9.1 Container Responsibilities for XPC: * When a business method of the stateful session bean is invoked, * if the stateful session bean uses container managed transaction demarcation, * and the entity manager is not already associated with the current Jakarta Transactions transaction, * the container associates the entity manager with the current Jakarta Transactions transaction and * calls EntityManager.joinTransaction. */ public void runTest2() { Motorbike bike = bikeShop.downPaymentOnBikeNoTx(2, "Bike2"); Assert.assertNotNull("newly created bike not be null", bike); bikeShop.purchaseNowAndFlushDbChanges(); Assert.assertNotNull("should be able to find bike in database after purchaseNowAndFlushDbChanges.", bikeShop.find(2)); try { bikeShop.forceRollback(2); } catch (RuntimeException ignore) {} Assert.assertNotNull("extended persistence context must be associated with Jakarta Transactions tx during " + "call to purchaseNowAndFlushDbChanges() and db changes saved when that method ends its Jakarta Transactions tx.", bikeRace.find(2)); } }
2,959
38.466667
148
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/requiresnew/BikeShop.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.requiresnew; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionAttribute; import jakarta.ejb.TransactionAttributeType; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * SFSB with REQUIRES_NEW transaction attribute. Even though a transaction scoped * em has been enrolled in the outer transaction, this bean should still work. * * @author Stuart Douglas */ @Stateful @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public class BikeShop { @PersistenceContext(type = PersistenceContextType.EXTENDED) protected EntityManager entityManager; public Motorbike createMotorBike(int id, String name) { Motorbike bike = new Motorbike(id, name); entityManager.persist(bike); entityManager.flush(); return bike; } /** * create new motor bike but don't save to the DB until purchaseNow is invoked. * * @param id * @param name * @return */ @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public Motorbike downPaymentOnBikeNoTx(int id, String name) { Motorbike bike = new Motorbike(id, name); entityManager.persist(bike); return bike; } /** * save MotorBikes that we previously put a down payment on * pending DB changes should be flushed as per JPA 7.9.1 Container Responsibilities for XPC: * When a business method of the stateful session bean is invoked, * if the stateful session bean uses container managed transaction demarcation, * and the entity manager is not already associated with the current JTA transaction, * the container associates the entity manager with the current JTA transaction and * calls EntityManager.joinTransaction. */ public void purchaseNowAndFlushDbChanges() { } public void forceRollback(int id) { // purchaseNow should of saved the MotorBike, so we should be able to find the MotorBike in the db Motorbike bike = entityManager.find(Motorbike.class, id); throw new RuntimeException("for ejb container to rollback tx"); } public Motorbike find(int id) { Motorbike bike = entityManager.find(Motorbike.class, id); return bike; } }
3,391
36.688889
106
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/requiresnew/Motorbike.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.requiresnew; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * @author Stuart Douglas */ @Entity public class Motorbike { private Integer id; private String name; public Motorbike() { } public Motorbike(int id, String name) { this.name = name; this.id = id; } @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "Motorbike:id=" + id + ",name=" + name; } }
1,756
25.621212
73
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/requiresnew/BikeRace.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.requiresnew; import java.util.List; import jakarta.ejb.EJB; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * @author Stuart Douglas */ @Stateless public class BikeRace { @PersistenceContext private EntityManager entityManager; @EJB private BikeShop bikeShop; public List<Motorbike> allMotorBikes() { return entityManager.createQuery("Select m from Motorbike m").getResultList(); } /** * As this em is enrolled in the transaction, if BikeShop executed in the context of this transaction * it would throw an exception, as as PC has already been bound to the current transaction. * * @return */ public Motorbike createNewBike(int id, String name) { return bikeShop.createMotorBike(id, name); } public Motorbike find(int id) { return bikeShop.find(id); } public boolean contains(Object object) { return entityManager.contains(object); } }
2,104
31.384615
105
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/unsync/Employee.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.unsync; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * Employee entity class * * @author Scott Marlow */ @Entity public class Employee { @Id private int id; private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1,725
24.761194
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/unsync/BMTEPCStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.unsync; import jakarta.annotation.Resource; import jakarta.ejb.SessionContext; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionManagement; import jakarta.ejb.TransactionManagementType; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; import jakarta.persistence.SynchronizationType; import jakarta.transaction.HeuristicMixedException; import jakarta.transaction.HeuristicRollbackException; import jakarta.transaction.NotSupportedException; import jakarta.transaction.RollbackException; import jakarta.transaction.SystemException; import jakarta.transaction.UserTransaction; /** * " * If there is a persistence context of type SynchronizationType.UNSYNCHRONIZED * associated with the Jakarta Transactions transaction and the target component specifies a persistence context of * type SynchronizationType.SYNCHRONIZED, the IllegalStateException is * thrown by the container. * " * * @author Scott Marlow */ @ApplicationScoped @Stateful @TransactionManagement(TransactionManagementType.BEAN) public class BMTEPCStatefulBean { @PersistenceContext(synchronization= SynchronizationType.UNSYNCHRONIZED, type = PersistenceContextType.EXTENDED, unitName = "mypc") EntityManager em; @PersistenceContext(synchronization= SynchronizationType.UNSYNCHRONIZED, type = PersistenceContextType.EXTENDED, unitName = "allowjoinedunsyncPU") EntityManager emAllowjoinedunsyncPU; @Resource SessionContext sessionContext; @Inject private CMTPCStatefulBean cmtpcStatefulBean; public void willThrowError() throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException { em.isJoinedToTransaction(); //Begin transaction (before Joining) UserTransaction userTxn = sessionContext.getUserTransaction(); userTxn.begin(); try { cmtpcStatefulBean.getEmp(1); // show throw IllegalStateException throw new RuntimeException("did not get expected IllegalStateException when transaction scoped entity manager used existing unsynchronized xpc"); } finally { userTxn.rollback(); } } public void allowjoinedunsync() throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException { UserTransaction userTxn = sessionContext.getUserTransaction(); userTxn.begin(); try { //Join Transaction to force unsynchronized persistence context to be associated with Jakarta Transactions tx em.joinTransaction(); cmtpcStatefulBean.getEmpAllowJoinedUnsync(1); } finally { userTxn.rollback(); } } public void allowjoinedunsyncPersistenceXML() throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException { UserTransaction userTxn = sessionContext.getUserTransaction(); userTxn.begin(); try { //Join Transaction to force unsynchronized persistence context to be associated with Jakarta Transactions tx emAllowjoinedunsyncPU.joinTransaction(); cmtpcStatefulBean.getEmpAllowJoinedUnsyncPersistenceXML(1); } finally { userTxn.rollback(); } } }
4,560
41.626168
170
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/unsync/TestForMixedSynchronizationTypes.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.unsync; import static org.junit.Assert.fail; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * " * If there is a persistence context of type SynchronizationType.UNSYNCHRONIZED * associated with the Jakarta Transactions transaction and the target component specifies a persistence context of * type SynchronizationType.SYNCHRONIZED, the IllegalStateException is * thrown by the container. * " * * @author Scott Marlow */ @RunWith(Arquillian.class) public class TestForMixedSynchronizationTypes { private static final String ARCHIVE_NAME = "jpa_testForMixedSynchronizationTypes"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses( TestForMixedSynchronizationTypes.class, Employee.class, BMTEPCStatefulBean.class, CMTPCStatefulBean.class); jar.addAsManifestResource(TestForMixedSynchronizationTypes.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } @Test public void testShouldGetEjbExceptionBecauseEPCIsAddedToTxAfterPc() throws Exception { final String errorCode = "WFLYJPA0064"; BMTEPCStatefulBean stateful = lookup("BMTEPCStatefulBean", BMTEPCStatefulBean.class); try { stateful.willThrowError(); } catch (Throwable expected) { Throwable cause = expected.getCause(); while(cause != null) { if( cause instanceof IllegalStateException && cause.getMessage().contains(errorCode)) { break; // success } cause = cause.getCause(); if (cause == null) { fail(String.format("didn't throw IllegalStateException that contains '%s', instead got message chain: %s", errorCode, messages(expected))); } } } } @Test public void testAllowjoinedunsyncEm() throws Exception { BMTEPCStatefulBean stateful = lookup("BMTEPCStatefulBean", BMTEPCStatefulBean.class); stateful.allowjoinedunsync(); } @Test public void testAllowjoinedunsyncEmPersistenceXML() throws Exception { BMTEPCStatefulBean stateful = lookup("BMTEPCStatefulBean", BMTEPCStatefulBean.class); stateful.allowjoinedunsyncPersistenceXML(); } private String messages(Throwable exception) { String message = exception.getMessage(); while(exception != null) { message = message + " => " + exception.getMessage(); exception = exception.getCause(); } return message; } }
4,451
37.051282
159
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/unsync/CMTPCStatefulBean.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.unsync; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; import jakarta.persistence.PersistenceProperty; /** * CMT stateful bean * * @author Scott Marlow */ @Stateful public class CMTPCStatefulBean { @PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "mypc") private EntityManager em; @PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "mypc", properties={@PersistenceProperty(name="wildfly.jpa.allowjoinedunsync", value="true")}) private EntityManager allowjoinedunsyncEm; @PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "allowjoinedunsyncPU") private EntityManager allowjoinedunsyncEmViaPersistenceXml; public Employee getEmp(int id) { return em.find(Employee.class, id); } public Employee getEmpAllowJoinedUnsync(int id) { return allowjoinedunsyncEm.find(Employee.class, id); } public Employee getEmpAllowJoinedUnsyncPersistenceXML(int id) { return allowjoinedunsyncEmViaPersistenceXml.find(Employee.class, id); } }
2,277
36.344262
172
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/slsbxpc/FailBecauseOfXPCNotInSFSBTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2014, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.slsbxpc; import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import jakarta.ejb.EJBException; import javax.naming.InitialContext; import javax.naming.NamingException; import org.hamcrest.MatcherAssert; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * This test should fail due to use of extended persistence context in a stateless bean. * Added for WFLY-69 * * @author Scott Marlow */ @RunWith(Arquillian.class) public class FailBecauseOfXPCNotInSFSBTestCase { private static final String ARCHIVE_NAME = "jpa_FailBecauseOfXPCNotInSFSBTestCase"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses(FailBecauseOfXPCNotInSFSBTestCase.class, StatelessBeanWithXPC.class ); jar.addAsManifestResource(FailBecauseOfXPCNotInSFSBTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } protected <T> T rawLookup(String name, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup(name)); } @Test public void testSerialization() throws Exception { final String errorCode = "WFLYJPA0070"; StatelessBeanWithXPC bean = lookup("StatelessBeanWithXPC", StatelessBeanWithXPC.class); try { bean.test(); } catch (EJBException expected) { //expected.printStackTrace(); Throwable cause = expected.getCause(); while (cause != null && !(cause.getMessage().contains(errorCode))) { cause = cause.getCause(); } assertTrue("expected IllegalStateException was not thrown", cause instanceof IllegalStateException); MatcherAssert.assertThat("Wrong error message", cause.getMessage(), containsString(errorCode)); } catch (Throwable unexpected) { fail("unexcepted exception " + unexpected.toString()); } } }
3,763
38.621053
129
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/slsbxpc/StatelessBeanWithXPC.java
/* * JBoss, Home of Professional Open Source. * Copyright 2014, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.slsbxpc; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * stateful session bean * * @author Scott Marlow */ @Stateless public class StatelessBeanWithXPC { @PersistenceContext(unitName = "mypc", type = PersistenceContextType.EXTENDED) EntityManager em; public void test() { em.getEntityManagerFactory(); } }
1,546
34.159091
82
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/serialization/Employee.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.serialization; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * Employee entity class * * @author Scott Marlow */ @Entity public class Employee { @Id private int id; private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1,732
24.865672
71
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/serialization/SerializationTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.serialization; import static org.junit.Assert.assertNotNull; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * simple serialization of an extended persistence context test * * @author Scott Marlow */ @RunWith(Arquillian.class) public class SerializationTestCase { private static final String ARCHIVE_NAME = "jpa_SerializationTestCase"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses(SerializationTestCase.class, Employee.class, SFSB1.class ); jar.addAsManifestResource(SerializationTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } protected <T> T rawLookup(String name, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup(name)); } @Test public void testSerialization() throws Exception { SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class); sfsb1.createEmployeeNoTx("name", "address", 100); byte[] serialized = sfsb1.getSerializedXPC(); assertNotNull("could serialize JPA extended persistence context", serialized); } }
2,992
35.950617
129
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/epcpropagation/serialization/SFSB1.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.epcpropagation.serialization; import java.io.ByteArrayOutputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; /** * stateful session bean * * @author Scott Marlow */ @Stateful public class SFSB1 { @PersistenceContext(unitName = "mypc", type = PersistenceContextType.EXTENDED) EntityManager em; public void createEmployeeNoTx(String name, String address, int id) { Employee emp = new Employee(); emp.setId(id); emp.setAddress(address); emp.setName(name); } public byte[] getSerializedXPC() throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos); out.writeObject(em); return bos.toByteArray(); } }
2,017
34.403509
82
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/dsrestart/JpaInjectedSfsb.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.dsrestart; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * @author <a href="[email protected]">Kabir Khan</a> */ @Stateless public class JpaInjectedSfsb { @PersistenceContext private EntityManager entityManager; public EntityManager getEntityManager() { return entityManager; } }
1,445
33.428571
69
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/dsrestart/NonJpaSfsb.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.dsrestart; import jakarta.ejb.Stateless; /** * @author <a href="[email protected]">Kabir Khan</a> */ @Stateless public class NonJpaSfsb { public String echo(String s) { return s; } }
1,261
34.055556
69
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/dsrestart/Car.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.dsrestart; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * @author Stuart Douglas */ @Entity public class Car { @Id private int id; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1,498
26.254545
73
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/dsrestart/EjbInjectedSfsb.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.dsrestart; import jakarta.ejb.EJB; import jakarta.ejb.Stateless; /** * @author <a href="[email protected]">Kabir Khan</a> */ @Stateless public class EjbInjectedSfsb { @EJB JpaInjectedSfsb jpaEjb; public JpaInjectedSfsb getJpaEjb() { return jpaEjb; } }
1,339
32.5
69
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/dsrestart/JpaDsRestartTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.dsrestart; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ALLOW_RESOURCE_SERVICE_RESTART; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILED; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OPERATION_HEADERS; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME; import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS; import java.security.Permission; import javax.naming.InitialContext; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.as.controller.ModelController; import org.jboss.as.controller.ModelController.OperationTransactionControl; import org.jboss.as.controller.client.OperationMessageHandler; import org.jboss.as.controller.security.ControllerPermission; import org.jboss.as.server.Services; import org.jboss.as.test.shared.PermissionUtils; import org.jboss.dmr.ModelNode; import org.jboss.msc.service.ServiceContainer; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; /** * @author <a href="[email protected]">Kabir Khan</a> */ @RunWith(Arquillian.class) public class JpaDsRestartTestCase { @ArquillianResource private InitialContext iniCtx; @ArquillianResource ServiceContainer serviceContainer; @Deployment public static Archive<?> deploy() { WebArchive war = ShrinkWrap.create(WebArchive.class, "dsrestartjpa.war"); war.addPackage(JpaInjectedSfsb.class.getPackage()); // WEB-INF/classes is implied war.addAsResource(JpaDsRestartTestCase.class.getPackage(), "persistence.xml", "META-INF/persistence.xml"); war.addAsManifestResource(JpaDsRestartTestCase.class.getPackage(), "MANIFEST.MF", "MANIFEST.MF"); final Permission[] permissions = new Permission[] { ControllerPermission.CAN_ACCESS_MODEL_CONTROLLER, ControllerPermission.CAN_ACCESS_IMMUTABLE_MANAGEMENT_RESOURCE_REGISTRATION }; war.addAsManifestResource(PermissionUtils.createPermissionsXmlAsset(permissions), "permissions.xml"); return war; } @Test public void testRestartDataSource() throws Exception { Assert.assertNotNull(serviceContainer); NonJpaSfsb nonJpaSfsb = (NonJpaSfsb) iniCtx.lookup("java:module/" + NonJpaSfsb.class.getSimpleName()); Assert.assertEquals("test", nonJpaSfsb.echo("test")); JpaInjectedSfsb jpaInjected = (JpaInjectedSfsb) iniCtx.lookup("java:module/" + JpaInjectedSfsb.class.getSimpleName()); Assert.assertNotNull(jpaInjected.getEntityManager()); EjbInjectedSfsb ejbInjected = (EjbInjectedSfsb) iniCtx.lookup("java:module/" + EjbInjectedSfsb.class.getSimpleName()); Assert.assertNotNull(ejbInjected.getJpaEjb()); Assert.assertNotNull(ejbInjected.getJpaEjb().getEntityManager()); //Should fail since app has dependencies on it toggleDataSource(false, true); //The persistence unit service should be restarted Assert.assertEquals("test", nonJpaSfsb.echo("test")); Assert.assertNotNull(jpaInjected.getEntityManager()); Assert.assertNotNull(ejbInjected.getJpaEjb()); Assert.assertNotNull(ejbInjected.getJpaEjb().getEntityManager()); } private void toggleDataSource(boolean enable, boolean expectFailure) throws Exception { ModelNode op = new ModelNode(); op.get(OP).set(enable ? "enable" : "disable"); op.get(OP_ADDR).add("subsystem", "datasources").add("data-source", "H2DS"); if (!enable) { op.get(OPERATION_HEADERS, ALLOW_RESOURCE_SERVICE_RESTART).set(true); } ModelController controller = (ModelController) serviceContainer.getRequiredService(Services.JBOSS_SERVER_CONTROLLER).getValue(); ModelNode result = controller.execute(op, OperationMessageHandler.logging, OperationTransactionControl.COMMIT, null); if (expectFailure) { //System.out.println("Expected failure " + result.get(FAILURE_DESCRIPTION).asString()); Assert.assertEquals(FAILED, result.get(OUTCOME).asString()); } else { Assert.assertEquals(result.get(FAILURE_DESCRIPTION).asString(), SUCCESS, result.get(OUTCOME).asString()); } } }
5,879
47.595041
140
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/webtxem/WebJPATestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.webtxem; import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertEquals; import java.net.URL; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.RunAsClient; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.as.test.integration.common.HttpRequest; import org.jboss.as.test.integration.jpa.hibernate.entity.Company; import org.jboss.as.test.integration.jpa.hibernate.entity.Customer; import org.jboss.as.test.integration.jpa.hibernate.entity.Flight; import org.jboss.as.test.integration.jpa.hibernate.entity.Ticket; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * This test writes and reads entity in the {@link TestServlet}. (based on the * EAP 5 testsuite). * * @author Zbyněk Roubalík */ @RunWith(Arquillian.class) @RunAsClient public class WebJPATestCase { private static final String ARCHIVE_NAME = "web_jpa"; @ArquillianResource static URL baseUrl; @Deployment public static WebArchive deployment() { WebArchive war = ShrinkWrap.create(WebArchive.class, ARCHIVE_NAME + ".war"); war.addClasses(WebJPATestCase.class, TestServlet.class, HttpRequest.class, Flight.class, Company.class, Customer.class, Ticket.class); // WEB-INF/classes/ is implied war.addAsResource(WebJPATestCase.class.getPackage(), "persistence.xml", "META-INF/persistence.xml"); return war; } private static String performCall(String urlPattern, String param) throws Exception { return HttpRequest.get(baseUrl.toString() + urlPattern + "?mode=" + param, 20, SECONDS); } @Test public void testReadWrite() throws Exception { performCall("test", "write"); String result = performCall("test", "read"); assertEquals("Flight number one", result); } }
3,124
36.202381
108
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/webtxem/TestServlet.java
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.webtxem; import java.io.IOException; import java.io.PrintWriter; import jakarta.annotation.Resource; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.transaction.Status; import jakarta.transaction.UserTransaction; import org.jboss.as.test.integration.jpa.hibernate.entity.Flight; /** * Test servlet used by {@link WebJPATestCase}. Entity {@link Flight} is read or written, type of operation depends on * parameter: mode=write or mode=read. * * @author Zbyněk Roubalík */ @WebServlet(name = "TestServlet", urlPatterns = {"/test"}) public class TestServlet extends HttpServlet { @PersistenceContext(unitName = "web_jpa_pc") EntityManager em; @Resource private UserTransaction tx; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { tx.begin(); try { String mode = req.getParameter("mode"); if (mode.equals("write")) { Flight f = new Flight(); f.setId(new Long(1)); f.setName("Flight number one"); em.merge(f); } else if (mode.equals("read")) { Flight f = em.find(Flight.class, Long.valueOf(1)); resp.setContentType("text/plain"); PrintWriter out = resp.getWriter(); out.print(f.getName()); out.close(); } } catch (Exception e) { e.printStackTrace(); tx.setRollbackOnly(); throw e; } finally { if (tx.getStatus() == Status.STATUS_MARKED_ROLLBACK) { tx.rollback(); } else { tx.commit(); } } } catch (Exception e) { throw new ServletException(e); } } }
3,275
31.435644
118
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/resourcelocal/Employee.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.resourcelocal; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * Employee entity class * * @author Scott Marlow */ @Entity public class Employee { @Id private int id; private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1,717
24.641791
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/resourcelocal/DataSourceBean.java
/* * JBoss, Home of Professional Open Source * Copyright 2010, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.resourcelocal; import jakarta.annotation.Resource; import jakarta.annotation.sql.DataSourceDefinition; import jakarta.ejb.Stateless; import javax.sql.DataSource; /** * @author Stuart Douglas */ @DataSourceDefinition( name = "java:app/DataSource", user = "sa", password = "sa", className = "org.h2.jdbcx.JdbcDataSource", url = "jdbc:h2:mem:test", transactional = false ) @Stateless public class DataSourceBean { @Resource(lookup = "java:app/DataSource") private DataSource dataSource; public DataSource getDataSource() { return dataSource; } }
1,679
32.6
73
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/resourcelocal/SFSB1.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.resourcelocal; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionAttribute; import jakarta.ejb.TransactionAttributeType; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.EntityTransaction; import jakarta.persistence.PersistenceUnit; /** * stateful session bean * * @author Stuart Douglas */ @Stateful public class SFSB1 { @PersistenceUnit(unitName = "mypc") private EntityManagerFactory emf; @TransactionAttribute(TransactionAttributeType.NEVER) public void createEmployeeNoJTATransaction(String name, String address, int id) { EntityManager em = emf.createEntityManager(); Employee emp = new Employee(); emp.setId(id); emp.setAddress(address); emp.setName(name); EntityTransaction tx1 = em.getTransaction(); try { tx1.begin(); em.joinTransaction(); em.persist(emp); tx1.commit(); } catch (Exception e) { throw new RuntimeException("couldn't start tx", e); } } public void flushWithNoTx() { EntityManager em = emf.createEntityManager(); em.flush();// should throw TransactionRequiredException } @TransactionAttribute(TransactionAttributeType.NEVER) public Employee getEmployeeNoTX(int id) { EntityManager em = emf.createEntityManager(); return em.find(Employee.class, id); } }
2,537
32.84
85
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/resourcelocal/ResourceLocalTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.resourcelocal; import jakarta.ejb.EJBException; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; /** * Transaction tests for a RESOURCE_LOCAL entity manager * * @author Stuart Douglas */ @RunWith(Arquillian.class) public class ResourceLocalTestCase { private static final String ARCHIVE_NAME = "jpa_sessionfactory"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addPackage(ResourceLocalTestCase.class.getPackage()); jar.addAsManifestResource(ResourceLocalTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); jar.addAsManifestResource(new StringAsset("Dependencies: com.h2database.h2\n"), "MANIFEST.MF"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } protected <T> T rawLookup(String name, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup(name)); } /** * Even though a JTA Transaction is in progress this should throw an exception * * @throws NamingException */ @Test public void flushOutSideTransaction() throws NamingException { SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class); try { sfsb1.flushWithNoTx(); } catch (EJBException e) { Assert.assertEquals(jakarta.persistence.TransactionRequiredException.class, e.getCause().getClass()); } } @Test public void testResourceLocalRollback() throws Exception { SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class); sfsb1.createEmployeeNoJTATransaction("Bob", "Home", 1); Employee emp = sfsb1.getEmployeeNoTX(1); Assert.assertEquals("Bob", emp.getName()); } }
3,529
35.770833
129
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/webnontxem/Employee.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.webnontxem; import javax.naming.InitialContext; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * Employee entity class * * @author Scott Marlow */ @Entity public class Employee { static { try { // WFLY-6441: verify that java:comp/env values can be read from web.xml when persistence provider loads entity class new InitialContext().lookup("java:comp/env/simpleString"); } catch (Exception e) { throw new RuntimeException("unable to get java:app/env/simpleString from JPA deployer", e); } } @Id private int id; private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
2,131
26.333333
128
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/webnontxem/NonTransactionalEmTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright (c) 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.webnontxem; import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertEquals; import java.net.URL; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.RunAsClient; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.as.test.integration.common.HttpRequest; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * test case for injecting transactional entity manager into web servlet, without a jta transaction * * @author Scott Marlow (based on Carlo's webejb test case) */ @RunWith(Arquillian.class) @RunAsClient public class NonTransactionalEmTestCase { @ArquillianResource static URL baseUrl; @Deployment public static WebArchive deployment() { WebArchive war = ShrinkWrap.create(WebArchive.class, "NonTransactionalEmTestCase.war"); war.addClasses(HttpRequest.class, SimpleServlet.class, Employee.class); // WEB-INF/classes is implied war.addAsResource(NonTransactionalEmTestCase.class.getPackage(), "persistence.xml", "META-INF/persistence.xml"); war.addAsWebInfResource(NonTransactionalEmTestCase.class.getPackage(), "web.xml", "web.xml"); return war; } private static String performCall(String urlPattern, String param) throws Exception { return HttpRequest.get(baseUrl.toString() + urlPattern + "?input=" + param, 10, SECONDS); } @Test public void testEcho() throws Exception { String result = performCall("simple", "Hello+world"); assertEquals("0", result); result = performCall("simple", "Hello+world"); assertEquals("0", result); } }
2,906
38.283784
120
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/webnontxem/SimpleServlet.java
/* * JBoss, Home of Professional Open Source. * Copyright (c) 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.webnontxem; import java.io.IOException; import java.io.Writer; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceUnit; import jakarta.persistence.Query; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; /** * @author Scott Marlow (based on Carlo's webejb test) * <p> * Exercise the code paths reached when a container managed entity manager is injected into a servlet */ @WebServlet(name = "SimpleServlet", urlPatterns = {"/simple"}) public class SimpleServlet extends HttpServlet { @PersistenceUnit(unitName = "mypc") EntityManagerFactory emf; // servlet is in control of when obtained entity managers are closed @PersistenceContext(unitName = "mypc", name = "persistence/em") EntityManager em; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String msg = req.getParameter("input"); Writer writer = resp.getWriter(); // This is how a servlet could get an entity manager from an entity manager factory EntityManager localEntityManager = emf.createEntityManager(); Query query = localEntityManager.createQuery("select count(*) from Employee"); Long count = (Long) query.getSingleResult(); localEntityManager.close(); // a new underlying entity manager will be obtained to handle creating query // the em will stay open until the container closes it. query = em.createQuery("select count(*) from Employee"); count = (Long) query.getSingleResult(); writer.write(count.toString()); } }
2,979
40.971831
113
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/Employee.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.validation.constraints.NotNull; /** * Employee entity class * * @author Scott Marlow */ @Entity public class Employee { @Id private int id; @NotNull(message = "Name must be specified.") private String name; @NotNull(message = "Address must be specified.") private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1,869
25.338028
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/SLSBInheritance.java
package org.jboss.as.test.integration.jpa.beanvalidation; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * @author Madhumita Sadhukhan */ @Stateless public class SLSBInheritance { @PersistenceContext(unitName = "myPlayer") EntityManager em; public SoccerPlayer createSoccerPlayer(String firstName, String lastName, String game, String clubName) { SoccerPlayer socplayer = new SoccerPlayer(); socplayer.setFirstName(firstName); socplayer.setLastName(lastName); socplayer.setGame(game); socplayer.setClubName(clubName); em.persist(socplayer); return socplayer; } public SoccerPlayer updateSoccerPlayer(SoccerPlayer p) { em.merge(p); return p; } }
827
23.352941
109
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/Player.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Inheritance; import jakarta.persistence.InheritanceType; import jakarta.persistence.Table; import org.hibernate.envers.Audited; import org.hibernate.envers.NotAudited; import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotEmpty; /** * @author Madhumita Sadhukhan */ @Entity @Audited @Table(name = "PLAYER") @Inheritance(strategy = InheritanceType.JOINED) public class Player { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "PLAYER_ID") private Integer id; @Column(length = 30) protected String firstName; @Column(length = 30) @NotBlank @NotEmpty protected String lastName; @NotAudited protected String game; public Integer getId() { return id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getGame() { return game; } public void setGame(String game) { this.game = game; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Player)) { return false; } final Player player = (Player) o; return id != null ? id.equals(player.id) : player.id == null; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
2,893
25.550459
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/SoccerPlayer.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation; import jakarta.persistence.Entity; import jakarta.persistence.PrimaryKeyJoinColumn; import jakarta.persistence.Table; import org.hibernate.envers.Audited; import org.hibernate.validator.constraints.NotBlank; /** * @author Madhumita Sadhukhan */ @Entity @Audited @Table(name = "SOCCERPLAYER") @PrimaryKeyJoinColumn(name = "SOCCERPLAYER_ID") public class SoccerPlayer extends Player { @NotBlank private String clubName; public String getClubName() { return clubName; } public void setClubName(String clubName) { this.clubName = clubName; } }
1,670
31.134615
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/SFSB1.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * stateful session bean * * @author Scott Marlow */ @Stateful public class SFSB1 { @PersistenceContext(unitName = "mypc") EntityManager em; public void createEmployee(String name, String address, int id) { Employee emp = new Employee(); emp.setId(id); emp.setAddress(address); emp.setName(name); em.persist(emp); em.flush(); } public Employee getEmployee(int id) { return em.find(Employee.class, id); } }
1,699
30.481481
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/beanvalidationtest/JPABeanValidationTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation.beanvalidationtest; import static org.jboss.as.test.shared.PermissionUtils.createPermissionsXmlAsset; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import javax.naming.InitialContext; import javax.naming.NamingException; import jakarta.validation.ConstraintViolationException; import org.hibernate.validator.HibernateValidatorPermission; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.as.test.integration.jpa.beanvalidation.Employee; import org.jboss.as.test.integration.jpa.beanvalidation.SFSB1; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * Jakarta Bean Validation with Jakarta Persistence test * * @author Scott Marlow */ @RunWith(Arquillian.class) public class JPABeanValidationTestCase { private static final String ARCHIVE_NAME = "JPABeanValidationTestCase"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses(JPABeanValidationTestCase.class, Employee.class, SFSB1.class ); jar.addAsManifestResource(JPABeanValidationTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); jar.addAsManifestResource(createPermissionsXmlAsset( HibernateValidatorPermission.ACCESS_PRIVATE_MEMBERS ), "permissions.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } protected <T> T rawLookup(String name, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup(name)); } /** * Test that a Jakarta Bean Validation error is not thrown * * @throws Exception */ @Test public void testSuccessfulBeanValidation() throws Exception { SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class); sfsb1.createEmployee("name", "address", 1); } /** * Test that a Jakarta Bean Validation error is thrown * * @throws Exception */ @Test public void testFailingBeanValidationNullAddress() throws Exception { SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class); try { sfsb1.createEmployee("name", null, 2); fail("should of thrown validation error for null address in Employee entity"); } catch (Throwable throwable) { ConstraintViolationException constraintViolationException = null; // find the ConstraintViolationException while (throwable != null && !(throwable instanceof ConstraintViolationException)) { throwable = throwable.getCause(); } // should be null or instanceof ConstraintViolationException constraintViolationException = (ConstraintViolationException) throwable; assertTrue("expected ConstraintViolationException but got " + constraintViolationException, constraintViolationException instanceof ConstraintViolationException); } } }
4,579
38.482759
174
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/cdi/MinimumValueProvider.java
/* * JBoss, Home of Professional Open Source * Copyright 2013, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation.cdi; /** * Provides a minimum value for the number of people for a Reservation entity. * * @author Farah Juma */ public class MinimumValueProvider { public int getMin() { return 5; } }
1,280
35.6
78
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/cdi/CustomMin.java
/* * JBoss, Home of Professional Open Source * Copyright 2013, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation.cdi; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import jakarta.validation.Constraint; import jakarta.validation.Payload; /** * A custom constraint that uses CDI in its validator class. * * @author Farah Juma */ @Constraint(validatedBy = CustomMinValidator.class) @Documented @Target({METHOD, FIELD, TYPE, PARAMETER}) @Retention(RUNTIME) public @interface CustomMin { String message() default "Not enough people for a reservation"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
1,960
36.711538
73
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/cdi/SFSB.java
/* * JBoss, Home of Professional Open Source. * Copyright 2013, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation.cdi; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * A stateful session bean. * * @author Farah Juma */ @Stateful public class SFSB { @PersistenceContext(unitName = "mypc") EntityManager em; public void createReservation(int numberOfPeople, String lastName) { Reservation reservation = new Reservation(numberOfPeople, lastName); em.persist(reservation); em.flush(); } }
1,575
33.26087
76
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/cdi/Reservation.java
/* * JBoss, Home of Professional Open Source * Copyright 2013, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation.cdi; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.validation.constraints.NotNull; /** * Reservation entity class. * * @author Farah Juma */ @Entity public class Reservation { @Id @GeneratedValue private int id; @CustomMin private int numberOfPeople; @NotNull(message = "may not be null") private String lastName; public Reservation(int numberOfPeople, String lastName) { this.numberOfPeople = numberOfPeople; this.lastName = lastName; } }
1,653
30.807692
73
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/cdi/BeanValidationCdiIntegrationTestCase.java
/* * JBoss, Home of Professional Open Source * Copyright 2013, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation.cdi; import static org.jboss.as.test.shared.PermissionUtils.createPermissionsXmlAsset; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; import javax.naming.InitialContext; import javax.naming.NamingException; import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolationException; import org.hibernate.validator.HibernateValidatorPermission; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; /** * Tests for the integration of Jakarta Persistence, Jakarta Contexts and Dependency Injection, and Jakarta Bean Validation. * * @author Farah Juma */ @RunWith(Arquillian.class) public class BeanValidationCdiIntegrationTestCase { private static final String ARCHIVE_NAME = "BeanValidationCdiIntegrationTestCase"; @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addPackage(BeanValidationCdiIntegrationTestCase.class.getPackage()); jar.addAsManifestResource(new StringAsset("<beans bean-discovery-mode=\"all\"></beans>"), "beans.xml"); jar.addAsManifestResource(BeanValidationCdiIntegrationTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); jar.addAsManifestResource(createPermissionsXmlAsset( HibernateValidatorPermission.ACCESS_PRIVATE_MEMBERS ), "permissions.xml"); return jar; } @ArquillianResource private InitialContext iniCtx; protected <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } @Test public void testSuccessfulBeanValidation() throws Exception { SFSB sfsb = lookup("SFSB", SFSB.class); sfsb.createReservation(6, "Smith"); } @Test public void testFailingBeanValidation() throws Exception { SFSB sfsb = lookup("SFSB", SFSB.class); try { sfsb.createReservation(1, null); fail("Should have thrown validation error for invalid values in Reservation entity"); } catch (Throwable throwable) { ConstraintViolationException constraintViolationException = null; // Find the ConstraintViolationException while (throwable != null && !(throwable instanceof ConstraintViolationException)) { throwable = throwable.getCause(); } constraintViolationException = (ConstraintViolationException) throwable; Set<ConstraintViolation<?>> violations = constraintViolationException.getConstraintViolations(); List<String> actualViolations = new ArrayList<String>(); for (ConstraintViolation<?> violation : violations) { actualViolations.add(violation.getMessage()); } List<String> expectedViolations = new ArrayList<String>(); expectedViolations.add("may not be null"); expectedViolations.add("Not enough people for a reservation"); Collections.sort(actualViolations); Collections.sort(expectedViolations); assertEquals(expectedViolations, actualViolations); } } }
4,834
41.043478
129
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/cdi/CustomMinValidator.java
/* * JBoss, Home of Professional Open Source * Copyright 2013, Red Hat Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation.cdi; import jakarta.inject.Inject; import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; /** * A validator that uses setter injection. * * @author Farah Juma */ public class CustomMinValidator implements ConstraintValidator<CustomMin, Integer> { private MinimumValueProvider minimumValueProvider; @Inject public void setMinimumValueProvider(MinimumValueProvider minimumValueProvider) { this.minimumValueProvider = minimumValueProvider; } @Override public void initialize(CustomMin constraintAnnotation) { } @Override public boolean isValid(Integer value, ConstraintValidatorContext context) { return value >= minimumValueProvider.getMin(); } }
1,830
34.901961
84
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/beanvalidation/beanvalidationinheritancetest/BeanValidationJPAInheritanceTestCase.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.beanvalidation.beanvalidationinheritancetest; import static org.jboss.as.test.shared.PermissionUtils.createPermissionsXmlAsset; import java.io.PrintWriter; import java.io.StringWriter; import java.sql.SQLException; import java.util.Locale; import javax.naming.InitialContext; import javax.naming.NamingException; import org.hibernate.validator.HibernateValidatorPermission; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.as.test.integration.jpa.beanvalidation.Player; import org.jboss.as.test.integration.jpa.beanvalidation.SLSBInheritance; import org.jboss.as.test.integration.jpa.beanvalidation.SoccerPlayer; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; /** * Test Jakarta Bean Validation is propagated on inherited attributes * * @author Madhumita Sadhukhan */ @RunWith(Arquillian.class) public class BeanValidationJPAInheritanceTestCase { private static final String ARCHIVE_NAME = "jpa_TestBeanValidationJPAInheritance"; @ArquillianResource private static InitialContext iniCtx; @BeforeClass public static void beforeClass() throws NamingException { iniCtx = new InitialContext(); } @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses(Player.class, SoccerPlayer.class, SLSBInheritance.class); jar.addAsManifestResource(BeanValidationJPAInheritanceTestCase.class.getPackage(), "persistence.xml", "persistence.xml"); jar.addAsManifestResource(createPermissionsXmlAsset( HibernateValidatorPermission.ACCESS_PRIVATE_MEMBERS ), "permissions.xml"); return jar; } protected static <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType .cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } /* Ensure that Jakarta Bean Validation works for inheritance across persistent objects */ @Test public void testConstraintValidationforJPA() throws NamingException, SQLException { SLSBInheritance slsb = lookup("SLSBInheritance", SLSBInheritance.class); try { SoccerPlayer socplayer = slsb.createSoccerPlayer("LEONARDO", "", "SOCCER", "REAL MADRID"); socplayer.setFirstName("Christiano"); socplayer.setLastName(""); socplayer.setGame("FOOTBALL"); socplayer = slsb.updateSoccerPlayer(socplayer); } catch (Exception e) { StringWriter w = new StringWriter(); e.printStackTrace(new PrintWriter(w)); String stacktrace = w.toString(); if (Locale.getDefault().getLanguage().equals("en")) { Assert.assertTrue(stacktrace.contains("interpolatedMessage='may not be empty', propertyPath=lastName, rootBeanClass=class org.jboss.as.test.integration.jpa.beanvalidation.SoccerPlayer")); } else { Assert.assertTrue(stacktrace.contains("propertyPath=lastName, rootBeanClass=class org.jboss.as.test.integration.jpa.beanvalidation.SoccerPlayer")); } } } }
4,580
39.901786
203
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/Employee.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate; import jakarta.persistence.Cacheable; import jakarta.persistence.Entity; import jakarta.persistence.Id; /** * Cachable Employee entity class * * @author Scott Marlow */ @Entity @Cacheable(true) public class Employee { @Id private int id; private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1,778
24.414286
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/EntityTest.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate; import java.util.HashSet; import java.util.Set; import jakarta.ejb.Stateful; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; import org.hibernate.Session; import org.jboss.as.test.integration.jpa.hibernate.entity.Company; import org.jboss.as.test.integration.jpa.hibernate.entity.Customer; import org.jboss.as.test.integration.jpa.hibernate.entity.Flight; import org.jboss.as.test.integration.jpa.hibernate.entity.Ticket; /** * Stateful session bean for testing Hibernate entities: * One to Many, Many to One, Many to Many mapping and named queries. * * @author Zbyněk Roubalík */ @Stateful public class EntityTest { @PersistenceContext(unitName = "entity_pc", type = PersistenceContextType.EXTENDED) private Session session; public Customer oneToManyCreate() throws Exception { Ticket t = new Ticket(); t.setNumber("111"); Customer c = new Customer(); Set<Ticket> tickets = new HashSet<Ticket>(); tickets.add(t); t.setCustomer(c); c.setTickets(tickets); session.save(c); return c; } public Flight manyToOneCreate() throws Exception { Flight f = new Flight(); f.setName("Flight number one"); f.setId(new Long(1)); Company comp = new Company(); comp.setName("Airline 1"); f.setCompany(comp); session.save(f); return f; } public void manyToManyCreate() throws Exception { Flight f1 = findFlightById(new Long(1)); Flight f2 = new Flight(); f2.setId(new Long(2)); f2.setName("Flight two"); Company us = new Company(); us.setName("Airline 2"); f2.setCompany(us); Set<Customer> customers1 = new HashSet<Customer>(); Set<Customer> customers2 = new HashSet<Customer>(); Customer c1 = new Customer(); c1.setName("John"); customers1.add(c1); Customer c2 = new Customer(); c2.setName("Tom"); customers2.add(c2); Customer c3 = new Customer(); c3.setName("Pete"); customers2.add(c3); f1.setCustomers(customers1); f2.setCustomers(customers2); session.save(c1); session.save(c2); session.save(c3); session.save(f2); } public int testAllCustomersQuery() { //session.flush(); return session.getNamedQuery("allCustomers").list().size(); } public Customer testCustomerByIdQuery() { Customer c = new Customer(); c.setName("Peter"); session.save(c); session.flush(); return (Customer) session.getNamedQuery("customerById").setParameter("id", c.getId()).uniqueResult(); } public Customer createCustomer(String name) { Customer c = new Customer(); c.setName(name); session.save(c); return c; } public void changeCustomer(Long id, String name) { Customer c = session.load(Customer.class, id); c.setName(name); } public Flight findFlightById(Long id) { return session.load(Flight.class, id); } public Company findCompanyById(Long id) { return session.load(Company.class, id); } public Customer findCustomerById(Long id) { return session.load(Customer.class, id); } }
4,457
26.689441
109
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/SFSB1.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate; import jakarta.ejb.Stateful; import jakarta.persistence.EntityManager; import jakarta.persistence.LockModeType; import jakarta.persistence.PersistenceContext; /** * stateful session bean * * @author Scott Marlow */ @Stateful public class SFSB1 { @PersistenceContext(unitName = "mypc") EntityManager em; public void createEmployee(String name, String address, int id) { Employee emp = new Employee(); emp.setId(id); emp.setAddress(address); emp.setName(name); em.persist(emp); } public Employee getEmployeeNoTX(int id) { return em.find(Employee.class, id, LockModeType.NONE); } }
1,737
31.792453
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/SFSBHibernateSessionFactory.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate; import jakarta.ejb.Stateful; import jakarta.ejb.TransactionManagement; import jakarta.ejb.TransactionManagementType; import jakarta.persistence.PersistenceUnit; import org.hibernate.Session; import org.hibernate.SessionFactory; /** * Test that a peristence unit can be injected into a Hibernate session * * @author Scott Marlow */ @Stateful @TransactionManagement(TransactionManagementType.BEAN) public class SFSBHibernateSessionFactory { @PersistenceUnit(unitName = "mypc") SessionFactory sessionFactory; public void createEmployee(String name, String address, int id) { Employee emp = new Employee(); emp.setId(id); emp.setAddress(address); emp.setName(name); try { Session session = sessionFactory.openSession(); session.persist(emp); session.flush(); session.close(); } catch (Exception e) { throw new RuntimeException("transactional failure while persisting employee entity", e); } } public Employee getEmployee(int id) { Employee emp = sessionFactory.openSession().load(Employee.class, id); return emp; } }
2,258
32.716418
100
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/SFSBHibernateSession.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate; import jakarta.ejb.Stateful; import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContextType; import org.hibernate.Session; /** * Test that a peristence context can be injected into a Hibernate session * * @author Scott Marlow */ @Stateful public class SFSBHibernateSession { @PersistenceContext(unitName = "mypc", type = PersistenceContextType.EXTENDED) Session session; public void createEmployee(String name, String address, int id) { Employee emp = new Employee(); emp.setId(id); emp.setAddress(address); emp.setName(name); session.persist(emp); } public Employee getEmployee(int id) { Employee emp = session.load(Employee.class, id); return emp; } }
1,856
32.160714
82
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/SLSBOrg.java
package org.jboss.as.test.integration.jpa.hibernate.envers; import java.util.ArrayList; import java.util.List; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReaderFactory; import org.hibernate.envers.RevisionType; import org.hibernate.envers.query.AuditEntity; import org.hibernate.envers.query.AuditQuery; /** * @author Madhumita Sadhukhan */ @Stateless public class SLSBOrg { @PersistenceContext(unitName = "myOrg") EntityManager em; public Organization createOrg(String name, String type, String startDate, String endDate, String location) { Organization org = new Organization(); org.setName(name); org.setType(type); org.setStartDate(startDate); org.setEndDate(endDate); org.setLocation(location); em.persist(org); return org; } public Organization updateOrg(Organization o) { return em.merge(o); } public void deleteOrg(Organization o) { em.remove(em.merge(o)); } public Organization retrieveOldOrgbyId(int id) { AuditReader reader = AuditReaderFactory.get(em); List<Number> revList = reader.getRevisions(Organization.class, id); Organization org1_rev = reader.find(Organization.class, id, 2); return org1_rev; } public Organization retrieveDeletedOrgbyId(int id) { AuditReader reader = AuditReaderFactory.get(em); List<Number> revList = reader.getRevisions(Organization.class, id); /*for (Number revisionNumber : revList) { System.out.println("Available revisionNumber for o1:" + revisionNumber); }*/ List<Object> custHistory = new ArrayList<Object>(); AuditQuery query = reader.createQuery().forRevisionsOfEntity(Organization.class, true, true); query.add(AuditEntity.revisionType().eq(RevisionType.DEL)); Organization rev = (Organization) (((List<Object>) (query.getResultList())).toArray()[0]); return rev; } public Organization retrieveOldOrgbyEntityName(String name, int id) { AuditReader reader = AuditReaderFactory.get(em); Organization org1_rev = reader.find(Organization.class, name, id, 3); return org1_rev; } }
2,372
29.818182
112
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/AuditJoinTableoverOnetoManyJoinColumnTest.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import java.util.List; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; /** * Test @AuditJoinTable over Uni-directional One-to-Many Relationship * * @author Madhumita Sadhukhan */ @RunWith(Arquillian.class) public class AuditJoinTableoverOnetoManyJoinColumnTest { private static final String ARCHIVE_NAME = "jpa_AuditMappedByoverOnetoManyJoinColumnTest"; @ArquillianResource private static InitialContext iniCtx; @BeforeClass public static void beforeClass() throws NamingException { iniCtx = new InitialContext(); } @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses(Customer.class, Phone.class, SLSBAudit.class); jar.addAsManifestResource(AuditJoinTableoverOnetoManyJoinColumnTest.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } protected static <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType .cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } @Test public void testRevisionsfromAuditJoinTable() throws Exception { SLSBAudit slsbAudit = lookup("SLSBAudit", SLSBAudit.class); Customer c1 = slsbAudit.createCustomer("MADHUMITA", "SADHUKHAN", "WORK", "+420", "543789654"); Phone p1 = c1.getPhones().get(1); p1.setType("Emergency"); slsbAudit.updatePhone(p1); c1.setSurname("Mondal"); slsbAudit.updateCustomer(c1); c1.setFirstname("Steve"); c1.setSurname("Jobs"); slsbAudit.updateCustomer(c1); // delete phone c1.getPhones().remove(p1); slsbAudit.updateCustomer(c1); slsbAudit.deletePhone(p1); Assert.assertEquals(1, c1.getPhones().size()); testRevisionDatafromAuditJoinTable(c1, slsbAudit); testRevisionTypefromAuditJoinTable(c1, slsbAudit); testOtherFieldslikeForeignKeysfromAuditJoinTable(c1, slsbAudit); } private void testRevisionDatafromAuditJoinTable(Customer c1, SLSBAudit sb) throws Exception { // fetch REV List<Object> custHistory = sb.verifyRevision(c1.getId()); // verify size Assert.assertEquals(2, custHistory.size()); int counter = 0; for (Object revisionEntity : custHistory) { counter++; Assert.assertNotNull(revisionEntity); Customer rev = (Customer) (((List<Object>) (revisionEntity)).toArray()[0]); Assert.assertNotNull(rev); // check if revision obtained is not null Assert.assertEquals("MADHUMITA", rev.getFirstname()); if (counter == 1) { Assert.assertEquals("SADHUKHAN", rev.getSurname()); } if (counter == 2) { Assert.assertEquals("Mondal", rev.getSurname()); } } } private void testRevisionTypefromAuditJoinTable(Customer c1, SLSBAudit sb) throws Exception { // fetch REVType List<Object> custRevision = sb.verifyRevisionType(c1.getId()); int counter = 0; for (Object revisionTypeEntity : custRevision) { counter++; Assert.assertNotNull(revisionTypeEntity); Customer rev = (Customer) (((List<Object>) (revisionTypeEntity)).toArray()[0]); Assert.assertNotNull(rev); // check if revision obtained is not null Assert.assertNotNull(rev.getFirstname()); } } private void testOtherFieldslikeForeignKeysfromAuditJoinTable(Customer c1, SLSBAudit sb) throws Exception { List<Object> phHistory = sb.verifyOtherFields(c1.getId()); Assert.assertNotNull(phHistory); // just to check correct values are returned for (Object phoneIdEntity : phHistory) { Assert.assertNotNull(phoneIdEntity); //System.out.println("revendPhoneID::--" + phoneIdEntity.toString()); } } }
5,547
34.33758
134
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/Organization.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; import org.hibernate.envers.Audited; /** * @author Madhumita Sadhukhan */ @Entity @Table(name = "ORG") public class Organization { @Id @GeneratedValue @Audited private int id; @Audited @Column(name = "ORG_NAME") private String name; private String type; @Audited private String startDate; private String endDate; private String location; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getStartDate() { return startDate; } public void setStartDate(String startDate) { this.startDate = startDate; } public String getEndDate() { return endDate; } public void setEndDate(String endDate) { this.endDate = endDate; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } }
2,493
22.980769
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/SLSBPU.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReaderFactory; /** * @author Strong Liu */ @Stateless public class SLSBPU { @PersistenceContext(unitName = "mypc") EntityManager em; public Person createPerson(String firstName, String secondName, String streetName, int houseNumber) { Address address = new Address(); address.setHouseNumber(houseNumber); address.setStreetName(streetName); Person person = new Person(); person.setName(firstName); person.setSurname(secondName); person.setAddress(address); address.getPersons().add(person); em.persist(address); em.persist(person); return person; } public Person updatePerson(Person p) { return em.merge(p); } public Address updateAddress(Address a) { return em.merge(a); } public int retrieveOldPersonVersionFromAddress(int id) { AuditReader reader = AuditReaderFactory.get(em); Address address1_rev = reader.find(Address.class, id, 1); return address1_rev.getPersons().size(); } }
2,336
33.367647
105
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/SLSBInheritance.java
package org.jboss.as.test.integration.jpa.hibernate.envers; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; /** * @author Madhumita Sadhukhan */ @Stateless public class SLSBInheritance { @PersistenceContext(unitName = "myPlayer") EntityManager em; public SoccerPlayer createSoccerPlayer(String firstName, String lastName, String game, String clubName) { SoccerPlayer socplayer = new SoccerPlayer(); socplayer.setFirstName(firstName); socplayer.setLastName(lastName); socplayer.setGame(game); socplayer.setClubName(clubName); em.persist(socplayer); return socplayer; } public SoccerPlayer updateSoccerPlayer(SoccerPlayer p) { em.merge(p); return p; } }
829
23.411765
109
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/SLSBAuditInheritance.java
package org.jboss.as.test.integration.jpa.hibernate.envers; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReaderFactory; /** * @author Madhumita Sadhukhan */ @Stateless public class SLSBAuditInheritance { @PersistenceContext(unitName = "myPlayer") EntityManager em; public SoccerPlayer createSoccerPlayer(String firstName, String lastName, String game, String clubName) { SoccerPlayer socplayer = new SoccerPlayer(); socplayer.setFirstName(firstName); socplayer.setLastName(lastName); socplayer.setGame(game); socplayer.setClubName(clubName); em.persist(socplayer); return socplayer; } public SoccerPlayer updateSoccerPlayer(SoccerPlayer p) { em.merge(p); return p; } public SoccerPlayer retrieveSoccerPlayerbyId(int id) { AuditReader reader = AuditReaderFactory.get(em); SoccerPlayer val = reader.find(SoccerPlayer.class, id, 1); return val; } }
1,135
24.818182
109
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/SLSBValidityStrategyOrg.java
package org.jboss.as.test.integration.jpa.hibernate.envers; import java.util.List; import java.util.Map; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.Query; import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReaderFactory; /** * @author Madhumita Sadhukhan */ @Stateless public class SLSBValidityStrategyOrg { @PersistenceContext(unitName = "myValidOrg") EntityManager em; public Organization createOrg(String name, String type, String startDate, String endDate, String location) { Organization org = new Organization(); org.setName(name); org.setType(type); org.setStartDate(startDate); org.setEndDate(endDate); org.setLocation(location); em.persist(org); return org; } public Organization updateOrg(Organization o) { return em.merge(o); } public void deleteOrg(Organization o) { em.remove(em.merge(o)); } public List<Map<String, Object>> verifyEndRevision(Integer id) { AuditReader reader = AuditReaderFactory.get(em); boolean b; String queryString = "select a from " + Organization.class.getName() + "_AUD a where a.originalId.id=:id"; Query query = em.createQuery(queryString); query.setParameter("id", id); List<Map<String, Object>> orgHistory = query.getResultList(); return orgHistory; } public Organization retrieveOldOrgbyId(int id) { AuditReader reader = AuditReaderFactory.get(em); Organization org1_rev = reader.find(Organization.class, id, 2); return org1_rev; } }
1,726
26.854839
114
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/Person.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import org.hibernate.envers.Audited; /** * @author Strong Liu */ @Entity @Audited // that's the important part :) public class Person { @Id @GeneratedValue private int id; private String name; private String surname; @ManyToOne private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } }
2,062
24.469136
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/CustomerMO.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import java.util.ArrayList; import java.util.List; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.OneToMany; import org.hibernate.envers.Audited; /** * @author Madhumita Sadhukhan */ @Entity @Audited public class CustomerMO { @Id @GeneratedValue @Column(name = "CUST_ID") private Integer id; private String firstname; private String surname; @OneToMany private List<PhoneMO> phones = new ArrayList<PhoneMO>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public List<PhoneMO> getPhones() { return phones; } public void setSurname(List<PhoneMO> phones) { this.phones = phones; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof CustomerMO)) { return false; } final CustomerMO cust = (CustomerMO) o; return id != null ? id.equals(cust.id) : cust.id == null; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
2,608
24.578431
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/Player.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Inheritance; import jakarta.persistence.InheritanceType; import jakarta.persistence.Table; import org.hibernate.envers.Audited; import org.hibernate.envers.NotAudited; /** * @author Madhumita Sadhukhan */ @Entity @Audited @Table(name = "PLAYER") @Inheritance(strategy = InheritanceType.JOINED) public class Player { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "PLAYER_ID") private Integer id; @Column(length = 30) protected String firstName; @Column(length = 30) protected String lastName; @NotAudited protected String game; public Integer getId() { return id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getGame() { return game; } public void setGame(String game) { this.game = game; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Player)) { return false; } final Player player = (Player) o; return id != null ? id.equals(player.id) : player.id == null; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
2,761
25.304762
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/SLSBAudit.java
package org.jboss.as.test.integration.jpa.hibernate.envers; import java.util.ArrayList; import java.util.List; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.persistence.Query; import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReaderFactory; import org.hibernate.envers.RevisionType; import org.hibernate.envers.query.AuditEntity; import org.hibernate.envers.query.AuditQuery; /** * @author Madhumita Sadhukhan */ @Stateless public class SLSBAudit { @PersistenceContext(unitName = "myCustPhone") EntityManager em; public Customer createCustomer(String firstName, String surName, String type, String areacode, String phnumber) { Phone phone1 = new Phone(); phone1.setNumber(phnumber); phone1.setAreacode(areacode); phone1.setType(type); Phone phone2 = new Phone(); phone2.setNumber("777222123"); phone2.setAreacode("+420"); phone2.setType("HOME"); Customer cust = new Customer(); cust.setFirstname(firstName); cust.setSurname(surName); cust.getPhones().add(phone1); cust.getPhones().add(phone2); em.persist(cust); em.persist(phone1); em.persist(phone2); return cust; } public Customer updateCustomer(Customer c) { return em.merge(c); } public Phone updatePhone(Phone p) { return em.merge(p); } public void deletePhone(Phone p) { em.remove(em.merge(p)); } public int retrieveOldPhoneListSizeFromCustomer(int id, int revnumber) { AuditReader reader = AuditReaderFactory.get(em); Customer cust_rev = reader.find(Customer.class, id, revnumber); return cust_rev.getPhones().size(); } public String retrieveOldPhoneListVersionFromCustomer(int id) { AuditReader reader = AuditReaderFactory.get(em); Customer cust_rev = reader.find(Customer.class, id, 2); return cust_rev.getPhones().get(1).getType(); } public List<Object> verifyRevision(int id) { AuditReader reader = AuditReaderFactory.get(em); // boolean b; // String queryString = "select a.originalId.REV from " + "CUSTOMER_PHONE" + "_AUD a"; // String queryString = "select column_name from information_schema.columns where table_name = 'CUSTOMER_PHONE_AUD'"; // Query query = em.createQuery(queryString); List<Object> custHistory = new ArrayList<Object>(); List<Number> revList = reader.getRevisions(Customer.class, id); for (Number revisionNumber : revList) { AuditQuery query = reader.createQuery().forEntitiesAtRevision(Customer.class, revisionNumber); query.add(AuditEntity.property("firstname").eq("MADHUMITA")); if (query.getResultList() != null && query.getResultList().size() > 0) { custHistory.add(query.getResultList()); } } return custHistory; } public List<Object> verifyRevisionType(int id) { AuditReader reader = AuditReaderFactory.get(em); List<Object> custHistory = new ArrayList<Object>(); List<Number> revList = reader.getRevisions(Customer.class, id); for (Number revisionNumber : revList) { AuditQuery query = reader.createQuery().forEntitiesAtRevision(Customer.class, revisionNumber); query.add(AuditEntity.revisionType().eq(RevisionType.MOD)); if (query.getResultList() != null && query.getResultList().size() > 0) { custHistory.add(query.getResultList()); } } return custHistory; } public List<Object> verifyOtherFields(int id) { AuditReader reader = AuditReaderFactory.get(em); boolean b; Customer cust1_rev = reader.find(Customer.class, id, 3); String queryString = "select a.originalId.phones_id from CUSTOMER_PHONE_AUD a"; Query query = em.createQuery(queryString); List<Object> custHistory = query.getResultList(); return custHistory; } }
4,105
31.587302
126
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/SoccerPlayer.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import jakarta.persistence.Entity; import jakarta.persistence.PrimaryKeyJoinColumn; import jakarta.persistence.Table; import org.hibernate.envers.Audited; /** * @author Madhumita Sadhukhan */ @Entity @Audited @Table(name = "SOCCERPLAYER") @PrimaryKeyJoinColumn(name = "SOCCERPLAYER_ID") public class SoccerPlayer extends Player { private String clubName; public String getClubName() { return clubName; } public void setClubName(String clubName) { this.clubName = clubName; } }
1,605
31.12
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/PhoneMO.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToOne; import org.hibernate.envers.AuditJoinTable; import org.hibernate.envers.Audited; /** * @author Madhumita Sadhukhan */ @Entity @Audited public class PhoneMO { @Id @GeneratedValue @Column(name = "PHONE_ID") private Integer id; private String type; private String number; private String areacode; @ManyToOne @JoinTable(name = "CUSTOMER_PHONE", joinColumns = {@JoinColumn(name = "PHONE_ID", referencedColumnName = "PHONE_ID")}, inverseJoinColumns = {@JoinColumn(name = "CUST_ID", referencedColumnName = "CUST_ID")}) @AuditJoinTable(name = "CUSTOMER_PHONE_AUD", inverseJoinColumns = {@JoinColumn(name = "CUST_ID", referencedColumnName = "CUST_ID")}) private CustomerMO customer; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getAreacode() { return areacode; } public void setAreacode(String areacode) { this.areacode = areacode; } public CustomerMO getCustomer() { return customer; } public void setCustomer(CustomerMO customer) { this.customer = customer; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof PhoneMO)) { return false; } final PhoneMO phone = (PhoneMO) o; return id != null ? id.equals(phone.id) : phone.id == null; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
3,137
26.286957
210
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/AuditJoinTableoverBidirectionalTest.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; /** * Test @AuditJoinTable over Bidirectional Relationship * * @author Madhumita Sadhukhan */ @RunWith(Arquillian.class) public class AuditJoinTableoverBidirectionalTest { private static final String ARCHIVE_NAME = "jpa_AuditJoinTableoverBidirectionalTest"; @ArquillianResource private static InitialContext iniCtx; @BeforeClass public static void beforeClass() throws NamingException { iniCtx = new InitialContext(); } @Deployment public static Archive<?> deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); jar.addClasses(CustomerMO.class, PhoneMO.class, SLSBAuditMO.class); jar.addAsManifestResource(AuditJoinTableoverBidirectionalTest.class.getPackage(), "persistence.xml", "persistence.xml"); return jar; } protected static <T> T lookup(String beanName, Class<T> interfaceType) throws NamingException { return interfaceType .cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanName + "!" + interfaceType.getName())); } @Test public void testRevisionsforValidityStrategyoverManytoOne() throws Exception { SLSBAuditMO slsbAudit = lookup("SLSBAuditMO", SLSBAuditMO.class); CustomerMO c1 = slsbAudit.createCustomer("MADHUMITA", "SADHUKHAN", "WORK", "+420", "543789654"); PhoneMO p1 = c1.getPhones().get(0); p1.setType("Emergency"); p1.setCustomer(c1); slsbAudit.updatePhone(p1); c1.setFirstname("Madhu"); slsbAudit.updateCustomer(c1); int c = slsbAudit.retrieveOldPhoneListSizeFromCustomer(c1.getId()); Assert.assertEquals(2, c); String phoneType = slsbAudit.retrieveOldPhoneListVersionFromCustomer(c1.getId()); // check that updating Phone updates audit information fetched from Customer Assert.assertEquals("WORK", phoneType); PhoneMO p3 = slsbAudit.createPhone("WORK", "+420", "543789654"); p3.setCustomer(c1); slsbAudit.updatePhone(p3); c1.getPhones().add(p3); slsbAudit.updateCustomer(c1); PhoneMO p4 = slsbAudit.createPhone("WORK", "+420", "88899912"); slsbAudit.updatePhone(p4); c1.getPhones().add(p4); //System.out.println("PhoneList size::" + c1.getPhones().size()); slsbAudit.updateCustomer(c1); int check = slsbAudit.retrieveOldPhoneListSizeFromCustomer(c1.getId()); Assert.assertEquals(4, check); } }
4,070
36.009091
128
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/Customer.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import java.util.ArrayList; import java.util.List; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; import jakarta.persistence.OneToMany; import org.hibernate.envers.AuditJoinTable; import org.hibernate.envers.Audited; /** * @author Madhumita Sadhukhan */ @Entity @Audited public class Customer { @Id @GeneratedValue @Column(name = "CUST_ID") private Integer id; private String firstname; private String surname; @OneToMany @JoinTable(name = "CUSTOMER_PHONE", joinColumns = {@JoinColumn(name = "CUST_ID", referencedColumnName = "CUST_ID")}, inverseJoinColumns = {@JoinColumn(name = "PHONE_ID", referencedColumnName = "PHONE_ID")}) @AuditJoinTable(name = "CUSTOMER_PHONE_AUD", inverseJoinColumns = {@JoinColumn(name = "PHONE_ID", referencedColumnName = "PHONE_ID")}) private List<Phone> phones = new ArrayList<Phone>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public List<Phone> getPhones() { return phones; } public void setSurname(List<Phone> phones) { this.phones = phones; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Customer)) { return false; } final Customer cust = (Customer) o; return id != null ? id.equals(cust.id) : cust.id == null; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
3,063
27.635514
210
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/Address.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import java.util.HashSet; import java.util.Set; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import org.hibernate.envers.Audited; /** * @author Strong Liu */ @Entity @Audited @Table(name = "ADDRESS_HIB") public class Address { @Id @GeneratedValue private int id; private String streetName; private Integer houseNumber; @OneToMany(mappedBy = "address") private Set<Person> persons = new HashSet<Person>(); public Integer getHouseNumber() { return houseNumber; } public void setHouseNumber(Integer houseNumber) { this.houseNumber = houseNumber; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; } public String getStreetName() { return streetName; } public void setStreetName(String streetName) { this.streetName = streetName; } }
2,274
25.453488
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/Phone.java
/* * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.test.integration.jpa.hibernate.envers; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import org.hibernate.envers.Audited; /** * @author Madhumita Sadhukhan */ @Entity @Audited public class Phone { @Id @GeneratedValue @Column(name = "PHONE_ID") private Integer id; private String type; private String number; private String areacode; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getAreacode() { return areacode; } public void setAreacode(String areacode) { this.areacode = areacode; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Phone)) { return false; } final Phone phone = (Phone) o; return id != null ? id.equals(phone.id) : phone.id == null; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
2,411
23.612245
70
java
null
wildfly-main/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/envers/SLSBAuditMO.java
package org.jboss.as.test.integration.jpa.hibernate.envers; import java.util.List; import jakarta.ejb.Stateless; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReaderFactory; /** * @author Madhumita Sadhukhan */ @Stateless public class SLSBAuditMO { @PersistenceContext(unitName = "myCustPhone") EntityManager em; public CustomerMO createCustomer(String firstName, String surName, String type, String areacode, String phnumber) { PhoneMO phone1 = new PhoneMO(); phone1.setNumber(phnumber); phone1.setAreacode(areacode); phone1.setType(type); PhoneMO phone2 = new PhoneMO(); phone2.setNumber("777222123"); phone2.setAreacode("+420"); phone2.setType("HOME"); CustomerMO cust = new CustomerMO(); cust.setFirstname(firstName); cust.setSurname(surName); cust.getPhones().add(phone1); cust.getPhones().add(phone2); em.persist(cust); em.persist(phone1); em.persist(phone2); return cust; } public CustomerMO updateCustomer(CustomerMO c) { return em.merge(c); } public PhoneMO createPhone(String type, String areacode, String phnumber) { PhoneMO phone1 = new PhoneMO(); phone1.setNumber(phnumber); phone1.setAreacode(areacode); phone1.setType(type); em.persist(phone1); return phone1; } public PhoneMO updatePhone(PhoneMO p) { return em.merge(p); } public void deletePhone(PhoneMO p) { em.remove(em.merge(p)); } public int retrieveOldPhoneListSizeFromCustomer(int id) { AuditReader reader = AuditReaderFactory.get(em); List<Number> revList = reader.getRevisions(CustomerMO.class, id); CustomerMO cust_rev = reader.find(CustomerMO.class, id, revList.get(revList.size() - 1)); return cust_rev.getPhones().size(); } public String retrieveOldPhoneListVersionFromCustomer(int id) { AuditReader reader = AuditReaderFactory.get(em); CustomerMO cust_rev = reader.find(CustomerMO.class, id, 1); return cust_rev.getPhones().get(0).getType(); } }
2,292
26.963415
119
java