### Question:
CompositeException extends RuntimeException { @Override public synchronized Throwable getCause() { if (cause == null) { CompositeExceptionCausalChain localCause = new CompositeExceptionCausalChain(); Set<Throwable> seenCauses = new HashSet<Throwable>(); Throwable chain = localCause; for (Throwable e : exceptions) { if (seenCauses.contains(e)) { continue; } seenCauses.add(e); List<Throwable> listOfCauses = getListOfCauses(e); for (Throwable child : listOfCauses) { if (seenCauses.contains(child)) { e = new RuntimeException("Duplicate found in causal chain so cropping to prevent loop ..."); continue; } seenCauses.add(child); } try { chain.initCause(e); } catch (Throwable t) { } chain = getRootCause(chain); } cause = localCause; } return cause; } CompositeException(Throwable... exceptions);  CompositeException(Iterable<? extends Throwable> errors); List<Throwable> getExceptions(); @Override String getMessage(); @Override synchronized Throwable getCause(); @Override void printStackTrace(); @Override void printStackTrace(PrintStream s); @Override void printStackTrace(PrintWriter s); int size();  }### Answer:
@Test public void cyclicRootCause() { RuntimeException te = new RuntimeException() { private static final long serialVersionUID = -8492568224555229753L; Throwable cause; @Override public Throwable initCause(Throwable c) { return this; } @Override public Throwable getCause() { return cause; } }; assertSame(te, new CompositeException(te).getCause().getCause()); assertSame(te, new CompositeException(new RuntimeException(te)).getCause().getCause().getCause()); }
@Test public void nullRootCause() { RuntimeException te = new RuntimeException() { private static final long serialVersionUID = -8492568224555229753L; @Override public Throwable getCause() { return null; } }; assertSame(te, new CompositeException(te).getCause().getCause()); assertSame(te, new CompositeException(new RuntimeException(te)).getCause().getCause().getCause()); }
@Test public void badException() { Throwable e = new BadException(); assertSame(e, new CompositeException(e).getCause().getCause()); assertSame(e, new CompositeException(new RuntimeException(e)).getCause().getCause().getCause()); }