podd.model.visitor.VisitorAdapterUnitTest.java Source code

Java tutorial

Introduction

Here is the source code for podd.model.visitor.VisitorAdapterUnitTest.java

Source

/*
 * Copyright (c) 2009 - 2010. School of Information Technology and Electrical
 * Engineering, The University of Queensland.  This software is being developed
 * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project.
 * PODD is a National e-Research Architecture Taskforce (NeAT) project
 * co-funded by ANDS and ARCS.
 *
 * PODD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PODD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PODD.  If not, see <http://www.gnu.org/licenses/>.
 */

package podd.model.visitor;

import org.junit.Test;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AssignableTypeFilter;
import podd.model.entity.Entity;

import java.lang.reflect.Method;
import java.util.Set;

import static org.junit.Assert.assertNotNull;

/**
 * @author Yuan-Fang Li
 * @version $Id$
 */
public class VisitorAdapterUnitTest {
    @Test
    public void testCoversAllClasses() throws Exception {
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProviderAllowInterface(
                true);
        provider.addIncludeFilter(new AssignableTypeFilter(Entity.class));

        Set<BeanDefinition> components = provider.findCandidateComponents("podd/model");
        resolveAndCheckPackage(components);
    }

    private void resolveAndCheckPackage(Set<BeanDefinition> components)
            throws ClassNotFoundException, NoSuchMethodException {
        for (BeanDefinition component : components) {
            Class cls = Class.forName(component.getBeanClassName());
            if (cls.isInterface()) {
                String methodName = "visit" + cls.getSimpleName();
                Visitor.class.getMethod(methodName, cls);
                Method mtd = VisitorAdapter.class.getMethod(methodName, cls);
                assertNotNull(mtd);
            }
        }
    }

    private class ClassPathScanningCandidateComponentProviderAllowInterface
            extends ClassPathScanningCandidateComponentProvider {

        /**
         * Create a ClassPathScanningCandidateComponentProvider.
         *
         * @param useDefaultFilters whether to register the default filters for the
         * {@link org.springframework.stereotype.Component @Component},
         * {@link org.springframework.stereotype.Repository @Repository},
         * {@link org.springframework.stereotype.Service @Service},
         * and {@link org.springframework.stereotype.Controller @Controller}
         * stereotype annotations
         * @see #registerDefaultFilters()
         */
        public ClassPathScanningCandidateComponentProviderAllowInterface(boolean useDefaultFilters) {
            super(useDefaultFilters);
        }

        @Override
        protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
            boolean result = super.isCandidateComponent(beanDefinition);
            try {
                result |= Class.forName(beanDefinition.getBeanClassName()).isInterface();
                return result;
            } catch (ClassNotFoundException e) {
                return false;
            }
        }
    }
}