<commit_before>package com.ripariandata.timberwolf.conf4j;

import org.apache.commons.configuration.Configuration;

import org.junit.Test;

import java.util.Vector;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ConfigFileParserTest
{
    private Configuration mockConfiguration(Object... settings)
    {
        if ((settings.length % 2) != 0)
        {
            return null;
        }

        Configuration config = mock(Configuration.class);

        Vector<String> keys = new Vector<String>();
        for (int i = 0; i < settings.length; i += 2)
        {
            String key = settings[i].toString();
            keys.add(key);
            when(config.getProperty(key)).thenReturn(settings[i + 1]);
        }
        when(config.getKeys()).thenReturn(keys.iterator());

        return config;
    }

    private class TypeWithNoEntries
    {
        public int x = 0;
    }

    @Test
    public void testTypeWithNoEntries()
    {
        TypeWithNoEntries target = new TypeWithNoEntries();
        ConfigFileParser parser = new ConfigFileParser(target);
        Configuration mockConfig = mockConfiguration();
        parser.parseConfiguration(mockConfig);

        assertEquals(0, target.x);

        mockConfig = mockConfiguration("wrong.entry", 100);
        parser.parseConfiguration(mockConfig);
        assertEquals(0, target.x);
    }

    private class TypeWithOneEntry
    {
        @ConfigEntry(name = "my.entry")
        public String entry = "";
    }

    @Test
    public void testTypeWithOneEntry()
    {
        TypeWithOneEntry target = new TypeWithOneEntry();
        ConfigFileParser parser = new ConfigFileParser(target);

        Configuration mockConfig = mockConfiguration();
        parser.parseConfiguration(mockConfig);
        assertEquals("", target.entry);

        mockConfig = mockConfiguration("wrong.entry", "value");
        parser.parseConfiguration(mockConfig);
        assertEquals("", target.entry);

        mockConfig = mockConfiguration("my.entry", "value");
        parser.parseConfiguration(mockConfig);
        assertEquals("value", target.entry);

        mockConfig = mockConfiguration("wrong.entry", "foo",
                                       "my.entry", "bar",
                                       "another.entry", "baz");
        parser.parseConfiguration(mockConfig);
        assertEquals("bar", target.entry);
    }

    private class TypeWithManyEntries
    {
        public String notAnEntry = "";

        @ConfigEntry(name = "entry.string")
        public String stringEntry = "";

        @ConfigEntry(name = "entry.int")
        public int intEntry = 0;

        @ConfigEntry(name = "entry.string.two")
        public String stringTwo = "asdf";
    }

    @Test
    public void testTypeWithManyEntries()
    {
        TypeWithManyEntries target = new TypeWithManyEntries();
        ConfigFileParser parser = new ConfigFileParser(target);

        Configuration mockConfig = mockConfiguration();
        parser.parseConfiguration(mockConfig);

        assertEquals("", target.notAnEntry);
        assertEquals("", target.stringEntry);
        assertEquals(0, target.intEntry);
        assertEquals("asdf", target.stringTwo);

        mockConfig = mockConfiguration("not.an.entry", "qwer",
                                       "entry.string", "string!",
                                       "entry.int", 10,
                                       "entry.string.two", "qwer");
        parser.parseConfiguration(mockConfig);

        assertEquals("", target.notAnEntry);
        assertEquals("string!", target.stringEntry);
        assertEquals(10, target.intEntry);
        assertEquals("qwer", target.stringTwo);

        mockConfig = mockConfiguration("not.an.entry", "qwer",
                                       "entry.string", "string?",
                                       "entry.int", 100);
        parser.parseConfiguration(mockConfig);

        assertEquals("", target.notAnEntry);
        assertEquals("string?", target.stringEntry);
        assertEquals(100, target.intEntry);
        assertEquals("qwer", target.stringTwo);
    }
}
<commit_msg>Adding test about missing config files.<commit_after>package com.ripariandata.timberwolf.conf4j;

import org.apache.commons.configuration.Configuration;

import org.junit.Test;

