<commit_before>/*
 * Copyright (c) 2013, <NAME>
 * All rights reserved.
 *
 * This software is distributable under the BSD license.
 * See the terms of the BSD license in the documentation provided with this software.
 */

package org.aeonbits.owner;

import org.aeonbits.owner.Config.Sources;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import static org.aeonbits.owner.UtilTest.save;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * @author <NAME>
 */
public class ConfigFactoryTest implements TestConstants {

    @Sources("file:${mypath}/myconfig.properties")
    interface MyConfig extends Config {
        @DefaultValue("defaultValue")
        String someValue();
    }

    @Before
    public void before() throws IOException {
        ConfigFactory.setProperties(new Properties());
        save(new File(RESOURCES_DIR + "/myconfig.properties"), new Properties() {{
            setProperty("someValue", "foobar");
        }});
    }

    @Test
    public void testSetProperty()  {
        ConfigFactory.setProperty("mypath", RESOURCES_DIR);

        MyConfig cfg = ConfigFactory.create(MyConfig.class);

        assertEquals("foobar", cfg.someValue());
    }

    @Test(expected = NullPointerException.class)
    public void testSetPropertyNullKey() {
        ConfigFactory.setProperty(null, "foobar");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testSetPropertyEmptyKey() {
        ConfigFactory.setProperty("", "foobar");
    }

    @Test
    public void testSetPropertyTwice()  {
        assertNull(ConfigFactory.setProperty("mypath", RESOURCES_DIR));
        assertEquals(RESOURCES_DIR, ConfigFactory.setProperty("mypath", RESOURCES_DIR + "-2"));
        assertEquals(RESOURCES_DIR + "-2", ConfigFactory.getProperty("mypath"));
    }

    @Test
    public void testGetProperties() {
        ConfigFactory.getProperties().setProperty("mypath", RESOURCES_DIR);

        MyConfig cfg = ConfigFactory.create(MyConfig.class);

        assertEquals("foobar", cfg.someValue());
    }

    @Test
    public void testSetProperties() {
        ConfigFactory.setProperties(new Properties() {{
            setProperty("mypath", RESOURCES_DIR);
        }});

        MyConfig cfg = ConfigFactory.create(MyConfig.class);

        assertEquals("foobar", cfg.someValue());
    }

    @Test
    public void testSetPropertiesNullObject() {
        ConfigFactory.setProperties(null);

        MyConfig cfg = ConfigFactory.create(MyConfig.class);

        assertEquals("defaultValue", cfg.someValue());
    }

    @Test
    public void testGetProperty() {
        ConfigFactory.setProperty("mypath", RESOURCES_DIR);
        assertEquals(RESOURCES_DIR, ConfigFactory.getProperty("mypath"));
    }

    @Test(expected = NullPointerException.class)
    public void testGetPropertyNullKey() {
        ConfigFactory.getProperty(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testGetPropertiesEmptyKey() {
        ConfigFactory.getProperty("");
    }

    @Test
    public void testGetClearProperty() {
        ConfigFactory.setProperty("mypath", RESOURCES_DIR);
        assertEquals(RESOURCES_DIR, ConfigFactory.clearProperty("mypath"));
        assertNull(ConfigFactory.getProperty("mypath"));
    }

    @Test(expected = NullPointerException.class)
    public void testClearPropertyNullKey() {
        ConfigFactory.clearProperty(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testClearPropertyEmptyKey() {
        ConfigFactory.clearProperty("");
    }

}<commit_msg>test expansion of @Sources urls when no protocol is specified<commit_after>/*
 * Copyright (c) 2013, <NAME>
 * All rights reserved.
 *
 * This software is distributable under the BSD license.
 * See the terms of the BSD license in the documentation provided with this software.
 */

package org.aeonbits.owner;

import org.aeonbits.owner.Config.Sources;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import static org.aeonbits.owner.UtilTest.save;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * @author <NAME>
 */
public class ConfigFactoryTest implements TestConstants {

    @Sources("file:${mypath}/myconfig.properties")
    interface MyConfig extends Config {
        @DefaultValue("defaultValue")
        String someValue();
    }

    @Before
    public void before() throws IOException {
        ConfigFactory.setProperties(new Properties());
        save(new File(RESOURCES_DIR + "/myconfig.properties"), new Properties() {{
            setProperty("someValue", "foobar");
        }});
    }

    @Test
    public void testSetProperty()  {
        ConfigFactory.setProperty("mypath", RESOURCES_DIR);

        MyConfig cfg = ConfigFactory.create(MyConfig.class);

        assertEquals("foobar", cfg.someValue());
    }

    @Test(expected = NullPointerException.class)
    public void testSetPropertyNullKey() {
        ConfigFactory.setProperty(null, "foobar");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testSetPropertyEmptyKey() {
        ConfigFactory.setProperty("", "foobar");
    }

    @Test
    public void testSetPropertyTwice()  {
        assertNull(ConfigFactory.setProperty("mypath", RESOURCES_DIR));
        assertEquals(RESOURCES_DIR, ConfigFactory.setProperty("mypath", RESOURCES_DIR + "-2"));
        assertEquals(RESOURCES_DIR + "-2", ConfigFactory.getProperty("mypath"));
    }

    @Test
    public void testGetProperties() {
        ConfigFactory.getProperties().setProperty("mypath", RESOURCES_DIR);

        MyConfig cfg = ConfigFactory.create(MyConfig.class);

        assertEquals("foobar", cfg.someValue());
    }

    @Test
    public void testSetProperties() {
        ConfigFactory.setProperties(new Properties() {{
            setProperty("mypath", RESOURCES_DIR);
        }});

        MyConfig cfg = ConfigFactory.create(MyConfig.class);

        assertEquals("foobar", cfg.someValue());
    }

    @Test
    public void testSetPropertiesNullObject() {
        ConfigFactory.setProperties(null);

        MyConfig cfg = ConfigFactory.create(MyConfig.class);

        assertEquals("defaultValue", cfg.someValue());
    }

    @Test
    public void testGetProperty() {
        ConfigFactory.setProperty("mypath", RESOURCES_DIR);
        assertEquals(RESOURCES_DIR, ConfigFactory.getProperty("mypath"));
    }

    @Test(expected = NullPointerException.class)
    public void testGetPropertyNullKey() {
        ConfigFactory.getProperty(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testGetPropertiesEmptyKey() {
        ConfigFactory.getProperty("");
    }

    @Test
    public void testGetClearProperty() {
        ConfigFactory.setProperty("mypath", RESOURCES_DIR);
        assertEquals(RESOURCES_DIR, ConfigFactory.clearProperty("mypath"));
        assertNull(ConfigFactory.getProperty("mypath"));
    }

    @Test(expected = NullPointerException.class)
    public void testClearPropertyNullKey() {
        ConfigFactory.clearProperty(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testClearPropertyEmptyKey() {
        ConfigFactory.clearProperty("");
    }

    @Sources("${myurl}")
    interface MyConfigWithoutProtocol extends Config {
        @DefaultValue("defaultValue")
        String someValue();
    }

    @Test
    public void testSetPropertyWithoutProtocol()  {
        ConfigFactory.setProperty("mypath", RESOURCES_DIR);
        ConfigFactory.setProperty("myurl", "file:${mypath}/myconfig.properties");

        MyConfigWithoutProtocol cfg = ConfigFactory.create(MyConfigWithoutProtocol.class);

        assertEquals("foobar", cfg.someValue());
    }

}
{'fraction_non_alphanumeric': 0.11403729838709678, 'fraction_numerical': 0.0015120967741935483, 'mean_word_length': 3.4640044994375705, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '412988', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2088, 'n_words': 422}