<commit_before>/*
 * Copyright 2016-2018 Crown Copyright
 *
 * 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 uk.gov.gchq.gaffer.integration;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.reflections.Reflections;

import uk.gov.gchq.gaffer.integration.AbstractStoreITs.StoreTestSuite;
import uk.gov.gchq.gaffer.store.StoreProperties;
import uk.gov.gchq.gaffer.store.schema.Schema;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Runs the full suite of gaffer store integration tests. To run the tests against
 * a specific store, simply extend this class - you don't need to annotate
 * your class.
 */
@RunWith(StoreTestSuite.class)
public abstract class AbstractStoreITs {
    private final StoreProperties storeProperties;
    private final Schema schema;
    private final Collection<Class<? extends AbstractStoreWithCustomGraphIT>> extraTests;
    private final Map<Class<? extends AbstractStoreWithCustomGraphIT>, String> skipTests = new HashMap<>();
    private final Map<Class<? extends AbstractStoreWithCustomGraphIT>, Map<String, String>> skipTestMethods = new HashMap<>();
    private Class<? extends AbstractStoreWithCustomGraphIT> singleTestClass;
    private String singleTestMethod;

    public AbstractStoreITs(final StoreProperties storeProperties, final Schema schema, final Collection<Class<? extends AbstractStoreWithCustomGraphIT>> extraTests) {
        this.schema = schema;
        this.storeProperties = storeProperties;
        this.extraTests = extraTests;
    }

    public AbstractStoreITs(final StoreProperties storeProperties, final Collection<Class<? extends AbstractStoreWithCustomGraphIT>> extraTests) {
        this(storeProperties, new Schema(), extraTests);
    }

    public AbstractStoreITs(final StoreProperties storeProperties, final Schema schema) {
        this(storeProperties, schema, new ArrayList<Class<? extends AbstractStoreWithCustomGraphIT>>());
    }

    public AbstractStoreITs(final StoreProperties storeProperties) {
        this(storeProperties, new Schema());
    }

    public void singleTest(final Class<? extends AbstractStoreWithCustomGraphIT> testClass) {
        singleTest(testClass, null);
    }

    public void singleTest(final Class<? extends AbstractStoreWithCustomGraphIT> testClass, final String testMethod) {
        this.singleTestClass = testClass;
        this.singleTestMethod = testMethod;
    }

    public Schema getStoreSchema() {
        return schema;
    }

    public StoreProperties getStoreProperties() {
        return storeProperties;
    }

    public void addExtraTest(final Class<? extends AbstractStoreWithCustomGraphIT> extraTest) {
        extraTests.add(extraTest);
    }

    public Collection<? extends Class> getExtraTests() {
        return extraTests;
    }

    public Map<? extends Class<? extends AbstractStoreWithCustomGraphIT>, String> getSkipTests() {
        return skipTests;
    }

    public Map<? extends Class<? extends AbstractStoreWithCustomGraphIT>, Map<String, String>> getSkipTestMethods() {
        return skipTestMethods;
    }

    protected void skipTest(final Class<? extends AbstractStoreWithCustomGraphIT> testClass, final String justification) {
        skipTests.put(testClass, justification);
    }

    protected void skipTestMethod(final Class<? extends AbstractStoreWithCustomGraphIT> testClass, final String method, final String justification) {
        Map<String, String> methods = skipTestMethods.get(testClass);
        if (null == methods) {
            methods = new HashMap<>();
            skipTestMethods.put(testClass, methods);
        }
        methods.put(method, justification);
    }

    public static class StoreTestSuite extends Suite {

        public StoreTestSuite(final Class<?> clazz, final RunnerBuilder builder) throws InitializationError, IllegalAccessException, InstantiationException {
            super(builder, clazz, getTestClasses(clazz));

            final AbstractStoreITs runner = clazz.asSubclass(AbstractStoreITs.class).newInstance();
            Schema storeSchema = runner.getStoreSchema();
            if (null == storeSchema) {
                storeSchema = new Schema();
            }

            AbstractStoreWithCustomGraphIT.setStoreSchema(storeSchema);
            AbstractStoreWithCustomGraphIT.setStoreProperties(runner.getStoreProperties());
            AbstractStoreWithCustomGraphIT.setSkipTests(runner.getSkipTests());
            AbstractStoreWithCustomGraphIT.setSkipTestMethods(runner.getSkipTestMethods());
            AbstractStoreWithCustomGraphIT.setSingleTestMethod(runner.singleTestMethod);
        }

        private static Class[] getTestClasses(final Class<?> clazz) throws IllegalAccessException, InstantiationException {
            final AbstractStoreITs runner = clazz.asSubclass(AbstractStoreITs.class).newInstance();
            if (null == runner.singleTestClass) {
                final Set<Class<? extends AbstractStoreWithCustomGraphIT>> classes = new Reflections(AbstractStoreWithCustomGraphIT.class.getPackage().getName()).getSubTypesOf(AbstractStoreWithCustomGraphIT.class);
                keepPublicConcreteClasses(classes);
                classes.addAll((Collection<? extends Class<? extends AbstractStoreWithCustomGraphIT>>) runner.getExtraTests());
                return classes.toArray(new Class[classes.size()]);
            }

            return new Class[]{runner.singleTestClass};
        }

