View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns;
17  
18  import static org.junit.Assert.fail;
19  
20  import java.lang.reflect.Method;
21  import java.util.Set;
22  
23  import javax.persistence.Embeddable;
24  import javax.persistence.Entity;
25  import javax.persistence.MappedSuperclass;
26  
27  import org.junit.Test;
28  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
29  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
30  import org.kuali.rice.krad.document.DocumentBase;
31  import org.kuali.rice.krad.maintenance.MaintenanceLock;
32  import org.kuali.rice.krad.messages.Message;
33  import org.reflections.Reflections;
34  
35  /**
36   * This test verifies that all of the JPA objects in this module are statically weaved.
37   *
38   * <p>If one executes this test from within an IDE environment, it very well may not pass if one of the JPA objects
39   * under examination was modified and then recompiled by the IDE. The static weaving process is handled by Maven,
40   * so without executing the appropriate Maven lifecycle phase, the class will not get weaved. Regardless, this test
41   * should *always* pass when executed from the command line.</p>
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class StaticWeavingTest {
46  
47      // because a sample app package conflicts with our main package we are scanning, let's exclude those from
48      // this analysis
49      private static final String TEST_PACKAGE_PREFIX = "org.kuali.rice.krad.sampleapp";
50  
51      @Test
52      public void testStaticWeaving() {
53          // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation
54          Reflections reflections = new Reflections(
55          		PersistableBusinessObjectBase.class.getPackage().getName());
56          Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class);
57          Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class);
58          Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class);
59  
60          // next, let's assert that they have been statically weaved
61          assertStaticWeaved(entityTypes, superTypes, embeddableTypes);
62      }
63  
64      private void assertStaticWeaved(Set<Class<?>>... types) {
65          for (Set<Class<?>> typeSet : types) {
66              for (Class<?> type : typeSet) {
67                  if (type.getName().startsWith(TEST_PACKAGE_PREFIX)) {
68                      continue;
69                  }
70                  boolean foundWeaved = false;
71                  Method[] methods = type.getDeclaredMethods();
72                  for (Method method : methods) {
73                      if (method.getName().startsWith("_persistence")) {
74                          foundWeaved = true;
75                          break;
76                      }
77                  }
78                  if (!foundWeaved) {
79                      fail("(NOTE: it is expected this test may fail if executed from the IDE instead of command line "
80                              + "since the IDE will not execute the static weaving automatically). Found a class which is "
81                              + "not bytecode weaved (contains no methods starting with '_persistence'): " + type + " "
82                              + "In order to resolve this, please ensure that this type is included in "
83                              + "META-INF/persistence-weaving.xml");
84                  }
85              }
86          }
87      }
88  
89  }