<commit_before>package com.github.bednar.base.utils.resource;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author <NAME> (13/01/2014 18:55)
 */
@SuppressWarnings("ConstantConditions")
public class FluentChangeTest
{
    private static final Logger LOG = LoggerFactory.getLogger(FluentChangeTest.class);

    private static final int REGISTER_WAIT = 5000;
    private static final int CHANGE_WAIT = 20000;

    private FluentResource resourceForChange;
    private FluentResource resourceNotInterest;

    @Before
    public void before()
    {
        resourceForChange = FluentResource.byPath("/com/github/bednar/base/fileToChange.txt");
        resourceNotInterest = FluentResource.byPath("/com/github/bednar/base/notInterestFile.txt");
    }

    @After
    public void after()
    {
        resourceForChange.close();
        resourceNotInterest.close();
    }

    @Test
    public void createInstance()
    {
        FileChangeAnnounce announce = Mockito.mock(FileChangeAnnounce.class);

        FluentChange.byResources(announce, resourceForChange);
    }

    @Test
    public void watchAssync() throws Exception
    {
        FileChangeAnnounce announce = Mockito.mock(FileChangeAnnounce.class);

        FluentChange
                .byResources(announce, resourceForChange)
                .watchAssync();

        LOG.info("wait for register watch service");
        Thread.sleep(REGISTER_WAIT);

        LOG.info("write changes 1");
        resourceForChange.update(String.valueOf(System.currentTimeMillis()));

        LOG.info("wait for detect changes 1");
        Thread.sleep(CHANGE_WAIT);

        Mockito.verify(announce, Mockito.times(1)).modified(Mockito.<FileChangeContext>any());

        LOG.info("write changes 2");
        resourceForChange.update(String.valueOf(System.currentTimeMillis()));

        LOG.info("wait for detect changes 2");
        Thread.sleep(CHANGE_WAIT);

        Mockito.verify(announce, Mockito.times(2)).modified(Mockito.<FileChangeContext>any());
    }

    @Test
    public void modifiedChangeContext() throws Exception
    {
        FileChangeAnnounce announce = Mockito.mock(FileChangeAnnounce.class);

        FileChangeContext changeContext = FileChangeContext
                .byResource(resourceForChange);

        FluentChange
                .byResources(announce)
                .addFileChangeContext(changeContext)
                .watchAssync();

        LOG.info("wait for register watch service");
        Thread.sleep(REGISTER_WAIT);

        LOG.info("write changes");
        resourceForChange.update(String.valueOf(System.currentTimeMillis()));

        LOG.info("wait for detect changes 1");
        Thread.sleep(CHANGE_WAIT);

        Mockito.verify(announce, Mockito.atLeastOnce()).modified(changeContext);
    }

    @Test
    public void watchAssyncOnlyInterestFile() throws Exception
    {
        FileChangeAnnounce announce = Mockito.mock(FileChangeAnnounce.class);

        FluentChange
                .byResources(announce, resourceForChange)
                .watchAssync();

        LOG.info("wait for register watch service");
        Thread.sleep(REGISTER_WAIT);

        LOG.info("write changes");
        resourceForChange.update(String.valueOf(System.currentTimeMillis()));
        resourceNotInterest.update(String.valueOf(System.currentTimeMillis()));

        LOG.info("wait for detect changes 1");
        Thread.sleep(CHANGE_WAIT);

        Mockito.verify(announce, Mockito.times(1)).modified(Mockito.<FileChangeContext>any());
    }

    @Test
    public void addNewResource() throws Exception<commit_msg>* Fix not atomic update<commit_after>package com.github.bednar.base.utils.resource;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author <NAME> (13/01/2014 18:55)
 */
@SuppressWarnings("ConstantConditions")
public class FluentChangeTest
{
    private static final Logger LOG = LoggerFactory.getLogger(FluentChangeTest.class);

    private static final int REGISTER_WAIT = 5000;
    private static final int CHANGE_WAIT = 20000;

    private FluentResource resourceForChange;
    private FluentResource resourceNotInterest;

    @Before
    public void before()
    {
        resourceForChange = FluentResource.byPath("/com/github/bednar/base/fileToChange.txt");
        resourceNotInterest = FluentResource.byPath("/com/github/bednar/base/notInterestFile.txt");
    }

    @After
    public void after()
    {
        resourceForChange.close();
        resourceNotInterest.close();
    }

    @Test
    public void createInstance()
    {
        FileChangeAnnounce announce = Mockito.mock(FileChangeAnnounce.class);

        FluentChange.byResources(announce, resourceForChange);
    }

    @Test
    public void watchAssync() throws Exception
    {
        FileChangeAnnounce announce = Mockito.mock(FileChangeAnnounce.class);

        FileChangeContext context = FileChangeContext
                .byResource(resourceForChange);

        FluentChange
                .byResources(announce)
                .addFileChangeContext(context)
                .watchAssync();

        LOG.info("wait for register watch service");
        Thread.sleep(REGISTER_WAIT);

        LOG.info("write changes 1");
        resourceForChange.update(String.valueOf(System.currentTimeMillis()));

        LOG.info("wait for detect changes 1");
        Thread.sleep(CHANGE_WAIT);

        Mockito.verify(announce, Mockito.atLeast(1)).modified(context);

        LOG.info("write changes 2");
        resourceForChange.update(String.valueOf(System.currentTimeMillis()));

        LOG.info("wait for detect changes 2");
        Thread.sleep(CHANGE_WAIT);

        Mockito.verify(announce, Mockito.atLeast(2)).modified(context);
    }

    @Test
    public void modifiedChangeContext() throws Exception
    {
        FileChangeAnnounce announce = Mockito.mock(FileChangeAnnounce.class);

        FileChangeContext changeContext = FileChangeContext
                .byResource(resourceForChange);

        FluentChange
                .byResources(announce)
                .addFileChangeContext(changeContext)
                .watchAssync();

        LOG.info("wait for register watch service");
        Thread.sleep(REGISTER_WAIT);

        LOG.info("write changes");
        resourceForChange.update(String.valueOf(System.currentTimeMillis()));

        LOG.info("wait for detect changes 1");
        Thread.sleep(CHANGE_WAIT);

        Mockito.verify(announce, Mockito.atLeastOnce()).modified(changeContext);
    }

    @Test
    public void watchAssyncOnlyInterestFile() throws Exception
    {
        FileChangeAnnounce announce = Mockito.mock(FileChangeAnnounce.class);

        FileChangeContext context = FileChangeContext
                .byResource(resourceForChange);

        FluentChange
                .byResources(announce)
                .addFileChangeContext(context)
                .watchAssync();

        LOG.info("wait for register watch service");
        Thread.sleep(REGISTER_WAIT);

        LOG.info("write changes");
        resourceForChange.update(String.valueOf(System.currentTimeMillis()));
        resourceNotInterest.update(String.valueOf(System.currentTimeMillis()));

        LOG.info("wait for detect changes 1");
        Thread.sleep(CHANGE_WAIT);

        Mockito.verify(announce, Mockito.times(1)).modified(context);
    }

    @Test
    public void addNewResource() throws Exception
{'fraction_non_alphanumeric': 0.10179248986000261, 'fraction_numerical': 0.008373675258406385, 'mean_word_length': 2.9565217391304346, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '5453671', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2015, 'n_words': 377}