@Override public Object runMethodInEjbContext(String className, String methodName, Class<?> argumentTypes[], Object[] args) throws Exception { try (TxHandler txHandler = new TxHandler(this.tx, true)) { Class<?> clazz = Class.forName(className); Object instance = clazz.newInstance(); Method method = clazz.getMethod(methodName, argumentTypes); Object res = method.invoke(instance, args); txHandler.commit(); return res; } }
private TxHandler(EntityManager em, UserTransaction tx, boolean begin) { this.em = em; this.tx = tx; if (begin) this.begin(); }
@Override public void close() { this.rollbackIfNotCommitted(); } }
/** * Commits the transaction without beginning a new one. */ public void commit() { this.commit(false); }
@Override public Object runMethodInEjbContext(String className, String methodName, Class<?> argumentTypes[], Object[] args) throws Exception { try (TxHandler txHandler = new TxHandler(this.tx, true)) { Class<?> clazz = Class.forName(className); Object instance = clazz.newInstance(); Method method = clazz.getMethod(methodName, argumentTypes); Object res = method.invoke(instance, args); txHandler.commit(); return res; } }
/** * Commits the transaction * @param beginNew If true, begins a new transaction */ public void commit(boolean beginNew) { if (this.em != null) { if (!this.committed) { this.em.getTransaction().commit(); this.committed = true; } if (beginNew) this.em.getTransaction().begin(); } else { try { if (!this.committed) { if (this.tx != null) tx.commit(); this.committed = true; } } catch (Exception ex) { throw new JuDbException("Couldn't commit JTA transaction", ex); } if (beginNew) this.begin(); } }
@Override public void runTestMethodInEjbContext(TestRunnerAnnotationHandler handler) throws Exception { try (ContainerTestContextSetter s = handler.new ContainerTestContextSetter()) { try (TxHandler txHandler = new TxHandler(this.tx, true)) { logger.debug(String.format("Running Test %s", handler)); // this.dateProvider.resetProvider(); // Run the test method handler.executeTestMethod(txHandler); txHandler.commit(); // Perform a commit after the execution of the test method } } }
@Override public void runTestMethodInEjbContext(TestRunnerAnnotationHandler handler) throws Exception { try (ContainerTestContextSetter s = handler.new ContainerTestContextSetter()) { try (TxHandler txHandler = new TxHandler(this.tx, true)) { logger.debug(String.format("Running Test %s", handler)); // this.dateProvider.resetProvider(); // Run the test method handler.executeTestMethod(txHandler); txHandler.commit(); // Perform a commit after the execution of the test method } } }
@Override public void runPostTestActionsInEjbContext(TestRunnerAnnotationHandler handler) throws Exception { try (ContainerTestContextSetter s = handler.new ContainerTestContextSetter()) { // Run post server annotations in an own annotation so any changed made there is available in the export / verifying phase try (TxHandler txHandler = new TxHandler(this.tx, true)) { // Execute post test annotations (dataset exporting, data verifying) handler.executePostServerCode(new JuEmUtil(this.em)); txHandler.commit(); // Commit after data verifying / exporting } // Run post test annotations (export, verify) try (TxHandler txHandler = new TxHandler(this.tx, true)) { // Execute post test annotations (dataset exporting, data verifying) handler.executePostTestAnnotations(new JuEmUtil(this.em)); txHandler.commit(); // Commit after data verifying / exporting } } }
@Override public void runPostTestActionsInEjbContext(TestRunnerAnnotationHandler handler) throws Exception { try (ContainerTestContextSetter s = handler.new ContainerTestContextSetter()) { // Run post server annotations in an own annotation so any changed made there is available in the export / verifying phase try (TxHandler txHandler = new TxHandler(this.tx, true)) { // Execute post test annotations (dataset exporting, data verifying) handler.executePostServerCode(new JuEmUtil(this.em)); txHandler.commit(); // Commit after data verifying / exporting } // Run post test annotations (export, verify) try (TxHandler txHandler = new TxHandler(this.tx, true)) { // Execute post test annotations (dataset exporting, data verifying) handler.executePostTestAnnotations(new JuEmUtil(this.em)); txHandler.commit(); // Commit after data verifying / exporting } } }
@Override public SystemPropertyTempSetter runPreTestActionsInEjbContext(TestRunnerAnnotationHandler handler) throws Exception { try (ContainerTestContextSetter s = handler.new ContainerTestContextSetter()) { SystemPropertyTempSetter tempSetter = handler.initContainerTestEnv(); try (TxHandler txHandler = new TxHandler(this.tx, true)) { // Execute post test annotations (dataset exporting, data verifying) handler.executePreTestAnnotations(new JuEmUtil(this.em)); txHandler.commit(); // Commit after data verifying / exporting } catch (Exception ex) { // Reset properties in case of an exception tempSetter.close(); throw ex; } return tempSetter; } }
@Override public SystemPropertyTempSetter runPreTestActionsInEjbContext(TestRunnerAnnotationHandler handler) throws Exception { try (ContainerTestContextSetter s = handler.new ContainerTestContextSetter()) { SystemPropertyTempSetter tempSetter = handler.initContainerTestEnv(); try (TxHandler txHandler = new TxHandler(this.tx, true)) { // Execute post test annotations (dataset exporting, data verifying) handler.executePreTestAnnotations(new JuEmUtil(this.em)); txHandler.commit(); // Commit after data verifying / exporting } catch (Exception ex) { // Reset properties in case of an exception tempSetter.close(); throw ex; } return tempSetter; } }