<commit_before>package pl.ais.tools.apivisitor.test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import pl.ais.tools.apivisitor.APIWalker;
import pl.ais.tools.apivisitor.BaseVisitor;
import pl.ais.tools.apivisitor.visitors.ServiceVisitor;
import pl.ais.tools.apivisitor.visitors.TypeVisitor;

@RunWith(Parameterized.class)
public class VisitorsNamingStrategyTest {

    @Parameters(name = "{0}")
    public static Collection<Object[]> parameters() {
        return Arrays.asList(new Object[] { TypeVisitor.class }, new Object[] { ServiceVisitor.class });
    }

    private final Class<?> serviceClass;

    public VisitorsNamingStrategyTest(Class<?> serviceClass) {
        this.serviceClass = serviceClass;
    }

    @Test
    public void testNaming() {
        APIWalker walker = new APIWalker();
        VisitorNamingEnforcingVisitor visitor = new VisitorNamingEnforcingVisitor();
        walker.setVisitor(visitor);
        walker.visit(serviceClass);
    }

    class VisitorNamingEnforcingVisitor extends BaseVisitor {

        private Set<Method> beginMethods;
        private Set<Method> finishMethods;

        @Override
        public void beginServiceProcessing(Class<?> clazz) {
            beginMethods = new HashSet<>();
            finishMethods = new HashSet<>();
        }

        @Override
        public void beginMethodProcessing(Method method) {
            Assert.assertEquals("method " + method  + " must return void", void.class, method.getReturnType());
            if (method.getName().startsWith("begin")) {
                beginMethods.add(method);
                return;
            }
            if (method.getName().startsWith("finish")) {
                finishMethods.add(method);
                return;
            }
            throw new IllegalArgumentException("method " + method + " does not have allowed name");
        }

        @Override
        public void finishServiceProcessing(Class<?> clazz) {
            Assert.assertEquals(beginMethods.size(), finishMethods.size());
            matchMethods("begin", "finish", beginMethods, finishMethods);
            matchMethods("finish", "begin", finishMethods, beginMethods);
        }

        private void matchMethods(final String leftPrefix, final String rightPrefix, final Set<Method> left, final Set<Method> right) {
            Assert.assertTrue(left.stream().allMatch(lmethod -> {
                final String rightMethodName = rightPrefix + lmethod.getName().substring(leftPrefix.length());
                final Method rmethod = right.stream().filter(rm -> rightMethodName.contentEquals(rm.getName())).findFirst().get();
                Assert.assertArrayEquals("comparing arguments of " + lmethod.getName() + " and " + rmethod.getName(),
                        lmethod.getParameterTypes(), rmethod.getParameterTypes());
                Assert.assertEquals(lmethod.getParameterCount(), rmethod.getParameterCount());
                return true;
            }));
        }
    }
}
<commit_msg>documenting test as it's also an example<commit_after>package pl.ais.tools.apivisitor.test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import pl.ais.tools.apivisitor.APIWalker;
import pl.ais.tools.apivisitor.BaseVisitor;
import pl.ais.tools.apivisitor.visitors.ServiceVisitor;
import pl.ais.tools.apivisitor.visitors.TypeVisitor;

/**
 * Eating our own dog's food - making sure, that {@link TypeVisitor} and {@link ServiceVisitor} have
 * consisting method names.
 *
 * Each <em>begin</em> method must have its <em>finish</em> method and vice-versa, each <em>finish</em>
 * method must have its <em>begin</em> method.
 */
@RunWith(Parameterized.class)
public class VisitorsNamingStrategyTest {

    @Parameters(name = "{0}")
    public static Collection<Object[]> parameters() {
        return Arrays.asList(new Object[] { TypeVisitor.class }, new Object[] { ServiceVisitor.class });
    }

    private final Class<?> serviceClass;

    public VisitorsNamingStrategyTest(Class<?> serviceClass) {
        this.serviceClass = serviceClass;
    }

    @Test
    public void testNaming() {
        APIWalker walker = new APIWalker();
        VisitorNamingEnforcingVisitor visitor = new VisitorNamingEnforcingVisitor();
        walker.setVisitor(visitor);
        walker.visit(serviceClass);
    }

    class VisitorNamingEnforcingVisitor extends BaseVisitor {

        private Set<Method> beginMethods;
        private Set<Method> finishMethods;

        @Override
        public void beginServiceProcessing(Class<?> clazz) {
            beginMethods = new HashSet<>();
            finishMethods = new HashSet<>();
        }

        @Override
        public void beginMethodProcessing(Method method) {
            Assert.assertEquals("method " + method  + " must return void", void.class, method.getReturnType());
            if (method.getName().startsWith("begin")) {
                beginMethods.add(method);
                return;
            }
            if (method.getName().startsWith("finish")) {
                finishMethods.add(method);
                return;
            }
            throw new IllegalArgumentException("method " + method + " does not have allowed name");
        }

        @Override
        public void finishServiceProcessing(Class<?> clazz) {
            Assert.assertEquals(beginMethods.size(), finishMethods.size());
            matchMethods("begin", "finish", beginMethods, finishMethods);
            matchMethods("finish", "begin", finishMethods, beginMethods);
        }

        private void matchMethods(final String leftPrefix, final String rightPrefix, final Set<Method> left, final Set<Method> right) {
            Assert.assertTrue(left.stream().allMatch(lmethod -> {
                final String rightMethodName = rightPrefix + lmethod.getName().substring(leftPrefix.length());
                final Method rmethod = right.stream().filter(rm -> rightMethodName.contentEquals(rm.getName())).findFirst().get();
                Assert.assertArrayEquals("comparing arguments of " + lmethod.getName() + " and " + rmethod.getName(),
                        lmethod.getParameterTypes(), rmethod.getParameterTypes());
                Assert.assertEquals(lmethod.getParameterCount(), rmethod.getParameterCount());
                return true;
            }));
        }
    }
}

{'fraction_non_alphanumeric': 0.10992453367506763, 'fraction_numerical': 0.0002847785846504343, 'mean_word_length': 2.6224858174316656, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, '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': '7152840', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1784, 'n_words': 385}