@Test
  public void testContextInitializers() {
    AppInsightsConfigurationBuilder mockParser = createMockParser(true, true, false);
    ApplicationInsightsXmlConfiguration appConf = mockParser.build(null);
    appConf.setInstrumentationKey(MOCK_IKEY);

    ContextInitializersXmlElement contextInitializersXmlElement =
        new ContextInitializersXmlElement();
    ArrayList<AddTypeXmlElement> contexts = new ArrayList<AddTypeXmlElement>();
    AddTypeXmlElement addXmlElement = new AddTypeXmlElement();
    addXmlElement.setType(
        "com.microsoft.applicationinsights.extensibility.initializer.DeviceInfoContextInitializer");
    contexts.add(addXmlElement);
    contextInitializersXmlElement.setAdds(contexts);
    appConf.setContextInitializers(contextInitializersXmlElement);

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.isTrackingDisabled(), false);
    assertEquals(mockConfiguration.getInstrumentationKey(), MOCK_IKEY);
    assertEquals(mockConfiguration.getContextInitializers().size(), 3);
    assertTrue(mockConfiguration.getTelemetryInitializers().isEmpty());
    assertTrue(mockConfiguration.getChannel() instanceof StdOutChannel);
  }
  private void ikeyTest(String configurationIkey, String expectedIkey) {
    // Make sure that there is no exception when fetching the i-key by having both
    // the i-key and channel in the configuration, otherwise the channel won't be instantiated
    AppInsightsConfigurationBuilder mockParser = createMockParser(true, false, false);

    ApplicationInsightsXmlConfiguration appConf = new ApplicationInsightsXmlConfiguration();
    appConf.setInstrumentationKey(configurationIkey);
    Mockito.doReturn(appConf).when(mockParser).build(any(InputStream.class));

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);
    assertEquals(mockConfiguration.getInstrumentationKey(), expectedIkey);
    assertTrue(mockConfiguration.getChannel() instanceof InProcessTelemetryChannel);
  }
  @Test
  public void testInitializeAllDefaults() throws Exception {
    AppInsightsConfigurationBuilder mockParser = createMockParser(true, true, false);
    ApplicationInsightsXmlConfiguration appConf = mockParser.build(null);
    appConf.setInstrumentationKey(MOCK_IKEY);

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.isTrackingDisabled(), false);
    assertEquals(mockConfiguration.getInstrumentationKey(), MOCK_IKEY);
    assertEquals(mockConfiguration.getContextInitializers().size(), 2);
    assertTrue(mockConfiguration.getTelemetryInitializers().isEmpty());
    assertTrue(mockConfiguration.getChannel() instanceof StdOutChannel);
  }
  @Test
  public void testWithEmptySections() {
    AppInsightsConfigurationBuilder mockParser =
        Mockito.mock(AppInsightsConfigurationBuilder.class);

    ApplicationInsightsXmlConfiguration appConf = new ApplicationInsightsXmlConfiguration();
    appConf.setInstrumentationKey(MOCK_IKEY);
    appConf.setTelemetryInitializers(null);
    appConf.setContextInitializers(null);
    appConf.setModules(null);
    appConf.setSdkLogger(null);
    Mockito.doReturn(appConf).when(mockParser).build(any(InputStream.class));

    TelemetryConfiguration mockConfiguration = new TelemetryConfiguration();

    initializeWithFactory(mockParser, mockConfiguration);

    assertEquals(mockConfiguration.isTrackingDisabled(), false);
    assertEquals(mockConfiguration.getInstrumentationKey(), MOCK_IKEY);
    assertEquals(mockConfiguration.getContextInitializers().size(), 2);
    assertTrue(mockConfiguration.getChannel() instanceof InProcessTelemetryChannel);
  }