SQLException extends Exception implements Iterable<Throwable> { public void setNextException(SQLException exception) { SQLException curr = this; while (true) { SQLException next = curr.next; if (next == null) { curr.next = exception; return; } curr = next; } } SQLException(String message, @Nullable String sqlState, int errorCode);  SQLException(String message, @Nullable String sqlState);  SQLException(String message);  SQLException();  SQLException(@Nullable Throwable cause);  SQLException(String message, @Nullable Throwable cause);  SQLException(String message, @Nullable String sqlState, @Nullable Throwable cause);  SQLException(String message, @Nullable String sqlState, int errorCode,
            @Nullable Throwable cause); @Nullable String getSQLState(); int getErrorCode(); @Nullable SQLException getNextException(); void setNextException(SQLException exception); @Override Iterator<Throwable> iterator();  }
@Test public void testIteratorWithCauseOnlyOnFirst() { Throwable t1 = new Throwable(); SQLException e1 = new SQLException(t1); SQLException e2 = new SQLException(); e1.setNextException(e2); List<Throwable> list = Lists.newArrayList(e1); assertThat(list.get(0)).isEqualTo(e1); assertThat(list.get(1)).isEqualTo(t1); assertThat(list.get(2)).isEqualTo(e2); }
@Test public void testIteratorWithCauseOnlyOnSecond() { Throwable t2 = new Throwable(); SQLException e1 = new SQLException(); SQLException e2 = new SQLException(t2); e1.setNextException(e2); List<Throwable> list = Lists.newArrayList(e1); assertThat(list.get(0)).isEqualTo(e1); assertThat(list.get(1)).isEqualTo(e2); assertThat(list.get(2)).isEqualTo(t2); }
@Test public void testIteratorWithNoCause() { SQLException e1 = new SQLException(); SQLException e2 = new SQLException(); e1.setNextException(e2); List<Throwable> list = Lists.newArrayList(e1); assertThat(list.get(0)).isEqualTo(e1); assertThat(list.get(1)).isEqualTo(e2); }
@Test public void testCallSetNextExceptionMultipleTimesOnSameSQLException() { Throwable t1 = new Throwable(); Throwable t2 = new Throwable(); Throwable t3 = new Throwable(); SQLException e1 = new SQLException(t1); SQLException e2 = new SQLException(t2); SQLException e3 = new SQLException(t3); e1.setNextException(e2); e1.setNextException(e3); List<Throwable> list = Lists.newArrayList(e1); assertThat(list.get(0)).isEqualTo(e1); assertThat(list.get(1)).isEqualTo(t1); assertThat(list.get(2)).isEqualTo(e2); assertThat(list.get(3)).isEqualTo(t2); assertThat(list.get(4)).isEqualTo(e3); assertThat(list.get(5)).isEqualTo(t3); }