        private static void keepPublicConcreteClasses(final Set<Class<? extends AbstractStoreWithCustomGraphIT>> classes) {<commit_msg>gh-1980 - test<commit_after>/*
 * Copyright 2016-2018 Crown Copyright
 *
 * 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 uk.gov.gchq.gaffer.integration;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.reflections.Reflections;

import uk.gov.gchq.gaffer.integration.AbstractStoreITs.StoreTestSuite;
import uk.gov.gchq.gaffer.store.StoreProperties;
import uk.gov.gchq.gaffer.store.schema.Schema;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Runs the full suite of gaffer store integration tests. To run the tests against
 * a specific store, simply extend this class - you don't need to annotate
 * your class.
 */
@RunWith(StoreTestSuite.class)
public abstract class AbstractStoreITs {
    private final StoreProperties storeProperties;
    private final Schema schema;
    private final Set<Class<? extends AbstractStoreWithCustomGraphIT>> tests = new Reflections(AbstractStoreWithCustomGraphIT.class.getPackage().getName()).getSubTypesOf(AbstractStoreWithCustomGraphIT.class);
    private final Map<Class<? extends AbstractStoreWithCustomGraphIT>, String> skipTests = new HashMap<>();
    private final Map<Class<? extends AbstractStoreWithCustomGraphIT>, Map<String, String>> skipTestMethods = new HashMap<>();
    private Class<? extends AbstractStoreWithCustomGraphIT> singleTestClass;
    private String singleTestMethod;

    public AbstractStoreITs(final StoreProperties storeProperties, final Schema schema, final Collection<Class<? extends AbstractStoreWithCustomGraphIT>> extraTests) {
        this.schema = schema;
        this.storeProperties = storeProperties;
        this.tests.addAll(extraTests);
    }

    public AbstractStoreITs(final StoreProperties storeProperties, final Collection<Class<? extends AbstractStoreWithCustomGraphIT>> extraTests) {
        this(storeProperties, new Schema(), extraTests);
    }

    public AbstractStoreITs(final StoreProperties storeProperties, final Schema schema) {
        this(storeProperties, schema, new ArrayList<>());
    }

    public AbstractStoreITs(final StoreProperties storeProperties) {
        this(storeProperties, new Schema());
    }

    public void singleTest(final Class<? extends AbstractStoreWithCustomGraphIT> testClass) {
        singleTest(testClass, null);
    }

    public void singleTest(final Class<? extends AbstractStoreWithCustomGraphIT> testClass, final String testMethod) {
        this.singleTestClass = testClass;
        this.singleTestMethod = testMethod;
    }

    public Schema getStoreSchema() {
        return schema;
    }

    public StoreProperties getStoreProperties() {
        return storeProperties;
    }

    public void addExtraTest(final Class<? extends AbstractStoreWithCustomGraphIT> extraTest) {
        tests.add(extraTest);
    }

    public Set<Class<? extends AbstractStoreWithCustomGraphIT>> getTests() {
        return tests;
    }

    public Map<? extends Class<? extends AbstractStoreWithCustomGraphIT>, String> getSkipTests() {
        return skipTests;
    }

    public Map<? extends Class<? extends AbstractStoreWithCustomGraphIT>, Map<String, String>> getSkipTestMethods() {
        return skipTestMethods;
    }

    protected void skipTest(final Class<? extends AbstractStoreWithCustomGraphIT> testClass, final String justification) {
        skipTests.put(testClass, justification);
    }

    protected void skipTestMethod(final Class<? extends AbstractStoreWithCustomGraphIT> testClass, final String method, final String justification) {
        Map<String, String> methods = skipTestMethods.get(testClass);
        if (null == methods) {
            methods = new HashMap<>();
            skipTestMethods.put(testClass, methods);
        }
        methods.put(method, justification);
    }

    public static class StoreTestSuite extends Suite {

        public StoreTestSuite(final Class<?> clazz, final RunnerBuilder builder) throws InitializationError, IllegalAccessException, InstantiationException {
            super(builder, clazz, getTestClasses(clazz));

            final AbstractStoreITs runner = clazz.asSubclass(AbstractStoreITs.class).newInstance();
            Schema storeSchema = runner.getStoreSchema();
            if (null == storeSchema) {
                storeSchema = new Schema();
            }

            AbstractStoreWithCustomGraphIT.setStoreSchema(storeSchema);
            AbstractStoreWithCustomGraphIT.setStoreProperties(runner.getStoreProperties());
            AbstractStoreWithCustomGraphIT.setSkipTests(runner.getSkipTests());
            AbstractStoreWithCustomGraphIT.setSkipTestMethods(runner.getSkipTestMethods());
            AbstractStoreWithCustomGraphIT.setSingleTestMethod(runner.singleTestMethod);
        }

        private static Class[] getTestClasses(final Class<?> clazz) throws IllegalAccessException, InstantiationException {
            final AbstractStoreITs runner = clazz.asSubclass(AbstractStoreITs.class).newInstance();
            if (null == runner.singleTestClass) {
                final Set<Class<? extends AbstractStoreWithCustomGraphIT>> classes = runner.getTests();
                 keepPublicConcreteClasses(classes);
                 return classes.toArray(new Class[classes.size()]);
            }
            return new Class[]{runner.singleTestClass};
        }

        private static void keepPublicConcreteClasses(final Set<Class<? extends AbstractStoreWithCustomGraphIT>> classes) {
{'fraction_non_alphanumeric': 0.08342638379326846, 'fraction_numerical': 0.002233211038443133, 'mean_word_length': 3.9288522012578615, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 75, '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': '4744131', 'n_tokens_mistral': 3133, 'n_tokens_neox': 3045, 'n_words': 853}