import java.util.Vector;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ConfigFileParserTest
{
    private Configuration mockConfiguration(Object... settings)
    {
        if ((settings.length % 2) != 0)
        {
            return null;
        }

        Configuration config = mock(Configuration.class);

        Vector<String> keys = new Vector<String>();
        for (int i = 0; i < settings.length; i += 2)
        {
            String key = settings[i].toString();
            keys.add(key);
            when(config.getProperty(key)).thenReturn(settings[i + 1]);
        }
        when(config.getKeys()).thenReturn(keys.iterator());

        return config;
    }

    private class TypeWithNoEntries
    {
        public int x = 0;
    }

    @Test
    public void testTypeWithNoEntries()
    {
        TypeWithNoEntries target = new TypeWithNoEntries();
        ConfigFileParser parser = new ConfigFileParser(target);
        Configuration mockConfig = mockConfiguration();
        parser.parseConfiguration(mockConfig);

        assertEquals(0, target.x);

        mockConfig = mockConfiguration("wrong.entry", 100);
        parser.parseConfiguration(mockConfig);
        assertEquals(0, target.x);
    }

    private class TypeWithOneEntry
    {
        @ConfigEntry(name = "my.entry")
        public String entry = "";
    }

    @Test
    public void testTypeWithOneEntry()
    {
        TypeWithOneEntry target = new TypeWithOneEntry();
        ConfigFileParser parser = new ConfigFileParser(target);

        Configuration mockConfig = mockConfiguration();
        parser.parseConfiguration(mockConfig);
        assertEquals("", target.entry);

        mockConfig = mockConfiguration("wrong.entry", "value");
        parser.parseConfiguration(mockConfig);
        assertEquals("", target.entry);

        mockConfig = mockConfiguration("my.entry", "value");
        parser.parseConfiguration(mockConfig);
        assertEquals("value", target.entry);

        mockConfig = mockConfiguration("wrong.entry", "foo",
                                       "my.entry", "bar",
                                       "another.entry", "baz");
        parser.parseConfiguration(mockConfig);
        assertEquals("bar", target.entry);
    }

    private class TypeWithManyEntries
    {
        public String notAnEntry = "";

        @ConfigEntry(name = "entry.string")
        public String stringEntry = "";

        @ConfigEntry(name = "entry.int")
        public int intEntry = 0;

        @ConfigEntry(name = "entry.string.two")
        public String stringTwo = "asdf";
    }

    @Test
    public void testTypeWithManyEntries()
    {
        TypeWithManyEntries target = new TypeWithManyEntries();
        ConfigFileParser parser = new ConfigFileParser(target);

        Configuration mockConfig = mockConfiguration();
        parser.parseConfiguration(mockConfig);

        assertEquals("", target.notAnEntry);
        assertEquals("", target.stringEntry);
        assertEquals(0, target.intEntry);
        assertEquals("asdf", target.stringTwo);

        mockConfig = mockConfiguration("not.an.entry", "qwer",
                                       "entry.string", "string!",
                                       "entry.int", 10,
                                       "entry.string.two", "qwer");
        parser.parseConfiguration(mockConfig);

        assertEquals("", target.notAnEntry);
        assertEquals("string!", target.stringEntry);
        assertEquals(10, target.intEntry);
        assertEquals("qwer", target.stringTwo);

        mockConfig = mockConfiguration("not.an.entry", "qwer",
                                       "entry.string", "string?",
                                       "entry.int", 100);
        parser.parseConfiguration(mockConfig);

        assertEquals("", target.notAnEntry);
        assertEquals("string?", target.stringEntry);
        assertEquals(100, target.intEntry);
        assertEquals("qwer", target.stringTwo);
    }

    @Test
    public void testMissingFile()
    {
        TypeWithNoEntries target = new TypeWithNoEntries();
        ConfigFileParser parser = new ConfigFileParser(target);

        try
        {
            parser.parseConfigFile("not/a/real/file.properties");
            fail("Attempting to parse a file that doesn't exist should throw an exception.");
        }
        catch (ConfigFileMissingException e)
        {
            // Once we get there the test has passed, but checkstyle complains
            // about a block with no statements.
            assertEquals(true, true);
        }
        catch (Exception e)
        {
            fail("Wrong exception was thrown when attempting to parse missing file: " + e.getMessage());
        }
    }
}

{'fraction_non_alphanumeric': 0.10997854077253219, 'fraction_numerical': 0.005150214592274678, 'mean_word_length': 2.242086956521739, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3753122', 'n_tokens_mistral': 2391, 'n_tokens_neox': 2273, 'n_words': 501}