SeverityComparator implements Comparator<LogRecord>, Serializable { public Throwable apply(final Throwable chain) { int limit = 0; Throwable root = chain; Throwable high = null; Throwable normal = null; for (Throwable cause = chain; cause != null; cause = cause.getCause()) { root = cause; if (isNormal(cause)) { normal = cause; } if (normal == null && cause instanceof Error) { high = cause; } if (++limit == (1 << 16)) { break; } } return high != null ? high : normal != null ? normal : root; }  Throwable apply(final Throwable chain); final int applyThenCompare(Throwable tc1, Throwable tc2); int compareThrowable(final Throwable t1, final Throwable t2); int compare(final LogRecord o1, final LogRecord o2); @Override boolean equals(final Object o); @Override int hashCode(); boolean isNormal(final Throwable t);  }
@Test public void testApplyNull() { SeverityComparator a = new SeverityComparator(); assertNull(a.apply(null)); }
@Test public void testApplyErrorByNormal() { SeverityComparator a = new SeverityComparator(); Throwable next = this.headIeChain(new AssertionError()); next = new Error(next); next = new NoClassDefFoundError().initCause(next); Throwable reduced = a.apply(next); assertEquals(Error.class, reduced.getClass()); }
@Test(timeout = 30000) public void testApplyEvil() { SeverityComparator a = new SeverityComparator(); final int len = 7; final Class<? extends Throwable> type = Exception.class; Throwable l = a.apply(createEvilThrowable(type, len)); assertEquals(type, l.getClass()); }
@Test public void testApplyProxyNormal() { SeverityComparator a = new SeverityComparator(); Throwable next = this.headIeChain(new AssertionError()); next = new UndeclaredThrowableException(next); Throwable reduced = a.apply(next); assertEquals(InterruptedException.class, reduced.getClass()); }
@Test public void testApplyReflectNormal() { SeverityComparator a = new SeverityComparator(); Throwable next = this.headIeChain(new AssertionError()); next = new InvocationTargetException(next); Throwable reduced = a.apply(next); assertEquals(InterruptedException.class, reduced.getClass()); }
@Test public void testApplyNormalByNormal() { SeverityComparator a = new SeverityComparator(); Throwable next = this.headIeChain(new AssertionError()); next = new Error(next); next = this.headIioeChain(next); next = new ExecutionException(next); Throwable reduced = a.apply(next); assertEquals(InterruptedException.class, reduced.getClass()); }
@Test public void testApplyFindsRootCause() { SeverityComparator a = new SeverityComparator(); final Throwable root = new Exception(); Throwable next = root; for (int i = 0; i < 7; i++) { next = new RuntimeException(next); } Throwable reduced = a.apply(next); assertEquals(root, reduced); }