@Test
 public void testParseConfigReload() throws Exception {
   JmxTransExporterConfiguration config =
       new JmxTransConfigurationXmlLoader("classpath:jmxtrans-config-reload-test.xml")
           .loadConfiguration();
   assertThat(config.getConfigReloadInterval(), equalTo(2));
 }
  @Test
  public void testParseConfiguration() throws Exception {
    JmxTransExporterConfiguration config =
        new JmxTransConfigurationXmlLoader("classpath:jmxtrans-agent.xml").loadConfiguration();

    assertThat(config.collectInterval, is(11));
    assertThat(config.collectIntervalTimeUnit, is(TimeUnit.SECONDS));
    assertThat(config.getConfigReloadInterval(), equalTo(-1));

    OutputWriter decoratedOutputWriter = config.outputWriter;
    // CircuitBreaker
    assertTrue(decoratedOutputWriter.getClass().equals(OutputWriterCircuitBreakerDecorator.class));
    OutputWriterCircuitBreakerDecorator circuitBreakerDecorator =
        (OutputWriterCircuitBreakerDecorator) decoratedOutputWriter;
    assertThat(circuitBreakerDecorator.isDisabled(), is(false));

    // Graphite Writer
    assertTrue(
        circuitBreakerDecorator.delegate.getClass().equals(GraphitePlainTextTcpOutputWriter.class));
    GraphitePlainTextTcpOutputWriter graphiteWriter =
        (GraphitePlainTextTcpOutputWriter) circuitBreakerDecorator.delegate;
    assertThat(graphiteWriter.graphiteServerHostAndPort.getPort(), is(2203));
    assertThat(graphiteWriter.graphiteServerHostAndPort.getHost(), is("localhost"));
    assertThat(graphiteWriter.getMetricPathPrefix(), is("app_123456.server.i876543."));

    assertThat(config.queries.size(), is(13));

    Map<String, Query> queriesByResultAlias = indexQueriesByResultAlias(config.queries);

    {
      Query query = queriesByResultAlias.get("os.systemLoadAverage");
      assertThat(query.objectName, is(new ObjectName("java.lang:type=OperatingSystem")));
      assertThat(query.getAttributes(), contains("SystemLoadAverage"));
      assertThat(query.resultAlias, is("os.systemLoadAverage"));
      assertThat(query.key, is((String) null));
    }
    {
      Query query = queriesByResultAlias.get("jvm.heapMemoryUsage.used");
      assertThat(query.objectName, is(new ObjectName("java.lang:type=Memory")));
      assertThat(query.getAttributes(), contains("HeapMemoryUsage"));
      assertThat(query.resultAlias, is("jvm.heapMemoryUsage.used"));
      assertThat(query.key, is("used"));
    }
    Map<String, Invocation> invocationsByResultAlias =
        indexInvocationsByResultAlias(config.invocations);
    {
      Invocation invocation = invocationsByResultAlias.get("jvm.gc");
      assertThat(invocation.objectName, is(new ObjectName("java.lang:type=Memory")));
      assertThat(invocation.operationName, is("gc"));
      assertThat(invocation.resultAlias, is("jvm.gc"));
    }
  }
  @Test
  public void testErrorOnLoadingExternalPropertiesDoesNotPropagate() throws Exception {
    PropertiesLoader propertiesLoader =
        new PropertiesLoader() {

          @Override
          public Map<String, String> loadProperties() {
            throw new RuntimeException("Expected - thrown by test");
          }
        };
    JmxTransConfigurationXmlLoader configLoader =
        new JmxTransConfigurationXmlLoader(
            "classpath:jmxtrans-external-properties-test.xml", propertiesLoader);
    JmxTransExporterConfiguration config = configLoader.build(configLoader);
    assertThat(config.getCollectInterval(), equalTo(222));
  }
  @Test
  public void testExternalPropertiesSourceIsUsed() throws Exception {
    PropertiesLoader propertiesLoader =
        new PropertiesLoader() {

          @Override
          public Map<String, String> loadProperties() {
            HashMap<String, String> m = new HashMap<>();
            m.put("jmxtrans.agent.collect.interval", "999");
            return m;
          }
        };
    JmxTransConfigurationXmlLoader configLoader =
        new JmxTransConfigurationXmlLoader(
            "classpath:jmxtrans-external-properties-test.xml", propertiesLoader);
    JmxTransExporterConfiguration config = configLoader.build(configLoader);
    assertThat(config.getCollectInterval(), equalTo(999));
  }