<commit_before>/*
 * Copyright 2015 <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cfg4j.source.classpath;

import static org.assertj.core.api.Assertions.assertThat;

import org.assertj.core.data.MapEntry;
import org.cfg4j.source.context.DefaultEnvironment;
import org.cfg4j.source.context.Environment;
import org.cfg4j.source.context.ImmutableEnvironment;
import org.cfg4j.source.context.MissingEnvironmentException;
import org.cfg4j.source.git.ConfigFilesProvider;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;


@RunWith(MockitoJUnitRunner.class)
public class ClasspathConfigurationSourceTest {

  @Rule
  public ExpectedException expectedException = ExpectedException.none();

  private TempConfigurationClasspathRepo classpathRepo;
  private ConfigFilesProvider configFilesProvider;
  private ClasspathConfigurationSource source;

  @Before
  public void setUp() throws Exception {
    classpathRepo = new TempConfigurationClasspathRepo();

    configFilesProvider = () -> Collections.singletonList(
        new File("application.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);
  }

  @Test
  public void getConfigurationShouldReadConfigFromAbsolutePaths() throws Exception {
    assertThat(source.getConfiguration()).containsOnlyKeys("some.setting");
  }

  @Test
  public void getConfigurationShouldThrowOnMissingConfigFile() throws Exception {
    configFilesProvider = () -> Collections.singletonList(
        new File("nonexistent.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    expectedException.expect(IllegalStateException.class);
    source.getConfiguration();
  }

  @Test
  public void getConfigurationShouldThrowOnMalformedConfigFile() throws Exception {
    configFilesProvider = () -> Collections.singletonList(
        new File("malformed.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    expectedException.expect(IllegalStateException.class);
    source.getConfiguration();
  }

  @Test
  public void getConfigurationShouldReadFromGivenFiles() throws Exception {
    configFilesProvider = () -> Arrays.asList(
        new File("application.properties"),
        new File("otherConfig.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    assertThat(source.getConfiguration()).containsOnlyKeys("some.setting", "otherConfig.setting");
  }

  @Test
  public void getConfiguration2ShouldReadFromGivenPath() throws Exception {
    configFilesProvider = () -> Collections.singletonList(
        new File("application.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    Environment environment = new ImmutableEnvironment("/otherApplicationConfigs/");

    assertThat(source.getConfiguration(environment)).containsOnly(MapEntry.entry("some.setting", "otherAppSetting"));
  }

  @Test
  public void getConfiguration2ShouldReadFromGivenFiles() throws Exception {
    configFilesProvider = () -> Arrays.asList(
        new File("application.properties"),
        new File("otherConfig.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);
    assertThat(source.getConfiguration(new DefaultEnvironment())).containsOnlyKeys("some.setting", "otherConfig.setting");
  }

  @Test
  public void getConfiguration2ShouldThrowOnMissingEnvironment() throws Exception {
    expectedException.expect(MissingEnvironmentException.class);
    source.getConfiguration(new ImmutableEnvironment("awlerijawoetinawwerlkjn"));
  }

  @Test
  public void getConfiguration2ShouldThrowOnMissingConfigFile() throws Exception {
    configFilesProvider = () -> Collections.singletonList(
        new File("nonexistent.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    expectedException.expect(IllegalStateException.class);
    source.getConfiguration(new DefaultEnvironment());
  }

  @Test
  public void refreshShouldUpdateGetConfigurationResults() throws Exception {
    classpathRepo.changeProperty("application.properties", "some.setting", "changedValue");
    source.refresh();

    assertThat(source.getConfiguration()).containsOnly(MapEntry.entry("some.setting", "changedValue"));
  }

  @Test
  public void refreshShouldUpdateGetConfiguration2OnDefaultBranch() throws Exception {
    classpathRepo.changeProperty("application.properties", "some.setting", "changedValue");
    source.refresh();

    assertThat(source.getConfiguration(new DefaultEnvironment())).containsOnly(MapEntry.entry("some.setting", "changedValue"));
  }
}<commit_msg>add support for malformed properties file in classpath source<commit_after>/*
 * Copyright 2015 <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cfg4j.source.classpath;

import static org.assertj.core.api.Assertions.assertThat;

import org.assertj.core.data.MapEntry;
import org.cfg4j.source.context.DefaultEnvironment;
import org.cfg4j.source.context.Environment;
import org.cfg4j.source.context.ImmutableEnvironment;
import org.cfg4j.source.context.MissingEnvironmentException;
import org.cfg4j.source.git.ConfigFilesProvider;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;


@RunWith(MockitoJUnitRunner.class)
public class ClasspathConfigurationSourceTest {

  @Rule
  public ExpectedException expectedException = ExpectedException.none();

  private TempConfigurationClasspathRepo classpathRepo;
  private ConfigFilesProvider configFilesProvider;
  private ClasspathConfigurationSource source;

  @Before
  public void setUp() throws Exception {
    classpathRepo = new TempConfigurationClasspathRepo();

    configFilesProvider = () -> Collections.singletonList(
        new File("application.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);
  }

  @Test
  public void getConfigurationShouldReadConfigFromAbsolutePaths() throws Exception {
    assertThat(source.getConfiguration()).containsOnlyKeys("some.setting");
  }

  @Test
  public void getConfigurationShouldThrowOnMissingConfigFile() throws Exception {
    configFilesProvider = () -> Collections.singletonList(
        new File("nonexistent.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    expectedException.expect(IllegalStateException.class);
    source.getConfiguration();
  }

  @Test
  public void getConfigurationShouldThrowOnMalformedConfigFile() throws Exception {
    configFilesProvider = () -> Collections.singletonList(
        new File("malformed.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    expectedException.expect(IllegalStateException.class);
    source.getConfiguration();
  }

  @Test
  public void getConfigurationShouldReadFromGivenFiles() throws Exception {
    configFilesProvider = () -> Arrays.asList(
        new File("application.properties"),
        new File("otherConfig.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    assertThat(source.getConfiguration()).containsOnlyKeys("some.setting", "otherConfig.setting");
  }

  @Test
  public void getConfiguration2ShouldReadFromGivenPath() throws Exception {
    configFilesProvider = () -> Collections.singletonList(
        new File("application.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    Environment environment = new ImmutableEnvironment("/otherApplicationConfigs/");

    assertThat(source.getConfiguration(environment)).containsOnly(MapEntry.entry("some.setting", "otherAppSetting"));
  }

  @Test
  public void getConfiguration2ShouldReadFromGivenFiles() throws Exception {
    configFilesProvider = () -> Arrays.asList(
        new File("application.properties"),
        new File("otherConfig.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);
    assertThat(source.getConfiguration(new DefaultEnvironment())).containsOnlyKeys("some.setting", "otherConfig.setting");
  }

  @Test
  public void getConfiguration2ShouldThrowOnMissingEnvironment() throws Exception {
    expectedException.expect(MissingEnvironmentException.class);
    source.getConfiguration(new ImmutableEnvironment("awlerijawoetinawwerlkjn"));
  }

  @Test
  public void getConfiguration2ShouldThrowOnMissingConfigFile() throws Exception {
    configFilesProvider = () -> Collections.singletonList(
        new File("nonexistent.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    expectedException.expect(IllegalStateException.class);
    source.getConfiguration(new DefaultEnvironment());
  }

  @Test
  public void getConfiguration2ShouldThrowOnMalformedConfigFile() throws Exception {
    configFilesProvider = () -> Collections.singletonList(
        new File("malformed.properties")
    );

    source = new ClasspathConfigurationSource(configFilesProvider);

    expectedException.expect(IllegalStateException.class);
    source.getConfiguration(new DefaultEnvironment());
  }

  @Test
  public void refreshShouldUpdateGetConfigurationResults() throws Exception {
    classpathRepo.changeProperty("application.properties", "some.setting", "changedValue");
    source.refresh();

    assertThat(source.getConfiguration()).containsOnly(MapEntry.entry("some.setting", "changedValue"));
  }

  @Test
  public void refreshShouldUpdateGetConfiguration2OnDefaultBranch() throws Exception {
    classpathRepo.changeProperty("application.properties", "some.setting", "changedValue");
    source.refresh();

    assertThat(source.getConfiguration(new DefaultEnvironment())).containsOnly(MapEntry.entry("some.setting", "changedValue"));
  }
}
{'fraction_non_alphanumeric': 0.0957987690660958, 'fraction_numerical': 0.0034787262510034785, 'mean_word_length': 5.211634349030471, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '142603', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2786, 'n_words': 610}