001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice;
017
018import org.junit.Test;
019import org.reflections.Reflections;
020
021import javax.persistence.Embeddable;
022import javax.persistence.Entity;
023import javax.persistence.MappedSuperclass;
024import java.lang.reflect.Method;
025import java.util.Set;
026
027import static org.junit.Assert.fail;
028
029/**
030 * This test verifies that all of the JPA objects in this module are statically weaved.
031 *
032 * <p>If one executes this test from within an IDE environment, it very well may not pass if one of the JPA objects
033 * under examination was modified and then recompiled by the IDE. The static weaving process is handled by Maven,
034 * so without executing the appropriate Maven lifecycle phase, the class will not get weaved. Regardless, this test
035 * should *always* pass when executed from the command line.</p>
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039public class StaticWeavingTest {
040
041    @Test
042    public void testStaticWeaving() {
043        // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation
044        Reflections reflections = new Reflections("org.kuali.rice.kew", "org.kuali.rice.kim", "org.kuali.rice.kcb", "org.kuali.rice.ken");
045        Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class);
046        Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class);
047        Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class);
048
049        // next, let's assert that they have been statically weaved
050        assertStaticWeaved(entityTypes, superTypes, embeddableTypes);
051    }
052
053    private void assertStaticWeaved(Set<Class<?>>... types) {
054        for (Set<Class<?>> typeSet : types) {
055            for (Class<?> type : typeSet) {
056                boolean foundWeaved = false;
057                Method[] methods = type.getDeclaredMethods();
058                for (Method method : methods) {
059                    if (method.getName().startsWith("_persistence")) {
060                        foundWeaved = true;
061                        break;
062                    }
063                }
064                if (!foundWeaved) {
065                    fail("(NOTE: it is expected this test may fail if executed from the IDE instead of command line "
066                            + "since the IDE will not execute the static weaving automatically). Found a class which is "
067                            + "not bytecode weaved (contains no methods starting with '_persistence'): " + type + " "
068                            + "In order to resolve this, please ensure that this type is included in "
069                            + "META-INF/persistence-weaving.xml");
070                }
071            }
072        }
073    }
074
075}