LegacyJavaRuntime extends AbstractJavaRuntime { @Override public StackTraceElement getCallerStackTraceElement(final int depth) { if (stackTraceElementGetter != null) { try { return (StackTraceElement) stackTraceElementGetter.invoke(new Throwable(), depth); } catch (IllegalAccessException ex) { InternalLogger.log(Level.ERROR, ex, "Failed getting single stack trace element from throwable"); } catch (InvocationTargetException ex) { InternalLogger.log(Level.ERROR, ex.getTargetException(), "Failed getting single stack trace element from throwable"); } } return new Throwable().getStackTrace()[depth]; } LegacyJavaRuntime(); @Override boolean isAndroid(); @Override long getProcessId(); @Override Timestamp getStartTime(); @Override @SuppressWarnings("removal") @IgnoreJRERequirement String getCallerClassName(final int depth); @Override String getCallerClassName(final String loggerClassName); @Override StackTraceElement getCallerStackTraceElement(final int depth); @Override StackTraceElement getCallerStackTraceElement(final String loggerClassName); @Override Timestamp createTimestamp(); @Override TimestampFormatter createTimestampFormatter(final String pattern, final Locale locale);  }
@Test public void callerStackTraceElementByIndex() { assertThat(new LegacyJavaRuntime().getCallerStackTraceElement(1)).isEqualTo(new Throwable().getStackTrace()[0]); }
@Test public void callerStackTraceElementBySuccessor() { String className = LegacyJavaRuntime.class.getName(); assertThat(new LegacyJavaRuntime().getCallerStackTraceElement(className)).isEqualTo(new Throwable().getStackTrace()[0]); }
@Test public void missingSuccessorForCallerStackTraceElement() { LegacyJavaRuntime runtime = new LegacyJavaRuntime(); assertThatThrownBy(() -> runtime.getCallerStackTraceElement(Logger.class.getName())) .isInstanceOf(IllegalStateException.class) .hasMessageContaining(Logger.class.getName()); }
@Test public void missingStackTraceElementGetter() { LegacyJavaRuntime runtime = new LegacyJavaRuntime(); Whitebox.setInternalState(runtime, Method.class, (Object) null); assertThat(runtime.getCallerStackTraceElement(1)).isEqualTo(new Throwable().getStackTrace()[0]); }
@Test public void notAccessibleSingleStackTraceElementGetter() { LegacyJavaRuntime runtime = new LegacyJavaRuntime(); Method method = Whitebox.getMethod(LegacyJavaRuntimeTest.class, "getStackTraceElement", int.class); method.setAccessible(false); Whitebox.setInternalState(runtime, Method.class, method); assertThat(runtime.getCallerStackTraceElement(1)).isEqualTo(new Throwable().getStackTrace()[0]); assertThat(systemStream.consumeErrorOutput()) .containsOnlyOnce("ERROR") .containsOnlyOnce(IllegalAccessException.class.getName()); }
@Test public void stackTraceElementGetterThrowsException() { LegacyJavaRuntime runtime = new LegacyJavaRuntime(); Method method = Whitebox.getMethod(LegacyJavaRuntimeTest.class, "getStackTraceElement", int.class); Whitebox.setInternalState(runtime, Method.class, method); assertThat(runtime.getCallerStackTraceElement(1)).isEqualTo(new Throwable().getStackTrace()[0]); assertThat(systemStream.consumeErrorOutput()) .containsOnlyOnce("ERROR") .containsOnlyOnce(UnsupportedOperationException.class.getName()); }