@Test public void testCorrectHandlerUsedForSubclass() throws Throwable { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); // Extends RemoteException TransactionRolledbackException ex = new TransactionRolledbackException(); MethodInvocation mi = mock(MethodInvocation.class); given(mi.proceed()).willThrow(ex); try { ti.invoke(mi); fail(); } catch (Exception caught) { assertEquals(ex, caught); } assertEquals(1, th.getCalls()); assertEquals(1, th.getCalls("remoteException")); }
/** * Create TransactionRolledbackException with no-arg constructor */ @Test public void test1() { TransactionRolledbackException ex = new TransactionRolledbackException(); assertTrue(ex.getMessage() == null && ex.getCause() == null); }
/** * Create TransactionRolledbackException with message */ @Test public void test2() { TransactionRolledbackException ex = new TransactionRolledbackException(reason); assertTrue(ex.getMessage().equals(reason) && ex.getCause() == null); }
/** * De-Serialize a TransactionRolledbackException from JDBC 4.0 and make sure * you can read it back properly */ @Test public void test3() throws Exception { ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(SerializedTransactionExceptions.TRBE_DATA)); TransactionRolledbackException ex = (TransactionRolledbackException) ois.readObject(); assertTrue(reason.equals(ex.getMessage()) && ex.getCause() == null); }
@Test public void test() throws Exception { final Exception exception = Exception.class.cast(deserialize(serialize(new org.apache.openejb.core.transaction.TransactionRolledbackException("foo", new NullPointerException())))); assertThat(exception, instanceOf(TransactionRolledbackException.class)); assertThat(exception, not(instanceOf(org.apache.openejb.core.transaction.TransactionRolledbackException.class))); assertThat(exception.getMessage(), containsString("foo")); assertThat(exception.getMessage(), containsString("NullPointerException")); exception.printStackTrace(); }
/** * Manages CDI-Contexts to be started before entering end stopped after exiting a Statement. * Additionally it is aware of the DatabaseAware, DatabaseUnaware and BeforeDatabaseAware annotations. * It creates an EntityManagerFactory before entering the test method and closes it after exiting * to always have a clean state of the database. */ @Override protected List<TestRule> getTestRules(final Object target) { List<TestRule> rules = new ArrayList<TestRule>(super.getTestRules(target)); rules.add(new TestRule() { @Override public Statement apply(final Statement base, final Description description) { final DatabaseAware databaseAwareAnnotation; if (description.getAnnotation(DatabaseUnaware.class) != null) { databaseAwareAnnotation = null; } else if (description.getAnnotation(DatabaseAware.class) != null) { databaseAwareAnnotation = description.getAnnotation(DatabaseAware.class); } else { databaseAwareAnnotation = AnnotationUtils.findAnnotation(target.getClass(), DatabaseAware.class); } List<FrameworkMethod> befores = new ArrayList<FrameworkMethod>(0); if (databaseAwareAnnotation != null) { for (FrameworkMethod m : getTestClass().getAnnotatedMethods(BeforeDatabaseAware.class)) { if (databaseAwareAnnotation.unitName().equals(m.getAnnotation(BeforeDatabaseAware.class).unitName())) { befores.add(m); } } } final Statement statement = befores.isEmpty() ? base : new RunBefores(base, befores, target); return new Statement() { @Override public void evaluate() throws Throwable { EntityManagerFactory emf = null; try { if (databaseAwareAnnotation != null) { Map<String, String> properties = new HashMap<String, String>(); properties.put("hibernate.hbm2ddl.auto", "create-drop"); properties.put("hibernate.ejb.entitymanager_factory_name", databaseAwareAnnotation.unitName() + testRun++); emf = Persistence.createEntityManagerFactory(databaseAwareAnnotation.unitName(), properties); } statement.evaluate(); } catch (Throwable e) { // We do some exception unwrapping here to get the real exception @SuppressWarnings("unchecked") Throwable ex = ExceptionUtils.unwrap(e, InvocationTargetException.class, EJBException.class, TransactionRolledbackException.class); throw ex; } finally { if (emf != null && emf.isOpen()) { emf.close(); } } } }; } }); return rules; }
/** * Renamed method so it shows up with a much more understandable purpose as it * will be the top element in the stacktrace * * @param e Throwable * @param method Method */ protected Throwable convertException(final Throwable e, final Method method) { if (!remote && e instanceof RemoteException) { if (e instanceof TransactionRequiredException) { return new TransactionRequiredLocalException(e.getMessage()).initCause(getCause(e)); } if (e instanceof TransactionRolledbackException) { return new TransactionRolledbackLocalException(e.getMessage()).initCause(getCause(e)); } /** * If a client attempts to invoke a method on a removed bean's business interface, * we must throw a javax.ejb.NoSuchEJBException. If the business interface is a * remote business interface that extends java.rmi.Remote, the * java.rmi.NoSuchObjectException is thrown to the client instead. * See EJB 3.0, section 4.4 */ if (e instanceof NoSuchObjectException) { if (java.rmi.Remote.class.isAssignableFrom(method.getDeclaringClass())) { return e; } else { return new NoSuchEJBException(e.getMessage()).initCause(getCause(e)); } } if (e instanceof AccessException) { return new AccessLocalException(e.getMessage()).initCause(getCause(e)); } return new EJBException(e.getMessage()).initCause(getCause(e)); } if (remote && e instanceof EJBAccessException) { if (e.getCause() instanceof Exception) { return new AccessException(e.getMessage(), (Exception) e.getCause()); } else { return new AccessException(e.getMessage()); } } if (!remote && e instanceof EJBTransactionRolledbackException) { return new TransactionRolledbackLocalException(e.getMessage()).initCause(getCause(e)); } return e; }
/** * Renamed method so it shows up with a much more understandable purpose as it * will be the top element in the stacktrace * * @param e Throwable * @param method Method * @param interfce Class */ protected Throwable convertException(Throwable e, final Method method, final Class interfce) { final boolean rmiRemote = Remote.class.isAssignableFrom(interfce); if (e instanceof TransactionRequiredException) { if (!rmiRemote && interfaceType.isBusiness()) { return new EJBTransactionRequiredException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new TransactionRequiredLocalException(e.getMessage()).initCause(getCause(e)); } else { return e; } } if (e instanceof TransactionRolledbackException) { if (!rmiRemote && interfaceType.isBusiness()) { return new EJBTransactionRolledbackException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new TransactionRolledbackLocalException(e.getMessage()).initCause(getCause(e)); } else { return e; } } if (e instanceof NoSuchObjectException) { if (!rmiRemote && interfaceType.isBusiness()) { return new NoSuchEJBException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new NoSuchObjectLocalException(e.getMessage()).initCause(getCause(e)); } else { return e; } } if (e instanceof AccessException) { if (!rmiRemote && interfaceType.isBusiness()) { return new AccessLocalException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new AccessLocalException(e.getMessage()).initCause(getCause(e)); } else { return e; } } if (e instanceof RemoteException) { if (!rmiRemote && interfaceType.isBusiness()) { return new EJBException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new EJBException(e.getMessage()).initCause(getCause(e)); } else { return e; } } for (final Class<?> type : method.getExceptionTypes()) { if (type.isAssignableFrom(e.getClass())) { return e; } } // Exception is undeclared // Try and find a runtime exception in there while (e.getCause() != null && !(e instanceof RuntimeException)) { e = e.getCause(); } return e; }