<commit_before>package de.is24.util.monitoring;

import de.is24.util.monitoring.jmx.JmxAppMon4JNamingStrategy;
import de.is24.util.monitoring.keyhandler.DefaultKeyEscaper;
import de.is24.util.monitoring.tools.DoNothingReportVisitor;
import org.junit.Before;
import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;


public class InApplicationMonitorTest {
  private static final String INITIALIZED_COUNTER_1 = "initialized.counter.1";

  @Before
  public void setupClass() {
    InApplicationMonitor.resetInstanceForTesting();
  }

  @Test
  public void explicitInitializationShouldTakeOverInitializedCountersAndTimers() {
    InApplicationMonitor.getInstance().initializeCounter(INITIALIZED_COUNTER_1);

    DefaultKeyEscaper keyEscaper = new DefaultKeyEscaper();
    CorePlugin corePlugin = new CorePlugin(new JmxAppMon4JNamingStrategy() {
        @Override
        public String getJmxPrefix() {
          return "lala";
        }
      }, keyEscaper);
    InApplicationMonitor explicitInitializedInApplicationMonitor = InApplicationMonitor.initInstance(
      corePlugin, keyEscaper);

    CheckForCounterExistenceReportVisitor reportVisitor = new CheckForCounterExistenceReportVisitor(
      INITIALIZED_COUNTER_1);
    assertThat(explicitInitializedInApplicationMonitor.getCorePlugin()).isSameAs(corePlugin);
    explicitInitializedInApplicationMonitor.getCorePlugin().reportInto(reportVisitor);
    assertThat(reportVisitor.found).isTrue();
    assertThat(JMXTestHelper.checkInApplicationMonitorJMXBeanRegistered("lala")).isEqualTo(true);

    assertThat(JMXTestHelper.getCounterValue("lala", INITIALIZED_COUNTER_1)).isEqualTo(0);
  }

  private static class CheckForCounterExistenceReportVisitor extends DoNothingReportVisitor {
    private final String counterName;
    private boolean found = false;

    CheckForCounterExistenceReportVisitor(String counterName) {
      this.counterName = counterName;
    }

    @Override<commit_msg>make tests more specific, to explain contract better<commit_after>package de.is24.util.monitoring;

import de.is24.util.monitoring.jmx.JmxAppMon4JNamingStrategy;
import de.is24.util.monitoring.keyhandler.DefaultKeyEscaper;
import de.is24.util.monitoring.tools.DoNothingReportVisitor;
import org.junit.Before;
import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;


public class InApplicationMonitorTest {
  private static final String INITIALIZED_COUNTER_1 = "initialized.counter.1";

  @Before
  public void setupClass() {
    InApplicationMonitor.resetInstanceForTesting();
  }


  @Test
  public void explicitInitializationShouldKeepInApplicationMonitorInstance() {
    // given a default configured InApplication Monitor
    InApplicationMonitor defaultInstance = InApplicationMonitor.getInstance();

    // when explicitly initializing InApplicationMonitor
    initializeWithJMXNaming();

    // then the new CorePlugin Instance is used
    assertThat(InApplicationMonitor.getInstance()).isSameAs(defaultInstance);
  }

  @Test
  public void explicitInitializationShouldSetCorePlugin() {
    // given a default configured InApplication Monitor with a counter added
    InApplicationMonitor.getInstance().initializeCounter(INITIALIZED_COUNTER_1);

    // when explicitly initializing InApplicationMonitor
    DefaultKeyEscaper keyEscaper = new DefaultKeyEscaper();
    CorePlugin corePlugin = new CorePlugin(new JmxAppMon4JNamingStrategy() {
        @Override
        public String getJmxPrefix() {
          return "lala";
        }
      }, keyEscaper);
    InApplicationMonitor explicitInitializedInApplicationMonitor = InApplicationMonitor.initInstance(
      corePlugin, keyEscaper);

    // then the new CorePlugin Instance is used
    assertThat(explicitInitializedInApplicationMonitor.getCorePlugin()).isSameAs(corePlugin);
  }


  @Test
  public void explicitInitializationShouldTakeOverInitializedCountersAndTimersIntoCorePlugin() {
    // given a default configured InApplication Monitor with a counter added
    InApplicationMonitor.getInstance().initializeCounter(INITIALIZED_COUNTER_1);

    // when explicitly initializing InApplicationMonitor
    CorePlugin corePlugin = initializeWithJMXNaming();

    // then
    CheckForCounterExistenceReportVisitor reportVisitor = new CheckForCounterExistenceReportVisitor(
      INITIALIZED_COUNTER_1);
    corePlugin.reportInto(reportVisitor);
    assertThat(reportVisitor.found).isTrue();

  }

  @Test
  public void explicitInitializationShouldTakeOverInitializedCountersAndTimersIntoJMX() {
    // given a default configured InApplication Monitor with a counter added
    InApplicationMonitor.getInstance().initializeCounter(INITIALIZED_COUNTER_1);

    // when explicitly initializing InApplicationMonitor
    initializeWithJMXNaming();

    // then
    assertThat(JMXTestHelper.getCounterValue("lala", INITIALIZED_COUNTER_1)).isEqualTo(0);
  }


  @Test
  public void explicitInitializationWithJMXNamingStrategyShouldInitializeJMX() {
    // given a default configured InApplication Monitor
    InApplicationMonitor.getInstance();

    // when explicitly initializing InApplicationMonitor
    initializeWithJMXNaming();

    // then
    assertThat(JMXTestHelper.checkInApplicationMonitorJMXBeanRegistered("lala")).isEqualTo(true);
  }


  private CorePlugin initializeWithJMXNaming() {
    DefaultKeyEscaper keyEscaper = new DefaultKeyEscaper();
    CorePlugin corePlugin = new CorePlugin(new JmxAppMon4JNamingStrategy() {
        @Override
        public String getJmxPrefix() {
          return "lala";
        }
      }, keyEscaper);
    InApplicationMonitor explicitInitializedInApplicationMonitor = InApplicationMonitor.initInstance(
      corePlugin, keyEscaper);
    return corePlugin;
  }


  private static class CheckForCounterExistenceReportVisitor extends DoNothingReportVisitor {
    private final String counterName;
    private boolean found = false;

    CheckForCounterExistenceReportVisitor(String counterName) {
      this.counterName = counterName;
    }

    @Override
{'fraction_non_alphanumeric': 0.07125144652008597, 'fraction_numerical': 0.005786080343858489, 'mean_word_length': 5.001984126984127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1463924', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1583, 'n_words': 310}