cz.muni.fi.editor.database.test.helpers.AbstractDAOTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.database.test.helpers.AbstractDAOTest.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*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.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * 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 cz.muni.fi.editor.database.test.helpers;

import cz.muni.fi.editor.database.dao.*;
import io.github.benas.randombeans.api.EnhancedRandom;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 26.8.2016.
 */
public abstract class AbstractDAOTest {
    @Autowired
    protected EnhancedRandom enhancedRandom;
    @Autowired
    protected UserDAO userDAO;
    @Autowired
    protected OrganizationDAO organizationDAO;
    @Autowired
    protected RequestDAO requestDAO;
    @Autowired
    protected NotificationDAO notificationDAO;
    @Autowired
    protected CmisObjectDAO cmisObjectDAO;
    @Autowired
    protected Environment environment;

    protected static void checkMethods(Class testClass, Class testingInterface) {
        Set<String> testMethods = new HashSet<>();
        for (Method m : testClass.getDeclaredMethods()) {
            if (m.isAnnotationPresent(Test.class)) {
                testMethods.add(m.getName());
            }
        }

        List<String> targetMethods = new ArrayList<>(
                Arrays.asList("create", "update", "getById", "delete", "getAll", "getClassType"));
        targetMethods.addAll(new ArrayList<>(Arrays.asList(testingInterface.getDeclaredMethods())).stream()
                .map(m -> m.getName()).collect(Collectors.toList()));
        testMethods.forEach(targetMethods::remove);

        Assert.assertEquals("Following method(s) are missing in DAO test :" + targetMethods.toString(), 0,
                targetMethods.size());
    }

    protected <T> List<T> toList(T element) {
        return Stream.of(element).collect(Collectors.toList());
    }
}