001/** 002 * Copyright 2005-2015 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.kns; 017 018import static org.junit.Assert.fail; 019 020import java.lang.reflect.Method; 021import java.util.Set; 022 023import javax.persistence.Embeddable; 024import javax.persistence.Entity; 025import javax.persistence.MappedSuperclass; 026 027import org.junit.Test; 028import org.kuali.rice.krad.bo.PersistableBusinessObjectBase; 029import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter; 030import org.kuali.rice.krad.document.DocumentBase; 031import org.kuali.rice.krad.maintenance.MaintenanceLock; 032import org.kuali.rice.krad.messages.Message; 033import org.reflections.Reflections; 034 035/** 036 * This test verifies that all of the JPA objects in this module are statically weaved. 037 * 038 * <p>If one executes this test from within an IDE environment, it very well may not pass if one of the JPA objects 039 * under examination was modified and then recompiled by the IDE. The static weaving process is handled by Maven, 040 * so without executing the appropriate Maven lifecycle phase, the class will not get weaved. Regardless, this test 041 * should *always* pass when executed from the command line.</p> 042 * 043 * @author Kuali Rice Team (rice.collab@kuali.org) 044 */ 045public class StaticWeavingTest { 046 047 // because a sample app package conflicts with our main package we are scanning, let's exclude those from 048 // this analysis 049 private static final String TEST_PACKAGE_PREFIX = "org.kuali.rice.krad.sampleapp"; 050 051 @Test 052 public void testStaticWeaving() { 053 // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation 054 Reflections reflections = new Reflections( 055 PersistableBusinessObjectBase.class.getPackage().getName()); 056 Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class); 057 Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class); 058 Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class); 059 060 // next, let's assert that they have been statically weaved 061 assertStaticWeaved(entityTypes, superTypes, embeddableTypes); 062 } 063 064 private void assertStaticWeaved(Set<Class<?>>... types) { 065 for (Set<Class<?>> typeSet : types) { 066 for (Class<?> type : typeSet) { 067 if (type.getName().startsWith(TEST_PACKAGE_PREFIX)) { 068 continue; 069 } 070 boolean foundWeaved = false; 071 Method[] methods = type.getDeclaredMethods(); 072 for (Method method : methods) { 073 if (method.getName().startsWith("_persistence")) { 074 foundWeaved = true; 075 break; 076 } 077 } 078 if (!foundWeaved) { 079 fail("(NOTE: it is expected this test may fail if executed from the IDE instead of command line " 080 + "since the IDE will not execute the static weaving automatically). Found a class which is " 081 + "not bytecode weaved (contains no methods starting with '_persistence'): " + type + " " 082 + "In order to resolve this, please ensure that this type is included in " 083 + "META-INF/persistence-weaving.xml"); 084 } 085 } 086 } 087 } 088 089}