SimpleStackCollector implements ISampler { @Override public void sample() { Map<Thread, StackTraceElement[]> stackDump = Thread.getAllStackTraces(); stackDump.remove(ignore); for (Map.Entry<Thread, StackTraceElement[]> element : stackDump.entrySet()) { StackTraceElement[] stackTrace = element.getValue(); if (stackTrace.length > 0) { collector.collect(stackTrace); } else { collector.collect(new StackTraceElement[]{ new StackTraceElement("Thread", element.getKey().getName(), "", 0) }); } } } SimpleStackCollector(final Thread ignore); @Override void sample(); @Override Map<String, SampleNode> getCollectionsAndReset(); @Override Map<String, SampleNode> getCollections(); @Override String toString();  }
@Test @SuppressFBWarnings({"PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS", "ES_COMPARING_STRINGS_WITH_EQ"}) public void testSample() { StackCollector instance = new StackCollectorImpl(); StackTraceElement[] st1 = new StackTraceElement[3]; st1[0] = new StackTraceElement("C1", "m1", "C1.java", 10); st1[1] = new StackTraceElement("C1", "m2", "C1.java", 11); st1[2] = new StackTraceElement("C1", "m3", "C1.java", 12); instance.collect(st1); LOG.debug("Collector = {}", instance); Assert.assertEquals(4, instance.get().getNrNodes()); StackTraceElement[] st2 = new StackTraceElement[1]; st2[0] = new StackTraceElement("C1", "m1", "C1.java", 10); instance.collect(st2); LOG.debug("Collector = {}", instance); Assert.assertEquals(5, instance.get().getNrNodes()); StackTraceElement[] st3 = new StackTraceElement[3]; st3[0] = new StackTraceElement("C2", "m1", "C2.java", 10); st3[1] = new StackTraceElement("C2", "m2", "C2.java", 11); st3[2] = new StackTraceElement("C2", "m3", "C2.java", 12); instance.collect(st3); LOG.debug("Collector = {}", instance); StackTraceElement[] st4 = new StackTraceElement[3]; st4[0] = new StackTraceElement("C1", "m1", "C1.java", 10); st4[1] = new StackTraceElement("C1", "m2", "C1.java", 11); st4[2] = new StackTraceElement("C1", "m4", "C1.java", 14); instance.collect(st4); LOG.debug("Collector = {}", instance); Thread currentThread = Thread.currentThread(); Assert.assertSame(currentThread.getStackTrace()[0].getClassName(), currentThread.getStackTrace()[0].getClassName()); Assert.assertSame(currentThread.getStackTrace()[0].getMethodName(), currentThread.getStackTrace()[0].getMethodName()); }